1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Windows.Forms;
|
---|
27 | using HeuristicLab.Clients.Access;
|
---|
28 | using HeuristicLab.Common.Resources;
|
---|
29 | using HeuristicLab.Core.Views;
|
---|
30 | using HeuristicLab.MainForm;
|
---|
31 | using System.Collections;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Clients.Hive.Administrator.Views {
|
---|
34 | [View("ProjectView")]
|
---|
35 | [Content(typeof(Project), IsDefaultView = false)]
|
---|
36 | public partial class ProjectResourcesView : ItemView {
|
---|
37 | private const int slaveImageIndex = 0;
|
---|
38 | private const int slaveGroupImageIndex = 1;
|
---|
39 | public const string ungroupedGroupName = "UNGROUPED";
|
---|
40 | public const string ungroupedGroupDescription = "Contains slaves that are not assigned to any group.";
|
---|
41 |
|
---|
42 | private readonly HashSet<Resource> assignedResources = new HashSet<Resource>();
|
---|
43 | private readonly HashSet<Resource> newAssignedResources = new HashSet<Resource>();
|
---|
44 | private readonly HashSet<Resource> inheritedResources = new HashSet<Resource>();
|
---|
45 | private readonly HashSet<Resource> newInheritedResources = new HashSet<Resource>();
|
---|
46 |
|
---|
47 | private readonly Dictionary<Guid, HashSet<Project>> projectAncestors = new Dictionary<Guid, HashSet<Project>>();
|
---|
48 | private readonly Dictionary<Guid, HashSet<Project>> projectDescendants = new Dictionary<Guid, HashSet<Project>>();
|
---|
49 | private readonly Dictionary<Guid, HashSet<Resource>> resourceAncestors = new Dictionary<Guid, HashSet<Resource>>();
|
---|
50 | private readonly Dictionary<Guid, HashSet<Resource>> resourceDescendants = new Dictionary<Guid, HashSet<Resource>>();
|
---|
51 |
|
---|
52 | private readonly Color addedAssignmentColor = Color.FromArgb(255, 87, 191, 193); // #57bfc1
|
---|
53 | private readonly Color removedAssignmentColor = Color.FromArgb(255, 236, 159, 72); // #ec9f48
|
---|
54 | private readonly Color addedIncludeColor = Color.FromArgb(25, 169, 221, 221); // #a9dddd
|
---|
55 | private readonly Color removedIncludeColor = Color.FromArgb(25, 249, 210, 145); // #f9d291
|
---|
56 |
|
---|
57 | public new Project Content {
|
---|
58 | get { return (Project)base.Content; }
|
---|
59 | set { base.Content = value; }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public ProjectResourcesView() {
|
---|
63 | InitializeComponent();
|
---|
64 |
|
---|
65 | treeView.ImageList.Images.Add(VSImageLibrary.MonitorLarge);
|
---|
66 | treeView.ImageList.Images.Add(VSImageLibrary.NetworkCenterLarge);
|
---|
67 | }
|
---|
68 |
|
---|
69 | #region Overrides
|
---|
70 | protected override void OnContentChanged() {
|
---|
71 | base.OnContentChanged();
|
---|
72 | if (Content == null) {
|
---|
73 | assignedResources.Clear();
|
---|
74 | newAssignedResources.Clear();
|
---|
75 | inheritedResources.Clear();
|
---|
76 | resourceAncestors.Clear();
|
---|
77 | treeView.Nodes.Clear();
|
---|
78 | detailsViewHost.Content = null;
|
---|
79 | } else {
|
---|
80 | UpdateProjectGenealogy();
|
---|
81 | UpdateAssignedResources();
|
---|
82 | UpdateResourceGenealogy();
|
---|
83 | var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
|
---|
84 | detailsViewHost.Content = top;
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | protected override void SetEnabledStateOfControls() {
|
---|
89 | base.SetEnabledStateOfControls();
|
---|
90 | bool enabled = Content != null && !Locked && !ReadOnly;
|
---|
91 |
|
---|
92 | inheritButton.Enabled = enabled;
|
---|
93 | saveButton.Enabled = enabled;
|
---|
94 | treeView.Enabled = enabled;
|
---|
95 | }
|
---|
96 | #endregion
|
---|
97 |
|
---|
98 | #region Event Handlers
|
---|
99 | private void ProjectResourcesView_Load(object sender, EventArgs e) {
|
---|
100 |
|
---|
101 | }
|
---|
102 |
|
---|
103 | private void refreshButton_Click(object sender, EventArgs e) {
|
---|
104 | UpdateProjectGenealogy();
|
---|
105 | UpdateAssignedResources();
|
---|
106 | UpdateResourceGenealogy();
|
---|
107 | var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
|
---|
108 | detailsViewHost.Content = top;
|
---|
109 | }
|
---|
110 |
|
---|
111 | private async void inheritButton_Click(object sender, EventArgs e) {
|
---|
112 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
113 | action: () => {
|
---|
114 | SetAssignedProjectResources(Content.Id, newAssignedResources.Select(x => x.Id), false, true, false);
|
---|
115 | });
|
---|
116 | UpdateResourceTree();
|
---|
117 | }
|
---|
118 |
|
---|
119 | private async void saveButton_Click(object sender, EventArgs e) {
|
---|
120 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
121 | action: () => {
|
---|
122 | SetAssignedProjectResources(Content.Id, newAssignedResources.Select(x => x.Id), false, false, false);
|
---|
123 | });
|
---|
124 | UpdateResourceTree();
|
---|
125 | }
|
---|
126 |
|
---|
127 | private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
128 | var selectedResource = (Resource)e.Node.Tag;
|
---|
129 | detailsViewHost.Content = selectedResource;
|
---|
130 | }
|
---|
131 |
|
---|
132 | private void treeView_BeforeCheck(object sender, TreeViewCancelEventArgs e) {
|
---|
133 | var checkedResource = (Resource)e.Node.Tag;
|
---|
134 | if (newInheritedResources.Contains(checkedResource) || checkedResource.Id == Guid.Empty) {
|
---|
135 | e.Cancel = true;
|
---|
136 | } else if (!HiveRoles.CheckAdminUserPermissions()) {
|
---|
137 | if (!projectAncestors[Content.Id].Any()) {
|
---|
138 | e.Cancel = true;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | private void treeView_AfterCheck(object sender, TreeViewEventArgs e) {
|
---|
144 | var checkedResource = (Resource)e.Node.Tag;
|
---|
145 | if (e.Node.Checked) {
|
---|
146 | newAssignedResources.Add(checkedResource);
|
---|
147 | } else {
|
---|
148 | newAssignedResources.Remove(checkedResource);
|
---|
149 | }
|
---|
150 |
|
---|
151 | UpdateNewResourceTree();
|
---|
152 | }
|
---|
153 | #endregion
|
---|
154 |
|
---|
155 | #region Helpers
|
---|
156 |
|
---|
157 | private void UpdateResourceTree() {
|
---|
158 | UpdateAssignedResources();
|
---|
159 | UpdateResourceGenealogy();
|
---|
160 | var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
|
---|
161 | detailsViewHost.Content = top;
|
---|
162 | }
|
---|
163 |
|
---|
164 | private void UpdateNewResourceTree() {
|
---|
165 | UpdateNewAssignedResources();
|
---|
166 | UpdateNewInheritedResources();
|
---|
167 | var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
|
---|
168 | detailsViewHost.Content = top;
|
---|
169 | }
|
---|
170 |
|
---|
171 | private static IEnumerable<Resource> GetAssignedResourcesForProject(Guid projectId) {
|
---|
172 | if (projectId == Guid.Empty) return Enumerable.Empty<Resource>();
|
---|
173 | var assignedProjectResources = HiveServiceLocator.Instance.CallHiveService(s => s.GetAssignedResourcesForProjectAdministration(projectId));
|
---|
174 | return HiveAdminClient.Instance.Resources.Where(x => assignedProjectResources.Select(y => y.ResourceId).Contains(x.Id));
|
---|
175 | }
|
---|
176 |
|
---|
177 | private void SetAssignedProjectResources(Guid projectId, IEnumerable<Guid> resourceIds, bool reassign, bool cascading, bool reassignCascading) {
|
---|
178 | if (projectId == null || resourceIds == null) return;
|
---|
179 | HiveServiceLocator.Instance.CallHiveService(s => {
|
---|
180 | s.SaveProjectResourceAssignments(projectId, resourceIds.ToList(), reassign, cascading, reassignCascading);
|
---|
181 | });
|
---|
182 | }
|
---|
183 |
|
---|
184 | private void UpdateNewAssignedResources() {
|
---|
185 | for(int i = newAssignedResources.Count -1; i >= 0; i--) {
|
---|
186 | if(newAssignedResources.Intersect(resourceAncestors[newAssignedResources.ElementAt(i).Id]).Any()) {
|
---|
187 | newAssignedResources.Remove(newAssignedResources.ElementAt(i));
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | private void UpdateAssignedResources() {
|
---|
193 | assignedResources.Clear();
|
---|
194 | newAssignedResources.Clear();
|
---|
195 | foreach (var r in GetAssignedResourcesForProject(Content.Id)) {
|
---|
196 | assignedResources.Add(r);
|
---|
197 | newAssignedResources.Add(r);
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | private void UpdateNewInheritedResources() {
|
---|
202 | newInheritedResources.Clear();
|
---|
203 | foreach (var a in newAssignedResources) {
|
---|
204 | if (resourceDescendants.ContainsKey(a.Id)) {
|
---|
205 | foreach (var r in resourceDescendants[a.Id]) {
|
---|
206 | newInheritedResources.Add(r);
|
---|
207 | }
|
---|
208 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | private void UpdateResourceGenealogy() {
|
---|
213 | resourceAncestors.Clear();
|
---|
214 | resourceDescendants.Clear();
|
---|
215 | var resources = HiveAdminClient.Instance.Resources;
|
---|
216 |
|
---|
217 | foreach(var r in resources.Where(x => x.Id != Guid.Empty)) {
|
---|
218 | resourceAncestors.Add(r.Id, new HashSet<Resource>());
|
---|
219 | resourceDescendants.Add(r.Id, new HashSet<Resource>());
|
---|
220 | }
|
---|
221 |
|
---|
222 | foreach(var r in resources.Where(x => x.Id != Guid.Empty)) {
|
---|
223 | var parentResourceId = r.ParentResourceId;
|
---|
224 | while(parentResourceId != null) {
|
---|
225 | var parent = resources.SingleOrDefault(x => x.Id == parentResourceId);
|
---|
226 | if(parent != null) {
|
---|
227 | resourceAncestors[r.Id].Add(parent);
|
---|
228 | resourceDescendants[parent.Id].Add(r);
|
---|
229 | parentResourceId = parent.ParentResourceId;
|
---|
230 | } else {
|
---|
231 | parentResourceId = null;
|
---|
232 | }
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | inheritedResources.Clear();
|
---|
237 | newInheritedResources.Clear();
|
---|
238 | foreach(var a in assignedResources) {
|
---|
239 | if (resourceDescendants.ContainsKey(a.Id)) {
|
---|
240 | foreach(var r in resourceDescendants[a.Id]) {
|
---|
241 | inheritedResources.Add(r);
|
---|
242 | newInheritedResources.Add(r);
|
---|
243 | }
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | //foreach(var r in resources) {
|
---|
248 | // if (resourceAncestors.ContainsKey(r.Id)
|
---|
249 | // && resourceAncestors[r.Id].Intersect(assignedResources.Select(x => x.Id)).Any()) {
|
---|
250 | // inheritedResources.Add(r);
|
---|
251 | // }
|
---|
252 | //}
|
---|
253 | }
|
---|
254 |
|
---|
255 | private Resource BuildResourceTree(IEnumerable<Resource> resources) {
|
---|
256 | treeView.Nodes.Clear();
|
---|
257 | if (!resources.Any()) return null;
|
---|
258 |
|
---|
259 | treeView.BeforeCheck -= treeView_BeforeCheck;
|
---|
260 | treeView.AfterCheck -= treeView_AfterCheck;
|
---|
261 |
|
---|
262 | resources = resources.Where(x => x.Id != Guid.Empty);
|
---|
263 | var mainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>().Where(x => x.ParentResourceId == null));
|
---|
264 | var parentedMainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>()
|
---|
265 | .Where(x => x.ParentResourceId.HasValue && !resources.Select(y => y.Id).Contains(x.ParentResourceId.Value)));
|
---|
266 | mainResources.UnionWith(parentedMainResources);
|
---|
267 | var subResources = new HashSet<Resource>(resources.Except(mainResources));
|
---|
268 |
|
---|
269 | var stack = new Stack<Resource>(mainResources.OrderByDescending(x => x.Name));
|
---|
270 | Resource top = null;
|
---|
271 |
|
---|
272 | TreeNode currentNode = null;
|
---|
273 | Resource currentResource = null;
|
---|
274 |
|
---|
275 |
|
---|
276 | var addedAssignments = newAssignedResources.Except(assignedResources);
|
---|
277 | var removedAssignments = assignedResources.Except(newAssignedResources);
|
---|
278 | var addedIncludes = newInheritedResources.Except(inheritedResources);
|
---|
279 | var removedIncludes = inheritedResources.Except(newInheritedResources);
|
---|
280 |
|
---|
281 | //var assignmentDiff = new HashSet<Resource>(newAssignedResources);
|
---|
282 | //assignmentDiff.SymmetricExceptWith(assignedResources);
|
---|
283 | //var inheritanceDiff = new HashSet<Resource>(newInheritedResources);
|
---|
284 | //inheritanceDiff.SymmetricExceptWith(inheritedResources);
|
---|
285 |
|
---|
286 | while (stack.Any()) {
|
---|
287 | if(top == null) top = stack.Peek();
|
---|
288 | var newResource = stack.Pop();
|
---|
289 | var newNode = new TreeNode(newResource.Name) { Tag = newResource };
|
---|
290 |
|
---|
291 | // search for parent node of newNode and save in currentNode
|
---|
292 | // necessary since newNodes (stack top items) might be siblings
|
---|
293 | // or grand..grandparents of previous node (currentNode)
|
---|
294 | while (currentNode != null && newResource.ParentResourceId != currentResource.Id) {
|
---|
295 | currentNode = currentNode.Parent;
|
---|
296 | currentResource = currentNode == null ? null : (Resource)currentNode.Tag;
|
---|
297 | }
|
---|
298 |
|
---|
299 | if (currentNode == null) {
|
---|
300 | treeView.Nodes.Add(newNode);
|
---|
301 | } else {
|
---|
302 | currentNode.Nodes.Add(newNode);
|
---|
303 | }
|
---|
304 |
|
---|
305 | if (newAssignedResources.Contains(newResource)) {
|
---|
306 | newNode.Checked = true;
|
---|
307 | if(!HiveRoles.CheckAdminUserPermissions()) {
|
---|
308 | if(!projectAncestors[Content.Id].Any()) {
|
---|
309 | newNode.ForeColor = SystemColors.GrayText;
|
---|
310 | newNode.Text += " [immutable]";
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | } else if (newInheritedResources.Contains(newResource)) {
|
---|
315 | newNode.Checked = true;
|
---|
316 | newNode.ForeColor = SystemColors.GrayText;
|
---|
317 | }
|
---|
318 |
|
---|
319 | if (inheritedResources.Contains(newResource) && newInheritedResources.Contains(newResource)) {
|
---|
320 | newNode.Text += " [included]";
|
---|
321 | } else if (addedIncludes.Contains(newResource)) {
|
---|
322 | newNode.BackColor = addedIncludeColor;
|
---|
323 | newNode.ForeColor = SystemColors.GrayText;
|
---|
324 | newNode.Text += " [added include]";
|
---|
325 | } else if (removedIncludes.Contains(newResource)) {
|
---|
326 | newNode.BackColor = removedIncludeColor;
|
---|
327 | newNode.Text += " [removed include]";
|
---|
328 | }
|
---|
329 |
|
---|
330 | if (addedAssignments.Contains(newResource)) {
|
---|
331 | newNode.BackColor = addedAssignmentColor;
|
---|
332 | newNode.ForeColor = SystemColors.ControlText;
|
---|
333 | newNode.Text += " [added assignment]";
|
---|
334 | } else if (removedAssignments.Contains(newResource)) {
|
---|
335 | newNode.BackColor = removedAssignmentColor;
|
---|
336 | newNode.ForeColor = SystemColors.ControlText;
|
---|
337 | newNode.Text += " [removed assignment]";
|
---|
338 | }
|
---|
339 |
|
---|
340 | if (newResource is Slave) {
|
---|
341 | newNode.ImageIndex = slaveImageIndex;
|
---|
342 | } else {
|
---|
343 | newNode.ImageIndex = slaveGroupImageIndex;
|
---|
344 |
|
---|
345 | var childResources = subResources.Where(x => x.ParentResourceId == newResource.Id);
|
---|
346 | if (childResources.Any()) {
|
---|
347 | foreach (var resource in childResources.OrderByDescending(x => x.Name)) {
|
---|
348 | subResources.Remove(resource);
|
---|
349 | stack.Push(resource);
|
---|
350 | }
|
---|
351 | currentNode = newNode;
|
---|
352 | currentResource = newResource;
|
---|
353 | }
|
---|
354 | }
|
---|
355 | newNode.SelectedImageIndex = newNode.ImageIndex;
|
---|
356 | //if (newResource.OwnerUserId == UserInformation.Instance.User.Id)
|
---|
357 | // newNode.BackColor = ownedResourceColor;
|
---|
358 | }
|
---|
359 |
|
---|
360 | var ungroupedSlaves = subResources.OfType<Slave>().OrderBy(x => x.Name);
|
---|
361 | if(ungroupedSlaves.Any()) {
|
---|
362 | var ungroupedNode = new TreeNode(ungroupedGroupName) {
|
---|
363 | ForeColor = SystemColors.GrayText,
|
---|
364 | Tag = new SlaveGroup() {
|
---|
365 | Name = ungroupedGroupName,
|
---|
366 | Description = ungroupedGroupDescription
|
---|
367 | }
|
---|
368 | };
|
---|
369 |
|
---|
370 | foreach (var slave in ungroupedSlaves) {
|
---|
371 | var slaveNode = new TreeNode(slave.Name) { Tag = slave };
|
---|
372 | ungroupedNode.Nodes.Add(slaveNode);
|
---|
373 | }
|
---|
374 | treeView.Nodes.Add(ungroupedNode);
|
---|
375 | }
|
---|
376 |
|
---|
377 | treeView.BeforeCheck += treeView_BeforeCheck;
|
---|
378 | treeView.AfterCheck += treeView_AfterCheck;
|
---|
379 | treeView.ExpandAll();
|
---|
380 |
|
---|
381 | return top;
|
---|
382 | }
|
---|
383 |
|
---|
384 | private void UpdateProjectGenealogy() {
|
---|
385 | projectAncestors.Clear();
|
---|
386 | projectDescendants.Clear();
|
---|
387 | var projects = HiveAdminClient.Instance.Projects;
|
---|
388 |
|
---|
389 | foreach(var p in projects.Where(x => x.Id != Guid.Empty)) {
|
---|
390 | projectAncestors.Add(p.Id, new HashSet<Project>());
|
---|
391 | projectDescendants.Add(p.Id, new HashSet<Project>());
|
---|
392 | }
|
---|
393 |
|
---|
394 | foreach (var p in projects.Where(x => x.Id != Guid.Empty)) {
|
---|
395 | var parentProjectId = p.ParentProjectId;
|
---|
396 | while (parentProjectId != null) {
|
---|
397 | var parent = projects.SingleOrDefault(x => x.Id == parentProjectId);
|
---|
398 | if (parent != null) {
|
---|
399 | projectAncestors[p.Id].Add(parent);
|
---|
400 | projectDescendants[parent.Id].Add(p);
|
---|
401 | parentProjectId = parent.ParentProjectId;
|
---|
402 | } else {
|
---|
403 | parentProjectId = null;
|
---|
404 | }
|
---|
405 | }
|
---|
406 | }
|
---|
407 | }
|
---|
408 |
|
---|
409 | #endregion
|
---|
410 | }
|
---|
411 | }
|
---|