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