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