Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2839 worked views for Projects and ProjectResources

File size: 12.4 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;
31
32namespace HeuristicLab.Clients.Hive.Administrator.Views {
33  [View("ProjectView")]
34  [Content(typeof(Project), IsDefaultView = false)]
35  public partial class ProjectResourcesView : ItemView {
36    private const int slaveImageIndex = 0;
37    private const int slaveGroupImageIndex = 1;
38    public const string ungroupedGroupName = "UNGROUPED";
39    public const string ungroupedGroupDescription = "Contains slaves that are not assigned to any group.";
40
41    private readonly HashSet<Resource> assignedResources = new HashSet<Resource>();
42    private readonly HashSet<Resource> newAssignedResources = new HashSet<Resource>();
43    private readonly HashSet<Resource> inheritedResources = new HashSet<Resource>();
44    private readonly HashSet<Resource> newInheritedResources = new HashSet<Resource>();
45    private readonly Dictionary<Guid, HashSet<Resource>> resourceAncestors = new Dictionary<Guid, HashSet<Resource>>();
46    private readonly Dictionary<Guid, HashSet<Resource>> resourceDescendants = new Dictionary<Guid, HashSet<Resource>>();
47    //private readonly Color ownedResourceColor = Color.LightGreen;
48    private readonly Color changedAssignmentColor = Color.FromArgb(255, Color.LightGreen);
49    private readonly Color changedInheritanceColor = Color.FromArgb(255, Color.LightGray);
50
51    public new Project Content {
52      get { return (Project)base.Content; }
53      set { base.Content = value; }
54    }
55
56    public ProjectResourcesView() {
57      InitializeComponent();
58
59      treeView.ImageList.Images.Add(VSImageLibrary.MonitorLarge);
60      treeView.ImageList.Images.Add(VSImageLibrary.NetworkCenterLarge);
61    }
62
63    #region Overrides
64    protected override void OnContentChanged() {
65      base.OnContentChanged();
66      if (Content == null) {
67        assignedResources.Clear();
68        newAssignedResources.Clear();
69        inheritedResources.Clear();
70        resourceAncestors.Clear();
71        treeView.Nodes.Clear();
72        detailsViewHost.Content = null;
73      } else {
74        UpdateAssignedResources();
75        UpdateResourceGenealogy();
76        var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
77        detailsViewHost.Content = top;
78      }
79    }
80
81    #endregion
82
83    #region Event Handlers
84    private void ProjectResourcesView_Load(object sender, EventArgs e) {
85
86    }
87
88    private void refreshButton_Click(object sender, EventArgs e) {
89      UpdateAssignedResources();
90      UpdateResourceGenealogy();
91      var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
92      detailsViewHost.Content = top;
93    }
94
95    private void inheritButton_Click(object sender, EventArgs e) {
96      return;
97      SetAssignedProjectResources(Content.Id, Enumerable.Empty<Guid>());
98      UpdateAssignedResources();
99      UpdateResourceGenealogy();
100      var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
101      detailsViewHost.Content = top;
102    }
103
104    private async void saveButton_Click(object sender, EventArgs e) {
105      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
106        action: () => {
107          SetAssignedProjectResources(Content.Id, newAssignedResources.Select(x => x.Id));
108        });
109      UpdateResourceTree();
110    }
111
112    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
113      var selectedResource = (Resource)e.Node.Tag;
114      detailsViewHost.Content = selectedResource;
115    }
116
117    private void treeView_BeforeCheck(object sender, TreeViewCancelEventArgs e) {
118      var checkedResource = (Resource)e.Node.Tag;
119      if (newInheritedResources.Contains(checkedResource) || checkedResource.Id == Guid.Empty) e.Cancel = true;
120    }
121
122    private void treeView_AfterCheck(object sender, TreeViewEventArgs e) {
123      var checkedResource = (Resource)e.Node.Tag;
124      if (e.Node.Checked) {
125        newAssignedResources.Add(checkedResource);
126      } else {
127        newAssignedResources.Remove(checkedResource);
128      }
129
130      UpdateNewAssignedResources();
131      UpdateNewInheritedResources();
132      var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
133      detailsViewHost.Content = top;
134    }
135    #endregion
136
137    #region Helpers
138
139    //private void DisableResourceTree(TreeNode node) {
140    //  foreach(var n in node.Nodes) {
141
142    //  }
143    //}
144
145    private void UpdateResourceTree() {
146      UpdateAssignedResources();
147      UpdateResourceGenealogy();
148      var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
149      detailsViewHost.Content = top;
150    }
151
152    private static IEnumerable<Resource> GetAssignedResourcesForProject(Guid projectId) {
153      var assignedProjectResources = HiveServiceLocator.Instance.CallHiveService(s => s.GetAssignedResourcesForProject(projectId));
154      return HiveAdminClient.Instance.Resources.Where(x => assignedProjectResources.Select(y => y.ResourceId).Contains(x.Id));
155    }
156
157    private void SetAssignedProjectResources(Guid projectId, IEnumerable<Guid> resourceIds) {
158      HiveServiceLocator.Instance.CallHiveService(s => {
159        var currentlyAssignedResources = s.GetAssignedResourcesForProject(projectId);
160        s.UnassignProjectResources(projectId, currentlyAssignedResources.Select(x => x.ResourceId).ToList());
161        s.AssignProjectResources(projectId, resourceIds.ToList());
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      var assignmentDiff = new HashSet<Resource>(newAssignedResources);
253      assignmentDiff.SymmetricExceptWith(assignedResources);
254      var inheritanceDiff = new HashSet<Resource>(newInheritedResources);
255      inheritanceDiff.SymmetricExceptWith(inheritedResources);
256
257      while (stack.Any()) {
258        var newResource = stack.Pop();
259        var newNode = new TreeNode(newResource.Name) { Tag = newResource };
260
261        // search for parent node of newNode and save in currentNode
262        // necessary since newNodes (stack top items) might be siblings
263        // or grand..grandparents of previous node (currentNode)
264        while (currentNode != null && newResource.ParentResourceId != currentResource.Id) {
265          currentNode = currentNode.Parent;
266          currentResource = currentNode == null ? null : (Resource)currentNode.Tag;
267        }
268
269        if (currentNode == null) {
270          treeView.Nodes.Add(newNode);
271        } else {
272          currentNode.Nodes.Add(newNode);
273        }
274
275
276        if (newAssignedResources.Contains(newResource)) {
277          newNode.Checked = true;
278        } else if (newInheritedResources.Contains(newResource)) {
279          newNode.Checked = true;
280          newNode.Text += " (Inherited)";
281          newNode.ForeColor = SystemColors.GrayText;
282        }
283
284        if (assignmentDiff.Contains(newResource)) {
285          newNode.BackColor = changedAssignmentColor;
286          newNode.Text += " [changed]";
287        } else if(inheritanceDiff.Contains(newResource)) {
288          newNode.BackColor = changedInheritanceColor;
289          newNode.Text += " [changed]";
290        }
291
292        if (newResource is Slave) {
293          newNode.ImageIndex = slaveImageIndex;
294        } else {
295          newNode.ImageIndex = slaveGroupImageIndex;
296
297          var childResources = subResources.Where(x => x.ParentResourceId == newResource.Id);
298          if (childResources.Any()) {
299            foreach (var resource in childResources.OrderByDescending(x => x.Name)) {
300              subResources.Remove(resource);
301              stack.Push(resource);
302            }
303            currentNode = newNode;
304            currentResource = newResource;
305          }
306        }
307        newNode.SelectedImageIndex = newNode.ImageIndex;
308        //if (newResource.OwnerUserId == UserInformation.Instance.User.Id)
309        //  newNode.BackColor = ownedResourceColor;
310      }
311
312      var ungroupedNode = new TreeNode(ungroupedGroupName) {
313        ForeColor = SystemColors.GrayText,
314        Tag = new SlaveGroup() {
315          Name = ungroupedGroupName,
316          Description = ungroupedGroupDescription
317        }
318      };
319
320      foreach (var slave in subResources.OfType<Slave>().OrderBy(x => x.Name)) {
321        var slaveNode = new TreeNode(slave.Name) { Tag = slave };
322        ungroupedNode.Nodes.Add(slaveNode);
323      }
324
325      treeView.Nodes.Add(ungroupedNode);
326      treeView.BeforeCheck += treeView_BeforeCheck;
327      treeView.AfterCheck += treeView_AfterCheck;
328      treeView.ExpandAll();
329
330      return top;
331    }
332
333    #endregion
334  }
335}
Note: See TracBrowser for help on using the repository browser.