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 |
|
---|
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 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 | 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>>();
|
---|
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
|
---|
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();
|
---|
71 | newAssignedResources.Clear();
|
---|
72 | inheritedResources.Clear();
|
---|
73 | resourceAncestors.Clear();
|
---|
74 | treeView.Nodes.Clear();
|
---|
75 | detailsViewHost.Content = null;
|
---|
76 | } else {
|
---|
77 | UpdateAssignedResources();
|
---|
78 | UpdateResourceGenealogy();
|
---|
79 | var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
|
---|
80 | detailsViewHost.Content = top;
|
---|
81 | detailsViewHost.ActiveView.Locked = true;
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
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();
|
---|
94 | UpdateResourceGenealogy();
|
---|
95 | var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
|
---|
96 | detailsViewHost.Content = top;
|
---|
97 | }
|
---|
98 |
|
---|
99 | private async void inheritButton_Click(object sender, EventArgs e) {
|
---|
100 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
101 | action: () => {
|
---|
102 | SetAssignedProjectResources(Content.Id, newAssignedResources.Select(x => x.Id), false, true);
|
---|
103 | });
|
---|
104 | UpdateResourceTree();
|
---|
105 | }
|
---|
106 |
|
---|
107 | private async void saveButton_Click(object sender, EventArgs e) {
|
---|
108 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
109 | action: () => {
|
---|
110 | SetAssignedProjectResources(Content.Id, newAssignedResources.Select(x => x.Id), false, false);
|
---|
111 | });
|
---|
112 | UpdateResourceTree();
|
---|
113 | }
|
---|
114 |
|
---|
115 | private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
116 | var selectedResource = (Resource)e.Node.Tag;
|
---|
117 | detailsViewHost.Content = selectedResource;
|
---|
118 | }
|
---|
119 |
|
---|
120 | private void treeView_BeforeCheck(object sender, TreeViewCancelEventArgs e) {
|
---|
121 | var checkedResource = (Resource)e.Node.Tag;
|
---|
122 | if (newInheritedResources.Contains(checkedResource) || checkedResource.Id == Guid.Empty) e.Cancel = true;
|
---|
123 | }
|
---|
124 |
|
---|
125 | private void treeView_AfterCheck(object sender, TreeViewEventArgs e) {
|
---|
126 | var checkedResource = (Resource)e.Node.Tag;
|
---|
127 | if (e.Node.Checked) {
|
---|
128 | newAssignedResources.Add(checkedResource);
|
---|
129 | } else {
|
---|
130 | newAssignedResources.Remove(checkedResource);
|
---|
131 | }
|
---|
132 |
|
---|
133 | UpdateNewResourceTree();
|
---|
134 | }
|
---|
135 | #endregion
|
---|
136 |
|
---|
137 | #region Helpers
|
---|
138 |
|
---|
139 | private void UpdateResourceTree() {
|
---|
140 | UpdateAssignedResources();
|
---|
141 | UpdateResourceGenealogy();
|
---|
142 | var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
|
---|
143 | detailsViewHost.Content = top;
|
---|
144 | }
|
---|
145 |
|
---|
146 | private void UpdateNewResourceTree() {
|
---|
147 | UpdateNewAssignedResources();
|
---|
148 | UpdateNewInheritedResources();
|
---|
149 | var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
|
---|
150 | detailsViewHost.Content = top;
|
---|
151 | }
|
---|
152 |
|
---|
153 | private static IEnumerable<Resource> GetAssignedResourcesForProject(Guid projectId) {
|
---|
154 | var assignedProjectResources = HiveServiceLocator.Instance.CallHiveService(s => s.GetAssignedResourcesForProjectAdministration(projectId));
|
---|
155 | return HiveAdminClient.Instance.Resources.Where(x => assignedProjectResources.Select(y => y.ResourceId).Contains(x.Id));
|
---|
156 | }
|
---|
157 |
|
---|
158 | private void SetAssignedProjectResources(Guid projectId, IEnumerable<Guid> resourceIds, bool reassign, bool cascading) {
|
---|
159 | if (projectId == null || resourceIds == null) return;
|
---|
160 | HiveServiceLocator.Instance.CallHiveService(s => {
|
---|
161 | s.SaveProjectResourceAssignments(projectId, resourceIds.ToList(), reassign, cascading, true);
|
---|
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 parentedMainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>()
|
---|
245 | .Where(x => x.ParentResourceId.HasValue && !resources.Select(y => y.Id).Contains(x.ParentResourceId.Value)));
|
---|
246 | mainResources.UnionWith(parentedMainResources);
|
---|
247 | var subResources = new HashSet<Resource>(resources.Except(mainResources));
|
---|
248 |
|
---|
249 | var stack = new Stack<Resource>(mainResources.OrderByDescending(x => x.Name));
|
---|
250 | Resource top = null;
|
---|
251 |
|
---|
252 | TreeNode currentNode = null;
|
---|
253 | Resource currentResource = null;
|
---|
254 |
|
---|
255 |
|
---|
256 | var addedAssignments = newAssignedResources.Except(assignedResources);
|
---|
257 | var removedAssignments = assignedResources.Except(newAssignedResources);
|
---|
258 | var addedIncludes = newInheritedResources.Except(inheritedResources);
|
---|
259 | var removedIncludes = inheritedResources.Except(newInheritedResources);
|
---|
260 |
|
---|
261 | //var assignmentDiff = new HashSet<Resource>(newAssignedResources);
|
---|
262 | //assignmentDiff.SymmetricExceptWith(assignedResources);
|
---|
263 | //var inheritanceDiff = new HashSet<Resource>(newInheritedResources);
|
---|
264 | //inheritanceDiff.SymmetricExceptWith(inheritedResources);
|
---|
265 |
|
---|
266 | while (stack.Any()) {
|
---|
267 | if(top == null) top = stack.Peek();
|
---|
268 | var newResource = stack.Pop();
|
---|
269 | var newNode = new TreeNode(newResource.Name) { Tag = newResource };
|
---|
270 |
|
---|
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)
|
---|
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 |
|
---|
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 |
|
---|
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 += " [added include]";
|
---|
298 | } else if (removedIncludes.Contains(newResource)) {
|
---|
299 | newNode.BackColor = removedIncludeColor;
|
---|
300 | newNode.Text += " [removed include]";
|
---|
301 | }
|
---|
302 |
|
---|
303 | if (addedAssignments.Contains(newResource)) {
|
---|
304 | newNode.BackColor = addedAssignmentColor;
|
---|
305 | newNode.ForeColor = SystemColors.ControlText;
|
---|
306 | newNode.Text += " [added assignment]";
|
---|
307 | } else if (removedAssignments.Contains(newResource)) {
|
---|
308 | newNode.BackColor = removedAssignmentColor;
|
---|
309 | newNode.ForeColor = SystemColors.ControlText;
|
---|
310 | newNode.Text += " [removed assignment]";
|
---|
311 | }
|
---|
312 |
|
---|
313 | if (newResource is Slave) {
|
---|
314 | newNode.ImageIndex = slaveImageIndex;
|
---|
315 | } else {
|
---|
316 | newNode.ImageIndex = slaveGroupImageIndex;
|
---|
317 |
|
---|
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);
|
---|
323 | }
|
---|
324 | currentNode = newNode;
|
---|
325 | currentResource = newResource;
|
---|
326 | }
|
---|
327 | }
|
---|
328 | newNode.SelectedImageIndex = newNode.ImageIndex;
|
---|
329 | //if (newResource.OwnerUserId == UserInformation.Instance.User.Id)
|
---|
330 | // newNode.BackColor = ownedResourceColor;
|
---|
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 |
|
---|
341 | foreach (var slave in subResources.OfType<Slave>().OrderBy(x => x.Name)) {
|
---|
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 | }
|
---|
353 |
|
---|
354 | #endregion
|
---|
355 | }
|
---|
356 | }
|
---|