Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB (trunk integration)/HeuristicLab.Services.OKB/3.3/Administration/AdministrationService.cs @ 5479

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

Worked on OKB (#1174)

File size: 14.8 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.Data.Linq;
25using System.Linq;
26using System.Security.Cryptography;
27using System.ServiceModel;
28using HeuristicLab.Services.OKB.DataAccess;
29
30namespace HeuristicLab.Services.OKB.Administration {
31  /// <summary>
32  /// Implementation of the OKB administration service (interface <see cref="IAdministrationService"/>).
33  /// </summary>
34  [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
35  public class AdministrationService : IAdministrationService {
36    #region Platform Methods
37    public DataTransfer.Platform GetPlatform(long id) {
38      using (OKBDataContext okb = new OKBDataContext()) {
39        return Convert.ToDto(okb.Platforms.FirstOrDefault(x => x.Id == id));
40      }
41    }
42    public IEnumerable<DataTransfer.Platform> GetPlatforms() {
43      using (OKBDataContext okb = new OKBDataContext()) {
44        return okb.Platforms.Select(x => Convert.ToDto(x)).ToArray();
45      }
46    }
47    public long AddPlatform(DataTransfer.Platform dto) {
48      using (OKBDataContext okb = new OKBDataContext()) {
49        DataAccess.Platform entity = Convert.ToEntity(dto); entity.Id = 0;
50        okb.Platforms.InsertOnSubmit(entity);
51        okb.SubmitChanges();
52        return entity.Id;
53      }
54    }
55    public void UpdatePlatform(DataTransfer.Platform dto) {
56      using (OKBDataContext okb = new OKBDataContext()) {
57        DataAccess.Platform entity = okb.Platforms.FirstOrDefault(x => x.Id == dto.Id);
58        Convert.ToEntity(dto, entity);
59        okb.SubmitChanges();
60      }
61    }
62    public void DeletePlatform(long id) {
63      using (OKBDataContext okb = new OKBDataContext()) {
64        DataAccess.Platform entity = okb.Platforms.FirstOrDefault(x => x.Id == id);
65        if (entity != null) okb.Platforms.DeleteOnSubmit(entity);
66        okb.SubmitChanges();
67      }
68    }
69    #endregion
70
71    #region DataType Methods
72    public DataTransfer.DataType GetDataType(long id) {
73      using (OKBDataContext okb = new OKBDataContext()) {
74        return Convert.ToDto(okb.DataTypes.FirstOrDefault(x => x.Id == id));
75      }
76    }
77    public long AddDataType(DataTransfer.DataType dto) {
78      using (OKBDataContext okb = new OKBDataContext()) {
79        var entity = okb.DataTypes.Where(x => (x.PlatformId == dto.PlatformId) && (x.Name == dto.Name) && (x.TypeName == dto.TypeName)).FirstOrDefault();
80        if (entity == null) {
81          entity = Convert.ToEntity(dto); entity.Id = 0;
82          okb.DataTypes.InsertOnSubmit(entity);
83          okb.SubmitChanges();
84        }
85        return entity.Id;
86      }
87    }
88    #endregion
89
90    #region BinaryData Methods
91    public long GetBinaryDataId(byte[] hash) {
92      using (OKBDataContext okb = new OKBDataContext()) {
93        var id = okb.BinaryDatas.Where(x => x.Hash.Equals(hash)).Select(x => x.Id).FirstOrDefault();
94        if (id == 0) return -1;
95        else return id;
96      }
97    }
98    public long AddBinaryData(DataTransfer.BinaryData dto) {
99      byte[] hash;
100      using (SHA1 sha1 = SHA1.Create()) {
101        hash = sha1.ComputeHash(dto.Data);
102      }
103
104      using (OKBDataContext okb = new OKBDataContext()) {
105        var id = okb.BinaryDatas.Where(x => x.Hash.Equals(hash)).Select(x => x.Id).FirstOrDefault();
106        if (id != 0) {
107          return id;
108        } else {
109          DataAccess.BinaryData entity = Convert.ToEntity(dto); entity.Id = 0;
110          okb.BinaryDatas.InsertOnSubmit(entity);
111          okb.SubmitChanges();
112          return entity.Id;
113        }
114      }
115    }
116    #endregion
117
118    #region AlgorithmClass Methods
119    public DataTransfer.AlgorithmClass GetAlgorithmClass(long id) {
120      using (OKBDataContext okb = new OKBDataContext()) {
121        return Convert.ToDto(okb.AlgorithmClasses.FirstOrDefault(x => x.Id == id));
122      }
123    }
124    public IEnumerable<DataTransfer.AlgorithmClass> GetAlgorithmClasses() {
125      using (OKBDataContext okb = new OKBDataContext()) {
126        return okb.AlgorithmClasses.Select(x => Convert.ToDto(x)).ToArray();
127      }
128    }
129    public long AddAlgorithmClass(DataTransfer.AlgorithmClass dto) {
130      using (OKBDataContext okb = new OKBDataContext()) {
131        DataAccess.AlgorithmClass entity = Convert.ToEntity(dto); entity.Id = 0;
132        okb.AlgorithmClasses.InsertOnSubmit(entity);
133        okb.SubmitChanges();
134        return entity.Id;
135      }
136    }
137    public void UpdateAlgorithmClass(DataTransfer.AlgorithmClass dto) {
138      using (OKBDataContext okb = new OKBDataContext()) {
139        DataAccess.AlgorithmClass entity = okb.AlgorithmClasses.FirstOrDefault(x => x.Id == dto.Id);
140        Convert.ToEntity(dto, entity);
141        okb.SubmitChanges();
142      }
143    }
144    public void DeleteAlgorithmClass(long id) {
145      using (OKBDataContext okb = new OKBDataContext()) {
146        DataAccess.AlgorithmClass entity = okb.AlgorithmClasses.FirstOrDefault(x => x.Id == id);
147        if (entity != null) okb.AlgorithmClasses.DeleteOnSubmit(entity);
148        okb.SubmitChanges();
149      }
150    }
151    #endregion
152
153    #region Algorithm Methods
154    public DataTransfer.Algorithm GetAlgorithm(long id) {
155      using (OKBDataContext okb = new OKBDataContext()) {
156        return Convert.ToDto(okb.Algorithms.FirstOrDefault(x => x.Id == id));
157      }
158    }
159    public IEnumerable<DataTransfer.Algorithm> GetAlgorithms() {
160      using (OKBDataContext okb = new OKBDataContext()) {
161        return okb.Algorithms.Select(x => Convert.ToDto(x)).ToArray();
162      }
163    }
164    public long AddAlgorithm(DataTransfer.Algorithm dto) {
165      using (OKBDataContext okb = new OKBDataContext()) {
166        DataAccess.Algorithm entity = Convert.ToEntity(dto); entity.Id = 0;
167        okb.Algorithms.InsertOnSubmit(entity);
168        okb.SubmitChanges();
169        return entity.Id;
170      }
171    }
172    public void UpdateAlgorithm(DataTransfer.Algorithm dto) {
173      using (OKBDataContext okb = new OKBDataContext()) {
174        DataAccess.Algorithm entity = okb.Algorithms.FirstOrDefault(x => x.Id == dto.Id);
175        Convert.ToEntity(dto, entity);
176        okb.SubmitChanges();
177      }
178    }
179    public void DeleteAlgorithm(long id) {
180      using (OKBDataContext okb = new OKBDataContext()) {
181        DataAccess.Algorithm entity = okb.Algorithms.FirstOrDefault(x => x.Id == id);
182        if (entity != null) okb.Algorithms.DeleteOnSubmit(entity);
183        okb.SubmitChanges();
184      }
185    }
186    public IEnumerable<Guid> GetAlgorithmUsers(long algorithmId) {
187      using (OKBDataContext okb = new OKBDataContext()) {
188        return okb.AlgorithmUsers.Where(x => x.AlgorithmId == algorithmId).Select(x => x.UserId).ToArray();
189      }
190    }
191    public void UpdateAlgorithmUsers(long algorithmId, IEnumerable<Guid> users) {
192      using (OKBDataContext okb = new OKBDataContext()) {
193        okb.AlgorithmUsers.DeleteAllOnSubmit(okb.AlgorithmUsers.Where(x => x.AlgorithmId == algorithmId));
194        okb.AlgorithmUsers.InsertAllOnSubmit(users.Select(x => new DataAccess.AlgorithmUser { AlgorithmId = algorithmId, UserId = x }));
195        okb.SubmitChanges();
196      }
197    }
198    public DataTransfer.BinaryData GetAlgorithmData(long algorithmId) {
199      using (OKBDataContext okb = new OKBDataContext()) {
200        DataLoadOptions dlo = new DataLoadOptions();
201        dlo.LoadWith<Algorithm>(x => x.BinaryData);
202        okb.LoadOptions = dlo;
203        return Convert.ToDto(okb.Algorithms.Where(x => x.Id == algorithmId).Select(x => x.BinaryData).FirstOrDefault());
204      }
205    }
206    #endregion
207
208    #region ProblemClass Methods
209    public DataTransfer.ProblemClass GetProblemClass(long id) {
210      using (OKBDataContext okb = new OKBDataContext()) {
211        return Convert.ToDto(okb.ProblemClasses.FirstOrDefault(x => x.Id == id));
212      }
213    }
214    public IEnumerable<DataTransfer.ProblemClass> GetProblemClasses() {
215      using (OKBDataContext okb = new OKBDataContext()) {
216        return okb.ProblemClasses.Select(x => Convert.ToDto(x)).ToArray();
217      }
218    }
219    public long AddProblemClass(DataTransfer.ProblemClass dto) {
220      using (OKBDataContext okb = new OKBDataContext()) {
221        DataAccess.ProblemClass entity = Convert.ToEntity(dto); entity.Id = 0;
222        okb.ProblemClasses.InsertOnSubmit(entity);
223        okb.SubmitChanges();
224        return entity.Id;
225      }
226    }
227    public void UpdateProblemClass(DataTransfer.ProblemClass dto) {
228      using (OKBDataContext okb = new OKBDataContext()) {
229        DataAccess.ProblemClass entity = okb.ProblemClasses.FirstOrDefault(x => x.Id == dto.Id);
230        Convert.ToEntity(dto, entity);
231        okb.SubmitChanges();
232      }
233    }
234    public void DeleteProblemClass(long id) {
235      using (OKBDataContext okb = new OKBDataContext()) {
236        DataAccess.ProblemClass entity = okb.ProblemClasses.FirstOrDefault(x => x.Id == id);
237        if (entity != null) okb.ProblemClasses.DeleteOnSubmit(entity);
238        okb.SubmitChanges();
239      }
240    }
241    #endregion
242
243    #region Problem Methods
244    public DataTransfer.Problem GetProblem(long id) {
245      using (OKBDataContext okb = new OKBDataContext()) {
246        return Convert.ToDto(okb.Problems.FirstOrDefault(x => x.Id == id));
247      }
248    }
249    public IEnumerable<DataTransfer.Problem> GetProblems() {
250      using (OKBDataContext okb = new OKBDataContext()) {
251        return okb.Problems.Select(x => Convert.ToDto(x)).ToArray();
252      }
253    }
254    public long AddProblem(DataTransfer.Problem dto) {
255      using (OKBDataContext okb = new OKBDataContext()) {
256        DataAccess.Problem entity = Convert.ToEntity(dto); entity.Id = 0;
257        okb.Problems.InsertOnSubmit(entity);
258        okb.SubmitChanges();
259        return entity.Id;
260      }
261    }
262    public void UpdateProblem(DataTransfer.Problem dto) {
263      using (OKBDataContext okb = new OKBDataContext()) {
264        DataAccess.Problem entity = okb.Problems.FirstOrDefault(x => x.Id == dto.Id);
265        Convert.ToEntity(dto, entity);
266        okb.SubmitChanges();
267      }
268    }
269    public void DeleteProblem(long id) {
270      using (OKBDataContext okb = new OKBDataContext()) {
271        DataAccess.Problem entity = okb.Problems.FirstOrDefault(x => x.Id == id);
272        if (entity != null) okb.Problems.DeleteOnSubmit(entity);
273        okb.SubmitChanges();
274      }
275    }
276    public IEnumerable<Guid> GetProblemUsers(long problemId) {
277      using (OKBDataContext okb = new OKBDataContext()) {
278        return okb.ProblemUsers.Where(x => x.ProblemId == problemId).Select(x => x.UserId).ToArray();
279      }
280    }
281    public void UpdateProblemUsers(long problemId, IEnumerable<Guid> users) {
282      using (OKBDataContext okb = new OKBDataContext()) {
283        okb.ProblemUsers.DeleteAllOnSubmit(okb.ProblemUsers.Where(x => x.ProblemId == problemId));
284        okb.ProblemUsers.InsertAllOnSubmit(users.Select(x => new DataAccess.ProblemUser { ProblemId = problemId, UserId = x }));
285        okb.SubmitChanges();
286      }
287    }
288    public DataTransfer.BinaryData GetProblemData(long problemId) {
289      using (OKBDataContext okb = new OKBDataContext()) {
290        DataLoadOptions dlo = new DataLoadOptions();
291        dlo.LoadWith<Problem>(x => x.BinaryData);
292        okb.LoadOptions = dlo;
293        return Convert.ToDto(okb.Problems.Where(x => x.Id == problemId).Select(x => x.BinaryData).FirstOrDefault());
294      }
295    }
296    #endregion
297
298    #region Run Methods
299    public DataTransfer.Run GetRun(long id) {
300      using (OKBDataContext okb = new OKBDataContext()) {
301        DataLoadOptions dlo = new DataLoadOptions();
302        dlo.LoadWith<Run>(x => x.Values);
303        dlo.LoadWith<Value>(x => x.ValueName);
304        okb.LoadOptions = dlo;
305        return Convert.ToDto(okb.Runs.FirstOrDefault(x => x.Id == id));
306      }
307    }
308    public IEnumerable<DataTransfer.Run> GetRuns(long algorithmId, long problemId) {
309      using (OKBDataContext okb = new OKBDataContext()) {
310        DataLoadOptions dlo = new DataLoadOptions();
311        dlo.LoadWith<Run>(x => x.Values);
312        dlo.LoadWith<Value>(x => x.ValueName);
313        okb.LoadOptions = dlo;
314        if ((algorithmId != 0) && (problemId != 0))
315          return okb.Runs.Where(x => (x.AlgorithmId == algorithmId) && (x.ProblemId == problemId)).Select(x => Convert.ToDto(x)).ToArray();
316        else if (algorithmId != 0)
317          return okb.Runs.Where(x => x.AlgorithmId == algorithmId).Select(x => Convert.ToDto(x)).ToArray();
318        else if (problemId != 0)
319          return okb.Runs.Where(x => x.ProblemId == problemId).Select(x => Convert.ToDto(x)).ToArray();
320        else
321          return okb.Runs.Select(x => Convert.ToDto(x)).ToArray();
322      }
323    }
324    public long AddRun(DataTransfer.Run dto) {
325      using (OKBDataContext okb = new OKBDataContext()) {
326        DataAccess.Run entity = Convert.ToEntity(dto); entity.Id = 0;
327        okb.Runs.InsertOnSubmit(entity);
328        okb.SubmitChanges();
329        return entity.Id;
330      }
331    }
332    public void DeleteRun(long id) {
333      using (OKBDataContext okb = new OKBDataContext()) {
334        IEnumerable<DataAccess.ResultBlobValue> resultBlobValues = okb.ResultBlobValues.Where(x => x.RunId == id);
335        okb.ResultBlobValues.DeleteAllOnSubmit(resultBlobValues);
336        IEnumerable<DataAccess.ResultBoolValue> resultBoolValues = okb.ResultBoolValues.Where(x => x.RunId == id);
337        okb.ResultBoolValues.DeleteAllOnSubmit(resultBoolValues);
338        IEnumerable<DataAccess.ResultFloatValue> resultFloatValues = okb.ResultFloatValues.Where(x => x.RunId == id);
339        okb.ResultFloatValues.DeleteAllOnSubmit(resultFloatValues);
340        IEnumerable<DataAccess.ResultIntValue> resultIntValues = okb.ResultIntValues.Where(x => x.RunId == id);
341        okb.ResultIntValues.DeleteAllOnSubmit(resultIntValues);
342        IEnumerable<DataAccess.ResultStringValue> resultStringValues = okb.ResultStringValues.Where(x => x.RunId == id);
343        okb.ResultStringValues.DeleteAllOnSubmit(resultStringValues);
344
345        DataAccess.Run entity = okb.Runs.FirstOrDefault(x => x.Id == id);
346        if (entity != null) okb.Runs.DeleteOnSubmit(entity);
347        okb.SubmitChanges();
348      }
349    }
350    #endregion
351  }
352}
Note: See TracBrowser for help on using the repository browser.