[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 |
|
---|
| 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 |
|
---|
| 32 | namespace 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>();
|
---|
[15559] | 42 | private readonly HashSet<Resource> newAssignedResources = new HashSet<Resource>();
|
---|
[15422] | 43 | private readonly HashSet<Resource> inheritedResources = new HashSet<Resource>();
|
---|
[15559] | 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);
|
---|
[15422] | 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();
|
---|
[15559] | 68 | newAssignedResources.Clear();
|
---|
[15422] | 69 | inheritedResources.Clear();
|
---|
[15559] | 70 | resourceAncestors.Clear();
|
---|
[15422] | 71 | treeView.Nodes.Clear();
|
---|
| 72 | detailsViewHost.Content = null;
|
---|
| 73 | } else {
|
---|
| 74 | UpdateAssignedResources();
|
---|
[15559] | 75 | UpdateResourceGenealogy();
|
---|
[15422] | 76 | var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
|
---|
| 77 | detailsViewHost.Content = top;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
[15559] | 80 |
|
---|
[15422] | 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();
|
---|
[15559] | 90 | UpdateResourceGenealogy();
|
---|
[15422] | 91 | var top = BuildResourceTree(HiveAdminClient.Instance.Resources);
|
---|
| 92 | detailsViewHost.Content = top;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | private void inheritButton_Click(object sender, EventArgs e) {
|
---|
[15559] | 96 | return;
|
---|
[15422] | 97 | SetAssignedProjectResources(Content.Id, Enumerable.Empty<Guid>());
|
---|
| 98 | UpdateAssignedResources();
|
---|
[15559] | 99 | UpdateResourceGenealogy();
|
---|
[15422] | 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(
|
---|
[15559] | 106 | action: () => {
|
---|
| 107 | SetAssignedProjectResources(Content.Id, newAssignedResources.Select(x => x.Id));
|
---|
| 108 | });
|
---|
| 109 | UpdateResourceTree();
|
---|
[15422] | 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;
|
---|
[15559] | 119 | if (newInheritedResources.Contains(checkedResource) || checkedResource.Id == Guid.Empty) e.Cancel = true;
|
---|
[15422] | 120 | }
|
---|
| 121 |
|
---|
| 122 | private void treeView_AfterCheck(object sender, TreeViewEventArgs e) {
|
---|
| 123 | var checkedResource = (Resource)e.Node.Tag;
|
---|
[15559] | 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;
|
---|
[15422] | 134 | }
|
---|
| 135 | #endregion
|
---|
| 136 |
|
---|
| 137 | #region Helpers
|
---|
[15559] | 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 |
|
---|
[15422] | 152 | private static IEnumerable<Resource> GetAssignedResourcesForProject(Guid projectId) {
|
---|
| 153 | var assignedProjectResources = HiveServiceLocator.Instance.CallHiveService(s => s.GetAssignedResourcesForProject(projectId));
|
---|
[15559] | 154 | return HiveAdminClient.Instance.Resources.Where(x => assignedProjectResources.Select(y => y.ResourceId).Contains(x.Id));
|
---|
[15422] | 155 | }
|
---|
| 156 |
|
---|
[15559] | 157 | private void SetAssignedProjectResources(Guid projectId, IEnumerable<Guid> resourceIds) {
|
---|
[15422] | 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 |
|
---|
[15559] | 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 |
|
---|
[15422] | 173 | private void UpdateAssignedResources() {
|
---|
| 174 | assignedResources.Clear();
|
---|
[15559] | 175 | newAssignedResources.Clear();
|
---|
| 176 | foreach (var r in GetAssignedResourcesForProject(Content.Id)) {
|
---|
[15422] | 177 | assignedResources.Add(r);
|
---|
[15559] | 178 | newAssignedResources.Add(r);
|
---|
| 179 | }
|
---|
[15422] | 180 | }
|
---|
| 181 |
|
---|
[15559] | 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 |
|
---|
[15422] | 217 | inheritedResources.Clear();
|
---|
[15559] | 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 | }
|
---|
[15422] | 226 | }
|
---|
[15559] | 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 | //}
|
---|
[15422] | 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 |
|
---|
[15559] | 243 | var mainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>().Where(x => x.ParentResourceId == null));
|
---|
| 244 | var subResources = new HashSet<Resource>(resources.Except(mainResources));
|
---|
[15422] | 245 |
|
---|
[15559] | 246 | var stack = new Stack<Resource>(mainResources.OrderByDescending(x => x.Name));
|
---|
[15422] | 247 | var top = stack.Peek();
|
---|
| 248 |
|
---|
| 249 | TreeNode currentNode = null;
|
---|
| 250 | Resource currentResource = null;
|
---|
| 251 |
|
---|
[15559] | 252 | var assignmentDiff = new HashSet<Resource>(newAssignedResources);
|
---|
| 253 | assignmentDiff.SymmetricExceptWith(assignedResources);
|
---|
| 254 | var inheritanceDiff = new HashSet<Resource>(newInheritedResources);
|
---|
| 255 | inheritanceDiff.SymmetricExceptWith(inheritedResources);
|
---|
| 256 |
|
---|
[15422] | 257 | while (stack.Any()) {
|
---|
| 258 | var newResource = stack.Pop();
|
---|
| 259 | var newNode = new TreeNode(newResource.Name) { Tag = newResource };
|
---|
| 260 |
|
---|
[15559] | 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)
|
---|
[15422] | 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 |
|
---|
[15559] | 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 |
|
---|
[15422] | 292 | if (newResource is Slave) {
|
---|
| 293 | newNode.ImageIndex = slaveImageIndex;
|
---|
| 294 | } else {
|
---|
| 295 | newNode.ImageIndex = slaveGroupImageIndex;
|
---|
| 296 |
|
---|
[15559] | 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);
|
---|
[15422] | 302 | }
|
---|
| 303 | currentNode = newNode;
|
---|
| 304 | currentResource = newResource;
|
---|
| 305 | }
|
---|
| 306 | }
|
---|
| 307 | newNode.SelectedImageIndex = newNode.ImageIndex;
|
---|
[15559] | 308 | //if (newResource.OwnerUserId == UserInformation.Instance.User.Id)
|
---|
| 309 | // newNode.BackColor = ownedResourceColor;
|
---|
[15422] | 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 |
|
---|
[15559] | 320 | foreach (var slave in subResources.OfType<Slave>().OrderBy(x => x.Name)) {
|
---|
[15422] | 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 | }
|
---|
[15559] | 332 |
|
---|
[15422] | 333 | #endregion
|
---|
| 334 | }
|
---|
| 335 | }
|
---|