Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15642 was 15642, checked in by jzenisek, 7 years ago

#2839

  • adapted handling of cascading revocations of project-user permissions and project-resource assignments: from deleting all AssignedJobResource (formerly) to deleting only entries which are effected by the previously mentioned revocations (now)
  • implemented job update (client side)
  • corrected & improved stateful project/resource selection dialog
  • corrected resource statistic calculation in project/resource selection dialog
File size: 13.9 KB
RevLine 
[15422]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;
[15567]31using System.Collections;
[15422]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>();
[15559]43    private readonly HashSet<Resource> newAssignedResources = new HashSet<Resource>();
[15422]44    private readonly HashSet<Resource> inheritedResources = new HashSet<Resource>();
[15559]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>>();
[15567]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
[15422]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();
[15559]71        newAssignedResources.Clear();
[15422]72        inheritedResources.Clear();
[15559]73        resourceAncestors.Clear();
[15422]74        treeView.Nodes.Clear();
75        detailsViewHost.Content = null;
76      } else {
77        UpdateAssignedResources();
[15559]78        UpdateResourceGenealogy();
[15422]79        var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
80        detailsViewHost.Content = top;
[15567]81        detailsViewHost.ActiveView.Locked = true;
[15422]82      }
83    }
[15559]84
[15422]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();
[15559]94      UpdateResourceGenealogy();
[15422]95      var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
96      detailsViewHost.Content = top;
97    }
98
[15576]99    private async void inheritButton_Click(object sender, EventArgs e) {
100      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
101        action: () => {
[15642]102          SetAssignedProjectResources(Content.Id, newAssignedResources.Select(x => x.Id), false, true, false);
[15576]103        });
104      UpdateResourceTree();
[15422]105    }
106
107    private async void saveButton_Click(object sender, EventArgs e) {
108      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
[15559]109        action: () => {
[15642]110          SetAssignedProjectResources(Content.Id, newAssignedResources.Select(x => x.Id), false, false, false);
[15559]111        });
112      UpdateResourceTree();
[15422]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;
[15559]122      if (newInheritedResources.Contains(checkedResource) || checkedResource.Id == Guid.Empty) e.Cancel = true;
[15422]123    }
124
125    private void treeView_AfterCheck(object sender, TreeViewEventArgs e) {
126      var checkedResource = (Resource)e.Node.Tag;
[15559]127      if (e.Node.Checked) {
128        newAssignedResources.Add(checkedResource);
129      } else {
130        newAssignedResources.Remove(checkedResource);
131      }
132
[15576]133      UpdateNewResourceTree();
[15422]134    }
135    #endregion
136
137    #region Helpers
[15559]138
139    private void UpdateResourceTree() {
140      UpdateAssignedResources();
141      UpdateResourceGenealogy();
142      var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
143      detailsViewHost.Content = top;
144    }
145
[15576]146    private void UpdateNewResourceTree() {
147      UpdateNewAssignedResources();
148      UpdateNewInheritedResources();
149      var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
150      detailsViewHost.Content = top;
151    }
152
[15422]153    private static IEnumerable<Resource> GetAssignedResourcesForProject(Guid projectId) {
[15627]154      var assignedProjectResources = HiveServiceLocator.Instance.CallHiveService(s => s.GetAssignedResourcesForProjectAdministration(projectId));
[15559]155      return HiveAdminClient.Instance.Resources.Where(x => assignedProjectResources.Select(y => y.ResourceId).Contains(x.Id));
[15422]156    }
157
[15642]158    private void SetAssignedProjectResources(Guid projectId, IEnumerable<Guid> resourceIds, bool reassign, bool cascading, bool reassignCascading) {
[15576]159      if (projectId == null || resourceIds == null) return;
[15422]160      HiveServiceLocator.Instance.CallHiveService(s => {
[15642]161       s.SaveProjectResourceAssignments(projectId, resourceIds.ToList(), reassign, cascading, reassignCascading);
[15422]162      });
163    }
164
[15559]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
[15422]173    private void UpdateAssignedResources() {
174      assignedResources.Clear();
[15559]175      newAssignedResources.Clear();
176      foreach (var r in GetAssignedResourcesForProject(Content.Id)) {
[15422]177        assignedResources.Add(r);
[15559]178        newAssignedResources.Add(r);
179      }
[15422]180    }
181
[15559]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
[15422]217      inheritedResources.Clear();
[15559]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        }
[15422]226      }
[15559]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      //}
[15422]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
[15559]243      var mainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>().Where(x => x.ParentResourceId == null));
[15627]244      var parentedMainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>()
245        .Where(x => x.ParentResourceId.HasValue && !resources.Select(y => y.Id).Contains(x.ParentResourceId.Value)));
246      mainResources.UnionWith(parentedMainResources);
[15559]247      var subResources = new HashSet<Resource>(resources.Except(mainResources));
[15422]248
[15559]249      var stack = new Stack<Resource>(mainResources.OrderByDescending(x => x.Name));
[15627]250      Resource top = null;
[15422]251
252      TreeNode currentNode = null;
253      Resource currentResource = null;
254
[15559]255
[15567]256      var addedAssignments = newAssignedResources.Except(assignedResources);
257      var removedAssignments = assignedResources.Except(newAssignedResources);
258      var addedIncludes = newInheritedResources.Except(inheritedResources);
259      var removedIncludes = inheritedResources.Except(newInheritedResources);
260
261      //var assignmentDiff = new HashSet<Resource>(newAssignedResources);
262      //assignmentDiff.SymmetricExceptWith(assignedResources);
263      //var inheritanceDiff = new HashSet<Resource>(newInheritedResources);
264      //inheritanceDiff.SymmetricExceptWith(inheritedResources);
265
[15422]266      while (stack.Any()) {
[15627]267        if(top == null)  top = stack.Peek();
[15422]268        var newResource = stack.Pop();
269        var newNode = new TreeNode(newResource.Name) { Tag = newResource };
270
[15559]271        // search for parent node of newNode and save in currentNode
272        // necessary since newNodes (stack top items) might be siblings
273        // or grand..grandparents of previous node (currentNode)
[15422]274        while (currentNode != null && newResource.ParentResourceId != currentResource.Id) {
275          currentNode = currentNode.Parent;
276          currentResource = currentNode == null ? null : (Resource)currentNode.Tag;
277        }
278
279        if (currentNode == null) {
280          treeView.Nodes.Add(newNode);
281        } else {
282          currentNode.Nodes.Add(newNode);
283        }
284
[15559]285        if (newAssignedResources.Contains(newResource)) {
286          newNode.Checked = true;
287        } else if (newInheritedResources.Contains(newResource)) {
288          newNode.Checked = true;
289          newNode.ForeColor = SystemColors.GrayText;
290        }
291
[15567]292          if (inheritedResources.Contains(newResource) && newInheritedResources.Contains(newResource)) {
293          newNode.Text += " [included]";
294        } else if (addedIncludes.Contains(newResource)) {
295          newNode.BackColor = addedIncludeColor;
296          newNode.ForeColor = SystemColors.GrayText;
[15576]297          newNode.Text += " [added include]";
[15567]298        } else if (removedIncludes.Contains(newResource)) {
299          newNode.BackColor = removedIncludeColor;
[15576]300          newNode.Text += " [removed include]";
[15559]301        }
302
[15567]303        if (addedAssignments.Contains(newResource)) {
304          newNode.BackColor = addedAssignmentColor;
305          newNode.ForeColor = SystemColors.ControlText;
[15576]306          newNode.Text += " [added assignment]";
[15567]307        } else if (removedAssignments.Contains(newResource)) {
308          newNode.BackColor = removedAssignmentColor;
309          newNode.ForeColor = SystemColors.ControlText;
[15576]310          newNode.Text += " [removed assignment]";
[15567]311        }
312
[15422]313        if (newResource is Slave) {
314          newNode.ImageIndex = slaveImageIndex;
315        } else {
316          newNode.ImageIndex = slaveGroupImageIndex;
317
[15559]318          var childResources = subResources.Where(x => x.ParentResourceId == newResource.Id);
319          if (childResources.Any()) {
320            foreach (var resource in childResources.OrderByDescending(x => x.Name)) {
321              subResources.Remove(resource);
322              stack.Push(resource);
[15422]323            }
324            currentNode = newNode;
325            currentResource = newResource;
326          }
327        }
328        newNode.SelectedImageIndex = newNode.ImageIndex;
[15559]329        //if (newResource.OwnerUserId == UserInformation.Instance.User.Id)
330        //  newNode.BackColor = ownedResourceColor;
[15422]331      }
332
333      var ungroupedNode = new TreeNode(ungroupedGroupName) {
334        ForeColor = SystemColors.GrayText,
335        Tag = new SlaveGroup() {
336          Name = ungroupedGroupName,
337          Description = ungroupedGroupDescription
338        }
339      };
340
[15559]341      foreach (var slave in subResources.OfType<Slave>().OrderBy(x => x.Name)) {
[15422]342        var slaveNode = new TreeNode(slave.Name) { Tag = slave };
343        ungroupedNode.Nodes.Add(slaveNode);
344      }
345
346      treeView.Nodes.Add(ungroupedNode);
347      treeView.BeforeCheck += treeView_BeforeCheck;
348      treeView.AfterCheck += treeView_AfterCheck;
349      treeView.ExpandAll();
350
351      return top;
352    }
[15559]353
[15422]354    #endregion
355  }
356}
Note: See TracBrowser for help on using the repository browser.