Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.Services.OKB/3.3/AdminService.cs @ 4481

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

Worked on OKB (#1174)

File size: 17.5 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 System.ServiceModel;
26using HeuristicLab.Services.OKB.DataAccess;
27
28namespace HeuristicLab.Services.OKB {
29  /// <summary>
30  /// Implementation of the OKB administration service (interface <see cref="IAdminService"/>).
31  /// </summary>
32  [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
33  public class AdminService : IAdminService {
34    #region Platform Methods
35    public DataTransfer.Platform GetPlatform(long id) {
36      using (OKBDataContext okb = new OKBDataContext()) {
37        return Convert.ToDto(okb.Platforms.FirstOrDefault(x => x.Id == id));
38      }
39    }
40    public IEnumerable<DataTransfer.Platform> GetPlatforms() {
41      using (OKBDataContext okb = new OKBDataContext()) {
42        return okb.Platforms.Select(x => Convert.ToDto(x)).ToArray();
43      }
44    }
45    public long AddPlatform(DataTransfer.Platform dto) {
46      using (OKBDataContext okb = new OKBDataContext()) {
47        DataAccess.Platform entity = Convert.ToEntity(dto); entity.Id = 0;
48        okb.Platforms.InsertOnSubmit(entity);
49        okb.SubmitChanges();
50        return entity.Id;
51      }
52    }
53    public void UpdatePlatform(DataTransfer.Platform dto) {
54      using (OKBDataContext okb = new OKBDataContext()) {
55        DataAccess.Platform entity = okb.Platforms.FirstOrDefault(x => x.Id == dto.Id);
56        Convert.ToEntity(dto, entity);
57        okb.SubmitChanges();
58      }
59    }
60    public void DeletePlatform(long id) {
61      using (OKBDataContext okb = new OKBDataContext()) {
62        DataAccess.Platform entity = okb.Platforms.FirstOrDefault(x => x.Id == id);
63        if (entity != null) okb.Platforms.DeleteOnSubmit(entity);
64        okb.SubmitChanges();
65      }
66    }
67    #endregion
68
69    #region DataType Methods
70    public DataTransfer.DataType GetDataType(long id) {
71      using (OKBDataContext okb = new OKBDataContext()) {
72        return Convert.ToDto(okb.DataTypes.FirstOrDefault(x => x.Id == id));
73      }
74    }
75    public IEnumerable<DataTransfer.DataType> GetDataTypes() {
76      using (OKBDataContext okb = new OKBDataContext()) {
77        return okb.DataTypes.Select(x => Convert.ToDto(x)).ToArray();
78      }
79    }
80    public long AddDataType(DataTransfer.DataType dto) {
81      using (OKBDataContext okb = new OKBDataContext()) {
82        DataAccess.DataType entity = Convert.ToEntity(dto); entity.Id = 0;
83        okb.DataTypes.InsertOnSubmit(entity);
84        okb.SubmitChanges();
85        return entity.Id;
86      }
87    }
88    public void UpdateDataType(DataTransfer.DataType dto) {
89      using (OKBDataContext okb = new OKBDataContext()) {
90        DataAccess.DataType entity = okb.DataTypes.FirstOrDefault(x => x.Id == dto.Id);
91        Convert.ToEntity(dto, entity);
92        okb.SubmitChanges();
93      }
94    }
95    public void DeleteDataType(long id) {
96      using (OKBDataContext okb = new OKBDataContext()) {
97        DataAccess.DataType entity = okb.DataTypes.FirstOrDefault(x => x.Id == id);
98        if (entity != null) okb.DataTypes.DeleteOnSubmit(entity);
99        okb.SubmitChanges();
100      }
101    }
102    #endregion
103
104    #region AlgorithmClass Methods
105    public DataTransfer.AlgorithmClass GetAlgorithmClass(long id) {
106      using (OKBDataContext okb = new OKBDataContext()) {
107        return Convert.ToDto(okb.AlgorithmClasses.FirstOrDefault(x => x.Id == id));
108      }
109    }
110    public IEnumerable<DataTransfer.AlgorithmClass> GetAlgorithmClasses() {
111      using (OKBDataContext okb = new OKBDataContext()) {
112        return okb.AlgorithmClasses.Select(x => Convert.ToDto(x)).ToArray();
113      }
114    }
115    public long AddAlgorithmClass(DataTransfer.AlgorithmClass dto) {
116      using (OKBDataContext okb = new OKBDataContext()) {
117        DataAccess.AlgorithmClass entity = Convert.ToEntity(dto); entity.Id = 0;
118        okb.AlgorithmClasses.InsertOnSubmit(entity);
119        okb.SubmitChanges();
120        return entity.Id;
121      }
122    }
123    public void UpdateAlgorithmClass(DataTransfer.AlgorithmClass dto) {
124      using (OKBDataContext okb = new OKBDataContext()) {
125        DataAccess.AlgorithmClass entity = okb.AlgorithmClasses.FirstOrDefault(x => x.Id == dto.Id);
126        Convert.ToEntity(dto, entity);
127        okb.SubmitChanges();
128      }
129    }
130    public void DeleteAlgorithmClass(long id) {
131      using (OKBDataContext okb = new OKBDataContext()) {
132        DataAccess.AlgorithmClass entity = okb.AlgorithmClasses.FirstOrDefault(x => x.Id == id);
133        if (entity != null) okb.AlgorithmClasses.DeleteOnSubmit(entity);
134        okb.SubmitChanges();
135      }
136    }
137    #endregion
138
139    #region Algorithm Methods
140    public DataTransfer.Algorithm GetAlgorithm(long id) {
141      using (OKBDataContext okb = new OKBDataContext()) {
142        return Convert.ToDto(okb.Algorithms.FirstOrDefault(x => x.Id == id));
143      }
144    }
145    public IEnumerable<DataTransfer.Algorithm> GetAlgorithms() {
146      using (OKBDataContext okb = new OKBDataContext()) {
147        return okb.Algorithms.Select(x => Convert.ToDto(x)).ToArray();
148      }
149    }
150    public long AddAlgorithm(DataTransfer.Algorithm dto) {
151      using (OKBDataContext okb = new OKBDataContext()) {
152        DataAccess.Algorithm entity = Convert.ToEntity(dto); entity.Id = 0;
153        okb.Algorithms.InsertOnSubmit(entity);
154        okb.SubmitChanges();
155        return entity.Id;
156      }
157    }
158    public void UpdateAlgorithm(DataTransfer.Algorithm dto) {
159      using (OKBDataContext okb = new OKBDataContext()) {
160        DataAccess.Algorithm entity = okb.Algorithms.FirstOrDefault(x => x.Id == dto.Id);
161        Convert.ToEntity(dto, entity);
162        okb.SubmitChanges();
163      }
164    }
165    public void DeleteAlgorithm(long id) {
166      using (OKBDataContext okb = new OKBDataContext()) {
167        DataAccess.Algorithm entity = okb.Algorithms.FirstOrDefault(x => x.Id == id);
168        if (entity != null) okb.Algorithms.DeleteOnSubmit(entity);
169        okb.SubmitChanges();
170      }
171    }
172    public IEnumerable<Guid> GetAlgorithmUsers(long algorithmId) {
173      using (OKBDataContext okb = new OKBDataContext()) {
174        return okb.AlgorithmUsers.Where(x => x.AlgorithmId == algorithmId).Select(x => x.UserId).ToArray();
175      }
176    }
177    public void UpdateAlgorithmUsers(long algorithmId, IEnumerable<Guid> users) {
178      using (OKBDataContext okb = new OKBDataContext()) {
179        okb.AlgorithmUsers.DeleteAllOnSubmit(okb.AlgorithmUsers.Where(x => x.AlgorithmId == algorithmId));
180        okb.AlgorithmUsers.InsertAllOnSubmit(users.Select(x => new DataAccess.AlgorithmUser { AlgorithmId = algorithmId, UserId = x }));
181        okb.SubmitChanges();
182      }
183    }
184    #endregion
185
186    #region AlgorithmData Methods
187    public DataTransfer.AlgorithmData GetAlgorithmData(long algorithmId) {
188      using (OKBDataContext okb = new OKBDataContext()) {
189        return Convert.ToDto(okb.AlgorithmDatas.FirstOrDefault(x => x.AlgorithmId == algorithmId));
190      }
191    }
192    public void UpdateAlgorithmData(DataTransfer.AlgorithmData dto) {
193      using (OKBDataContext okb = new OKBDataContext()) {
194        DataAccess.AlgorithmData entity = okb.AlgorithmDatas.FirstOrDefault(x => x.AlgorithmId == dto.AlgorithmId);
195        if (entity == null) okb.AlgorithmDatas.InsertOnSubmit(Convert.ToEntity(dto));
196        else Convert.ToEntity(dto, entity);
197        okb.SubmitChanges();
198      }
199    }
200    #endregion
201
202    #region ProblemClass Methods
203    public DataTransfer.ProblemClass GetProblemClass(long id) {
204      using (OKBDataContext okb = new OKBDataContext()) {
205        return Convert.ToDto(okb.ProblemClasses.FirstOrDefault(x => x.Id == id));
206      }
207    }
208    public IEnumerable<DataTransfer.ProblemClass> GetProblemClasses() {
209      using (OKBDataContext okb = new OKBDataContext()) {
210        return okb.ProblemClasses.Select(x => Convert.ToDto(x)).ToArray();
211      }
212    }
213    public long AddProblemClass(DataTransfer.ProblemClass dto) {
214      using (OKBDataContext okb = new OKBDataContext()) {
215        DataAccess.ProblemClass entity = Convert.ToEntity(dto); entity.Id = 0;
216        okb.ProblemClasses.InsertOnSubmit(entity);
217        okb.SubmitChanges();
218        return entity.Id;
219      }
220    }
221    public void UpdateProblemClass(DataTransfer.ProblemClass dto) {
222      using (OKBDataContext okb = new OKBDataContext()) {
223        DataAccess.ProblemClass entity = okb.ProblemClasses.FirstOrDefault(x => x.Id == dto.Id);
224        Convert.ToEntity(dto, entity);
225        okb.SubmitChanges();
226      }
227    }
228    public void DeleteProblemClass(long id) {
229      using (OKBDataContext okb = new OKBDataContext()) {
230        DataAccess.ProblemClass entity = okb.ProblemClasses.FirstOrDefault(x => x.Id == id);
231        if (entity != null) okb.ProblemClasses.DeleteOnSubmit(entity);
232        okb.SubmitChanges();
233      }
234    }
235    #endregion
236
237    #region Problem Methods
238    public DataTransfer.Problem GetProblem(long id) {
239      using (OKBDataContext okb = new OKBDataContext()) {
240        return Convert.ToDto(okb.Problems.FirstOrDefault(x => x.Id == id));
241      }
242    }
243    public IEnumerable<DataTransfer.Problem> GetProblems() {
244      using (OKBDataContext okb = new OKBDataContext()) {
245        return okb.Problems.Select(x => Convert.ToDto(x)).ToArray();
246      }
247    }
248    public long AddProblem(DataTransfer.Problem dto) {
249      using (OKBDataContext okb = new OKBDataContext()) {
250        DataAccess.Problem entity = Convert.ToEntity(dto); entity.Id = 0;
251        okb.Problems.InsertOnSubmit(entity);
252        okb.SubmitChanges();
253        return entity.Id;
254      }
255    }
256    public void UpdateProblem(DataTransfer.Problem dto) {
257      using (OKBDataContext okb = new OKBDataContext()) {
258        DataAccess.Problem entity = okb.Problems.FirstOrDefault(x => x.Id == dto.Id);
259        Convert.ToEntity(dto, entity);
260        okb.SubmitChanges();
261      }
262    }
263    public void DeleteProblem(long id) {
264      using (OKBDataContext okb = new OKBDataContext()) {
265        DataAccess.Problem entity = okb.Problems.FirstOrDefault(x => x.Id == id);
266        if (entity != null) okb.Problems.DeleteOnSubmit(entity);
267        okb.SubmitChanges();
268      }
269    }
270    public IEnumerable<Guid> GetProblemUsers(long problemId) {
271      using (OKBDataContext okb = new OKBDataContext()) {
272        return okb.ProblemUsers.Where(x => x.ProblemId == problemId).Select(x => x.UserId).ToArray();
273      }
274    }
275    public void UpdateProblemUsers(long problemId, IEnumerable<Guid> users) {
276      using (OKBDataContext okb = new OKBDataContext()) {
277        okb.ProblemUsers.DeleteAllOnSubmit(okb.ProblemUsers.Where(x => x.ProblemId == problemId));
278        okb.ProblemUsers.InsertAllOnSubmit(users.Select(x => new DataAccess.ProblemUser { ProblemId = problemId, UserId = x }));
279        okb.SubmitChanges();
280      }
281    }
282    #endregion
283
284    #region ProblemData Methods
285    public DataTransfer.ProblemData GetProblemData(long problemId) {
286      using (OKBDataContext okb = new OKBDataContext()) {
287        return Convert.ToDto(okb.ProblemDatas.FirstOrDefault(x => x.ProblemId == problemId));
288      }
289    }
290    public void UpdateProblemData(DataTransfer.ProblemData dto) {
291      using (OKBDataContext okb = new OKBDataContext()) {
292        DataAccess.ProblemData entity = okb.ProblemDatas.FirstOrDefault(x => x.ProblemId == dto.ProblemId);
293        if (entity == null) okb.ProblemDatas.InsertOnSubmit(Convert.ToEntity(dto));
294        else Convert.ToEntity(dto, entity);
295        okb.SubmitChanges();
296      }
297    }
298    #endregion
299
300    /*
301
302    /// <summary>
303    /// Gets the complete algorithm object graph up to the following entities:
304    /// <list type="bullet">
305    ///   <item>Parameter</item>
306    ///   <item>Algorithm_Paramters.Parameter.DataType</item>
307    ///   <item>Algorithm_Results.Result.DataType</item>
308    /// </list>
309    /// </summary>
310    /// <param name="id">The algorithm id.</param>
311    /// <returns>An <see cref="Algorithm"/></returns>
312    public Algorithm GetCompleteAlgorithm(int id) {
313      using (OKBDataContext okb = new OKBDataContext()) {
314        var dlo = new DataLoadOptions();
315        dlo.LoadWith<Algorithm>(a => a.AlgorithmClass);
316        dlo.LoadWith<Algorithm>(a => a.Platform);
317        dlo.LoadWith<Algorithm>(a => a.AlgorithmUsers);
318        dlo.LoadWith<AlgorithmUser>(u => u.User);
319        okb.LoadOptions = dlo;
320        return okb.Algorithms.Single(a => a.Id == id);
321      }
322    }
323
324    /// <summary>
325    /// Gets the complete problem object graph up to the following entities:
326    /// <list type="bullet">
327    ///   <item>Platform</item>
328    ///   <item>SolutionRepresentation</item>
329    ///   <item>Problem_Parameters.Parameter</item>
330    ///   <item>IntProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
331    ///   <item>FloatProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
332    ///   <item>CharProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
333    /// </list>
334    /// </summary>
335    /// <param name="id">The problem id.</param>
336    /// <returns>A <see cref="Problem"/></returns>
337    public Problem GetCompleteProblem(int id) {
338      using (OKBDataContext okb = new OKBDataContext()) {
339        var dlo = new DataLoadOptions();
340        dlo.LoadWith<Problem>(p => p.ProblemClass);
341        dlo.LoadWith<Problem>(p => p.Platform);
342        dlo.LoadWith<Problem>(p => p.ProblemUsers);
343        dlo.LoadWith<ProblemUser>(u => u.User);
344        okb.LoadOptions = dlo;
345        return okb.Problems.Single(p => p.Id == id);
346      }
347    }
348
349    /// <summary>
350    /// Updates the algorithm object graph including the following properties and linked entitites:
351    /// <list type="bullet">
352    ///   <item>Name</item>
353    ///   <item>Description</item>
354    ///   <item>AlgorithmClassId</item>
355    ///   <item>PlatformId</item>
356    ///   <item>Algorithm_Parameters</item>
357    ///   <item>Algorithm_Results</item>
358    /// </list>
359    /// <remarks>
360    /// New <see cref="Parameter"/>s or <see cref="Result"/>s will not be
361    /// created but have to be pre-existing.
362    /// </remarks>
363    /// </summary>
364    /// <param name="algorithm">The algorithm.</param>
365    public void UpdateCompleteAlgorithm(Algorithm algorithm) {
366      using (OKBDataContext okb = new OKBDataContext()) {
367        Algorithm original = okb.Algorithms.Single(a => a.Id == algorithm.Id);
368        UpdateAlgorithmData(algorithm, original, okb);
369        okb.SubmitChanges();
370      }
371    }
372
373    /// <summary>
374    /// Updates the problem object graph including the following properties and linked entities:
375    /// <list type="bullet">
376    ///   <item>Name</item>
377    ///   <item>Description</item>
378    ///   <item>ProblemClassId</item>
379    ///   <item>PlatformId</item>
380    ///   <item>SolutionRepresentationId</item>
381    ///   <item>IntProblemCharacteristicValues.Value</item>
382    ///   <item>FloatProblemCharacteristicValues.Value</item>
383    ///   <item>CharProblemCharacteristicValues.Value</item>
384    ///   <item>Problem_Parameters</item>
385    /// </list>
386    /// <remarks>
387    /// New <see cref="ProblemCharacteristic"/>s or <see cref="Parameter"/>s will
388    /// not be created but have to be pre-existing.
389    /// </remarks>
390    /// </summary>
391    /// <param name="problem">The problem.</param>
392    public void UpdateCompleteProblem(Problem problem) {
393      using (OKBDataContext okb = new OKBDataContext()) {
394        Problem originalProblem = okb.Problems.Single(p => p.Id == problem.Id);
395        UpdateProblemData(problem, originalProblem, okb);
396        okb.SubmitChanges();
397      }
398    }
399
400    private static void UpdateAlgorithmData(Algorithm algorithm, Algorithm original, OKBDataContext okb) {
401      original.Name = algorithm.Name;
402      original.Description = algorithm.Description;
403      original.AlgorithmClassId = algorithm.AlgorithmClassId;
404      original.PlatformId = algorithm.PlatformId;
405      okb.AlgorithmUsers.DeleteAllOnSubmit(original.AlgorithmUsers);
406      original.AlgorithmUsers.Clear();
407      foreach (var u in algorithm.AlgorithmUsers) {
408        original.AlgorithmUsers.Add(new AlgorithmUser() {
409          AlgorithmId = original.Id,
410          UserId = u.UserId
411        });
412      }
413    }
414
415    private static void UpdateProblemData(Problem problem, Problem original, OKBDataContext okb) {
416      original.Name = problem.Name;
417      original.Description = problem.Description;
418      original.ProblemClassId = problem.ProblemClassId;
419      original.SolutionRepresentationId = problem.SolutionRepresentationId;
420      original.PlatformId = problem.PlatformId;
421      okb.ProblemUsers.DeleteAllOnSubmit(original.ProblemUsers);
422      original.ProblemUsers.Clear();
423      foreach (var u in problem.ProblemUsers) {
424        original.ProblemUsers.Add(new ProblemUser() {
425          ProblemId = original.Id,
426          UserId = u.UserId
427        });
428      }
429    }*/
430  }
431}
Note: See TracBrowser for help on using the repository browser.