Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HLScript/HeuristicLab.Services.OKB/3.3/RunCreation/RunCreationService.cs @ 11290

Last change on this file since 11290 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Collections.Generic;
23using System.Data.Linq;
24using System.Linq;
25using System.ServiceModel;
26using HeuristicLab.Services.Access;
27using HeuristicLab.Services.OKB.DataAccess;
28
29namespace HeuristicLab.Services.OKB.RunCreation {
30  /// <summary>
31  /// Implementation of the OKB run creation service (interface <see cref="IRunCreationService"/>).
32  /// </summary>
33  [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
34  public class RunCreationService : IRunCreationService {
35    IRoleVerifier roleVerifier = AccessServiceLocator.Instance.RoleVerifier;
36    IUserManager userManager = AccessServiceLocator.Instance.UserManager;
37
38    public IEnumerable<DataTransfer.Algorithm> GetAlgorithms(string platformName) {
39      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
40
41      using (OKBDataContext okb = new OKBDataContext()) {
42        DataLoadOptions dlo = new DataLoadOptions();
43        dlo.LoadWith<Algorithm>(x => x.AlgorithmClass);
44        dlo.LoadWith<Algorithm>(x => x.DataType);
45        dlo.LoadWith<Algorithm>(x => x.AlgorithmUsers);
46        okb.LoadOptions = dlo;
47
48        var query = okb.Algorithms.Where(x => x.Platform.Name == platformName);
49        List<Algorithm> results = new List<Algorithm>();
50
51        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
52          results.AddRange(query);
53        } else {
54          foreach (var alg in query) {
55            if (alg.AlgorithmUsers.Count() == 0 || userManager.VerifyUser(userManager.CurrentUserId, alg.AlgorithmUsers.Select(y => y.UserGroupId).ToList())) {
56              results.Add(alg);
57            }
58          }
59        }
60        return results.Select(x => Convert.ToDto(x)).ToArray();
61      }
62    }
63
64    public byte[] GetAlgorithmData(long algorithmId) {
65      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
66
67      using (OKBDataContext okb = new OKBDataContext()) {
68        var result = okb.Algorithms.Where(x => x.Id == algorithmId).Select(x => x.BinaryData.Data.ToArray()).FirstOrDefault();
69
70        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
71          return result;
72        } else {
73          var algUsers = okb.AlgorithmUsers.Where(x => x.AlgorithmId == algorithmId);
74          if (algUsers.Count() == 0 || userManager.VerifyUser(userManager.CurrentUserId, algUsers.Select(y => y.UserGroupId).ToList())) {
75            return result;
76          } else {
77            return null;
78          }
79        }
80      }
81    }
82
83    public IEnumerable<DataTransfer.Problem> GetProblems(string platformName) {
84      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
85
86      using (OKBDataContext okb = new OKBDataContext()) {
87        DataLoadOptions dlo = new DataLoadOptions();
88        dlo.LoadWith<Problem>(x => x.ProblemClass);
89        dlo.LoadWith<Problem>(x => x.DataType);
90        dlo.LoadWith<Problem>(x => x.ProblemUsers);
91        okb.LoadOptions = dlo;
92
93        var query = okb.Problems.Where(x => x.Platform.Name == platformName);
94        List<Problem> results = new List<Problem>();
95
96        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
97          results.AddRange(query);
98        } else {
99          foreach (var problem in query) {
100            if (problem.ProblemUsers.Count() == 0 || userManager.VerifyUser(userManager.CurrentUserId, problem.ProblemUsers.Select(y => y.UserGroupId).ToList())) {
101              results.Add(problem);
102            }
103          }
104        }
105        return results.Select(x => Convert.ToDto(x)).ToArray();
106      }
107    }
108
109    public byte[] GetProblemData(long problemId) {
110      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
111
112      using (OKBDataContext okb = new OKBDataContext()) {
113        var result = okb.Problems.Where(x => x.Id == problemId).Select(x => x.BinaryData.Data.ToArray()).FirstOrDefault();
114
115        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
116          return result;
117        } else {
118          var problemUsers = okb.ProblemUsers.Where(x => x.ProblemId == problemId);
119          if (problemUsers.Count() == 0 || userManager.VerifyUser(userManager.CurrentUserId, problemUsers.Select(y => y.UserGroupId).ToList())) {
120            return result;
121          } else {
122            return null;
123          }
124        }
125      }
126    }
127
128    public void AddRun(DataTransfer.Run run) {
129      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
130
131      using (OKBDataContext okb = new OKBDataContext()) {
132        DataAccess.Run entity = Convert.ToEntity(run, okb);
133        okb.Runs.InsertOnSubmit(entity);
134        okb.SubmitChanges();
135      }
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.