Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.Clients.OKB-3.3/Administrator.cs @ 4481

Last change on this file since 4481 was 4481, checked in by swagner, 13 years ago

Worked on OKB (#1174)

File size: 13.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Clients.Common;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Clients.OKB {
32  [Item("Administrator", "OKB administrator front-end.")]
33  public sealed class Administrator : IContent {
34    private static Administrator instance;
35    public static Administrator Instance {
36      get {
37        if (instance == null) instance = new Administrator();
38        return instance;
39      }
40    }
41
42    #region Properties
43    private ItemCollection<Platform> platforms;
44    public ItemCollection<Platform> Platforms {
45      get { return platforms; }
46    }
47    private ItemCollection<DataType> dataTypes;
48    public ItemCollection<DataType> DataTypes {
49      get { return dataTypes; }
50    }
51    private IEnumerable<User> users;
52    public IEnumerable<User> Users {
53      get { return users; }
54    }
55    private ItemCollection<AlgorithmClass> algorithmClasses;
56    public ItemCollection<AlgorithmClass> AlgorithmClasses {
57      get { return algorithmClasses; }
58    }
59    private ItemCollection<Algorithm> algorithms;
60    public ItemCollection<Algorithm> Algorithms {
61      get { return algorithms; }
62    }
63    private ItemCollection<ProblemClass> problemClasses;
64    public ItemCollection<ProblemClass> ProblemClasses {
65      get { return problemClasses; }
66    }
67    private ItemCollection<Problem> problems;
68    public ItemCollection<Problem> Problems {
69      get { return problems; }
70    }
71    #endregion
72
73    private Administrator() { }
74
75    #region Refresh
76    public void Refresh() {
77      OnRefreshing();
78      if (platforms == null) {
79        platforms = new ItemCollection<Platform>();
80        platforms.ItemsRemoved += new CollectionItemsChangedEventHandler<Platform>(platforms_ItemsRemoved);
81      }
82      platforms.Clear();
83      if (dataTypes == null) {
84        dataTypes = new ItemCollection<DataType>();
85        dataTypes.ItemsRemoved += new CollectionItemsChangedEventHandler<DataType>(dataTypes_ItemsRemoved);
86      }
87      dataTypes.Clear();
88      if (algorithmClasses == null) {
89        algorithmClasses = new ItemCollection<AlgorithmClass>();
90        algorithmClasses.ItemsRemoved += new CollectionItemsChangedEventHandler<AlgorithmClass>(algorithmClasses_ItemsRemoved);
91      }
92      algorithmClasses.Clear();
93      if (algorithms == null) {
94        algorithms = new ItemCollection<Algorithm>();
95        algorithms.ItemsRemoved += new CollectionItemsChangedEventHandler<Algorithm>(algorithms_ItemsRemoved);
96      }
97      algorithms.Clear();
98      if (problemClasses == null) {
99        problemClasses = new ItemCollection<ProblemClass>();
100        problemClasses.ItemsRemoved += new CollectionItemsChangedEventHandler<ProblemClass>(problemClasses_ItemsRemoved);
101      }
102      problemClasses.Clear();
103      if (problems == null) {
104        problems = new ItemCollection<Problem>();
105        problems.ItemsRemoved += new CollectionItemsChangedEventHandler<Problem>(problems_ItemsRemoved);
106      }
107      problems.Clear();
108
109      var call = new Func<Exception>(delegate() {
110        try {
111          platforms.AddRange(CallAdminService<Platform[]>(s => s.GetPlatforms()).OrderBy(x => x.Name));
112          dataTypes.AddRange(CallAdminService<DataType[]>(s => s.GetDataTypes()).OrderBy(x => x.Name));
113          users = CallAuthenticationService<User[]>(s => s.GetUsers()).OrderBy(x => x.Name);
114          algorithmClasses.AddRange(CallAdminService<AlgorithmClass[]>(s => s.GetAlgorithmClasses()).OrderBy(x => x.Name));
115          algorithms.AddRange(CallAdminService<Algorithm[]>(s => s.GetAlgorithms()).OrderBy(x => x.Name));
116          problemClasses.AddRange(CallAdminService<ProblemClass[]>(s => s.GetProblemClasses()).OrderBy(x => x.Name));
117          problems.AddRange(CallAdminService<Problem[]>(s => s.GetProblems()).OrderBy(x => x.Name));
118          return null;
119        }
120        catch (Exception ex) {
121          return ex;
122        }
123      });
124      call.BeginInvoke(delegate(IAsyncResult result) {
125        Exception ex = call.EndInvoke(result);
126        if (ex != null) ErrorHandling.ShowErrorDialog("Refresh failed.", ex);
127        OnRefreshed();
128      }, null);
129    }
130    #endregion
131
132    #region Store
133    public bool Store(IOKBItem item) {
134      try {
135        if (item.Id == 0) {
136          if (item is Platform)
137            item.Id = CallAdminService<long>(s => s.AddPlatform((Platform)item));
138          else if (item is DataType)
139            item.Id = CallAdminService<long>(s => s.AddDataType((DataType)item));
140          else if (item is AlgorithmClass)
141            item.Id = CallAdminService<long>(s => s.AddAlgorithmClass((AlgorithmClass)item));
142          else if (item is Algorithm)
143            item.Id = CallAdminService<long>(s => s.AddAlgorithm((Algorithm)item));
144          else if (item is ProblemClass)
145            item.Id = CallAdminService<long>(s => s.AddProblemClass((ProblemClass)item));
146          else if (item is Problem)
147            item.Id = CallAdminService<long>(s => s.AddProblem((Problem)item));
148        } else {
149          if (item is Platform)
150            CallAdminService(s => s.UpdatePlatform((Platform)item));
151          else if (item is DataType)
152            CallAdminService(s => s.UpdateDataType((DataType)item));
153          else if (item is AlgorithmClass)
154            CallAdminService(s => s.UpdateAlgorithmClass((AlgorithmClass)item));
155          else if (item is Algorithm)
156            CallAdminService(s => s.UpdateAlgorithm((Algorithm)item));
157          else if (item is ProblemClass)
158            CallAdminService(s => s.UpdateProblemClass((ProblemClass)item));
159          else if (item is Problem)
160            CallAdminService(s => s.UpdateProblem((Problem)item));
161        }
162        return true;
163      }
164      catch (Exception ex) {
165        ErrorHandling.ShowErrorDialog("Store failed.", ex);
166        return false;
167      }
168    }
169    #endregion
170
171    #region Algorithm Methods
172    public Guid[] GetAlgorithmUsers(long algorithmId) {
173      try {
174        return CallAdminService<Guid[]>(s => s.GetAlgorithmUsers(algorithmId));
175      }
176      catch (Exception ex) {
177        ErrorHandling.ShowErrorDialog("Refresh authorized algorithm users failed.", ex);
178        return null;
179      }
180    }
181    public bool UpdateAlgorithmUsers(long algorithmId, Guid[] users) {
182      try {
183        CallAdminService(s => s.UpdateAlgorithmUsers(algorithmId, users));
184        return true;
185      }
186      catch (Exception ex) {
187        ErrorHandling.ShowErrorDialog("Update authorized algorithm users failed.", ex);
188        return false;
189      }
190    }
191    public AlgorithmData GetAlgorithmData(long algorithmId) {
192      try {
193        return CallAdminService<AlgorithmData>(s => s.GetAlgorithmData(algorithmId));
194      }
195      catch (Exception ex) {
196        ErrorHandling.ShowErrorDialog("Refresh algorithm data failed.", ex);
197        return null;
198      }
199    }
200    public bool UpdateAlgorithmData(AlgorithmData algorithmData) {
201      try {
202        CallAdminService(s => s.UpdateAlgorithmData(algorithmData));
203        return true;
204      }
205      catch (Exception ex) {
206        ErrorHandling.ShowErrorDialog("Update algorithm data failed.", ex);
207        return false;
208      }
209    }
210    #endregion
211
212    #region Problem Methods
213    public Guid[] GetProblemUsers(long problemId) {
214      try {
215        return CallAdminService<Guid[]>(s => s.GetProblemUsers(problemId));
216      }
217      catch (Exception ex) {
218        ErrorHandling.ShowErrorDialog("Refresh authorized problem users failed.", ex);
219        return null;
220      }
221    }
222    public bool UpdateProblemUsers(long problemId, Guid[] users) {
223      try {
224        CallAdminService(s => s.UpdateProblemUsers(problemId, users));
225        return true;
226      }
227      catch (Exception ex) {
228        ErrorHandling.ShowErrorDialog("Update authorized problem users failed.", ex);
229        return false;
230      }
231    }
232    public ProblemData GetProblemData(long problemId) {
233      try {
234        return CallAdminService<ProblemData>(s => s.GetProblemData(problemId));
235      }
236      catch (Exception ex) {
237        ErrorHandling.ShowErrorDialog("Refresh problem data failed.", ex);
238        return null;
239      }
240    }
241    public bool UpdateProblemData(ProblemData problemData) {
242      try {
243        CallAdminService(s => s.UpdateProblemData(problemData));
244        return true;
245      }
246      catch (Exception ex) {
247        ErrorHandling.ShowErrorDialog("Update problem data failed.", ex);
248        return false;
249      }
250    }
251    #endregion
252
253    #region Events
254    public event EventHandler Refreshing;
255    private void OnRefreshing() {
256      EventHandler handler = Refreshing;
257      if (handler != null) handler(this, EventArgs.Empty);
258    }
259    public event EventHandler Refreshed;
260    private void OnRefreshed() {
261      EventHandler handler = Refreshed;
262      if (handler != null) handler(this, EventArgs.Empty);
263    }
264
265    private void platforms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Platform> e) {
266      try {
267        foreach (Platform p in e.Items)
268          CallAdminService(s => s.DeletePlatform(p.Id));
269      }
270      catch (Exception ex) {
271        ErrorHandling.ShowErrorDialog("Delete failed.", ex);
272      }
273    }
274    private void dataTypes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<DataType> e) {
275      try {
276        foreach (DataType d in e.Items)
277          CallAdminService(s => s.DeleteDataType(d.Id));
278      }
279      catch (Exception ex) {
280        ErrorHandling.ShowErrorDialog("Delete failed.", ex);
281      }
282    }
283    private void algorithmClasses_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<AlgorithmClass> e) {
284      try {
285        foreach (AlgorithmClass a in e.Items)
286          CallAdminService(s => s.DeleteAlgorithmClass(a.Id));
287      }
288      catch (Exception ex) {
289        ErrorHandling.ShowErrorDialog("Delete failed.", ex);
290      }
291    }
292    private void algorithms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Algorithm> e) {
293      try {
294        foreach (Algorithm a in e.Items)
295          CallAdminService(s => s.DeleteAlgorithm(a.Id));
296      }
297      catch (Exception ex) {
298        ErrorHandling.ShowErrorDialog("Delete failed.", ex);
299      }
300    }
301    private void problemClasses_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<ProblemClass> e) {
302      try {
303        foreach (ProblemClass p in e.Items)
304          CallAdminService(s => s.DeleteProblemClass(p.Id));
305      }
306      catch (Exception ex) {
307        ErrorHandling.ShowErrorDialog("Delete failed.", ex);
308      }
309    }
310    private void problems_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Problem> e) {
311      try {
312        foreach (Problem p in e.Items)
313          CallAdminService(s => s.DeleteProblem(p.Id));
314      }
315      catch (Exception ex) {
316        ErrorHandling.ShowErrorDialog("Delete failed.", ex);
317      }
318    }
319    #endregion
320
321    #region Helpers
322    private void CallAdminService(Action<IAdminService> call) {
323      AdminServiceClient client = ClientFactory.CreateClient<AdminServiceClient, IAdminService>();
324      try {
325        call(client);
326      }
327      finally {
328        try {
329          client.Close();
330        }
331        catch (Exception) {
332          client.Abort();
333        }
334      }
335    }
336    private T CallAdminService<T>(Func<IAdminService, T> call) {
337      AdminServiceClient client = ClientFactory.CreateClient<AdminServiceClient, IAdminService>();
338      try {
339        return call(client);
340      }
341      finally {
342        try {
343          client.Close();
344        }
345        catch (Exception) {
346          client.Abort();
347        }
348      }
349    }
350    private T CallAuthenticationService<T>(Func<IAuthenticationService, T> call) {
351      AuthenticationServiceClient client = ClientFactory.CreateClient<AuthenticationServiceClient, IAuthenticationService>();
352      try {
353        return call(client);
354      }
355      finally {
356        try {
357          client.Close();
358        }
359        catch (Exception) {
360          client.Abort();
361        }
362      }
363    }
364    #endregion
365  }
366}
Note: See TracBrowser for help on using the repository browser.