Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15742


Ignore:
Timestamp:
02/09/18 07:56:01 (6 years ago)
Author:
jzenisek
Message:

#2839 worked on HiveAdministrator:

  • corrected and modified CRUD operations
  • improved usability by providing detailed state information, adding dialogs etc.
Location:
branches/HiveProjectManagement
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/HiveProjectManagement/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ProjectsView.cs

    r15658 r15742  
    4040    private const int greenFlagImageIndex = 0;
    4141    private const int redFlagImageIndex = 1;
     42
    4243    private readonly Color changedColor = Color.FromArgb(255, 87, 191, 193); // #57bfc1
    4344    private readonly Color selectedColor = Color.FromArgb(255, 240, 194, 59); // #f0c23b
     45    private Project selectedProject = null;
    4446
    4547    private readonly object locker = new object();
    46 
    47     private Project selectedProject = null;
    4848
    4949    public new IItemList<Project> Content {
     
    173173    private async void ProjectsView_Load(object sender, EventArgs e) {
    174174      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
    175         action: () => UpdateProjects(),
    176         finallyCallback: () => Content = HiveAdminClient.Instance.Projects);
     175        action: () => UpdateProjects());
    177176    }
    178177
     
    186185        action: () => UpdateProjects(),
    187186        finallyCallback: () => {
    188           Content = HiveAdminClient.Instance.Projects;
    189187          refreshButton.Enabled = true;
    190188        });
     
    193191    private void addButton_Click(object sender, EventArgs e) {
    194192      Guid? parentProjectId = null;
     193
     194      if (selectedProject != null && selectedProject.Id == Guid.Empty) {
     195        MessageBox.Show(
     196          "You cannot add a project to a not yet stored project.",
     197          "HeuristicLab Hive Administrator",
     198          MessageBoxButtons.OK,
     199          MessageBoxIcon.Information);
     200        return;
     201      }
     202
    195203      if (selectedProject != null) parentProjectId = selectedProject.Id;
    196204      var project = new Project {
     
    199207        ParentProjectId = parentProjectId
    200208      };
     209
     210      selectedProject = project;
    201211      Content.Add(project);
    202212    }
    203213
    204214    private async void removeButton_Click(object sender, EventArgs e) {
     215      if (selectedProject == null) return;
     216
    205217      lock (locker) {
    206218        if (!removeButton.Enabled) return;
     
    208220      }
    209221
    210       var selectedNode = projectsTreeView.SelectedNode;
    211       if (selectedNode == null || selectedNode.Tag == null) return;
    212 
    213       var project = (Project)selectedNode.Tag;
     222      if(!IsAdmin()
     223        && HiveAdminClient.Instance.CheckOwnershipOfParentProject(selectedProject,
     224          UserInformation.Instance.User.Id)) {
     225        MessageBox.Show(
     226          "Only subprojects of owned projects can be deleted.",
     227          "HeuristicLab Hive Administrator",
     228          MessageBoxButtons.OK,
     229          MessageBoxIcon.Error);
     230        return;
     231      }
     232
     233      if (!IsAdmin() && Content.Any(x => x.ParentProjectId == selectedProject.Id)) {
     234        MessageBox.Show(
     235          "Only empty projects can be deleted.",
     236          "HeuristicLab Hive Administrator",
     237          MessageBoxButtons.OK,
     238          MessageBoxIcon.Error);
     239        return;
     240      }
     241
    214242      var result = MessageBox.Show(
    215         "Do you really want to delete " + project.Name + "?",
     243        "Do you really want to delete " + selectedProject.Name + "?",
    216244        "HeuristicLab Hive Administrator",
    217245        MessageBoxButtons.YesNo,
     
    219247
    220248      if (result == DialogResult.Yes) {
    221         if (Content.Any(x => x.ParentProjectId == project.Id)) {
    222           MessageBox.Show(
    223             "Only empty projects can be deleted.",
    224             "HeuristicLab Hive Administrator",
    225             MessageBoxButtons.OK,
    226             MessageBoxIcon.Error);
    227         } else {
    228           await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
    229             action: () => RemoveProject(project),
    230             finallyCallback: () => {
    231               Content.Remove(project);
    232               removeButton.Enabled = true;
    233             });
    234         }
     249        await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
     250          action: () => RemoveProject(selectedProject),
     251          finallyCallback: () => {
     252            removeButton.Enabled = true;
     253          });
    235254      }
    236255    }
     
    247266          foreach (var project in projectsToSave)
    248267            project.Store();
     268          UpdateProjects();
    249269        },
    250270        finallyCallback: () => saveProjectButton.Enabled = true);
     
    254274
    255275    private void projectsTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
    256       selectedProject = (Project)e.Node.Tag;
    257       ReColorTreeNodes(projectsTreeView.Nodes, selectedColor, Color.Transparent, true);
    258       e.Node.BackColor = selectedColor;
    259       e.Node.Text += " [selected]";
    260      
     276      ChangeSelectedProjectNode(e.Node);
    261277
    262278      //if (projectView.Content != null)
     
    269285      //if (selectedProject != null)
    270286      //  selectedProject.PropertyChanged += ProjectViewContent_PropertyChanged;
    271 
    272       //if (IsAuthorized(selectedProject)) {
    273       //  if (!tabControl.TabPages.Contains(permissionsTabPage))
    274       //    tabControl.TabPages.Add(permissionsTabPage);
    275       //  if (!tabControl.TabPages.Contains(resourcesTabPage))
    276       //    tabControl.TabPages.Add(resourcesTabPage);
    277       //} else {
    278       //  if (tabControl.TabPages.Contains(permissionsTabPage))
    279       //    tabControl.TabPages.Remove(permissionsTabPage);
    280       //  if (tabControl.TabPages.Contains(resourcesTabPage))
    281       //    tabControl.TabPages.Remove(resourcesTabPage);
    282       //}
    283287    }
    284288
     
    286290      var sourceNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
    287291      if (sourceNode == null) return;
     292      var sourceResource = ((Project)sourceNode.Tag);
     293      if (sourceResource == null) return;
    288294
    289295      var treeView = (TreeView)sender;
     
    294300
    295301      if (sourceNode == targetNode) return;
    296 
    297       if (sourceNode.Parent == null)
     302      var targetProject = (targetNode != null) ? (Project)targetNode.Tag : null;
     303
     304      if(targetProject != null && targetProject.Id == Guid.Empty) {
     305        MessageBox.Show(
     306          "You cannot drag projects to a not yet stored project.",
     307          "HeuristicLab Hive Administrator",
     308          MessageBoxButtons.OK,
     309          MessageBoxIcon.Information);
     310            return;
     311      }
     312
     313      if (sourceNode.Parent == null) {
    298314        treeView.Nodes.Remove(sourceNode);
    299       else {
     315      } else {
    300316        sourceNode.Parent.Nodes.Remove(sourceNode);
    301         ((Project)sourceNode.Tag).ParentProjectId = null;
     317        sourceResource.ParentProjectId = null;
    302318      }
    303319
    304320      if (targetNode == null) {
    305321        treeView.Nodes.Add(sourceNode);
    306       } else {
     322      } else if(targetProject.Id != Guid.Empty) {
    307323        targetNode.Nodes.Add(sourceNode);
    308         ((Project)sourceNode.Tag).ParentProjectId = ((Project)targetNode.Tag).Id;
    309       }
     324        sourceResource.ParentProjectId = targetProject.Id;
     325      }
     326
     327      selectedProject = sourceResource;
     328      OnContentChanged();
    310329    }
    311330
     
    330349
    331350    #region Helpers
    332     private void ReColorTreeNodes(TreeNodeCollection nodes, Color c1, Color c2, bool resetText) {
    333       foreach(TreeNode n in nodes) {
    334         if(n.BackColor.Equals(c1)) {
     351    private void ResetTreeNodes(TreeNodeCollection nodes, Color c1, Color c2, bool resetText) {
     352      foreach (TreeNode n in nodes) {
     353        var pro = ((Project)n.Tag);
     354        if (n.BackColor.Equals(c1)) {
    335355          n.BackColor = c2;
    336           if (resetText) n.Text = ((Project)n.Tag).Name;
     356        }
     357        if (resetText) {
     358          n.Text = pro.Name;
     359
     360          if (pro.Id == Guid.Empty) {
     361            n.Text += " [not stored]";
     362          } else if (pro.Modified) {
     363            n.Text += " [changes not stored]";
     364          }
    337365        }
    338366        if (n.Nodes.Count > 0) {
    339           ReColorTreeNodes(n.Nodes, c1, c2, resetText);
    340         }
    341       }
     367          ResetTreeNodes(n.Nodes, c1, c2, resetText);
     368        }
     369      }
     370    }
     371
     372    private void ChangeSelectedProjectNode(TreeNode projectNode) {
     373      selectedProject = (Project)projectNode.Tag;
     374      ResetTreeNodes(projectsTreeView.Nodes, selectedColor, Color.Transparent, true);
     375      projectNode.BackColor = selectedColor;
     376      projectNode.Text += " [selected]";
    342377    }
    343378
     
    354389
    355390      var stack = new Stack<Project>(mainProjects.OrderByDescending(x => x.Name));
    356       Project top = null;
     391      if (selectedProject != null) selectedProject = projects.Where(x => x.Id == selectedProject.Id).FirstOrDefault();
    357392
    358393      TreeNode currentNode = null;
     
    362397        var newProject = stack.Pop();
    363398        var newNode = new TreeNode(newProject.Name) { Tag = newProject };
    364         if (top == null) {
    365           top = newProject;
     399
     400        if (newProject.Id == Guid.Empty) {
     401          newNode.Text += " [not stored]";
     402        } else if(newProject.Modified) {
     403          newNode.Text += " [changes not stored]";
     404        }
     405        if (selectedProject == null) {
     406          selectedProject = newProject;
     407        }
     408        if (newProject.Id == selectedProject.Id) {
    366409          newNode.BackColor = selectedColor;
    367410          newNode.Text += " [selected]";
    368411        }
    369412
     413        // search for parent node of newNode and save in currentNode
     414        // necessary since newNodes (stack top items) might be siblings
     415        // or grand..grandparents of previous node (currentNode)
    370416        while (currentNode != null && newProject.ParentProjectId != currentProject.Id) {
    371417          currentNode = currentNode.Parent;
     
    396442      projectsTreeView.ExpandAll();
    397443
    398       return top;
     444      return selectedProject;
    399445    }
    400446
     
    402448      try {
    403449        HiveAdminClient.Instance.Refresh();
     450        Content = HiveAdminClient.Instance.Projects;
    404451      } catch (AnonymousUserException) {
    405452        ShowHiveInformationDialog();
     
    408455
    409456    private void RemoveProject(Project project) {
     457      if (project == null || project.Id == Guid.Empty) return;
     458
    410459      try {
    411460        HiveAdminClient.Delete(project);
     461        Content.Remove(selectedProject);
    412462      } catch (AnonymousUserException) {
    413463        ShowHiveInformationDialog();
     
    420470    }
    421471
     472    private bool IsAdmin() {
     473      return HiveRoles.CheckAdminUserPermissions();
     474    }
     475
    422476    private void ShowHiveInformationDialog() {
    423477      if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
  • branches/HiveProjectManagement/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ResourceView.cs

    r15576 r15742  
    5959      base.SetEnabledStateOfControls();
    6060      bool enabled = Content != null && !Locked;
     61      idTextBox.ReadOnly = true;
    6162      nameTextBox.ReadOnly = !enabled;
    6263      descriptionTextBox.ReadOnly = !enabled;
  • branches/HiveProjectManagement/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ResourcesView.Designer.cs

    r15422 r15742  
    157157      this.treeView.TabIndex = 0;
    158158      this.treeView.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeSlaveGroup_ItemDrag);
     159      this.treeView.BeforeSelect += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeSlaveGroup_BeforeSelect);
    159160      this.treeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeSlaveGroup_AfterSelect);
    160161      this.treeView.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeSlaveGroup_DragDrop);
  • branches/HiveProjectManagement/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ResourcesView.cs

    r15658 r15742  
    4343    public const string ungroupedGroupDescription = "Contains slaves that are not assigned to any group.";
    4444
    45     private readonly Color ownedResourceColor = Color.LightGreen;
     45    private readonly Color changedColor = Color.FromArgb(255, 87, 191, 193); // #57bfc1
     46    private readonly Color selectedColor = Color.FromArgb(255, 240, 194, 59); // #f0c23b
     47    private Resource selectedResource = null;
     48
    4649    private readonly object locker = new object();
    4750
     
    98101    protected override void SetEnabledStateOfControls() {
    99102      base.SetEnabledStateOfControls();
    100       bool enabled = Content != null && !Locked;
     103      bool enabled = Content != null && !Locked && IsAdmin();
    101104      btnAddGroup.Enabled = enabled;
    102105      btnRemoveGroup.Enabled = enabled;
    103106      btnSave.Enabled = enabled;
    104       scheduleView.SetEnabledStateOfSchedule(enabled && IsAuthorized((Resource)viewHost.Content));
     107      scheduleView.SetEnabledStateOfSchedule(enabled && IsAdmin()); // IsAuthorized((Resource)viewHost.Content));
    105108    }
    106109    #endregion
     
    184187
    185188    private void btnAddGroup_Click(object sender, EventArgs e) {
     189      Guid? parentResourceId = null;
     190      if(!IsAdmin()) {
     191        MessageBox.Show(
     192          "You have no permission to add a resource group.",
     193          "HeuristicLab Hive Administrator",
     194          MessageBoxButtons.OK,
     195          MessageBoxIcon.Information);
     196        return;
     197      } else if(selectedResource != null && selectedResource.Id == Guid.Empty) {
     198        MessageBox.Show(
     199          "You cannot add a resource group to a not yet stored group.",
     200          "HeuristicLab Hive Administrator",
     201          MessageBoxButtons.OK,
     202          MessageBoxIcon.Information);
     203        return;
     204      }
     205     
     206      if (selectedResource != null && selectedResource is SlaveGroup) parentResourceId = selectedResource.Id;
    186207      var group = new SlaveGroup {
    187208        Name = "New Group",
    188         OwnerUserId = UserInformation.Instance.User.Id
     209        OwnerUserId = UserInformation.Instance.User.Id,
     210        ParentResourceId = parentResourceId
    189211      };
     212
     213      selectedResource = group;
    190214      Content.Add(group);
    191215    }
    192216
    193217    private async void btnRemoveGroup_Click(object sender, EventArgs e) {
     218      if (selectedResource == null) return;
     219
    194220      lock (locker) {
    195221        if (!btnRemoveGroup.Enabled) return;
     
    197223      }
    198224
    199       await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
    200         action: () => RemoveResource(),
    201         finallyCallback: () => btnRemoveGroup.Enabled = true);
     225      if (!IsAdmin()) {
     226        MessageBox.Show(
     227          "You have no permission to delete resources.",
     228          "HeuristicLab Hive Administrator",
     229          MessageBoxButtons.OK,
     230          MessageBoxIcon.Information);
     231        return;
     232      }
     233
     234      if (Content.Any(x => x.ParentResourceId == selectedResource.Id)) {
     235        MessageBox.Show(
     236          "Only empty resources can be deleted.",
     237          "HeuristicLab Hive Administrator",
     238          MessageBoxButtons.OK,
     239          MessageBoxIcon.Error);
     240        return;
     241      }
     242
     243      var result = MessageBox.Show(
     244        "Do you really want to delete " + selectedResource.Name + "?",
     245        "HeuristicLab Hive Administrator",
     246        MessageBoxButtons.YesNo,
     247        MessageBoxIcon.Question);
     248      if (result == DialogResult.Yes) {
     249        await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
     250          action: () => RemoveResource(selectedResource),
     251          finallyCallback: () => {
     252            btnRemoveGroup.Enabled = true;
     253          });
     254      }
    202255    }
    203256
     
    213266          foreach (var resource in resourcesToSave)
    214267            resource.Store();
     268          UpdateResources();
    215269        },
    216270        finallyCallback: () => btnSave.Enabled = true);
     271
     272      OnContentChanged();
     273    }
     274
     275    private void treeSlaveGroup_BeforeSelect(object sender, TreeViewCancelEventArgs e) {
     276      if (((Resource)e.Node.Tag).Name == ungroupedGroupName) e.Cancel = true;
    217277    }
    218278
    219279    private async void treeSlaveGroup_AfterSelect(object sender, TreeViewEventArgs e) {
    220       var selectedResource = (Resource)e.Node.Tag;
    221 
    222       if (viewHost.Content != null && viewHost.Content is SlaveGroup)
    223         ((SlaveGroup)viewHost.Content).PropertyChanged -= SlaveViewContent_PropertyChanged;
     280      ChangeSelectedResourceNode(e.Node);
     281      SetEnabledStateOfControlsForSelectedResource();
     282
     283      //if (viewHost.Content != null && viewHost.Content is SlaveGroup)
     284      //  ((SlaveGroup)viewHost.Content).PropertyChanged -= SlaveViewContent_PropertyChanged;
    224285
    225286      viewHost.Content = selectedResource;
     
    229290        finallyCallback: () => scheduleView.Content = HiveAdminClient.Instance.Downtimes);
    230291
    231       if (selectedResource != null && selectedResource is SlaveGroup)
    232         selectedResource.PropertyChanged += SlaveViewContent_PropertyChanged;
    233 
    234       if (IsAuthorized(selectedResource)) {
    235         if (!tabSlaveGroup.TabPages.Contains(tabSchedule))
    236           tabSlaveGroup.TabPages.Add(tabSchedule);
    237       } else {
    238         if (tabSlaveGroup.TabPages.Contains(tabSchedule))
    239           tabSlaveGroup.TabPages.Remove(tabSchedule);
    240       }
     292      //if (selectedResource != null && selectedResource is SlaveGroup)
     293      //  selectedResource.PropertyChanged += SlaveViewContent_PropertyChanged;
     294
     295      bool locked = !IsAdmin();
     296      viewHost.Locked = locked;
     297      scheduleView.Locked = locked;
    241298    }
    242299
     
    244301      var sourceNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
    245302      if (sourceNode == null) return;
     303      var sourceResource = ((Resource)sourceNode.Tag);
     304      if (sourceResource == null) return;
    246305
    247306      var treeView = (TreeView)sender;
     
    252311
    253312      if (sourceNode == targetNode) return;
    254 
    255       if (targetNode.Text == ungroupedGroupName || targetNode.Parent != null && targetNode.Parent.Text == ungroupedGroupName) {
    256         MessageBox.Show(
    257           string.Format(@"You cannot drag resources to group ""{0}"". This group only contains slaves which have not been assigned to a real group yet.", ungroupedGroupName),
     313      var targetResource = (targetNode != null) ? (Resource)targetNode.Tag : null;
     314
     315      if (!IsAdmin()) {
     316        MessageBox.Show(
     317          "You cannot drag resources. This is solely granted for HeuristicLab Hive Administrators.",
    258318          "HeuristicLab Hive Administrator",
    259319          MessageBoxButtons.OK,
    260320          MessageBoxIcon.Information);
    261321        return;
     322      } else if (targetResource != null && targetResource is Slave) {
     323        MessageBox.Show(
     324          "You cannot drag resources on to a slave.",
     325          "HeuristicLab Hive Administrator",
     326          MessageBoxButtons.OK,
     327          MessageBoxIcon.Information);
     328        return;
     329      } else if(targetResource != null && targetResource.Id == Guid.Empty) {
     330        MessageBox.Show(
     331          "You cannot drag resources to a not yet stored resource group.",
     332          "HeuristicLab Hive Administrator",
     333          MessageBoxButtons.OK,
     334          MessageBoxIcon.Information);
     335            return;
     336      } else if (targetNode != null && (targetNode.Text == ungroupedGroupName || targetNode.Parent != null && targetNode.Parent.Text == ungroupedGroupName)) {
     337        MessageBox.Show(
     338          string.Format(@"You cannot drag resources to group ""{0}"". This group only contains slaves which have not been assigned to a real group yet.", ungroupedGroupName),
     339          "HeuristicLab Hive Administrator",
     340          MessageBoxButtons.OK,
     341          MessageBoxIcon.Information);
     342        return;
    262343      }
    263344
     
    266347      else {
    267348        sourceNode.Parent.Nodes.Remove(sourceNode);
    268         ((Resource)sourceNode.Tag).ParentResourceId = null;
     349        sourceResource.ParentResourceId = null;
    269350      }
    270351
    271352      if (targetNode == null) {
    272353        treeView.Nodes.Add(sourceNode);
    273       } else {
     354      } else if(targetResource.Id != Guid.Empty) {
    274355        targetNode.Nodes.Add(sourceNode);
    275         ((Resource)sourceNode.Tag).ParentResourceId = ((Project)targetNode.Tag).Id;
    276       }
     356        sourceResource.ParentResourceId = targetResource.Id;
     357      }
     358
     359      selectedResource = sourceResource;
     360      OnContentChanged();
    277361    }
    278362
    279363    private void treeSlaveGroup_ItemDrag(object sender, ItemDragEventArgs e) {
    280364      TreeNode sourceNode = (TreeNode)e.Item;
    281       if (IsAuthorized((Resource)sourceNode.Tag))
     365      if (IsAdmin())
    282366        DoDragDrop(sourceNode, DragDropEffects.All);
    283367    }
     
    297381
    298382    #region Helpers
     383    private void ResetTreeNodes(TreeNodeCollection nodes, Color c1, Color c2, bool resetText) {
     384      foreach (TreeNode n in nodes) {
     385        var res = ((Resource)n.Tag);
     386        if (n.BackColor.Equals(c1)) {
     387          n.BackColor = c2;
     388        }
     389        if (resetText) {
     390          n.Text = res.Name;
     391
     392          if (res.Id == Guid.Empty && res.Name != ungroupedGroupName) {
     393            n.Text += " [not stored]";
     394          } else if (res.Modified && res.Name != ungroupedGroupName) {
     395            n.Text += " [changes not stored]";
     396          }
     397        }
     398        if (n.Nodes.Count > 0) {
     399          ResetTreeNodes(n.Nodes, c1, c2, resetText);
     400        }
     401      }
     402    }
     403
    299404    private Resource BuildResourceTree(IEnumerable<Resource> resources) {
    300405      treeView.Nodes.Clear();
    301406      if (!resources.Any()) return null;
    302407
    303       var mainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>().Where(x => x.ParentResourceId == null));
     408      var mainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>()
     409        .Where(x => x.ParentResourceId == null));
    304410      var parentedMainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>()
    305411        .Where(x => x.ParentResourceId.HasValue && !resources.Select(y => y.Id).Contains(x.ParentResourceId.Value)));
     
    308414
    309415      var stack = new Stack<Resource>(mainResources.OrderByDescending(x => x.Name));
    310       Resource top = null;
     416      if (selectedResource != null) selectedResource = resources.Where(x => x.Id == selectedResource.Id).FirstOrDefault();
     417      //bool nodeSelected = false;
    311418
    312419      TreeNode currentNode = null;
     
    314421
    315422      while(stack.Any()) {
    316         if (top == null) top = stack.Peek();
    317423        var newResource = stack.Pop();
    318424        var newNode = new TreeNode(newResource.Name) { Tag = newResource };
     425
     426        if(newResource.Id == Guid.Empty) {
     427          newNode.Text += " [not stored]";
     428        } else if(newResource.Modified) {
     429          newNode.Text += " [changes not stored]";
     430        }
     431        if (selectedResource == null) {
     432          selectedResource = newResource;
     433        }
     434        if (newResource.Id == selectedResource.Id) {
     435          newNode.BackColor = selectedColor;
     436          newNode.Text += " [selected]";
     437        }
    319438
    320439        // search for parent node of newNode and save in currentNode
    321440        // necessary since newNodes (stack top items) might be siblings
    322441        // or grand..grandparents of previous node (currentNode)
    323         while(currentNode != null && newResource.ParentResourceId != currentResource.Id) {
     442        while (currentNode != null && newResource.ParentResourceId != currentResource.Id) {
    324443          currentNode = currentNode.Parent;
    325444          currentResource = currentNode == null ? null : (Resource)currentNode.Tag;
     
    363482        var slaveNode = new TreeNode(slave.Name) { Tag = slave };
    364483        ungroupedNode.Nodes.Add(slaveNode);
     484        if(selectedResource == null) {
     485          selectedResource = slave;
     486        }
     487        if (slave.Id == Guid.Empty) {
     488          slaveNode.Text += " [not stored]";
     489        } else if (slave.Modified) {
     490          slaveNode.Text += " [changes not stored]";
     491        }
     492        if (slave.Id == selectedResource.Id) {
     493          slaveNode.BackColor = selectedColor;
     494          slaveNode.Text += " [selected]";
     495        }
    365496      }
    366497
     
    368499      treeView.ExpandAll();
    369500
    370       return top;
     501      return selectedResource;
    371502    }
    372503
     
    411542
    412543    private void UpdateResources() {
    413       HiveAdminClient.Instance.Refresh();
    414       Content = HiveAdminClient.Instance.Resources;
    415     }
    416 
    417     private void RemoveResource() {
    418       var selectedNode = treeView.SelectedNode;
    419       if (selectedNode == null || selectedNode.Tag == null) return;
    420 
    421       var resource = (Resource)selectedNode.Tag;
    422       var result = MessageBox.Show(
    423         "Do you really want to delete " + resource.Name + "?",
    424         "HeuristicLab Hive Administrator",
    425         MessageBoxButtons.YesNo,
    426         MessageBoxIcon.Question);
    427 
    428       if (result == DialogResult.Yes) {
    429         if (resource is Slave) {
    430           Content.Remove(resource);
    431           HiveAdminClient.Delete(resource);
    432         } else if (resource is SlaveGroup) {
    433           if (Content.Any(x => x.ParentResourceId == resource.Id)) {
    434             MessageBox.Show(
    435               "Only empty resources can be deleted.",
    436               "HeuristicLab Hive Administrator",
    437               MessageBoxButtons.OK,
    438               MessageBoxIcon.Error);
    439           } else {
    440             Content.Remove(resource);
    441             HiveAdminClient.Delete(resource);
    442           }
    443         }
     544      try {
     545        HiveAdminClient.Instance.Refresh();
     546        Content = HiveAdminClient.Instance.Resources;
     547      } catch(AnonymousUserException) {
     548        ShowHiveInformationDialog();
     549      }
     550    }
     551
     552    private void RemoveResource(Resource resource) {
     553      if (resource == null || resource.Id == Guid.Empty) return;
     554
     555      try {
     556        HiveAdminClient.Delete(resource);
     557        Content.Remove(selectedResource);
     558      } catch(AnonymousUserException) {
     559        ShowHiveInformationDialog();
    444560      }
    445561    }
     
    456572      return resource != null
    457573          && resource.Name != ungroupedGroupName
    458           && resource.Id != Guid.Empty
     574          //&& resource.Id != Guid.Empty
    459575          && UserInformation.Instance.UserExists
    460           && (resource.OwnerUserId == UserInformation.Instance.User.Id || HiveRoles.CheckAdminUserPermissions());
     576          && (HiveRoles.CheckAdminUserPermissions()
     577            || HiveAdminClient.Instance.CheckOwnershipOfResource(resource, UserInformation.Instance.User.Id));
     578    }
     579
     580    private bool IsAdmin() {
     581      return HiveRoles.CheckAdminUserPermissions();
     582    }
     583
     584    private void ChangeSelectedResourceNode(TreeNode resourceNode) {
     585      selectedResource = (Resource)resourceNode.Tag;
     586      ResetTreeNodes(treeView.Nodes, selectedColor, Color.Transparent, true);
     587      resourceNode.BackColor = selectedColor;
     588      resourceNode.Text += " [selected]";
     589    }
     590
     591    private void SetEnabledStateOfControlsForSelectedResource() {
     592      bool enabled = (IsAdmin()) ? true : false;
     593      btnAddGroup.Enabled = enabled;
     594      btnRemoveGroup.Enabled = enabled;
     595      btnSave.Enabled = enabled;
    461596    }
    462597
  • branches/HiveProjectManagement/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ScheduleView.cs

    r14185 r15742  
    222222    protected override void SetEnabledStateOfControls() {
    223223      base.SetEnabledStateOfControls();
     224      bool enabled = Content != null && !Locked;
     225      btCreate.Enabled = enabled;
     226      btbDelete.Enabled = enabled;
     227      btnClearCal.Enabled = enabled;
     228      btnRecurrence.Enabled = enabled;
     229      btnSaveCal.Enabled = enabled;
    224230    }
    225231
  • branches/HiveProjectManagement/HeuristicLab.Clients.Hive/3.3/HiveAdminClient.cs

    r15658 r15742  
    2525using HeuristicLab.Core;
    2626using System.Collections.Generic;
     27using System.Linq;
    2728
    2829namespace HeuristicLab.Clients.Hive {
     
    6465    }
    6566
     67    private Dictionary<Guid, HashSet<Project>> projectAncestors;
     68    public Dictionary<Guid, HashSet<Project>> ProjectAncestors {
     69      get { return projectAncestors; }
     70    }
     71
     72    private Dictionary<Guid, HashSet<Project>> projectDescendants;
     73    public Dictionary<Guid, HashSet<Project>> ProjectDescendants {
     74      get { return projectDescendants; }
     75    }
     76
     77    private Dictionary<Guid, HashSet<Resource>> resourceAncestors;
     78    public Dictionary<Guid, HashSet<Resource>> ResourceAncestors {
     79      get { return ResourceAncestors; }
     80    }
     81
     82    private Dictionary<Guid, HashSet<Resource>> resourceDescendants;
     83    public Dictionary<Guid, HashSet<Resource>> ResourceDescendants {
     84      get { return ResourceDescendants; }
     85    }
     86
     87
    6688    #endregion
    6789
     
    89111        projects = new ItemList<Project>();
    90112
     113        projectAncestors = new Dictionary<Guid, HashSet<Project>>();
     114        projectDescendants = new Dictionary<Guid, HashSet<Project>>();
     115        resourceAncestors = new Dictionary<Guid, HashSet<Resource>>();
     116        resourceDescendants = new Dictionary<Guid, HashSet<Resource>>();
     117
    91118        HiveServiceLocator.Instance.CallHiveService(service => {
    92119          service.GetSlaveGroupsForAdministration().ForEach(g => resources.Add(g));
     
    94121          service.GetProjectsForAdministration().ForEach(p => projects.Add(p));
    95122        });
     123
     124        UpdateResourceGenealogy();
     125        UpdateProjectGenealogy();
    96126      }
    97127      catch {
     
    100130      finally {
    101131        OnRefreshed();
     132      }
     133    }
     134
     135    private void UpdateResourceGenealogy() {
     136      resourceAncestors.Clear();
     137      resourceDescendants.Clear();
     138
     139      foreach (var r in resources) {
     140        resourceAncestors.Add(r.Id, new HashSet<Resource>());
     141        resourceDescendants.Add(r.Id, new HashSet<Resource>());
     142      }
     143
     144      foreach (var r in resources) {
     145        var parentResourceId = r.ParentResourceId;
     146        while (parentResourceId != null) {
     147          var parent = resources.SingleOrDefault(x => x.Id == parentResourceId);
     148          if (parent != null) {
     149            resourceAncestors[r.Id].Add(parent);
     150            resourceDescendants[parent.Id].Add(r);
     151            parentResourceId = parent.ParentResourceId;
     152          } else {
     153            parentResourceId = null;
     154          }
     155        }
     156      }
     157    }
     158
     159    private void UpdateProjectGenealogy() {
     160      projectAncestors.Clear();
     161      projectDescendants.Clear();
     162
     163      foreach (var p in projects) {
     164        projectAncestors.Add(p.Id, new HashSet<Project>());
     165        projectDescendants.Add(p.Id, new HashSet<Project>());
     166      }
     167
     168      foreach (var p in projects) {
     169        var parentProjectId = p.ParentProjectId;
     170        while (parentProjectId != null) {
     171          var parent = projects.SingleOrDefault(x => x.Id == parentProjectId);
     172          if (parent != null) {
     173            projectAncestors[p.Id].Add(parent);
     174            projectDescendants[parent.Id].Add(p);
     175            parentProjectId = parent.ParentProjectId;
     176          } else {
     177            parentProjectId = null;
     178          }
     179        }
    102180      }
    103181    }
     
    179257    }
    180258
    181     #region
     259    #region Helper
    182260    public bool CheckAccessToAdminAreaGranted() {
    183261      if(projects != null) {
     
    191269      }
    192270    }
     271
     272    public bool CheckOwnershipOfResource(Resource res, Guid userId) {
     273      if (res == null || userId == Guid.Empty) return false;
     274
     275      if (res.OwnerUserId == userId) {
     276        return true;
     277      } else if(resourceAncestors.ContainsKey(res.Id)) {
     278        return resourceAncestors[res.Id].Where(x => x.OwnerUserId == userId).Any();
     279      }
     280
     281      return false;
     282    }
     283
     284    public bool CheckOwnershipOfProject(Project pro, Guid userId) {
     285      if (pro == null || userId == Guid.Empty) return false;
     286
     287      if (pro.OwnerUserId == userId) {
     288        return true;
     289      } else if (projectAncestors.ContainsKey(pro.Id)) {
     290        return projectAncestors[pro.Id].Where(x => x.OwnerUserId == userId).Any();
     291      }
     292
     293      return false;
     294    }
     295
     296    public bool CheckOwnershipOfParentProject(Project pro, Guid userId) {
     297      if (pro == null || userId == Guid.Empty) return false;
     298
     299      if(projectAncestors.ContainsKey(pro.Id)) {
     300        return projectAncestors[pro.Id].Where(x => x.OwnerUserId == userId).Any();
     301      }
     302
     303      return false;
     304    }
    193305    #endregion
    194306  }
  • branches/HiveProjectManagement/HeuristicLab.Clients.Hive/3.3/HiveClient.cs

    r15658 r15742  
    169169      if (item.Id == Guid.Empty) {
    170170        if (item is RefreshableJob) {
    171           HiveClient.Instance.UploadJob((RefreshableJob)item, cancellationToken);
     171          item.Id = HiveClient.Instance.UploadJob((RefreshableJob)item, cancellationToken);
    172172        }
    173173        if (item is JobPermission) {
     
    180180        }
    181181        if (item is Project) {
    182           HiveServiceLocator.Instance.CallHiveService(s => s.AddProject((Project)item));
     182          item.Id = HiveServiceLocator.Instance.CallHiveService(s => s.AddProject((Project)item));
    183183        }
    184184      } else {
     
    302302    private static object jobCountLocker = new object();
    303303    private static object pluginLocker = new object();
    304     private void UploadJob(RefreshableJob refreshableJob, CancellationToken cancellationToken) {
     304    private Guid UploadJob(RefreshableJob refreshableJob, CancellationToken cancellationToken) {
    305305      try {
    306306        refreshableJob.IsProgressing = true;
     
    347347        refreshableJob.Progress.Finish();
    348348      }
     349      return (refreshableJob.Job != null) ? refreshableJob.Job.Id : Guid.Empty;
    349350    }
    350351
Note: See TracChangeset for help on using the changeset viewer.