source: branches/HiveProjectManagement/HeuristicLab.Clients.Hive/3.3/HiveAdminClient.cs @ 15658

Last change on this file since 15658 was 15658, checked in by jzenisek, 3 years ago

#2839

  • worked on (restricted) accessibility of hive's administration area for non-admin roles
  • adapted HiveClient & HiveAdminClient entity loading (client- & service-side)
File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Threading;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using System.Collections.Generic;
27
28namespace HeuristicLab.Clients.Hive {
29  [Item("Hive Administrator", "Hive Administrator")]
30  public sealed class HiveAdminClient : IContent {
31    private static HiveAdminClient instance;
32    public static HiveAdminClient Instance {
33      get {
34        if (instance == null) instance = new HiveAdminClient();
35        return instance;
36      }
37    }
38
39    #region Properties
40    private IItemList<Resource> resources;
41    public IItemList<Resource> Resources {
42      get { return resources; }
43    }
44
45    private IItemList<Downtime> downtimes;
46    public IItemList<Downtime> Downtimes {
47      get { return downtimes; }
48    }
49
50    private Guid downtimeForResourceId;
51    public Guid DowntimeForResourceId {
52      get { return downtimeForResourceId; }
53      set {
54        downtimeForResourceId = value;
55        if (downtimes != null) {
56          downtimes.Clear();
57        }
58      }
59    }
60
61    private IItemList<Project> projects;
62    public IItemList<Project> Projects {
63      get { return projects; }
64    }
65
66    #endregion
67
68    #region Events
69    public event EventHandler Refreshing;
70    private void OnRefreshing() {
71      EventHandler handler = Refreshing;
72      if (handler != null) handler(this, EventArgs.Empty);
73    }
74    public event EventHandler Refreshed;
75    private void OnRefreshed() {
76      var handler = Refreshed;
77      if (handler != null) handler(this, EventArgs.Empty);
78    }
79    #endregion
80
81    private HiveAdminClient() { }
82
83    #region Refresh
84    public void Refresh() {
85      OnRefreshing();
86
87      try {
88        resources = new ItemList<Resource>();
89        projects = new ItemList<Project>();
90
91        HiveServiceLocator.Instance.CallHiveService(service => {
92          service.GetSlaveGroupsForAdministration().ForEach(g => resources.Add(g));
93          service.GetSlavesForAdministration().ForEach(s => resources.Add(s));
94          service.GetProjectsForAdministration().ForEach(p => projects.Add(p));
95        });
96      }
97      catch {
98        throw;
99      }
100      finally {
101        OnRefreshed();
102      }
103    }
104    #endregion
105
106    #region Refresh downtime calendar
107    public void RefreshCalendar() {
108      if (downtimeForResourceId != null && downtimeForResourceId != Guid.Empty) {
109        OnRefreshing();
110
111        try {
112          downtimes = new ItemList<Downtime>();
113
114          HiveServiceLocator.Instance.CallHiveService(service => {
115            service.GetDowntimesForResource(downtimeForResourceId).ForEach(d => downtimes.Add(d));
116          });
117        }
118        catch {
119          throw;
120        }
121        finally {
122          OnRefreshed();
123        }
124      }
125    }
126    #endregion
127
128    #region Store
129    public static void Store(IHiveItem item, CancellationToken cancellationToken) {
130      if (item.Id == Guid.Empty) {
131        if (item is SlaveGroup) {
132          item.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddSlaveGroup((SlaveGroup)item));
133        }
134        if (item is Slave) {
135          item.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddSlave((Slave)item));
136        }
137        if (item is Downtime) {
138          item.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddDowntime((Downtime)item));
139        }
140        if (item is Project) {
141          item.Id = HiveServiceLocator.Instance.CallHiveService(s => s.AddProject((Project)item));
142        }
143      } else {
144        if (item is SlaveGroup) {
145          HiveServiceLocator.Instance.CallHiveService((s) => s.UpdateSlaveGroup((SlaveGroup)item));
146        }
147        if (item is Slave) {
148          HiveServiceLocator.Instance.CallHiveService((s) => s.UpdateSlave((Slave)item));
149        }
150        if (item is Downtime) {
151          HiveServiceLocator.Instance.CallHiveService((s) => s.UpdateDowntime((Downtime)item));
152        }
153        if (item is Project) {
154          HiveServiceLocator.Instance.CallHiveService((s) => s.UpdateProject((Project)item));
155        }
156      }
157    }
158    #endregion
159
160    #region Delete
161    public static void Delete(IHiveItem item) {
162      if (item is SlaveGroup) {
163        HiveServiceLocator.Instance.CallHiveService((s) => s.DeleteSlaveGroup(item.Id));
164      } else if (item is Slave) {
165        HiveServiceLocator.Instance.CallHiveService((s) => s.DeleteSlave(item.Id));
166      } else if (item is Downtime) {
167        HiveServiceLocator.Instance.CallHiveService((s) => s.DeleteDowntime(item.Id));
168      } else if (item is Project) {
169        HiveServiceLocator.Instance.CallHiveService((s) => s.DeleteProject(item.Id));
170      }
171    }
172    #endregion
173
174    public void ResetDowntime() {
175      downtimeForResourceId = Guid.Empty;
176      if (downtimes != null) {
177        downtimes.Clear();
178      }
179    }
180
181    #region
182    public bool CheckAccessToAdminAreaGranted() {
183      if(projects != null) {
184        return projects.Count > 0;
185      } else {
186        bool accessGranted = false;
187        HiveServiceLocator.Instance.CallHiveService(s => {
188          accessGranted = s.CheckAccessToAdminAreaGranted();
189        });
190        return accessGranted;
191      }
192    }
193    #endregion
194  }
195}
Note: See TracBrowser for help on using the repository browser.