Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ProjectResourcesView.cs @ 16202

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

#2839: fixed several bugs:

  • renamed tab "Slaves" to "Resources"
  • fixed bugs in job manager > resource selector reported by fholzinger
  • adapted client-side handling of disabled resource ancestors
  • adapted parentship check in HiveAdminClient
  • adapted seting of enabled state of controls for project details, project resources
  • introduced "inactive" tag in ProjectsView
File size: 18.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 UNGROUPED_GROUP_NAME = "UNGROUPED";
40    public const string UNGROUPED_GROUP_DESCRIPTION = "Contains slaves that are not assigned to any group.";
41    public const string IMMUTABLE_TAG = " [assigned, immutable]";
42    public const string INCLUDED_TAG = " [included]";
43    public const string ADDED_ASSIGNMENT_TAG = " [added assignment]";
44    public const string REMOVED_ASSIGNMENT_TAG = " [removed assignment]";
45    public const string ADDED_INCLUDE_TAG = " [added include]";
46    public const string REMOVED_INCLUDE_TAG = " [removed include]";
47
48    private readonly HashSet<Resource> assignedResources = new HashSet<Resource>();
49    private readonly HashSet<Resource> newAssignedResources = new HashSet<Resource>();
50    private readonly HashSet<Resource> includedResources = new HashSet<Resource>();
51    private readonly HashSet<Resource> newIncludedResources = new HashSet<Resource>();
52
53    private readonly Color addedAssignmentColor = Color.FromArgb(255, 87, 191, 193); // #57bfc1
54    private readonly Color removedAssignmentColor = Color.FromArgb(255, 236, 159, 72); // #ec9f48
55    private readonly Color addedIncludeColor = Color.FromArgb(25, 169, 221, 221); // #a9dddd
56    private readonly Color removedIncludeColor = Color.FromArgb(25, 249, 210, 145); // #f9d291
57    private readonly Color selectedBackColor = Color.DodgerBlue;
58    private readonly Color selectedForeColor = Color.White;
59    private readonly Color grayTextColor = SystemColors.GrayText;
60
61    private HashSet<Resource> projectExclusiveResources = new HashSet<Resource>();
62    private TreeNode ungroupedGroupNode;
63
64    public new Project Content {
65      get { return (Project)base.Content; }
66      set { base.Content = value; }
67    }
68
69    public ProjectResourcesView() {
70      InitializeComponent();
71
72      treeView.ImageList.Images.Add(VSImageLibrary.MonitorLarge);
73      treeView.ImageList.Images.Add(VSImageLibrary.NetworkCenterLarge);
74    }
75
76    #region Overrides
77    protected override void OnContentChanged() {
78      base.OnContentChanged();
79      if (Content == null) {
80        assignedResources.Clear();
81        newAssignedResources.Clear();
82        includedResources.Clear();
83        treeView.Nodes.Clear();
84        detailsViewHost.Content = null;
85      } else {
86        UpdateAssignedResources();
87        UpdateIncludedResources();
88        detailsViewHost.Content = BuildResourceTree();
89      }
90      SetEnabledStateOfControls();
91    }
92
93    protected override void SetEnabledStateOfControls() {
94      base.SetEnabledStateOfControls();
95      bool enabled = Content != null && !Locked && !ReadOnly;
96
97      inheritButton.Enabled = enabled;
98      saveButton.Enabled = enabled;
99      treeView.Enabled = enabled;
100
101      if (detailsViewHost != null) {
102        detailsViewHost.Locked = true;
103      }
104    }
105    #endregion
106
107    #region Event Handlers
108    private void ProjectResourcesView_Load(object sender, EventArgs e) {
109
110    }
111
112    private void refreshButton_Click(object sender, EventArgs e) {
113      HiveAdminClient.Instance.Refresh();
114      UpdateAssignedResources();
115      UpdateIncludedResources();
116      var top = BuildResourceTree();
117      detailsViewHost.Content = top;
118    }
119
120    private async void inheritButton_Click(object sender, EventArgs e) {
121      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
122        action: () => {
123          SetAssignedProjectResources(Content.Id, newAssignedResources.Select(x => x.Id), false, true, false);
124        });
125      HiveAdminClient.Instance.Refresh();
126      UpdateResourceTree();
127    }
128
129    private async void saveButton_Click(object sender, EventArgs e) {
130      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
131        action: () => {
132          SetAssignedProjectResources(Content.Id, newAssignedResources.Select(x => x.Id), false, false, false);
133        });
134      HiveAdminClient.Instance.Refresh();
135      UpdateResourceTree();
136    }
137
138    private void treeView_BeforeSelect(object sender, TreeViewCancelEventArgs e) {
139      if (e.Node == null || e.Node == ungroupedGroupNode)
140        e.Cancel = true;
141
142      var selectedResource = (Resource)e.Node.Tag;
143      if(HiveAdminClient.Instance.DisabledParentResources.Contains(selectedResource))
144        e.Cancel = true;
145    }
146
147    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
148      var selectedResource = (Resource)e.Node.Tag;
149      detailsViewHost.Content = selectedResource;
150    }
151
152    private void treeView_BeforeCheck(object sender, TreeViewCancelEventArgs e) {
153      if (e.Node == null || e.Node == ungroupedGroupNode) {
154        e.Cancel = true;
155        return;
156      }
157       
158      var checkedResource = (Resource)e.Node.Tag;
159      if (checkedResource == null
160        || checkedResource.Id == Guid.Empty
161        || HiveAdminClient.Instance.DisabledParentResources.Contains(checkedResource)
162        || newIncludedResources.Contains(checkedResource)) {
163        e.Cancel = true;
164      } else if (!IsAdmin()) {
165          if (!HiveAdminClient.Instance.CheckOwnershipOfParentProject(Content, UserInformation.Instance.User.Id)
166            || !HiveAdminClient.Instance.GetAvailableProjectAncestors(Content.Id).Any()
167            || projectExclusiveResources.Contains(checkedResource)) {
168            e.Cancel = true;
169          }
170      }
171    }
172
173    private void treeView_AfterCheck(object sender, TreeViewEventArgs e) {
174      var checkedResource = (Resource)e.Node.Tag;
175      if (e.Node.Checked) {
176        newAssignedResources.Add(checkedResource);
177      } else {
178        newAssignedResources.Remove(checkedResource);
179      }
180
181      UpdateNewResourceTree();
182    }
183    #endregion
184
185    #region Helpers
186
187    private void UpdateResourceTree() {
188      UpdateAssignedResources();
189      UpdateIncludedResources();
190
191      var top = BuildResourceTree();
192      detailsViewHost.Content = top;
193    }
194
195    private void UpdateNewResourceTree() {
196      UpdateNewAssignedResources();
197      UpdateNewIncludedResources();
198      var top = BuildResourceTree();
199      detailsViewHost.Content = top;
200    }
201
202    private IEnumerable<Resource> GetAvailableResourcesForProjectAdministration(Guid projectId) {
203      projectExclusiveResources.Clear();
204      if (projectId == Guid.Empty) return Enumerable.Empty<Resource>();
205      var resources = HiveAdminClient.Instance.Resources.Where(x => x.Id != Guid.Empty);
206      if (!resources.Any()) return resources;
207      if (IsAdmin()) return resources;
208
209      // get project specific assigned resources
210      var projectResources = resources.Where(x =>
211        HiveAdminClient.Instance.ProjectResourceAssignments
212          .Where(a => a.ProjectId == projectId)
213          .Select(a => a.ResourceId)
214        .Contains(x.Id));
215
216      // look up for assignments of ancestor projects
217      var projectIds = new HashSet<Guid>();
218      HiveAdminClient.Instance.GetAvailableProjectAncestors(projectId).ToList().ForEach(x => projectIds.Add(x.Id));
219
220      var ancestorProjectResources = resources.Where(x =>
221        HiveAdminClient.Instance.ProjectResourceAssignments
222          .Where(a => projectIds.Contains(a.ProjectId))
223          .Select(a => a.ResourceId)
224        .Contains(x.Id));
225
226      // look down for included resources of ancestor projects
227      HashSet<Resource> availableResources = new HashSet<Resource>(ancestorProjectResources);
228      foreach (var r in ancestorProjectResources) {
229        foreach(var d in HiveAdminClient.Instance.GetAvailableResourceDescendants(r.Id)) {
230          availableResources.Add(d);
231        }
232      }
233
234      // determine resources, which are exclusively assigned to the currently selected project
235      projectResources
236        .Except(availableResources)
237        .ToList()
238        .ForEach(x => projectExclusiveResources.Add(x));
239
240      // look down for included resources of currently selected project
241      if (projectExclusiveResources.Any()) {
242        foreach (var r in projectExclusiveResources.ToArray()) {
243          foreach (var d in HiveAdminClient.Instance.GetAvailableResourceDescendants(r.Id)) {
244            projectExclusiveResources.Add(d);
245          } 
246        }
247      }
248
249      return availableResources.Union(projectExclusiveResources);
250    }
251
252    private IEnumerable<Resource> GetAssignedResourcesForProject(Guid projectId) {
253      if (projectId == Guid.Empty) return Enumerable.Empty<Resource>();
254      return HiveAdminClient.Instance.Resources.Where(x =>
255        HiveAdminClient.Instance.ProjectResourceAssignments
256          .Where(a => a.ProjectId == projectId)
257          .Select(a => a.ResourceId)
258        .Contains(x.Id));
259    }
260
261    private void SetAssignedProjectResources(Guid projectId, IEnumerable<Guid> resourceIds, bool reassign, bool cascading, bool reassignCascading) {
262      if (projectId == null || resourceIds == null) return;
263      HiveServiceLocator.Instance.CallHiveService(s => {
264       s.SaveProjectResourceAssignments(projectId, resourceIds.ToList(), reassign, cascading, reassignCascading);
265      });
266    }
267
268    private void UpdateNewAssignedResources() {
269      for(int i = newAssignedResources.Count -1; i >= 0; i--) {
270        if(newAssignedResources.Intersect(HiveAdminClient.Instance.GetAvailableResourceAncestors(newAssignedResources.ElementAt(i).Id)).Any()) {
271          newAssignedResources.Remove(newAssignedResources.ElementAt(i));
272        }
273      }
274    }
275
276    private void UpdateAssignedResources() {
277      assignedResources.Clear();
278      newAssignedResources.Clear();
279      foreach (var r in GetAssignedResourcesForProject(Content.Id)) {
280        assignedResources.Add(r);
281        newAssignedResources.Add(r);
282      }
283    }
284
285    private void UpdateIncludedResources() {
286      includedResources.Clear();
287      newIncludedResources.Clear();
288      foreach (var a in assignedResources) {
289        foreach (var r in HiveAdminClient.Instance.GetAvailableResourceDescendants(a.Id)) {
290          includedResources.Add(r);
291          newIncludedResources.Add(r);
292        }
293      }
294    }
295
296    private void UpdateNewIncludedResources() {
297      newIncludedResources.Clear();
298      foreach (var a in newAssignedResources) {
299        foreach (var r in HiveAdminClient.Instance.GetAvailableResourceDescendants(a.Id)) {
300          newIncludedResources.Add(r);
301        }
302      }
303    }
304
305    private Resource BuildResourceTree() {
306      treeView.Nodes.Clear();
307
308      treeView.BeforeCheck -= treeView_BeforeCheck;
309      treeView.AfterCheck -= treeView_AfterCheck;
310
311      var resources = GetAvailableResourcesForProjectAdministration(Content.Id);
312
313      var disabledParentResources = HiveAdminClient.Instance.DisabledParentResources;
314      var mainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>().Where(x => x.ParentResourceId == null));
315      //var parentedMainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>()
316      //  .Where(x => x.ParentResourceId.HasValue && !resources.Select(y => y.Id).Contains(x.ParentResourceId.Value)));
317      //mainResources.UnionWith(parentedMainResources);
318      var mainDisabledParentResources = new HashSet<Resource>(disabledParentResources.Where(x => x.ParentResourceId == null || x.ParentResourceId == Guid.Empty));
319      mainResources.UnionWith(mainDisabledParentResources);
320      var subResources = new HashSet<Resource>(resources.Union(disabledParentResources).Except(mainResources).OrderByDescending(x => x.Name));
321
322      var stack = new Stack<Resource>(mainResources.OrderByDescending(x => x.Name));
323     
324      Resource top = null;
325      //bool nodeSelected = false;
326      if (detailsViewHost != null && detailsViewHost.Content != null && detailsViewHost.Content is Resource) {
327        var resourceId = ((Resource)detailsViewHost.Content).Id;
328        top = resources.Where(x => x.Id == resourceId).FirstOrDefault();
329      }
330     
331
332      TreeNode currentNode = null;
333      Resource currentResource = null;
334
335      var addedAssignments = newAssignedResources.Except(assignedResources);
336      var removedAssignments = assignedResources.Except(newAssignedResources);
337      var addedIncludes = newIncludedResources.Except(includedResources);
338      var removedIncludes = includedResources.Except(newIncludedResources);
339
340      while (stack.Any()) {
341        var newResource = stack.Pop();
342        var newNode = new TreeNode(newResource.Name) { Tag = newResource };
343
344        if(top == null && !disabledParentResources.Contains(newResource)) {
345          top = newResource;
346        }
347
348        //if(!nodeSelected && top != null && newResource.Id == top.Id) {
349        //  newNode.BackColor = selectedBackColor;
350        //  newNode.ForeColor = selectedForeColor;
351        //  nodeSelected = true;
352        //}
353
354        // search for parent node of newNode and save in currentNode
355        // necessary since newNodes (stack top items) might be siblings
356        // or grand..grandparents of previous node (currentNode)
357        while (currentNode != null && newResource.ParentResourceId != currentResource.Id) {
358          currentNode = currentNode.Parent;
359          currentResource = currentNode == null ? null : (Resource)currentNode.Tag;
360        }
361
362        if (currentNode == null) {
363          treeView.Nodes.Add(newNode);
364        } else {
365          currentNode.Nodes.Add(newNode);
366        }
367
368        if (disabledParentResources.Contains(newResource)) {
369          newNode.Checked = false;
370          newNode.ForeColor = grayTextColor;
371        } else if (newAssignedResources.Contains(newResource)) {
372          newNode.Checked = true;
373          if(!HiveRoles.CheckAdminUserPermissions()) {
374            if (!HiveAdminClient.Instance.CheckOwnershipOfParentProject(Content, UserInformation.Instance.User.Id)
375              || !HiveAdminClient.Instance.GetAvailableProjectAncestors(Content.Id).Any()
376              || projectExclusiveResources.Contains(newResource)) {
377              newNode.ForeColor = SystemColors.GrayText;
378              newNode.Text += IMMUTABLE_TAG;
379            }
380          }
381
382        } else if (newIncludedResources.Contains(newResource)) {
383          newNode.Checked = true;
384          newNode.ForeColor = SystemColors.GrayText;
385        }
386
387          if (includedResources.Contains(newResource) && newIncludedResources.Contains(newResource)) {
388          newNode.Text += INCLUDED_TAG;
389        } else if (addedIncludes.Contains(newResource)) {
390          newNode.BackColor = addedIncludeColor;
391          newNode.ForeColor = SystemColors.GrayText;
392          newNode.Text += ADDED_INCLUDE_TAG;
393        } else if (removedIncludes.Contains(newResource)) {
394          newNode.BackColor = removedIncludeColor;
395          newNode.Text += REMOVED_INCLUDE_TAG;
396        }
397
398        if (addedAssignments.Contains(newResource)) {
399          newNode.BackColor = addedAssignmentColor;
400          newNode.ForeColor = SystemColors.ControlText;
401          newNode.Text += ADDED_ASSIGNMENT_TAG;
402        } else if (removedAssignments.Contains(newResource)) {
403          newNode.BackColor = removedAssignmentColor;
404          newNode.ForeColor = SystemColors.ControlText;
405          newNode.Text += REMOVED_ASSIGNMENT_TAG;
406        }
407
408        if (newResource is Slave) {
409          newNode.ImageIndex = slaveImageIndex;
410        } else {
411          newNode.ImageIndex = slaveGroupImageIndex;
412
413          var childResources = subResources.Where(x => x.ParentResourceId == newResource.Id);
414          if (childResources.Any()) {
415            foreach (var resource in childResources.OrderByDescending(x => x.Name)) {
416              subResources.Remove(resource);
417              stack.Push(resource);
418            }
419            currentNode = newNode;
420            currentResource = newResource;
421          }
422        }
423        newNode.SelectedImageIndex = newNode.ImageIndex;
424      }
425
426      var ungroupedSlaves = subResources.OfType<Slave>().OrderBy(x => x.Name);
427      if(ungroupedSlaves.Any()) {
428        ungroupedGroupNode = new TreeNode(UNGROUPED_GROUP_NAME) {
429          ForeColor = SystemColors.GrayText,
430          Tag = new SlaveGroup() {
431            Name = UNGROUPED_GROUP_NAME,
432            Description = UNGROUPED_GROUP_DESCRIPTION
433          }
434        };
435
436        foreach (var slave in ungroupedSlaves) {
437          var slaveNode = new TreeNode(slave.Name) { Tag = slave };
438          ungroupedGroupNode.Nodes.Add(slaveNode);
439        }
440        treeView.Nodes.Add(ungroupedGroupNode);
441      } else if (ungroupedGroupNode != null) {
442        ungroupedGroupNode.Nodes.Clear();
443      }
444
445      treeView.BeforeCheck += treeView_BeforeCheck;
446      treeView.AfterCheck += treeView_AfterCheck;
447     
448      ExpandResourceNodesOfInterest(treeView.Nodes);
449
450      return top;
451    }
452
453    private void ExpandResourceNodesOfInterest(TreeNodeCollection nodes) {
454      foreach (TreeNode n in nodes) {
455        Resource r = (Resource)n.Tag;
456        if (n.Nodes.Count > 0) {
457          if (HiveAdminClient.Instance.GetAvailableResourceDescendants(r.Id).OfType<SlaveGroup>().Any()) {
458            n.Expand();
459            ExpandResourceNodesOfInterest(n.Nodes);
460          } else {
461            n.Collapse();
462          }
463        } else {
464          n.Collapse();
465        }
466      }
467    }
468
469    private bool IsAdmin() {
470      return HiveRoles.CheckAdminUserPermissions();
471    }
472
473    #endregion
474  }
475}
Note: See TracBrowser for help on using the repository browser.