Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on OKB data model and services (#1174)

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