Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.OKB/3.3/AdminService.cs @ 4467

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

Worked on OKB (#1174)

File size: 12.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 void StorePlatform(DataTransfer.Platform dto) {
46      using (OKBDataContext okb = new OKBDataContext()) {
47        DataAccess.Platform entity = okb.Platforms.FirstOrDefault(x => x.Id == dto.Id);
48        if (entity == null) okb.Platforms.InsertOnSubmit(Convert.ToEntity(dto));
49        else Convert.ToEntity(dto, entity);
50        okb.SubmitChanges();
51      }
52    }
53    public void DeletePlatform(long id) {
54      using (OKBDataContext okb = new OKBDataContext()) {
55        DataAccess.Platform entity = okb.Platforms.FirstOrDefault(x => x.Id == id);
56        if (entity != null) okb.Platforms.DeleteOnSubmit(entity);
57        okb.SubmitChanges();
58      }
59    }
60    #endregion
61
62    #region AlgorithmClass Methods
63    public DataTransfer.AlgorithmClass GetAlgorithmClass(long id) {
64      using (OKBDataContext okb = new OKBDataContext()) {
65        return Convert.ToDto(okb.AlgorithmClasses.FirstOrDefault(x => x.Id == id));
66      }
67    }
68    public IEnumerable<DataTransfer.AlgorithmClass> GetAlgorithmClasses() {
69      using (OKBDataContext okb = new OKBDataContext()) {
70        return okb.AlgorithmClasses.Select(x => Convert.ToDto(x)).ToArray();
71      }
72    }
73    public void StoreAlgorithmClass(DataTransfer.AlgorithmClass dto) {
74      using (OKBDataContext okb = new OKBDataContext()) {
75        DataAccess.AlgorithmClass entity = okb.AlgorithmClasses.FirstOrDefault(x => x.Id == dto.Id);
76        if (entity == null) okb.AlgorithmClasses.InsertOnSubmit(Convert.ToEntity(dto));
77        else Convert.ToEntity(dto, entity);
78        okb.SubmitChanges();
79      }
80    }
81    public void DeleteAlgorithmClass(long id) {
82      using (OKBDataContext okb = new OKBDataContext()) {
83        DataAccess.AlgorithmClass entity = okb.AlgorithmClasses.FirstOrDefault(x => x.Id == id);
84        if (entity != null) okb.AlgorithmClasses.DeleteOnSubmit(entity);
85        okb.SubmitChanges();
86      }
87    }
88    #endregion
89
90    #region Algorithm Methods
91    public DataTransfer.Algorithm GetAlgorithm(long id) {
92      using (OKBDataContext okb = new OKBDataContext()) {
93        return Convert.ToDto(okb.Algorithms.FirstOrDefault(x => x.Id == id));
94      }
95    }
96    public IEnumerable<DataTransfer.Algorithm> GetAlgorithms() {
97      using (OKBDataContext okb = new OKBDataContext()) {
98        return okb.Algorithms.Select(x => Convert.ToDto(x)).ToArray();
99      }
100    }
101    public void StoreAlgorithm(DataTransfer.Algorithm dto) {
102      using (OKBDataContext okb = new OKBDataContext()) {
103        DataAccess.Algorithm entity = okb.Algorithms.FirstOrDefault(x => x.Id == dto.Id);
104        if (entity == null) okb.Algorithms.InsertOnSubmit(Convert.ToEntity(dto));
105        else Convert.ToEntity(dto, entity);
106        okb.SubmitChanges();
107      }
108    }
109    public void DeleteAlgorithm(long id) {
110      using (OKBDataContext okb = new OKBDataContext()) {
111        DataAccess.Algorithm entity = okb.Algorithms.FirstOrDefault(x => x.Id == id);
112        if (entity != null) okb.Algorithms.DeleteOnSubmit(entity);
113        okb.SubmitChanges();
114      }
115    }
116    public IEnumerable<Guid> GetAlgorithmUsers(long algorithmId) {
117      using (OKBDataContext okb = new OKBDataContext()) {
118        return okb.AlgorithmUsers.Where(x => x.AlgorithmId == algorithmId).Select(x => x.UserId).ToArray();
119      }
120    }
121    public void StoreAlgorithmUsers(long algorithmId, IEnumerable<Guid> users) {
122      using (OKBDataContext okb = new OKBDataContext()) {
123        okb.AlgorithmUsers.DeleteAllOnSubmit(okb.AlgorithmUsers.Where(x => x.AlgorithmId == algorithmId));
124        okb.AlgorithmUsers.InsertAllOnSubmit(users.Select(x => new DataAccess.AlgorithmUser { AlgorithmId = algorithmId, UserId = x }));
125        okb.SubmitChanges();
126      }
127    }
128    #endregion
129
130    #region AlgorithmData Methods
131    public DataTransfer.AlgorithmData GetAlgorithmData(long algorithmId) {
132      using (OKBDataContext okb = new OKBDataContext()) {
133        return Convert.ToDto(okb.AlgorithmDatas.FirstOrDefault(x => x.AlgorithmId == algorithmId));
134      }
135    }
136    public void StoreAlgorithmData(DataTransfer.AlgorithmData dto) {
137      using (OKBDataContext okb = new OKBDataContext()) {
138        DataAccess.AlgorithmData entity = okb.AlgorithmDatas.FirstOrDefault(x => x.AlgorithmId == dto.AlgorithmId);
139        if (entity == null) okb.AlgorithmDatas.InsertOnSubmit(Convert.ToEntity(dto));
140        else Convert.ToEntity(dto, entity);
141        okb.SubmitChanges();
142      }
143    }
144    #endregion
145
146    #region DataType Methods
147    public DataTransfer.DataType GetDataType(long id) {
148      using (OKBDataContext okb = new OKBDataContext()) {
149        return Convert.ToDto(okb.DataTypes.FirstOrDefault(x => x.Id == id));
150      }
151    }
152    public IEnumerable<DataTransfer.DataType> GetDataTypes() {
153      using (OKBDataContext okb = new OKBDataContext()) {
154        return okb.DataTypes.Select(x => Convert.ToDto(x)).ToArray();
155      }
156    }
157    public void StoreDataType(DataTransfer.DataType dto) {
158      using (OKBDataContext okb = new OKBDataContext()) {
159        DataAccess.DataType entity = okb.DataTypes.FirstOrDefault(x => x.Id == dto.Id);
160        if (entity == null) okb.DataTypes.InsertOnSubmit(Convert.ToEntity(dto));
161        else Convert.ToEntity(dto, entity);
162        okb.SubmitChanges();
163      }
164    }
165    public void DeleteDataType(long id) {
166      using (OKBDataContext okb = new OKBDataContext()) {
167        DataAccess.DataType entity = okb.DataTypes.FirstOrDefault(x => x.Id == id);
168        if (entity != null) okb.DataTypes.DeleteOnSubmit(entity);
169        okb.SubmitChanges();
170      }
171    }
172    #endregion
173
174    /*
175
176    /// <summary>
177    /// Gets the complete algorithm object graph up to the following entities:
178    /// <list type="bullet">
179    ///   <item>Parameter</item>
180    ///   <item>Algorithm_Paramters.Parameter.DataType</item>
181    ///   <item>Algorithm_Results.Result.DataType</item>
182    /// </list>
183    /// </summary>
184    /// <param name="id">The algorithm id.</param>
185    /// <returns>An <see cref="Algorithm"/></returns>
186    public Algorithm GetCompleteAlgorithm(int id) {
187      using (OKBDataContext okb = new OKBDataContext()) {
188        var dlo = new DataLoadOptions();
189        dlo.LoadWith<Algorithm>(a => a.AlgorithmClass);
190        dlo.LoadWith<Algorithm>(a => a.Platform);
191        dlo.LoadWith<Algorithm>(a => a.AlgorithmUsers);
192        dlo.LoadWith<AlgorithmUser>(u => u.User);
193        okb.LoadOptions = dlo;
194        return okb.Algorithms.Single(a => a.Id == id);
195      }
196    }
197
198    /// <summary>
199    /// Gets the complete problem object graph up to the following entities:
200    /// <list type="bullet">
201    ///   <item>Platform</item>
202    ///   <item>SolutionRepresentation</item>
203    ///   <item>Problem_Parameters.Parameter</item>
204    ///   <item>IntProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
205    ///   <item>FloatProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
206    ///   <item>CharProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
207    /// </list>
208    /// </summary>
209    /// <param name="id">The problem id.</param>
210    /// <returns>A <see cref="Problem"/></returns>
211    public Problem GetCompleteProblem(int id) {
212      using (OKBDataContext okb = new OKBDataContext()) {
213        var dlo = new DataLoadOptions();
214        dlo.LoadWith<Problem>(p => p.ProblemClass);
215        dlo.LoadWith<Problem>(p => p.Platform);
216        dlo.LoadWith<Problem>(p => p.ProblemUsers);
217        dlo.LoadWith<ProblemUser>(u => u.User);
218        okb.LoadOptions = dlo;
219        return okb.Problems.Single(p => p.Id == id);
220      }
221    }
222
223    /// <summary>
224    /// Updates the algorithm object graph including the following properties and linked entitites:
225    /// <list type="bullet">
226    ///   <item>Name</item>
227    ///   <item>Description</item>
228    ///   <item>AlgorithmClassId</item>
229    ///   <item>PlatformId</item>
230    ///   <item>Algorithm_Parameters</item>
231    ///   <item>Algorithm_Results</item>
232    /// </list>
233    /// <remarks>
234    /// New <see cref="Parameter"/>s or <see cref="Result"/>s will not be
235    /// created but have to be pre-existing.
236    /// </remarks>
237    /// </summary>
238    /// <param name="algorithm">The algorithm.</param>
239    public void UpdateCompleteAlgorithm(Algorithm algorithm) {
240      using (OKBDataContext okb = new OKBDataContext()) {
241        Algorithm original = okb.Algorithms.Single(a => a.Id == algorithm.Id);
242        UpdateAlgorithmData(algorithm, original, okb);
243        okb.SubmitChanges();
244      }
245    }
246
247    /// <summary>
248    /// Updates the problem object graph including the following properties and linked entities:
249    /// <list type="bullet">
250    ///   <item>Name</item>
251    ///   <item>Description</item>
252    ///   <item>ProblemClassId</item>
253    ///   <item>PlatformId</item>
254    ///   <item>SolutionRepresentationId</item>
255    ///   <item>IntProblemCharacteristicValues.Value</item>
256    ///   <item>FloatProblemCharacteristicValues.Value</item>
257    ///   <item>CharProblemCharacteristicValues.Value</item>
258    ///   <item>Problem_Parameters</item>
259    /// </list>
260    /// <remarks>
261    /// New <see cref="ProblemCharacteristic"/>s or <see cref="Parameter"/>s will
262    /// not be created but have to be pre-existing.
263    /// </remarks>
264    /// </summary>
265    /// <param name="problem">The problem.</param>
266    public void UpdateCompleteProblem(Problem problem) {
267      using (OKBDataContext okb = new OKBDataContext()) {
268        Problem originalProblem = okb.Problems.Single(p => p.Id == problem.Id);
269        UpdateProblemData(problem, originalProblem, okb);
270        okb.SubmitChanges();
271      }
272    }
273
274    private static void UpdateAlgorithmData(Algorithm algorithm, Algorithm original, OKBDataContext okb) {
275      original.Name = algorithm.Name;
276      original.Description = algorithm.Description;
277      original.AlgorithmClassId = algorithm.AlgorithmClassId;
278      original.PlatformId = algorithm.PlatformId;
279      okb.AlgorithmUsers.DeleteAllOnSubmit(original.AlgorithmUsers);
280      original.AlgorithmUsers.Clear();
281      foreach (var u in algorithm.AlgorithmUsers) {
282        original.AlgorithmUsers.Add(new AlgorithmUser() {
283          AlgorithmId = original.Id,
284          UserId = u.UserId
285        });
286      }
287    }
288
289    private static void UpdateProblemData(Problem problem, Problem original, OKBDataContext okb) {
290      original.Name = problem.Name;
291      original.Description = problem.Description;
292      original.ProblemClassId = problem.ProblemClassId;
293      original.SolutionRepresentationId = problem.SolutionRepresentationId;
294      original.PlatformId = problem.PlatformId;
295      okb.ProblemUsers.DeleteAllOnSubmit(original.ProblemUsers);
296      original.ProblemUsers.Clear();
297      foreach (var u in problem.ProblemUsers) {
298        original.ProblemUsers.Add(new ProblemUser() {
299          ProblemId = original.Id,
300          UserId = u.UserId
301        });
302      }
303    }*/
304  }
305}
Note: See TracBrowser for help on using the repository browser.