Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on OKB (#1174)

File size: 10.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Administration {
29  /// <summary>
30  /// Implementation of the OKB administration service (interface <see cref="IAdministrationService"/>).
31  /// </summary>
32  [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
33  public class AdministrationService : IAdministrationService {
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 long AddPlatform(DataTransfer.Platform dto) {
46      using (OKBDataContext okb = new OKBDataContext()) {
47        DataAccess.Platform entity = Convert.ToEntity(dto); entity.Id = 0;
48        okb.Platforms.InsertOnSubmit(entity);
49        okb.SubmitChanges();
50        return entity.Id;
51      }
52    }
53    public void UpdatePlatform(DataTransfer.Platform dto) {
54      using (OKBDataContext okb = new OKBDataContext()) {
55        DataAccess.Platform entity = okb.Platforms.FirstOrDefault(x => x.Id == dto.Id);
56        Convert.ToEntity(dto, entity);
57        okb.SubmitChanges();
58      }
59    }
60    public void DeletePlatform(long id) {
61      using (OKBDataContext okb = new OKBDataContext()) {
62        DataAccess.Platform entity = okb.Platforms.FirstOrDefault(x => x.Id == id);
63        if (entity != null) okb.Platforms.DeleteOnSubmit(entity);
64        okb.SubmitChanges();
65      }
66    }
67    #endregion
68
69    #region AlgorithmClass Methods
70    public DataTransfer.AlgorithmClass GetAlgorithmClass(long id) {
71      using (OKBDataContext okb = new OKBDataContext()) {
72        return Convert.ToDto(okb.AlgorithmClasses.FirstOrDefault(x => x.Id == id));
73      }
74    }
75    public IEnumerable<DataTransfer.AlgorithmClass> GetAlgorithmClasses() {
76      using (OKBDataContext okb = new OKBDataContext()) {
77        return okb.AlgorithmClasses.Select(x => Convert.ToDto(x)).ToArray();
78      }
79    }
80    public long AddAlgorithmClass(DataTransfer.AlgorithmClass dto) {
81      using (OKBDataContext okb = new OKBDataContext()) {
82        DataAccess.AlgorithmClass entity = Convert.ToEntity(dto); entity.Id = 0;
83        okb.AlgorithmClasses.InsertOnSubmit(entity);
84        okb.SubmitChanges();
85        return entity.Id;
86      }
87    }
88    public void UpdateAlgorithmClass(DataTransfer.AlgorithmClass dto) {
89      using (OKBDataContext okb = new OKBDataContext()) {
90        DataAccess.AlgorithmClass entity = okb.AlgorithmClasses.FirstOrDefault(x => x.Id == dto.Id);
91        Convert.ToEntity(dto, entity);
92        okb.SubmitChanges();
93      }
94    }
95    public void DeleteAlgorithmClass(long id) {
96      using (OKBDataContext okb = new OKBDataContext()) {
97        DataAccess.AlgorithmClass entity = okb.AlgorithmClasses.FirstOrDefault(x => x.Id == id);
98        if (entity != null) okb.AlgorithmClasses.DeleteOnSubmit(entity);
99        okb.SubmitChanges();
100      }
101    }
102    #endregion
103
104    #region Algorithm Methods
105    public DataTransfer.Algorithm GetAlgorithm(long id) {
106      using (OKBDataContext okb = new OKBDataContext()) {
107        return Convert.ToDto(okb.Algorithms.FirstOrDefault(x => x.Id == id));
108      }
109    }
110    public IEnumerable<DataTransfer.Algorithm> GetAlgorithms() {
111      using (OKBDataContext okb = new OKBDataContext()) {
112        return okb.Algorithms.Select(x => Convert.ToDto(x)).ToArray();
113      }
114    }
115    public long AddAlgorithm(DataTransfer.Algorithm dto) {
116      using (OKBDataContext okb = new OKBDataContext()) {
117        DataAccess.Algorithm entity = Convert.ToEntity(dto, okb); entity.Id = 0;
118        okb.Algorithms.InsertOnSubmit(entity);
119        okb.SubmitChanges();
120        return entity.Id;
121      }
122    }
123    public void UpdateAlgorithm(DataTransfer.Algorithm dto) {
124      using (OKBDataContext okb = new OKBDataContext()) {
125        DataAccess.Algorithm entity = okb.Algorithms.FirstOrDefault(x => x.Id == dto.Id);
126        Convert.ToEntity(dto, entity, okb);
127        okb.SubmitChanges();
128      }
129    }
130    public void DeleteAlgorithm(long id) {
131      using (OKBDataContext okb = new OKBDataContext()) {
132        DataAccess.Algorithm entity = okb.Algorithms.FirstOrDefault(x => x.Id == id);
133        if (entity != null) okb.Algorithms.DeleteOnSubmit(entity);
134        okb.SubmitChanges();
135      }
136    }
137    public IEnumerable<Guid> GetAlgorithmUsers(long algorithmId) {
138      using (OKBDataContext okb = new OKBDataContext()) {
139        return okb.AlgorithmUsers.Where(x => x.AlgorithmId == algorithmId).Select(x => x.UserId).ToArray();
140      }
141    }
142    public void UpdateAlgorithmUsers(long algorithmId, IEnumerable<Guid> users) {
143      using (OKBDataContext okb = new OKBDataContext()) {
144        okb.AlgorithmUsers.DeleteAllOnSubmit(okb.AlgorithmUsers.Where(x => x.AlgorithmId == algorithmId));
145        okb.AlgorithmUsers.InsertAllOnSubmit(users.Select(x => new DataAccess.AlgorithmUser { AlgorithmId = algorithmId, UserId = x }));
146        okb.SubmitChanges();
147      }
148    }
149    public byte[] GetAlgorithmData(long algorithmId) {
150      using (OKBDataContext okb = new OKBDataContext()) {
151        var data = okb.Algorithms.Where(x => x.Id == algorithmId).Select(x => x.BinaryData).FirstOrDefault();
152        if (data != null) return data.Data.ToArray();
153        else return null;
154      }
155    }
156    public void UpdateAlgorithmData(long algorithmId, byte[] data) {
157      using (OKBDataContext okb = new OKBDataContext()) {
158        var entity = okb.Algorithms.Where(x => x.Id == algorithmId).FirstOrDefault();
159        if (entity != null)
160          entity.BinaryData = Convert.ToEntity(data, okb);
161        okb.SubmitChanges();
162      }
163    }
164    #endregion
165
166    #region ProblemClass Methods
167    public DataTransfer.ProblemClass GetProblemClass(long id) {
168      using (OKBDataContext okb = new OKBDataContext()) {
169        return Convert.ToDto(okb.ProblemClasses.FirstOrDefault(x => x.Id == id));
170      }
171    }
172    public IEnumerable<DataTransfer.ProblemClass> GetProblemClasses() {
173      using (OKBDataContext okb = new OKBDataContext()) {
174        return okb.ProblemClasses.Select(x => Convert.ToDto(x)).ToArray();
175      }
176    }
177    public long AddProblemClass(DataTransfer.ProblemClass dto) {
178      using (OKBDataContext okb = new OKBDataContext()) {
179        DataAccess.ProblemClass entity = Convert.ToEntity(dto); entity.Id = 0;
180        okb.ProblemClasses.InsertOnSubmit(entity);
181        okb.SubmitChanges();
182        return entity.Id;
183      }
184    }
185    public void UpdateProblemClass(DataTransfer.ProblemClass dto) {
186      using (OKBDataContext okb = new OKBDataContext()) {
187        DataAccess.ProblemClass entity = okb.ProblemClasses.FirstOrDefault(x => x.Id == dto.Id);
188        Convert.ToEntity(dto, entity);
189        okb.SubmitChanges();
190      }
191    }
192    public void DeleteProblemClass(long id) {
193      using (OKBDataContext okb = new OKBDataContext()) {
194        DataAccess.ProblemClass entity = okb.ProblemClasses.FirstOrDefault(x => x.Id == id);
195        if (entity != null) okb.ProblemClasses.DeleteOnSubmit(entity);
196        okb.SubmitChanges();
197      }
198    }
199    #endregion
200
201    #region Problem Methods
202    public DataTransfer.Problem GetProblem(long id) {
203      using (OKBDataContext okb = new OKBDataContext()) {
204        return Convert.ToDto(okb.Problems.FirstOrDefault(x => x.Id == id));
205      }
206    }
207    public IEnumerable<DataTransfer.Problem> GetProblems() {
208      using (OKBDataContext okb = new OKBDataContext()) {
209        return okb.Problems.Select(x => Convert.ToDto(x)).ToArray();
210      }
211    }
212    public long AddProblem(DataTransfer.Problem dto) {
213      using (OKBDataContext okb = new OKBDataContext()) {
214        DataAccess.Problem entity = Convert.ToEntity(dto, okb); entity.Id = 0;
215        okb.Problems.InsertOnSubmit(entity);
216        okb.SubmitChanges();
217        return entity.Id;
218      }
219    }
220    public void UpdateProblem(DataTransfer.Problem dto) {
221      using (OKBDataContext okb = new OKBDataContext()) {
222        DataAccess.Problem entity = okb.Problems.FirstOrDefault(x => x.Id == dto.Id);
223        Convert.ToEntity(dto, entity, okb);
224        okb.SubmitChanges();
225      }
226    }
227    public void DeleteProblem(long id) {
228      using (OKBDataContext okb = new OKBDataContext()) {
229        DataAccess.Problem entity = okb.Problems.FirstOrDefault(x => x.Id == id);
230        if (entity != null) okb.Problems.DeleteOnSubmit(entity);
231        okb.SubmitChanges();
232      }
233    }
234    public IEnumerable<Guid> GetProblemUsers(long problemId) {
235      using (OKBDataContext okb = new OKBDataContext()) {
236        return okb.ProblemUsers.Where(x => x.ProblemId == problemId).Select(x => x.UserId).ToArray();
237      }
238    }
239    public void UpdateProblemUsers(long problemId, IEnumerable<Guid> users) {
240      using (OKBDataContext okb = new OKBDataContext()) {
241        okb.ProblemUsers.DeleteAllOnSubmit(okb.ProblemUsers.Where(x => x.ProblemId == problemId));
242        okb.ProblemUsers.InsertAllOnSubmit(users.Select(x => new DataAccess.ProblemUser { ProblemId = problemId, UserId = x }));
243        okb.SubmitChanges();
244      }
245    }
246    public byte[] GetProblemData(long problemId) {
247      using (OKBDataContext okb = new OKBDataContext()) {
248        var data = okb.Problems.Where(x => x.Id == problemId).Select(x => x.BinaryData).FirstOrDefault();
249        if (data != null) return data.Data.ToArray();
250        else return null;
251      }
252    }
253    public void UpdateProblemData(long problemId, byte[] data) {
254      using (OKBDataContext okb = new OKBDataContext()) {
255        var entity = okb.Problems.Where(x => x.Id == problemId).FirstOrDefault();
256        if (entity != null)
257          entity.BinaryData = Convert.ToEntity(data, okb);
258        okb.SubmitChanges();
259      }
260    }
261    #endregion
262  }
263}
Note: See TracBrowser for help on using the repository browser.