Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on OKB data model and services (#1174)

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