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

Last change on this file since 13501 was 13501, checked in by abeham, 5 years ago

#2560: added service methods

File size: 8.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 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    }
138
139
140    public void SetCharacteristicValue(long problemId, string characteristicName, DataTransfer.Value value) {
141      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
142
143      using (OKBDataContext okb = new OKBDataContext()) {
144        var problem = okb.Problems.SingleOrDefault(x => x.Id == problemId);
145        if (problem == null) throw new FaultException<MissingProblem>(new MissingProblem("Problem with id {0} cannot be found", problemId));
146        CharacteristicType characteristicType;
147        try {
148          characteristicType = GetCharacteristicType(value);
149        } catch (ArgumentException ex) {
150          throw new FaultException<UnknownCharacteristicType>(new UnknownCharacteristicType(ex.Message));
151        }
152
153        var entity = okb.CharacteristicValues.SingleOrDefault(x => x.Characteristic.Name == characteristicName && x.Characteristic.Type == characteristicType);
154        if (entity != null) {
155          // Update
156          switch (characteristicType) {
157            case CharacteristicType.Bool: entity.BoolValue = ((DataTransfer.BoolValue)value).Value; break;
158            case CharacteristicType.Int: entity.IntValue = ((DataTransfer.IntValue)value).Value; break;
159            case CharacteristicType.Long: entity.LongValue = ((DataTransfer.LongValue)value).Value; break;
160            case CharacteristicType.Float: entity.FloatValue = ((DataTransfer.FloatValue)value).Value; break;
161            case CharacteristicType.Double: entity.DoubleValue = ((DataTransfer.DoubleValue)value).Value; break;
162            case CharacteristicType.Percent: entity.DoubleValue = ((DataTransfer.PercentValue)value).Value; break;
163            case CharacteristicType.String: entity.StringValue = ((DataTransfer.StringValue)value).Value; break;
164            case CharacteristicType.TimeSpan: entity.LongValue = ((DataTransfer.TimeSpanValue)value).Value; break;
165          }
166        } else {
167          // Insert
168          entity = Convert.ToEntity(value, okb, problem, characteristicName, characteristicType);
169          okb.CharacteristicValues.InsertOnSubmit(entity);
170        }
171        okb.SubmitChanges();
172      }
173    }
174
175    private CharacteristicType GetCharacteristicType(DataTransfer.Value source) {
176      if (source is DataTransfer.BoolValue) {
177        return CharacteristicType.Bool;
178      } else if (source is DataTransfer.IntValue) {
179        return CharacteristicType.Int;
180      } else if (source is DataTransfer.TimeSpanValue) {
181        return CharacteristicType.TimeSpan;
182      } else if (source is DataTransfer.LongValue) {
183        return CharacteristicType.Long;
184      } else if (source is DataTransfer.FloatValue) {
185        return CharacteristicType.Float;
186      } else if (source is DataTransfer.DoubleValue) {
187        return CharacteristicType.Double;
188      } else if (source is DataTransfer.PercentValue) {
189        return CharacteristicType.Percent;
190      } else if (source is DataTransfer.StringValue) {
191        return CharacteristicType.String;
192      } else {
193        throw new ArgumentException("Unknown characteristic type.", "source");
194      }
195    }
196  }
197}
Note: See TracBrowser for help on using the repository browser.