Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on OKB (#1174)

File size: 10.8 KB
RevLine 
[4279]1#region License Information
2/* HeuristicLab
[5902]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4279]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
[4467]22using System;
[4426]23using System.Collections.Generic;
[4279]24using System.Linq;
25using System.ServiceModel;
26using HeuristicLab.Services.OKB.DataAccess;
27
[5479]28namespace HeuristicLab.Services.OKB.Administration {
[4279]29  /// <summary>
[5299]30  /// Implementation of the OKB administration service (interface <see cref="IAdministrationService"/>).
[4279]31  /// </summary>
[4381]32  [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
[5299]33  public class AdministrationService : IAdministrationService {
[4442]34    #region Platform Methods
[5679]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    }
[4455]40    public IEnumerable<DataTransfer.Platform> GetPlatforms() {
[4442]41      using (OKBDataContext okb = new OKBDataContext()) {
[4455]42        return okb.Platforms.Select(x => Convert.ToDto(x)).ToArray();
[4442]43      }
44    }
[4481]45    public long AddPlatform(DataTransfer.Platform dto) {
[4442]46      using (OKBDataContext okb = new OKBDataContext()) {
[4481]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()) {
[4455]55        DataAccess.Platform entity = okb.Platforms.FirstOrDefault(x => x.Id == dto.Id);
[4481]56        Convert.ToEntity(dto, entity);
[4442]57        okb.SubmitChanges();
58      }
59    }
[4455]60    public void DeletePlatform(long id) {
[4442]61      using (OKBDataContext okb = new OKBDataContext()) {
[4455]62        DataAccess.Platform entity = okb.Platforms.FirstOrDefault(x => x.Id == id);
63        if (entity != null) okb.Platforms.DeleteOnSubmit(entity);
64        okb.SubmitChanges();
[4442]65      }
66    }
67    #endregion
68
69    #region AlgorithmClass Methods
[5679]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    }
[4455]75    public IEnumerable<DataTransfer.AlgorithmClass> GetAlgorithmClasses() {
[4407]76      using (OKBDataContext okb = new OKBDataContext()) {
[4455]77        return okb.AlgorithmClasses.Select(x => Convert.ToDto(x)).ToArray();
[4407]78      }
79    }
[4481]80    public long AddAlgorithmClass(DataTransfer.AlgorithmClass dto) {
[4407]81      using (OKBDataContext okb = new OKBDataContext()) {
[4481]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()) {
[4455]90        DataAccess.AlgorithmClass entity = okb.AlgorithmClasses.FirstOrDefault(x => x.Id == dto.Id);
[4481]91        Convert.ToEntity(dto, entity);
[4407]92        okb.SubmitChanges();
93      }
94    }
[4455]95    public void DeleteAlgorithmClass(long id) {
[4407]96      using (OKBDataContext okb = new OKBDataContext()) {
[4455]97        DataAccess.AlgorithmClass entity = okb.AlgorithmClasses.FirstOrDefault(x => x.Id == id);
98        if (entity != null) okb.AlgorithmClasses.DeleteOnSubmit(entity);
99        okb.SubmitChanges();
[4407]100      }
101    }
[4442]102    #endregion
[4407]103
[4442]104    #region Algorithm Methods
[5679]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    }
[4455]110    public IEnumerable<DataTransfer.Algorithm> GetAlgorithms() {
[4407]111      using (OKBDataContext okb = new OKBDataContext()) {
[4455]112        return okb.Algorithms.Select(x => Convert.ToDto(x)).ToArray();
[4407]113      }
114    }
[4481]115    public long AddAlgorithm(DataTransfer.Algorithm dto) {
[4407]116      using (OKBDataContext okb = new OKBDataContext()) {
[5482]117        DataAccess.Algorithm entity = Convert.ToEntity(dto, okb); entity.Id = 0;
[4481]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()) {
[4455]125        DataAccess.Algorithm entity = okb.Algorithms.FirstOrDefault(x => x.Id == dto.Id);
[5482]126        Convert.ToEntity(dto, entity, okb);
[4407]127        okb.SubmitChanges();
128      }
129    }
[4455]130    public void DeleteAlgorithm(long id) {
[4407]131      using (OKBDataContext okb = new OKBDataContext()) {
[4455]132        DataAccess.Algorithm entity = okb.Algorithms.FirstOrDefault(x => x.Id == id);
133        if (entity != null) okb.Algorithms.DeleteOnSubmit(entity);
134        okb.SubmitChanges();
[4407]135      }
136    }
[4467]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    }
[4481]142    public void UpdateAlgorithmUsers(long algorithmId, IEnumerable<Guid> users) {
[4467]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    }
[5482]149    public byte[] GetAlgorithmData(long algorithmId) {
[4467]150      using (OKBDataContext okb = new OKBDataContext()) {
[5534]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;
[4467]154      }
155    }
[5482]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    }
[4467]164    #endregion
165
[4481]166    #region ProblemClass Methods
[5679]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    }
[4481]172    public IEnumerable<DataTransfer.ProblemClass> GetProblemClasses() {
[4467]173      using (OKBDataContext okb = new OKBDataContext()) {
[4481]174        return okb.ProblemClasses.Select(x => Convert.ToDto(x)).ToArray();
[4467]175      }
176    }
[4481]177    public long AddProblemClass(DataTransfer.ProblemClass dto) {
[4467]178      using (OKBDataContext okb = new OKBDataContext()) {
[4481]179        DataAccess.ProblemClass entity = Convert.ToEntity(dto); entity.Id = 0;
180        okb.ProblemClasses.InsertOnSubmit(entity);
[4467]181        okb.SubmitChanges();
[4481]182        return entity.Id;
[4467]183      }
184    }
[4481]185    public void UpdateProblemClass(DataTransfer.ProblemClass dto) {
[4467]186      using (OKBDataContext okb = new OKBDataContext()) {
[4481]187        DataAccess.ProblemClass entity = okb.ProblemClasses.FirstOrDefault(x => x.Id == dto.Id);
188        Convert.ToEntity(dto, entity);
[4467]189        okb.SubmitChanges();
190      }
191    }
[4481]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    }
[4467]199    #endregion
200
[4481]201    #region Problem Methods
[5679]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    }
[4481]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()) {
[5482]214        DataAccess.Problem entity = Convert.ToEntity(dto, okb); entity.Id = 0;
[4481]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);
[5482]223        Convert.ToEntity(dto, entity, okb);
[4481]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    }
[5482]246    public byte[] GetProblemData(long problemId) {
[4481]247      using (OKBDataContext okb = new OKBDataContext()) {
[5534]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;
[4481]251      }
252    }
[5482]253    public void UpdateProblemData(long problemId, byte[] data) {
[4566]254      using (OKBDataContext okb = new OKBDataContext()) {
[5482]255        var entity = okb.Problems.Where(x => x.Id == problemId).FirstOrDefault();
256        if (entity != null)
257          entity.BinaryData = Convert.ToEntity(data, okb);
[5269]258        okb.SubmitChanges();
259      }
260    }
261    #endregion
[4279]262  }
263}
Note: See TracBrowser for help on using the repository browser.