Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ProjectResourcesView.cs @ 15761

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

#2839

  • minor changes regarding project- & resource selection
  • adapted DeleteOnNull rules in dbml
File size: 15.8 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
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      var assignedProjectResources = HiveServiceLocator.Instance.CallHiveService(s => s.GetAssignedResourcesForProjectAdministration(projectId));
173      return HiveAdminClient.Instance.Resources.Where(x => assignedProjectResources.Select(y => y.ResourceId).Contains(x.Id));
174    }
175
176    private void SetAssignedProjectResources(Guid projectId, IEnumerable<Guid> resourceIds, bool reassign, bool cascading, bool reassignCascading) {
177      if (projectId == null || resourceIds == null) return;
178      HiveServiceLocator.Instance.CallHiveService(s => {
179       s.SaveProjectResourceAssignments(projectId, resourceIds.ToList(), reassign, cascading, reassignCascading);
180      });
181    }
182
183    private void UpdateNewAssignedResources() {
184      for(int i = newAssignedResources.Count -1; i >= 0; i--) {
185        if(newAssignedResources.Intersect(resourceAncestors[newAssignedResources.ElementAt(i).Id]).Any()) {
186          newAssignedResources.Remove(newAssignedResources.ElementAt(i));
187        }
188      }
189    }
190
191    private void UpdateAssignedResources() {
192      assignedResources.Clear();
193      newAssignedResources.Clear();
194      foreach (var r in GetAssignedResourcesForProject(Content.Id)) {
195        assignedResources.Add(r);
196        newAssignedResources.Add(r);
197      }
198    }
199
200    private void UpdateNewInheritedResources() {
201      newInheritedResources.Clear();
202      foreach (var a in newAssignedResources) {
203        if (resourceDescendants.ContainsKey(a.Id)) {
204          foreach (var r in resourceDescendants[a.Id]) {
205            newInheritedResources.Add(r);
206          }
207        }
208      }
209    }
210
211    private void UpdateResourceGenealogy() {
212      resourceAncestors.Clear();
213      resourceDescendants.Clear();
214      var resources = HiveAdminClient.Instance.Resources;
215
216      foreach(var r in resources.Where(x => x.Id != Guid.Empty)) {
217        resourceAncestors.Add(r.Id, new HashSet<Resource>());
218        resourceDescendants.Add(r.Id, new HashSet<Resource>());
219      }
220
221      foreach(var r in resources.Where(x => x.Id != Guid.Empty)) {
222        var parentResourceId = r.ParentResourceId;
223        while(parentResourceId != null) {
224          var parent = resources.SingleOrDefault(x => x.Id == parentResourceId);
225          if(parent != null) {
226            resourceAncestors[r.Id].Add(parent);
227            resourceDescendants[parent.Id].Add(r);
228            parentResourceId = parent.ParentResourceId;
229          } else {
230            parentResourceId = null;
231          }
232        }
233      }
234
235      inheritedResources.Clear();
236      newInheritedResources.Clear();
237      foreach(var a in assignedResources) {
238        if (resourceDescendants.ContainsKey(a.Id)) {
239          foreach(var r in resourceDescendants[a.Id]) {
240            inheritedResources.Add(r);
241            newInheritedResources.Add(r);
242          }
243        }
244      }
245
246      //foreach(var r in resources) {
247      //  if (resourceAncestors.ContainsKey(r.Id)
248      //    && resourceAncestors[r.Id].Intersect(assignedResources.Select(x => x.Id)).Any()) {
249      //    inheritedResources.Add(r);
250      //  }
251      //}
252    }
253
254    private Resource BuildResourceTree(IEnumerable<Resource> resources) {
255      treeView.Nodes.Clear();
256      if (!resources.Any()) return null;
257
258      treeView.BeforeCheck -= treeView_BeforeCheck;
259      treeView.AfterCheck -= treeView_AfterCheck;
260
261      resources = resources.Where(x => x.Id != Guid.Empty);
262      var mainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>().Where(x => x.ParentResourceId == null));
263      var parentedMainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>()
264        .Where(x => x.ParentResourceId.HasValue && !resources.Select(y => y.Id).Contains(x.ParentResourceId.Value)));
265      mainResources.UnionWith(parentedMainResources);
266      var subResources = new HashSet<Resource>(resources.Except(mainResources));
267
268      var stack = new Stack<Resource>(mainResources.OrderByDescending(x => x.Name));
269      Resource top = null;
270
271      TreeNode currentNode = null;
272      Resource currentResource = null;
273
274
275      var addedAssignments = newAssignedResources.Except(assignedResources);
276      var removedAssignments = assignedResources.Except(newAssignedResources);
277      var addedIncludes = newInheritedResources.Except(inheritedResources);
278      var removedIncludes = inheritedResources.Except(newInheritedResources);
279
280      //var assignmentDiff = new HashSet<Resource>(newAssignedResources);
281      //assignmentDiff.SymmetricExceptWith(assignedResources);
282      //var inheritanceDiff = new HashSet<Resource>(newInheritedResources);
283      //inheritanceDiff.SymmetricExceptWith(inheritedResources);
284
285      while (stack.Any()) {
286        if(top == null)  top = stack.Peek();
287        var newResource = stack.Pop();
288        var newNode = new TreeNode(newResource.Name) { Tag = newResource };
289
290        // search for parent node of newNode and save in currentNode
291        // necessary since newNodes (stack top items) might be siblings
292        // or grand..grandparents of previous node (currentNode)
293        while (currentNode != null && newResource.ParentResourceId != currentResource.Id) {
294          currentNode = currentNode.Parent;
295          currentResource = currentNode == null ? null : (Resource)currentNode.Tag;
296        }
297
298        if (currentNode == null) {
299          treeView.Nodes.Add(newNode);
300        } else {
301          currentNode.Nodes.Add(newNode);
302        }
303
304        if (newAssignedResources.Contains(newResource)) {
305          newNode.Checked = true;
306          if(!HiveRoles.CheckAdminUserPermissions()) {
307            if(!projectAncestors[Content.Id].Any()) {
308              newNode.ForeColor = SystemColors.GrayText;
309              newNode.Text += " [immutable]";
310            }
311          }
312
313        } else if (newInheritedResources.Contains(newResource)) {
314          newNode.Checked = true;
315          newNode.ForeColor = SystemColors.GrayText;
316        }
317
318          if (inheritedResources.Contains(newResource) && newInheritedResources.Contains(newResource)) {
319          newNode.Text += " [included]";
320        } else if (addedIncludes.Contains(newResource)) {
321          newNode.BackColor = addedIncludeColor;
322          newNode.ForeColor = SystemColors.GrayText;
323          newNode.Text += " [added include]";
324        } else if (removedIncludes.Contains(newResource)) {
325          newNode.BackColor = removedIncludeColor;
326          newNode.Text += " [removed include]";
327        }
328
329        if (addedAssignments.Contains(newResource)) {
330          newNode.BackColor = addedAssignmentColor;
331          newNode.ForeColor = SystemColors.ControlText;
332          newNode.Text += " [added assignment]";
333        } else if (removedAssignments.Contains(newResource)) {
334          newNode.BackColor = removedAssignmentColor;
335          newNode.ForeColor = SystemColors.ControlText;
336          newNode.Text += " [removed assignment]";
337        }
338
339        if (newResource is Slave) {
340          newNode.ImageIndex = slaveImageIndex;
341        } else {
342          newNode.ImageIndex = slaveGroupImageIndex;
343
344          var childResources = subResources.Where(x => x.ParentResourceId == newResource.Id);
345          if (childResources.Any()) {
346            foreach (var resource in childResources.OrderByDescending(x => x.Name)) {
347              subResources.Remove(resource);
348              stack.Push(resource);
349            }
350            currentNode = newNode;
351            currentResource = newResource;
352          }
353        }
354        newNode.SelectedImageIndex = newNode.ImageIndex;
355        //if (newResource.OwnerUserId == UserInformation.Instance.User.Id)
356        //  newNode.BackColor = ownedResourceColor;
357      }
358
359      var ungroupedSlaves = subResources.OfType<Slave>().OrderBy(x => x.Name);
360      if(ungroupedSlaves.Any()) {
361        var ungroupedNode = new TreeNode(ungroupedGroupName) {
362          ForeColor = SystemColors.GrayText,
363          Tag = new SlaveGroup() {
364            Name = ungroupedGroupName,
365            Description = ungroupedGroupDescription
366          }
367        };
368
369        foreach (var slave in ungroupedSlaves) {
370          var slaveNode = new TreeNode(slave.Name) { Tag = slave };
371          ungroupedNode.Nodes.Add(slaveNode);
372        }
373        treeView.Nodes.Add(ungroupedNode);
374      }
375
376      treeView.BeforeCheck += treeView_BeforeCheck;
377      treeView.AfterCheck += treeView_AfterCheck;
378      treeView.ExpandAll();
379
380      return top;
381    }
382
383    private void UpdateProjectGenealogy() {
384      projectAncestors.Clear();
385      projectDescendants.Clear();
386      var projects = HiveAdminClient.Instance.Projects;
387
388      foreach(var p in projects.Where(x => x.Id != Guid.Empty)) {
389        projectAncestors.Add(p.Id, new HashSet<Project>());
390        projectDescendants.Add(p.Id, new HashSet<Project>());
391      }
392
393      foreach (var p in projects.Where(x => x.Id != Guid.Empty)) {
394        var parentProjectId = p.ParentProjectId;
395        while (parentProjectId != null) {
396          var parent = projects.SingleOrDefault(x => x.Id == parentProjectId);
397          if (parent != null) {
398            projectAncestors[p.Id].Add(parent);
399            projectDescendants[parent.Id].Add(p);
400            parentProjectId = parent.ParentProjectId;
401          } else {
402            parentProjectId = null;
403          }
404        }
405      }
406    }
407
408    #endregion
409  }
410}
Note: See TracBrowser for help on using the repository browser.