Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13599 was 13513, checked in by abeham, 9 years ago

#2560: fixed problem of characteristic values getting overridden

File size: 9.6 KB
RevLine 
[8049]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8049]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
[13501]22using System;
[8049]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 void AddRun(DataTransfer.Run run) {
130      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
131
132      using (OKBDataContext okb = new OKBDataContext()) {
133        DataAccess.Run entity = Convert.ToEntity(run, okb);
134        okb.Runs.InsertOnSubmit(entity);
135        okb.SubmitChanges();
136      }
137    }
[13501]138
[13511]139    public IEnumerable<DataTransfer.Value> GetCharacteristicValues(long problemId) {
140      using (OKBDataContext okb = new OKBDataContext()) {
141        var prob = okb.Problems.SingleOrDefault(x => x.Id == problemId);
142        if (prob == null) return Enumerable.Empty<DataTransfer.Value>();
143        return prob.CharacteristicValues.Select(Convert.ToDto).ToArray();
144      }
145    }
[13501]146
[13511]147    public void SetCharacteristicValue(long problemId, DataTransfer.Value value) {
[13501]148      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
149
150      using (OKBDataContext okb = new OKBDataContext()) {
151        var problem = okb.Problems.SingleOrDefault(x => x.Id == problemId);
152        if (problem == null) throw new FaultException<MissingProblem>(new MissingProblem("Problem with id {0} cannot be found", problemId));
153
[13511]154        DoSetCharacteristicValue(okb, problem, value);
155        okb.SubmitChanges();
156      }
157    }
158
159    public void SetCharacteristicValues(long problemId, DataTransfer.Value[] values) {
160      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
161
162      using (OKBDataContext okb = new OKBDataContext()) {
163        var problem = okb.Problems.SingleOrDefault(x => x.Id == problemId);
164        if (problem == null) throw new FaultException<MissingProblem>(new MissingProblem("Problem with id {0} cannot be found", problemId));
165
166        foreach (var v in values) {
167          DoSetCharacteristicValue(okb, problem, v);
[13501]168        }
169        okb.SubmitChanges();
170      }
171    }
172
[13511]173    private void DoSetCharacteristicValue(OKBDataContext okb, Problem problem, DataTransfer.Value value) {
174      CharacteristicType characteristicType;
175      try {
176        characteristicType = GetCharacteristicType(value);
177      } catch (ArgumentException ex) {
178        throw new FaultException<UnknownCharacteristicType>(new UnknownCharacteristicType(ex.Message));
179      }
180
[13513]181      var entity = problem.CharacteristicValues.SingleOrDefault(x => x.Characteristic.Name == value.Name && x.Characteristic.Type == characteristicType);
[13511]182      if (entity != null) {
183        // Update
184        switch (characteristicType) {
185          case CharacteristicType.Bool: entity.BoolValue = ((DataTransfer.BoolValue)value).Value; break;
186          case CharacteristicType.Int: entity.IntValue = ((DataTransfer.IntValue)value).Value; break;
187          case CharacteristicType.Long: entity.LongValue = ((DataTransfer.LongValue)value).Value; break;
188          case CharacteristicType.Float: entity.FloatValue = ((DataTransfer.FloatValue)value).Value; break;
189          case CharacteristicType.Double: entity.DoubleValue = ((DataTransfer.DoubleValue)value).Value; break;
190          case CharacteristicType.Percent: entity.DoubleValue = ((DataTransfer.PercentValue)value).Value; break;
191          case CharacteristicType.String: entity.StringValue = ((DataTransfer.StringValue)value).Value; break;
192          case CharacteristicType.TimeSpan: entity.LongValue = ((DataTransfer.TimeSpanValue)value).Value; break;
193        }
194      } else {
195        // Insert
196        entity = Convert.ToEntity(value, okb, problem, characteristicType);
197        okb.CharacteristicValues.InsertOnSubmit(entity);
198      }
199
200    }
201
[13501]202    private CharacteristicType GetCharacteristicType(DataTransfer.Value source) {
203      if (source is DataTransfer.BoolValue) {
204        return CharacteristicType.Bool;
205      } else if (source is DataTransfer.IntValue) {
206        return CharacteristicType.Int;
207      } else if (source is DataTransfer.TimeSpanValue) {
208        return CharacteristicType.TimeSpan;
209      } else if (source is DataTransfer.LongValue) {
210        return CharacteristicType.Long;
211      } else if (source is DataTransfer.FloatValue) {
212        return CharacteristicType.Float;
213      } else if (source is DataTransfer.DoubleValue) {
214        return CharacteristicType.Double;
215      } else if (source is DataTransfer.PercentValue) {
216        return CharacteristicType.Percent;
217      } else if (source is DataTransfer.StringValue) {
218        return CharacteristicType.String;
219      } else {
220        throw new ArgumentException("Unknown characteristic type.", "source");
221      }
222    }
[8049]223  }
224}
Note: See TracBrowser for help on using the repository browser.