Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on OKB (#1174)

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