source: branches/HiveProjectManagement/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ProjectResourcesView.cs @ 15576

Last change on this file since 15576 was 15576, checked in by jzenisek, 5 years ago

#2839 worked on client side mgmt of project-resource assignments and project-user permissions

File size: 13.5 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Clients.Access;
28using HeuristicLab.Common.Resources;
29using HeuristicLab.Core.Views;
30using HeuristicLab.MainForm;
31using System.Collections;
32
33namespace 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    private readonly Dictionary<Guid, HashSet<Resource>> resourceAncestors = new Dictionary<Guid, HashSet<Resource>>();
47    private readonly Dictionary<Guid, HashSet<Resource>> resourceDescendants = new Dictionary<Guid, HashSet<Resource>>();
48    //private readonly Color addedAssignmentColor = Color.FromArgb(255, 0, 174, 179); // #00aeb3
49    private readonly Color addedAssignmentColor = Color.FromArgb(255, 87, 191, 193); // #57bfc1
50    private readonly Color removedAssignmentColor = Color.FromArgb(255, 236, 159, 72); // #ec9f48
51    private readonly Color addedIncludeColor = Color.FromArgb(25, 169, 221, 221); // #a9dddd
52    private readonly Color removedIncludeColor = Color.FromArgb(25, 249, 210, 145); // #f9d291
53
54    public new Project Content {
55      get { return (Project)base.Content; }
56      set { base.Content = value; }
57    }
58
59    public ProjectResourcesView() {
60      InitializeComponent();
61
62      treeView.ImageList.Images.Add(VSImageLibrary.MonitorLarge);
63      treeView.ImageList.Images.Add(VSImageLibrary.NetworkCenterLarge);
64    }
65
66    #region Overrides
67    protected override void OnContentChanged() {
68      base.OnContentChanged();
69      if (Content == null) {
70        assignedResources.Clear();
71        newAssignedResources.Clear();
72        inheritedResources.Clear();
73        resourceAncestors.Clear();
74        treeView.Nodes.Clear();
75        detailsViewHost.Content = null;
76      } else {
77        UpdateAssignedResources();
78        UpdateResourceGenealogy();
79        var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
80        detailsViewHost.Content = top;
81        detailsViewHost.ActiveView.Locked = true;
82      }
83    }
84
85    #endregion
86
87    #region Event Handlers
88    private void ProjectResourcesView_Load(object sender, EventArgs e) {
89
90    }
91
92    private void refreshButton_Click(object sender, EventArgs e) {
93      UpdateAssignedResources();
94      UpdateResourceGenealogy();
95      var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
96      detailsViewHost.Content = top;
97    }
98
99    private async void inheritButton_Click(object sender, EventArgs e) {
100      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
101        action: () => {
102          SetAssignedProjectResources(Content.Id, newAssignedResources.Select(x => x.Id), false, true);
103        });
104      UpdateResourceTree();
105    }
106
107    private async void saveButton_Click(object sender, EventArgs e) {
108      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
109        action: () => {
110          SetAssignedProjectResources(Content.Id, newAssignedResources.Select(x => x.Id), false, false);
111        });
112      UpdateResourceTree();
113    }
114
115    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
116      var selectedResource = (Resource)e.Node.Tag;
117      detailsViewHost.Content = selectedResource;
118    }
119
120    private void treeView_BeforeCheck(object sender, TreeViewCancelEventArgs e) {
121      var checkedResource = (Resource)e.Node.Tag;
122      if (newInheritedResources.Contains(checkedResource) || checkedResource.Id == Guid.Empty) e.Cancel = true;
123    }
124
125    private void treeView_AfterCheck(object sender, TreeViewEventArgs e) {
126      var checkedResource = (Resource)e.Node.Tag;
127      if (e.Node.Checked) {
128        newAssignedResources.Add(checkedResource);
129      } else {
130        newAssignedResources.Remove(checkedResource);
131      }
132
133      UpdateNewResourceTree();
134    }
135    #endregion
136
137    #region Helpers
138
139    private void UpdateResourceTree() {
140      UpdateAssignedResources();
141      UpdateResourceGenealogy();
142      var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
143      detailsViewHost.Content = top;
144    }
145
146    private void UpdateNewResourceTree() {
147      UpdateNewAssignedResources();
148      UpdateNewInheritedResources();
149      var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
150      detailsViewHost.Content = top;
151    }
152
153    private static IEnumerable<Resource> GetAssignedResourcesForProject(Guid projectId) {
154      var assignedProjectResources = HiveServiceLocator.Instance.CallHiveService(s => s.GetAssignedResourcesForProject(projectId));
155      return HiveAdminClient.Instance.Resources.Where(x => assignedProjectResources.Select(y => y.ResourceId).Contains(x.Id));
156    }
157
158    private void SetAssignedProjectResources(Guid projectId, IEnumerable<Guid> resourceIds, bool reassign, bool cascading) {
159      if (projectId == null || resourceIds == null) return;
160      HiveServiceLocator.Instance.CallHiveService(s => {
161       s.SaveProjectResourceAssignments(projectId, resourceIds.ToList(), reassign, cascading, true);
162      });
163    }
164
165    private void UpdateNewAssignedResources() {
166      for(int i = newAssignedResources.Count -1; i >= 0; i--) {
167        if(newAssignedResources.Intersect(resourceAncestors[newAssignedResources.ElementAt(i).Id]).Any()) {
168          newAssignedResources.Remove(newAssignedResources.ElementAt(i));
169        }
170      }
171    }
172
173    private void UpdateAssignedResources() {
174      assignedResources.Clear();
175      newAssignedResources.Clear();
176      foreach (var r in GetAssignedResourcesForProject(Content.Id)) {
177        assignedResources.Add(r);
178        newAssignedResources.Add(r);
179      }
180    }
181
182    private void UpdateNewInheritedResources() {
183      newInheritedResources.Clear();
184      foreach (var a in newAssignedResources) {
185        if (resourceDescendants.ContainsKey(a.Id)) {
186          foreach (var r in resourceDescendants[a.Id]) {
187            newInheritedResources.Add(r);
188          }
189        }
190      }
191    }
192
193    private void UpdateResourceGenealogy() {
194      resourceAncestors.Clear();
195      resourceDescendants.Clear();
196      var resources = HiveAdminClient.Instance.Resources;
197
198      foreach(var r in resources) {
199        resourceAncestors.Add(r.Id, new HashSet<Resource>());
200        resourceDescendants.Add(r.Id, new HashSet<Resource>());
201      }
202
203      foreach(var r in resources) {
204        var parentResourceId = r.ParentResourceId;
205        while(parentResourceId != null) {
206          var parent = resources.SingleOrDefault(x => x.Id == parentResourceId);
207          if(parent != null) {
208            resourceAncestors[r.Id].Add(parent);
209            resourceDescendants[parent.Id].Add(r);
210            parentResourceId = parent.ParentResourceId;
211          } else {
212            parentResourceId = null;
213          }
214        }
215      }
216
217      inheritedResources.Clear();
218      newInheritedResources.Clear();
219      foreach(var a in assignedResources) {
220        if (resourceDescendants.ContainsKey(a.Id)) {
221          foreach(var r in resourceDescendants[a.Id]) {
222            inheritedResources.Add(r);
223            newInheritedResources.Add(r);
224          }
225        }
226      }
227
228      //foreach(var r in resources) {
229      //  if (resourceAncestors.ContainsKey(r.Id)
230      //    && resourceAncestors[r.Id].Intersect(assignedResources.Select(x => x.Id)).Any()) {
231      //    inheritedResources.Add(r);
232      //  }
233      //}
234    }
235
236    private Resource BuildResourceTree(IEnumerable<Resource> resources) {
237      treeView.Nodes.Clear();
238      if (!resources.Any()) return null;
239
240      treeView.BeforeCheck -= treeView_BeforeCheck;
241      treeView.AfterCheck -= treeView_AfterCheck;
242
243      var mainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>().Where(x => x.ParentResourceId == null));
244      var subResources = new HashSet<Resource>(resources.Except(mainResources));
245
246      var stack = new Stack<Resource>(mainResources.OrderByDescending(x => x.Name));
247      var top = stack.Peek();
248
249      TreeNode currentNode = null;
250      Resource currentResource = null;
251
252
253      var addedAssignments = newAssignedResources.Except(assignedResources);
254      var removedAssignments = assignedResources.Except(newAssignedResources);
255      var addedIncludes = newInheritedResources.Except(inheritedResources);
256      var removedIncludes = inheritedResources.Except(newInheritedResources);
257
258      //var assignmentDiff = new HashSet<Resource>(newAssignedResources);
259      //assignmentDiff.SymmetricExceptWith(assignedResources);
260      //var inheritanceDiff = new HashSet<Resource>(newInheritedResources);
261      //inheritanceDiff.SymmetricExceptWith(inheritedResources);
262
263      while (stack.Any()) {
264        var newResource = stack.Pop();
265        var newNode = new TreeNode(newResource.Name) { Tag = newResource };
266
267        // search for parent node of newNode and save in currentNode
268        // necessary since newNodes (stack top items) might be siblings
269        // or grand..grandparents of previous node (currentNode)
270        while (currentNode != null && newResource.ParentResourceId != currentResource.Id) {
271          currentNode = currentNode.Parent;
272          currentResource = currentNode == null ? null : (Resource)currentNode.Tag;
273        }
274
275        if (currentNode == null) {
276          treeView.Nodes.Add(newNode);
277        } else {
278          currentNode.Nodes.Add(newNode);
279        }
280
281        if (newAssignedResources.Contains(newResource)) {
282          newNode.Checked = true;
283        } else if (newInheritedResources.Contains(newResource)) {
284          newNode.Checked = true;
285          newNode.ForeColor = SystemColors.GrayText;
286        }
287
288          if (inheritedResources.Contains(newResource) && newInheritedResources.Contains(newResource)) {
289          newNode.Text += " [included]";
290        } else if (addedIncludes.Contains(newResource)) {
291          newNode.BackColor = addedIncludeColor;
292          newNode.ForeColor = SystemColors.GrayText;
293          newNode.Text += " [added include]";
294        } else if (removedIncludes.Contains(newResource)) {
295          newNode.BackColor = removedIncludeColor;
296          newNode.Text += " [removed include]";
297        }
298
299        if (addedAssignments.Contains(newResource)) {
300          newNode.BackColor = addedAssignmentColor;
301          newNode.ForeColor = SystemColors.ControlText;
302          newNode.Text += " [added assignment]";
303        } else if (removedAssignments.Contains(newResource)) {
304          newNode.BackColor = removedAssignmentColor;
305          newNode.ForeColor = SystemColors.ControlText;
306          newNode.Text += " [removed assignment]";
307        }
308
309        if (newResource is Slave) {
310          newNode.ImageIndex = slaveImageIndex;
311        } else {
312          newNode.ImageIndex = slaveGroupImageIndex;
313
314          var childResources = subResources.Where(x => x.ParentResourceId == newResource.Id);
315          if (childResources.Any()) {
316            foreach (var resource in childResources.OrderByDescending(x => x.Name)) {
317              subResources.Remove(resource);
318              stack.Push(resource);
319            }
320            currentNode = newNode;
321            currentResource = newResource;
322          }
323        }
324        newNode.SelectedImageIndex = newNode.ImageIndex;
325        //if (newResource.OwnerUserId == UserInformation.Instance.User.Id)
326        //  newNode.BackColor = ownedResourceColor;
327      }
328
329      var ungroupedNode = new TreeNode(ungroupedGroupName) {
330        ForeColor = SystemColors.GrayText,
331        Tag = new SlaveGroup() {
332          Name = ungroupedGroupName,
333          Description = ungroupedGroupDescription
334        }
335      };
336
337      foreach (var slave in subResources.OfType<Slave>().OrderBy(x => x.Name)) {
338        var slaveNode = new TreeNode(slave.Name) { Tag = slave };
339        ungroupedNode.Nodes.Add(slaveNode);
340      }
341
342      treeView.Nodes.Add(ungroupedNode);
343      treeView.BeforeCheck += treeView_BeforeCheck;
344      treeView.AfterCheck += treeView_AfterCheck;
345      treeView.ExpandAll();
346
347      return top;
348    }
349
350    #endregion
351  }
352}
Note: See TracBrowser for help on using the repository browser.