Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on OKB data model and services (#1174)

File size: 9.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.Collections.Generic;
23using System.Data.Linq;
24using System.Linq;
25using System.ServiceModel;
26using HeuristicLab.Services.OKB.DataAccess;
27
28namespace HeuristicLab.Services.OKB {
29  /// <summary>
30  /// Implementation of <see cref="IAdminService"/>
31  /// </summary>
32  [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
33  public class AdminService : IAdminService {
34    public AlgorithmClass GetAlgorithmClass(long algorithmClassId) {
35      using (OKBDataContext okb = new OKBDataContext()) {
36        return okb.AlgorithmClasses.FirstOrDefault(a => a.Id == algorithmClassId);
37      }
38    }
39    public IEnumerable<AlgorithmClass> GetAlgorithmClasses() {
40      using (OKBDataContext okb = new OKBDataContext()) {
41        return okb.AlgorithmClasses.ToArray();
42      }
43    }
44    public void StoreAlgorithmClass(AlgorithmClass algorithmClass) {
45      using (OKBDataContext okb = new OKBDataContext()) {
46        AlgorithmClass original = okb.AlgorithmClasses.FirstOrDefault(a => a.Id == algorithmClass.Id);
47        if (original == null) {
48          okb.AlgorithmClasses.InsertOnSubmit(new AlgorithmClass() {
49            Name = algorithmClass.Name,
50            Description = algorithmClass.Description
51          });
52        } else {
53          original.Name = algorithmClass.Name;
54          original.Description = algorithmClass.Description;
55        }
56        okb.SubmitChanges();
57      }
58    }
59    public void DeleteAlgorithmClass(long algorithmClassId) {
60      using (OKBDataContext okb = new OKBDataContext()) {
61        AlgorithmClass algorithmClass = okb.AlgorithmClasses.FirstOrDefault(a => a.Id == algorithmClassId);
62        if (algorithmClass != null) {
63          okb.AlgorithmClasses.DeleteOnSubmit(algorithmClass);
64          okb.SubmitChanges();
65        }
66      }
67    }
68
69    public Algorithm GetAlgorithm(long algorithmId) {
70      using (OKBDataContext okb = new OKBDataContext()) {
71        return okb.Algorithms.FirstOrDefault(a => a.Id == algorithmId);
72      }
73    }
74    public IEnumerable<Algorithm> GetAlgorithms() {
75      using (OKBDataContext okb = new OKBDataContext()) {
76        return okb.Algorithms.ToArray();
77      }
78    }
79    public void StoreAlgorithm(Algorithm algorithm) {
80      using (OKBDataContext okb = new OKBDataContext()) {
81        Algorithm original = okb.Algorithms.FirstOrDefault(a => a.Id == algorithm.Id);
82        if (original == null) {
83          okb.Algorithms.InsertOnSubmit(new Algorithm() {
84            Name = algorithm.Name,
85            Description = algorithm.Description
86          });
87        } else {
88          original.Name = algorithm.Name;
89          original.Description = algorithm.Description;
90        }
91        okb.SubmitChanges();
92      }
93    }
94    public void DeleteAlgorithm(long algorithmId) {
95      using (OKBDataContext okb = new OKBDataContext()) {
96        Algorithm algorithm = okb.Algorithms.FirstOrDefault(a => a.Id == algorithmId);
97        if (algorithm != null) {
98          okb.Algorithms.DeleteOnSubmit(algorithm);
99          okb.SubmitChanges();
100        }
101      }
102    }
103
104    /// <summary>
105    /// Gets all available platforms.
106    /// </summary>
107    /// <returns>A list of <see cref="Platform"/>s.</returns>
108    public Platform[] GetPlatforms() {
109      using (OKBDataContext okb = new OKBDataContext()) {
110        return okb.Platforms.ToArray();
111      }
112    }
113
114    /// <summary>
115    /// Gets the complete algorithm object graph up to the following entities:
116    /// <list type="bullet">
117    ///   <item>Parameter</item>
118    ///   <item>Algorithm_Paramters.Parameter.DataType</item>
119    ///   <item>Algorithm_Results.Result.DataType</item>
120    /// </list>
121    /// </summary>
122    /// <param name="id">The algorithm id.</param>
123    /// <returns>An <see cref="Algorithm"/></returns>
124    public Algorithm GetCompleteAlgorithm(int id) {
125      using (OKBDataContext okb = new OKBDataContext()) {
126        var dlo = new DataLoadOptions();
127        dlo.LoadWith<Algorithm>(a => a.AlgorithmClass);
128        dlo.LoadWith<Algorithm>(a => a.Platform);
129        dlo.LoadWith<Algorithm>(a => a.AlgorithmUsers);
130        dlo.LoadWith<AlgorithmUser>(u => u.User);
131        okb.LoadOptions = dlo;
132        return okb.Algorithms.Single(a => a.Id == id);
133      }
134    }
135
136    /// <summary>
137    /// Gets the complete problem object graph up to the following entities:
138    /// <list type="bullet">
139    ///   <item>Platform</item>
140    ///   <item>SolutionRepresentation</item>
141    ///   <item>Problem_Parameters.Parameter</item>
142    ///   <item>IntProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
143    ///   <item>FloatProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
144    ///   <item>CharProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
145    /// </list>
146    /// </summary>
147    /// <param name="id">The problem id.</param>
148    /// <returns>A <see cref="Problem"/></returns>
149    public Problem GetCompleteProblem(int id) {
150      using (OKBDataContext okb = new OKBDataContext()) {
151        var dlo = new DataLoadOptions();
152        dlo.LoadWith<Problem>(p => p.ProblemClass);
153        dlo.LoadWith<Problem>(p => p.Platform);
154        dlo.LoadWith<Problem>(p => p.ProblemUsers);
155        dlo.LoadWith<ProblemUser>(u => u.User);
156        okb.LoadOptions = dlo;
157        return okb.Problems.Single(p => p.Id == id);
158      }
159    }
160
161    /// <summary>
162    /// Updates the algorithm object graph including the following properties and linked entitites:
163    /// <list type="bullet">
164    ///   <item>Name</item>
165    ///   <item>Description</item>
166    ///   <item>AlgorithmClassId</item>
167    ///   <item>PlatformId</item>
168    ///   <item>Algorithm_Parameters</item>
169    ///   <item>Algorithm_Results</item>
170    /// </list>
171    /// <remarks>
172    /// New <see cref="Parameter"/>s or <see cref="Result"/>s will not be
173    /// created but have to be pre-existing.
174    /// </remarks>
175    /// </summary>
176    /// <param name="algorithm">The algorithm.</param>
177    public void UpdateCompleteAlgorithm(Algorithm algorithm) {
178      using (OKBDataContext okb = new OKBDataContext()) {
179        Algorithm original = okb.Algorithms.Single(a => a.Id == algorithm.Id);
180        UpdateAlgorithmData(algorithm, original, okb);
181        okb.SubmitChanges();
182      }
183    }
184
185    /// <summary>
186    /// Updates the problem object graph including the following properties and linked entities:
187    /// <list type="bullet">
188    ///   <item>Name</item>
189    ///   <item>Description</item>
190    ///   <item>ProblemClassId</item>
191    ///   <item>PlatformId</item>
192    ///   <item>SolutionRepresentationId</item>
193    ///   <item>IntProblemCharacteristicValues.Value</item>
194    ///   <item>FloatProblemCharacteristicValues.Value</item>
195    ///   <item>CharProblemCharacteristicValues.Value</item>
196    ///   <item>Problem_Parameters</item>
197    /// </list>
198    /// <remarks>
199    /// New <see cref="ProblemCharacteristic"/>s or <see cref="Parameter"/>s will
200    /// not be created but have to be pre-existing.
201    /// </remarks>
202    /// </summary>
203    /// <param name="problem">The problem.</param>
204    public void UpdateCompleteProblem(Problem problem) {
205      using (OKBDataContext okb = new OKBDataContext()) {
206        Problem originalProblem = okb.Problems.Single(p => p.Id == problem.Id);
207        UpdateProblemData(problem, originalProblem, okb);
208        okb.SubmitChanges();
209      }
210    }
211
212    private static void UpdateAlgorithmData(Algorithm algorithm, Algorithm original, OKBDataContext okb) {
213      original.Name = algorithm.Name;
214      original.Description = algorithm.Description;
215      original.AlgorithmClassId = algorithm.AlgorithmClassId;
216      original.PlatformId = algorithm.PlatformId;
217      okb.AlgorithmUsers.DeleteAllOnSubmit(original.AlgorithmUsers);
218      original.AlgorithmUsers.Clear();
219      foreach (var u in algorithm.AlgorithmUsers) {
220        original.AlgorithmUsers.Add(new AlgorithmUser() {
221          AlgorithmId = original.Id,
222          UserId = u.UserId
223        });
224      }
225    }
226
227    private static void UpdateProblemData(Problem problem, Problem original, OKBDataContext okb) {
228      original.Name = problem.Name;
229      original.Description = problem.Description;
230      original.ProblemClassId = problem.ProblemClassId;
231      original.SolutionRepresentationId = problem.SolutionRepresentationId;
232      original.PlatformId = problem.PlatformId;
233      okb.ProblemUsers.DeleteAllOnSubmit(original.ProblemUsers);
234      original.ProblemUsers.Clear();
235      foreach (var u in problem.ProblemUsers) {
236        original.ProblemUsers.Add(new ProblemUser() {
237          ProblemId = original.Id,
238          UserId = u.UserId
239        });
240      }
241    }
242  }
243}
Note: See TracBrowser for help on using the repository browser.