Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15567 was 15567, checked in by jzenisek, 6 years ago

#2839 worked on views for project and resource administration

File size: 13.6 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
99    private void inheritButton_Click(object sender, EventArgs e) {
[15559]100      return;
[15422]101      SetAssignedProjectResources(Content.Id, Enumerable.Empty<Guid>());
102      UpdateAssignedResources();
[15559]103      UpdateResourceGenealogy();
[15422]104      var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
105      detailsViewHost.Content = top;
106    }
107
108    private async void saveButton_Click(object sender, EventArgs e) {
109      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
[15559]110        action: () => {
111          SetAssignedProjectResources(Content.Id, newAssignedResources.Select(x => x.Id));
112        });
113      UpdateResourceTree();
[15422]114    }
115
116    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
117      var selectedResource = (Resource)e.Node.Tag;
118      detailsViewHost.Content = selectedResource;
119    }
120
121    private void treeView_BeforeCheck(object sender, TreeViewCancelEventArgs e) {
122      var checkedResource = (Resource)e.Node.Tag;
[15559]123      if (newInheritedResources.Contains(checkedResource) || checkedResource.Id == Guid.Empty) e.Cancel = true;
[15422]124    }
125
126    private void treeView_AfterCheck(object sender, TreeViewEventArgs e) {
127      var checkedResource = (Resource)e.Node.Tag;
[15559]128      if (e.Node.Checked) {
129        newAssignedResources.Add(checkedResource);
130      } else {
131        newAssignedResources.Remove(checkedResource);
132      }
133
134      UpdateNewAssignedResources();
135      UpdateNewInheritedResources();
136      var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
137      detailsViewHost.Content = top;
[15422]138    }
139    #endregion
140
141    #region Helpers
[15559]142
143    //private void DisableResourceTree(TreeNode node) {
144    //  foreach(var n in node.Nodes) {
145
146    //  }
147    //}
148
149    private void UpdateResourceTree() {
150      UpdateAssignedResources();
151      UpdateResourceGenealogy();
152      var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
153      detailsViewHost.Content = top;
154    }
155
[15422]156    private static IEnumerable<Resource> GetAssignedResourcesForProject(Guid projectId) {
157      var assignedProjectResources = HiveServiceLocator.Instance.CallHiveService(s => s.GetAssignedResourcesForProject(projectId));
[15559]158      return HiveAdminClient.Instance.Resources.Where(x => assignedProjectResources.Select(y => y.ResourceId).Contains(x.Id));
[15422]159    }
160
[15559]161    private void SetAssignedProjectResources(Guid projectId, IEnumerable<Guid> resourceIds) {
[15422]162      HiveServiceLocator.Instance.CallHiveService(s => {
163        var currentlyAssignedResources = s.GetAssignedResourcesForProject(projectId);
164        s.UnassignProjectResources(projectId, currentlyAssignedResources.Select(x => x.ResourceId).ToList());
165        s.AssignProjectResources(projectId, resourceIds.ToList());
166      });
167    }
168
[15559]169    private void UpdateNewAssignedResources() {
170      for(int i = newAssignedResources.Count -1; i >= 0; i--) {
171        if(newAssignedResources.Intersect(resourceAncestors[newAssignedResources.ElementAt(i).Id]).Any()) {
172          newAssignedResources.Remove(newAssignedResources.ElementAt(i));
173        }
174      }
175    }
176
[15422]177    private void UpdateAssignedResources() {
178      assignedResources.Clear();
[15559]179      newAssignedResources.Clear();
180      foreach (var r in GetAssignedResourcesForProject(Content.Id)) {
[15422]181        assignedResources.Add(r);
[15559]182        newAssignedResources.Add(r);
183      }
[15422]184    }
185
[15559]186    private void UpdateNewInheritedResources() {
187      newInheritedResources.Clear();
188      foreach (var a in newAssignedResources) {
189        if (resourceDescendants.ContainsKey(a.Id)) {
190          foreach (var r in resourceDescendants[a.Id]) {
191            newInheritedResources.Add(r);
192          }
193        }
194      }
195    }
196
197    private void UpdateResourceGenealogy() {
198      resourceAncestors.Clear();
199      resourceDescendants.Clear();
200      var resources = HiveAdminClient.Instance.Resources;
201
202      foreach(var r in resources) {
203        resourceAncestors.Add(r.Id, new HashSet<Resource>());
204        resourceDescendants.Add(r.Id, new HashSet<Resource>());
205      }
206
207      foreach(var r in resources) {
208        var parentResourceId = r.ParentResourceId;
209        while(parentResourceId != null) {
210          var parent = resources.SingleOrDefault(x => x.Id == parentResourceId);
211          if(parent != null) {
212            resourceAncestors[r.Id].Add(parent);
213            resourceDescendants[parent.Id].Add(r);
214            parentResourceId = parent.ParentResourceId;
215          } else {
216            parentResourceId = null;
217          }
218        }
219      }
220
[15422]221      inheritedResources.Clear();
[15559]222      newInheritedResources.Clear();
223      foreach(var a in assignedResources) {
224        if (resourceDescendants.ContainsKey(a.Id)) {
225          foreach(var r in resourceDescendants[a.Id]) {
226            inheritedResources.Add(r);
227            newInheritedResources.Add(r);
228          }
229        }
[15422]230      }
[15559]231
232      //foreach(var r in resources) {
233      //  if (resourceAncestors.ContainsKey(r.Id)
234      //    && resourceAncestors[r.Id].Intersect(assignedResources.Select(x => x.Id)).Any()) {
235      //    inheritedResources.Add(r);
236      //  }
237      //}
[15422]238    }
239
240    private Resource BuildResourceTree(IEnumerable<Resource> resources) {
241      treeView.Nodes.Clear();
242      if (!resources.Any()) return null;
243
244      treeView.BeforeCheck -= treeView_BeforeCheck;
245      treeView.AfterCheck -= treeView_AfterCheck;
246
[15559]247      var mainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>().Where(x => x.ParentResourceId == null));
248      var subResources = new HashSet<Resource>(resources.Except(mainResources));
[15422]249
[15559]250      var stack = new Stack<Resource>(mainResources.OrderByDescending(x => x.Name));
[15422]251      var top = stack.Peek();
252
253      TreeNode currentNode = null;
254      Resource currentResource = null;
255
[15559]256
[15567]257      var addedAssignments = newAssignedResources.Except(assignedResources);
258      var removedAssignments = assignedResources.Except(newAssignedResources);
259      var addedIncludes = newInheritedResources.Except(inheritedResources);
260      var removedIncludes = inheritedResources.Except(newInheritedResources);
261
262      //var assignmentDiff = new HashSet<Resource>(newAssignedResources);
263      //assignmentDiff.SymmetricExceptWith(assignedResources);
264      //var inheritanceDiff = new HashSet<Resource>(newInheritedResources);
265      //inheritanceDiff.SymmetricExceptWith(inheritedResources);
266
[15422]267      while (stack.Any()) {
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;
297          newNode.Text += " [+included]";
298        } else if (removedIncludes.Contains(newResource)) {
299          newNode.BackColor = removedIncludeColor;
300          newNode.Text += " [-included]";
[15559]301        }
302
[15567]303        if (addedAssignments.Contains(newResource)) {
304          newNode.BackColor = addedAssignmentColor;
305          newNode.ForeColor = SystemColors.ControlText;
306          newNode.Text += " [added]";
307        } else if (removedAssignments.Contains(newResource)) {
308          newNode.BackColor = removedAssignmentColor;
309          newNode.ForeColor = SystemColors.ControlText;
310          newNode.Text += " [removed]";
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.