Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.OKB/3.3/RunCreation/RunCreationService.cs @ 13682

Last change on this file since 13682 was 13682, checked in by abeham, 8 years ago

#2588:

  • Added table, FK constraints, and FK indexes to database
  • Updated Linq2Sql mapping
  • Added service methods and dtos for downloading
File size: 11.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.ServiceModel;
27using HeuristicLab.Services.Access;
28using HeuristicLab.Services.OKB.DataAccess;
29
30namespace HeuristicLab.Services.OKB.RunCreation {
31  /// <summary>
32  /// Implementation of the OKB run creation service (interface <see cref="IRunCreationService"/>).
33  /// </summary>
34  [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
35  public class RunCreationService : IRunCreationService {
36    IRoleVerifier roleVerifier = AccessServiceLocator.Instance.RoleVerifier;
37    IUserManager userManager = AccessServiceLocator.Instance.UserManager;
38
39    public IEnumerable<DataTransfer.Algorithm> GetAlgorithms(string platformName) {
40      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
41
42      using (OKBDataContext okb = new OKBDataContext()) {
43        DataLoadOptions dlo = new DataLoadOptions();
44        dlo.LoadWith<Algorithm>(x => x.AlgorithmClass);
45        dlo.LoadWith<Algorithm>(x => x.DataType);
46        dlo.LoadWith<Algorithm>(x => x.AlgorithmUsers);
47        okb.LoadOptions = dlo;
48
49        var query = okb.Algorithms.Where(x => x.Platform.Name == platformName);
50        List<Algorithm> results = new List<Algorithm>();
51
52        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
53          results.AddRange(query);
54        } else {
55          foreach (var alg in query) {
56            if (alg.AlgorithmUsers.Count() == 0 || userManager.VerifyUser(userManager.CurrentUserId, alg.AlgorithmUsers.Select(y => y.UserGroupId).ToList())) {
57              results.Add(alg);
58            }
59          }
60        }
61        return results.Select(x => Convert.ToDto(x)).ToArray();
62      }
63    }
64
65    public byte[] GetAlgorithmData(long algorithmId) {
66      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
67
68      using (OKBDataContext okb = new OKBDataContext()) {
69        var result = okb.Algorithms.Where(x => x.Id == algorithmId).Select(x => x.BinaryData.Data.ToArray()).FirstOrDefault();
70
71        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
72          return result;
73        } else {
74          var algUsers = okb.AlgorithmUsers.Where(x => x.AlgorithmId == algorithmId);
75          if (algUsers.Count() == 0 || userManager.VerifyUser(userManager.CurrentUserId, algUsers.Select(y => y.UserGroupId).ToList())) {
76            return result;
77          } else {
78            return null;
79          }
80        }
81      }
82    }
83
84    public IEnumerable<DataTransfer.Problem> GetProblems(string platformName) {
85      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
86
87      using (OKBDataContext okb = new OKBDataContext()) {
88        DataLoadOptions dlo = new DataLoadOptions();
89        dlo.LoadWith<Problem>(x => x.ProblemClass);
90        dlo.LoadWith<Problem>(x => x.DataType);
91        dlo.LoadWith<Problem>(x => x.ProblemUsers);
92        okb.LoadOptions = dlo;
93
94        var query = okb.Problems.Where(x => x.Platform.Name == platformName);
95        List<Problem> results = new List<Problem>();
96
97        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
98          results.AddRange(query);
99        } else {
100          foreach (var problem in query) {
101            if (problem.ProblemUsers.Count() == 0 || userManager.VerifyUser(userManager.CurrentUserId, problem.ProblemUsers.Select(y => y.UserGroupId).ToList())) {
102              results.Add(problem);
103            }
104          }
105        }
106        return results.Select(x => Convert.ToDto(x)).ToArray();
107      }
108    }
109
110    public byte[] GetProblemData(long problemId) {
111      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
112
113      using (OKBDataContext okb = new OKBDataContext()) {
114        var result = okb.Problems.Where(x => x.Id == problemId).Select(x => x.BinaryData.Data.ToArray()).FirstOrDefault();
115
116        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
117          return result;
118        } else {
119          var problemUsers = okb.ProblemUsers.Where(x => x.ProblemId == problemId);
120          if (problemUsers.Count() == 0 || userManager.VerifyUser(userManager.CurrentUserId, problemUsers.Select(y => y.UserGroupId).ToList())) {
121            return result;
122          } else {
123            return null;
124          }
125        }
126      }
127    }
128
129    public IEnumerable<DataTransfer.Solution> GetSolutions(long problemId) {
130      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
131
132      using (OKBDataContext okb = new OKBDataContext()) {
133        var problem = okb.Problems.SingleOrDefault(x => x.Id == problemId);
134        if (problem == null) throw new FaultException<MissingProblem>(new MissingProblem(problemId));
135        // TODO: In case of multi-objective problems one has to check whether it contains single- or multi-objective problems
136        var result = problem.SingleObjectiveSolutions.Select(x => Convert.ToDto(x)).ToList();
137        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
138          return result;
139        } else {
140          var problemUsers = okb.ProblemUsers.Where(x => x.ProblemId == problemId).ToList();
141          if (problemUsers.Count == 0 || userManager.VerifyUser(userManager.CurrentUserId, problemUsers.Select(y => y.UserGroupId).ToList())) {
142            return result;
143          } else {
144            return null;
145          }
146        }
147      }
148    }
149
150    public byte[] GetSolutionData(long solutionId) {
151      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
152
153      using (OKBDataContext okb = new OKBDataContext()) {
154        var solution = okb.SingleObjectiveSolutions.SingleOrDefault(x => x.Id == solutionId);
155        if (solution == null) throw new FaultException<MissingSolution>(new MissingSolution(solutionId));
156
157        var result = solution.BinaryData.Data.ToArray();
158        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
159          return result;
160        } else {
161          var problemUsers = okb.ProblemUsers.Where(x => x.ProblemId == solution.ProblemId).ToList();
162          if (problemUsers.Count == 0 || userManager.VerifyUser(userManager.CurrentUserId, problemUsers.Select(y => y.UserGroupId).ToList())) {
163            return result;
164          } else {
165            return null;
166          }
167        }
168      }
169    }
170
171    public void AddRun(DataTransfer.Run run) {
172      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
173
174      using (OKBDataContext okb = new OKBDataContext()) {
175        DataAccess.Run entity = Convert.ToEntity(run, okb);
176        okb.Runs.InsertOnSubmit(entity);
177        okb.SubmitChanges();
178      }
179    }
180
181    public IEnumerable<DataTransfer.Value> GetCharacteristicValues(long problemId) {
182      using (OKBDataContext okb = new OKBDataContext()) {
183        var prob = okb.Problems.SingleOrDefault(x => x.Id == problemId);
184        if (prob == null) return Enumerable.Empty<DataTransfer.Value>();
185        return prob.CharacteristicValues.Select(Convert.ToDto).ToArray();
186      }
187    }
188
189    public void SetCharacteristicValue(long problemId, DataTransfer.Value value) {
190      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
191
192      using (OKBDataContext okb = new OKBDataContext()) {
193        var problem = okb.Problems.SingleOrDefault(x => x.Id == problemId);
194        if (problem == null) throw new FaultException<MissingProblem>(new MissingProblem(problemId));
195
196        DoSetCharacteristicValue(okb, problem, value);
197        okb.SubmitChanges();
198      }
199    }
200
201    public void SetCharacteristicValues(long problemId, DataTransfer.Value[] values) {
202      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
203
204      using (OKBDataContext okb = new OKBDataContext()) {
205        var problem = okb.Problems.SingleOrDefault(x => x.Id == problemId);
206        if (problem == null) throw new FaultException<MissingProblem>(new MissingProblem(problemId));
207
208        foreach (var v in values) {
209          DoSetCharacteristicValue(okb, problem, v);
210        }
211        okb.SubmitChanges();
212      }
213    }
214
215    private void DoSetCharacteristicValue(OKBDataContext okb, Problem problem, DataTransfer.Value value) {
216      CharacteristicType characteristicType;
217      try {
218        characteristicType = GetCharacteristicType(value);
219      } catch (ArgumentException ex) {
220        throw new FaultException<UnknownCharacteristicType>(new UnknownCharacteristicType(ex.Message));
221      }
222
223      var entity = problem.CharacteristicValues.SingleOrDefault(x => x.Characteristic.Name == value.Name && x.Characteristic.Type == characteristicType);
224      if (entity != null) {
225        // Update
226        switch (characteristicType) {
227          case CharacteristicType.Bool: entity.BoolValue = ((DataTransfer.BoolValue)value).Value; break;
228          case CharacteristicType.Int: entity.IntValue = ((DataTransfer.IntValue)value).Value; break;
229          case CharacteristicType.Long: entity.LongValue = ((DataTransfer.LongValue)value).Value; break;
230          case CharacteristicType.Float: entity.FloatValue = ((DataTransfer.FloatValue)value).Value; break;
231          case CharacteristicType.Double: entity.DoubleValue = ((DataTransfer.DoubleValue)value).Value; break;
232          case CharacteristicType.Percent: entity.DoubleValue = ((DataTransfer.PercentValue)value).Value; break;
233          case CharacteristicType.String: entity.StringValue = ((DataTransfer.StringValue)value).Value; break;
234          case CharacteristicType.TimeSpan: entity.LongValue = ((DataTransfer.TimeSpanValue)value).Value; break;
235        }
236      } else {
237        // Insert
238        entity = Convert.ToEntity(value, okb, problem, characteristicType);
239        okb.CharacteristicValues.InsertOnSubmit(entity);
240      }
241
242    }
243
244    private CharacteristicType GetCharacteristicType(DataTransfer.Value source) {
245      if (source is DataTransfer.BoolValue) {
246        return CharacteristicType.Bool;
247      } else if (source is DataTransfer.IntValue) {
248        return CharacteristicType.Int;
249      } else if (source is DataTransfer.TimeSpanValue) {
250        return CharacteristicType.TimeSpan;
251      } else if (source is DataTransfer.LongValue) {
252        return CharacteristicType.Long;
253      } else if (source is DataTransfer.FloatValue) {
254        return CharacteristicType.Float;
255      } else if (source is DataTransfer.DoubleValue) {
256        return CharacteristicType.Double;
257      } else if (source is DataTransfer.PercentValue) {
258        return CharacteristicType.Percent;
259      } else if (source is DataTransfer.StringValue) {
260        return CharacteristicType.String;
261      } else {
262        throw new ArgumentException("Unknown characteristic type.", "source");
263      }
264    }
265  }
266}
Note: See TracBrowser for help on using the repository browser.