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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Windows.Forms;
|
---|
27 | using HeuristicLab.Clients.Access;
|
---|
28 | using HeuristicLab.Common.Resources;
|
---|
29 | using HeuristicLab.Core.Views;
|
---|
30 | using HeuristicLab.MainForm;
|
---|
31 | using System.Collections;
|
---|
32 |
|
---|
33 | namespace 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 |
|
---|
58 | private HashSet<Resource> projectExclusiveResources = new HashSet<Resource>();
|
---|
59 | private TreeNode ungroupedGroupNode;
|
---|
60 |
|
---|
61 | public new Project Content {
|
---|
62 | get { return (Project)base.Content; }
|
---|
63 | set { base.Content = value; }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public ProjectResourcesView() {
|
---|
67 | InitializeComponent();
|
---|
68 |
|
---|
69 | treeView.ImageList.Images.Add(VSImageLibrary.MonitorLarge);
|
---|
70 | treeView.ImageList.Images.Add(VSImageLibrary.NetworkCenterLarge);
|
---|
71 | }
|
---|
72 |
|
---|
73 | #region Overrides
|
---|
74 | protected override void OnContentChanged() {
|
---|
75 | base.OnContentChanged();
|
---|
76 | if (Content == null) {
|
---|
77 | assignedResources.Clear();
|
---|
78 | newAssignedResources.Clear();
|
---|
79 | includedResources.Clear();
|
---|
80 | treeView.Nodes.Clear();
|
---|
81 | detailsViewHost.Content = null;
|
---|
82 | } else {
|
---|
83 | UpdateAssignedResources();
|
---|
84 | UpdateIncludedResources();
|
---|
85 | var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
|
---|
86 | detailsViewHost.Content = top;
|
---|
87 | detailsViewHost.Locked = true;
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected override void SetEnabledStateOfControls() {
|
---|
92 | base.SetEnabledStateOfControls();
|
---|
93 | bool enabled = Content != null && !Locked && !ReadOnly;
|
---|
94 |
|
---|
95 | inheritButton.Enabled = enabled;
|
---|
96 | saveButton.Enabled = enabled;
|
---|
97 | treeView.Enabled = enabled;
|
---|
98 | }
|
---|
99 | #endregion
|
---|
100 |
|
---|
101 | #region Event Handlers
|
---|
102 | private void ProjectResourcesView_Load(object sender, EventArgs e) {
|
---|
103 |
|
---|
104 | }
|
---|
105 |
|
---|
106 | private void refreshButton_Click(object sender, EventArgs e) {
|
---|
107 | HiveAdminClient.Instance.Refresh();
|
---|
108 | UpdateAssignedResources();
|
---|
109 | UpdateIncludedResources();
|
---|
110 | var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
|
---|
111 | detailsViewHost.Content = top;
|
---|
112 | }
|
---|
113 |
|
---|
114 | private async void inheritButton_Click(object sender, EventArgs e) {
|
---|
115 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
116 | action: () => {
|
---|
117 | SetAssignedProjectResources(Content.Id, newAssignedResources.Select(x => x.Id), false, true, false);
|
---|
118 | });
|
---|
119 | UpdateResourceTree();
|
---|
120 | }
|
---|
121 |
|
---|
122 | private async void saveButton_Click(object sender, EventArgs e) {
|
---|
123 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
124 | action: () => {
|
---|
125 | SetAssignedProjectResources(Content.Id, newAssignedResources.Select(x => x.Id), false, false, false);
|
---|
126 | });
|
---|
127 | HiveAdminClient.Instance.Refresh();
|
---|
128 | UpdateResourceTree();
|
---|
129 | }
|
---|
130 |
|
---|
131 | private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
132 | var selectedResource = (Resource)e.Node.Tag;
|
---|
133 | detailsViewHost.Content = selectedResource;
|
---|
134 | }
|
---|
135 |
|
---|
136 | private void treeView_BeforeCheck(object sender, TreeViewCancelEventArgs e) {
|
---|
137 | var checkedResource = (Resource)e.Node.Tag;
|
---|
138 | if (newIncludedResources.Contains(checkedResource) || checkedResource.Id == Guid.Empty || e.Node == ungroupedGroupNode) {
|
---|
139 | e.Cancel = true;
|
---|
140 | } else if (!HiveRoles.CheckAdminUserPermissions()) {
|
---|
141 | if (!HiveAdminClient.Instance.GetAvailableProjectAncestors(Content.Id).Any() || projectExclusiveResources.Contains(checkedResource)) {
|
---|
142 | e.Cancel = true;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | private void treeView_AfterCheck(object sender, TreeViewEventArgs e) {
|
---|
148 | var checkedResource = (Resource)e.Node.Tag;
|
---|
149 | if (e.Node.Checked) {
|
---|
150 | newAssignedResources.Add(checkedResource);
|
---|
151 | } else {
|
---|
152 | newAssignedResources.Remove(checkedResource);
|
---|
153 | }
|
---|
154 |
|
---|
155 | UpdateNewResourceTree();
|
---|
156 | }
|
---|
157 | #endregion
|
---|
158 |
|
---|
159 | #region Helpers
|
---|
160 |
|
---|
161 | private void UpdateResourceTree() {
|
---|
162 | UpdateAssignedResources();
|
---|
163 | UpdateIncludedResources();
|
---|
164 |
|
---|
165 | var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
|
---|
166 | detailsViewHost.Content = top;
|
---|
167 | }
|
---|
168 |
|
---|
169 | private void UpdateNewResourceTree() {
|
---|
170 | UpdateNewAssignedResources();
|
---|
171 | UpdateNewIncludedResources();
|
---|
172 | var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
|
---|
173 | detailsViewHost.Content = top;
|
---|
174 | }
|
---|
175 |
|
---|
176 | private IEnumerable<Resource> GetAvailableResourcesForProjectAdministration(Guid projectId) {
|
---|
177 | projectExclusiveResources.Clear();
|
---|
178 | if (projectId == Guid.Empty) return Enumerable.Empty<Resource>();
|
---|
179 | var resources = HiveAdminClient.Instance.Resources.Where(x => x.Id != Guid.Empty);
|
---|
180 | if (!resources.Any()) return resources;
|
---|
181 | if (IsAdmin()) return resources;
|
---|
182 |
|
---|
183 | // get project specific assigned resources
|
---|
184 | var projectResources = resources.Where(x =>
|
---|
185 | HiveAdminClient.Instance.ProjectResourceAssignments
|
---|
186 | .Where(a => a.ProjectId == projectId)
|
---|
187 | .Select(a => a.ResourceId)
|
---|
188 | .Contains(x.Id));
|
---|
189 |
|
---|
190 | // look up for assignments of ancestor projects
|
---|
191 | var projectIds = new HashSet<Guid>();
|
---|
192 | HiveClient.Instance.GetAvailableProjectAncestors(projectId).ToList().ForEach(x => projectIds.Add(x.Id));
|
---|
193 |
|
---|
194 | var ancestorProjectResources = resources.Where(x =>
|
---|
195 | HiveAdminClient.Instance.ProjectResourceAssignments
|
---|
196 | .Where(a => projectIds.Contains(a.ProjectId))
|
---|
197 | .Select(a => a.ResourceId)
|
---|
198 | .Contains(x.Id));
|
---|
199 |
|
---|
200 | // look down for included resources of ancestor projects
|
---|
201 | HashSet<Resource> availableResources = new HashSet<Resource>(ancestorProjectResources);
|
---|
202 | foreach (var r in ancestorProjectResources) {
|
---|
203 | foreach(var d in HiveAdminClient.Instance.GetAvailableResourceDescendants(r.Id)) {
|
---|
204 | availableResources.Add(d);
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | // determine resources, which are exclusively assigned to the currently selected project
|
---|
209 | projectResources
|
---|
210 | .Except(availableResources)
|
---|
211 | .ToList()
|
---|
212 | .ForEach(x => projectExclusiveResources.Add(x));
|
---|
213 |
|
---|
214 | // look down for included resources of currently selected project
|
---|
215 | if (projectExclusiveResources.Any()) {
|
---|
216 | foreach (var r in projectExclusiveResources.ToArray()) {
|
---|
217 | foreach (var d in HiveAdminClient.Instance.GetAvailableResourceDescendants(r.Id)) {
|
---|
218 | projectExclusiveResources.Add(d);
|
---|
219 | }
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | return availableResources.Union(projectExclusiveResources);
|
---|
224 | }
|
---|
225 |
|
---|
226 | private IEnumerable<Resource> GetAssignedResourcesForProject(Guid projectId) {
|
---|
227 | if (projectId == Guid.Empty) return Enumerable.Empty<Resource>();
|
---|
228 | return HiveAdminClient.Instance.Resources.Where(x =>
|
---|
229 | HiveAdminClient.Instance.ProjectResourceAssignments
|
---|
230 | .Where(a => a.ProjectId == projectId)
|
---|
231 | .Select(a => a.ResourceId)
|
---|
232 | .Contains(x.Id));
|
---|
233 | }
|
---|
234 |
|
---|
235 | private void SetAssignedProjectResources(Guid projectId, IEnumerable<Guid> resourceIds, bool reassign, bool cascading, bool reassignCascading) {
|
---|
236 | if (projectId == null || resourceIds == null) return;
|
---|
237 | HiveServiceLocator.Instance.CallHiveService(s => {
|
---|
238 | s.SaveProjectResourceAssignments(projectId, resourceIds.ToList(), reassign, cascading, reassignCascading);
|
---|
239 | });
|
---|
240 | }
|
---|
241 |
|
---|
242 | private void UpdateNewAssignedResources() {
|
---|
243 | for(int i = newAssignedResources.Count -1; i >= 0; i--) {
|
---|
244 | if(newAssignedResources.Intersect(HiveAdminClient.Instance.GetAvailableResourceAncestors(newAssignedResources.ElementAt(i).Id)).Any()) {
|
---|
245 | newAssignedResources.Remove(newAssignedResources.ElementAt(i));
|
---|
246 | }
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | private void UpdateAssignedResources() {
|
---|
251 | assignedResources.Clear();
|
---|
252 | newAssignedResources.Clear();
|
---|
253 | foreach (var r in GetAssignedResourcesForProject(Content.Id)) {
|
---|
254 | assignedResources.Add(r);
|
---|
255 | newAssignedResources.Add(r);
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | private void UpdateIncludedResources() {
|
---|
260 | includedResources.Clear();
|
---|
261 | newIncludedResources.Clear();
|
---|
262 | foreach (var a in assignedResources) {
|
---|
263 | foreach (var r in HiveAdminClient.Instance.GetAvailableResourceDescendants(a.Id)) {
|
---|
264 | includedResources.Add(r);
|
---|
265 | newIncludedResources.Add(r);
|
---|
266 | }
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | private void UpdateNewIncludedResources() {
|
---|
271 | newIncludedResources.Clear();
|
---|
272 | foreach (var a in newAssignedResources) {
|
---|
273 | foreach (var r in HiveAdminClient.Instance.GetAvailableResourceDescendants(a.Id)) {
|
---|
274 | newIncludedResources.Add(r);
|
---|
275 | }
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | private Resource BuildResourceTree(IEnumerable<Resource> resources) {
|
---|
280 | treeView.Nodes.Clear();
|
---|
281 | if (!resources.Any()) return null;
|
---|
282 |
|
---|
283 | treeView.BeforeCheck -= treeView_BeforeCheck;
|
---|
284 | treeView.AfterCheck -= treeView_AfterCheck;
|
---|
285 |
|
---|
286 | resources = GetAvailableResourcesForProjectAdministration(Content.Id);
|
---|
287 |
|
---|
288 | var mainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>().Where(x => x.ParentResourceId == null));
|
---|
289 | var parentedMainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>()
|
---|
290 | .Where(x => x.ParentResourceId.HasValue && !resources.Select(y => y.Id).Contains(x.ParentResourceId.Value)));
|
---|
291 | mainResources.UnionWith(parentedMainResources);
|
---|
292 | var subResources = new HashSet<Resource>(resources.Except(mainResources));
|
---|
293 |
|
---|
294 | var stack = new Stack<Resource>(mainResources.OrderByDescending(x => x.Name));
|
---|
295 | Resource top = null;
|
---|
296 |
|
---|
297 | TreeNode currentNode = null;
|
---|
298 | Resource currentResource = null;
|
---|
299 |
|
---|
300 | var addedAssignments = newAssignedResources.Except(assignedResources);
|
---|
301 | var removedAssignments = assignedResources.Except(newAssignedResources);
|
---|
302 | var addedIncludes = newIncludedResources.Except(includedResources);
|
---|
303 | var removedIncludes = includedResources.Except(newIncludedResources);
|
---|
304 |
|
---|
305 | while (stack.Any()) {
|
---|
306 | if(top == null) top = stack.Peek();
|
---|
307 | var newResource = stack.Pop();
|
---|
308 | var newNode = new TreeNode(newResource.Name) { Tag = newResource };
|
---|
309 |
|
---|
310 | // search for parent node of newNode and save in currentNode
|
---|
311 | // necessary since newNodes (stack top items) might be siblings
|
---|
312 | // or grand..grandparents of previous node (currentNode)
|
---|
313 | while (currentNode != null && newResource.ParentResourceId != currentResource.Id) {
|
---|
314 | currentNode = currentNode.Parent;
|
---|
315 | currentResource = currentNode == null ? null : (Resource)currentNode.Tag;
|
---|
316 | }
|
---|
317 |
|
---|
318 | if (currentNode == null) {
|
---|
319 | treeView.Nodes.Add(newNode);
|
---|
320 | } else {
|
---|
321 | currentNode.Nodes.Add(newNode);
|
---|
322 | }
|
---|
323 |
|
---|
324 | if (newAssignedResources.Contains(newResource)) {
|
---|
325 | newNode.Checked = true;
|
---|
326 | if(!HiveRoles.CheckAdminUserPermissions()) {
|
---|
327 | if (!HiveAdminClient.Instance.GetAvailableProjectAncestors(Content.Id).Any()
|
---|
328 | || projectExclusiveResources.Contains(newResource)) {
|
---|
329 | newNode.ForeColor = SystemColors.GrayText;
|
---|
330 | newNode.Text += IMMUTABLE_TAG;
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | } else if (newIncludedResources.Contains(newResource)) {
|
---|
335 | newNode.Checked = true;
|
---|
336 | newNode.ForeColor = SystemColors.GrayText;
|
---|
337 | }
|
---|
338 |
|
---|
339 | if (includedResources.Contains(newResource) && newIncludedResources.Contains(newResource)) {
|
---|
340 | newNode.Text += INCLUDED_TAG;
|
---|
341 | } else if (addedIncludes.Contains(newResource)) {
|
---|
342 | newNode.BackColor = addedIncludeColor;
|
---|
343 | newNode.ForeColor = SystemColors.GrayText;
|
---|
344 | newNode.Text += ADDED_INCLUDE_TAG;
|
---|
345 | } else if (removedIncludes.Contains(newResource)) {
|
---|
346 | newNode.BackColor = removedIncludeColor;
|
---|
347 | newNode.Text += REMOVED_INCLUDE_TAG;
|
---|
348 | }
|
---|
349 |
|
---|
350 | if (addedAssignments.Contains(newResource)) {
|
---|
351 | newNode.BackColor = addedAssignmentColor;
|
---|
352 | newNode.ForeColor = SystemColors.ControlText;
|
---|
353 | newNode.Text += ADDED_ASSIGNMENT_TAG;
|
---|
354 | } else if (removedAssignments.Contains(newResource)) {
|
---|
355 | newNode.BackColor = removedAssignmentColor;
|
---|
356 | newNode.ForeColor = SystemColors.ControlText;
|
---|
357 | newNode.Text += REMOVED_ASSIGNMENT_TAG;
|
---|
358 | }
|
---|
359 |
|
---|
360 | if (newResource is Slave) {
|
---|
361 | newNode.ImageIndex = slaveImageIndex;
|
---|
362 | } else {
|
---|
363 | newNode.ImageIndex = slaveGroupImageIndex;
|
---|
364 |
|
---|
365 | var childResources = subResources.Where(x => x.ParentResourceId == newResource.Id);
|
---|
366 | if (childResources.Any()) {
|
---|
367 | foreach (var resource in childResources.OrderByDescending(x => x.Name)) {
|
---|
368 | subResources.Remove(resource);
|
---|
369 | stack.Push(resource);
|
---|
370 | }
|
---|
371 | currentNode = newNode;
|
---|
372 | currentResource = newResource;
|
---|
373 | }
|
---|
374 | }
|
---|
375 | newNode.SelectedImageIndex = newNode.ImageIndex;
|
---|
376 | }
|
---|
377 |
|
---|
378 | var ungroupedSlaves = subResources.OfType<Slave>().OrderBy(x => x.Name);
|
---|
379 | if(ungroupedSlaves.Any()) {
|
---|
380 | ungroupedGroupNode = new TreeNode(UNGROUPED_GROUP_NAME) {
|
---|
381 | ForeColor = SystemColors.GrayText,
|
---|
382 | Tag = new SlaveGroup() {
|
---|
383 | Name = UNGROUPED_GROUP_NAME,
|
---|
384 | Description = UNGROUPED_GROUP_DESCRIPTION
|
---|
385 | }
|
---|
386 | };
|
---|
387 |
|
---|
388 | foreach (var slave in ungroupedSlaves) {
|
---|
389 | var slaveNode = new TreeNode(slave.Name) { Tag = slave };
|
---|
390 | ungroupedGroupNode.Nodes.Add(slaveNode);
|
---|
391 | }
|
---|
392 | treeView.Nodes.Add(ungroupedGroupNode);
|
---|
393 | } else {
|
---|
394 | ungroupedGroupNode?.Nodes.Clear();
|
---|
395 | }
|
---|
396 |
|
---|
397 | treeView.BeforeCheck += treeView_BeforeCheck;
|
---|
398 | treeView.AfterCheck += treeView_AfterCheck;
|
---|
399 | treeView.ExpandAll();
|
---|
400 |
|
---|
401 | return top;
|
---|
402 | }
|
---|
403 |
|
---|
404 | private bool IsAdmin() {
|
---|
405 | return HiveRoles.CheckAdminUserPermissions();
|
---|
406 | }
|
---|
407 |
|
---|
408 | #endregion
|
---|
409 | }
|
---|
410 | }
|
---|