Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/08/16 14:40:02 (8 years ago)
Author:
gkronber
Message:

#2434: merged trunk changes r12934:14026 from trunk to branch

Location:
branches/crossvalidation-2434
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/crossvalidation-2434

  • branches/crossvalidation-2434/HeuristicLab.Services.OKB/3.3/RunCreation/RunCreationService.cs

    r12012 r14029  
    2020#endregion
    2121
     22using HeuristicLab.Services.Access;
     23using HeuristicLab.Services.OKB.DataAccess;
     24using System;
    2225using System.Collections.Generic;
    2326using System.Data.Linq;
    2427using System.Linq;
    2528using System.ServiceModel;
    26 using HeuristicLab.Services.Access;
    27 using HeuristicLab.Services.OKB.DataAccess;
    2829
    2930namespace HeuristicLab.Services.OKB.RunCreation {
     
    126127    }
    127128
     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 DataTransfer.Solution GetSolution(long solutionId) {
     151      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
     152
     153      using (OKBDataContext okb = new OKBDataContext()) {
     154        // TODO: In case of multi-objective problems one has to check whether it contains single- or multi-objective problems
     155        var result = Convert.ToDto(okb.SingleObjectiveSolutions.SingleOrDefault(x => x.Id == solutionId));
     156        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
     157          return result;
     158        } else {
     159          var problemUsers = okb.ProblemUsers.Where(x => x.ProblemId == result.ProblemId).ToList();
     160          if (problemUsers.Count == 0 || userManager.VerifyUser(userManager.CurrentUserId, problemUsers.Select(y => y.UserGroupId).ToList())) {
     161            return result;
     162          } else {
     163            return null;
     164          }
     165        }
     166      }
     167    }
     168
     169    public byte[] GetSolutionData(long solutionId) {
     170      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
     171
     172      using (OKBDataContext okb = new OKBDataContext()) {
     173        var solution = okb.SingleObjectiveSolutions.SingleOrDefault(x => x.Id == solutionId);
     174        if (solution == null) throw new FaultException<MissingSolution>(new MissingSolution(solutionId));
     175
     176        var result = solution.BinaryData.Data.ToArray();
     177        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
     178          return result;
     179        } else {
     180          var problemUsers = okb.ProblemUsers.Where(x => x.ProblemId == solution.ProblemId).ToList();
     181          if (problemUsers.Count == 0 || userManager.VerifyUser(userManager.CurrentUserId, problemUsers.Select(y => y.UserGroupId).ToList())) {
     182            return result;
     183          } else {
     184            return null;
     185          }
     186        }
     187      }
     188    }
     189
     190    public long AddSolution(DataTransfer.Solution solution, byte[] data) {
     191      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
     192
     193      using (OKBDataContext okb = new OKBDataContext()) {
     194        var soSolution = solution as DataTransfer.SingleObjectiveSolution;
     195        if (soSolution != null) {
     196          DataAccess.SingleObjectiveSolution entity = Convert.ToEntity(soSolution, data, okb);
     197          okb.SingleObjectiveSolutions.InsertOnSubmit(entity);
     198          okb.SubmitChanges();
     199          return entity.Id;
     200        }
     201      }
     202      throw new FaultException(new FaultReason("The solution could not be added."));
     203    }
     204
     205    public void DeleteSolution(DataTransfer.Solution solution) {
     206      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
     207
     208      using (OKBDataContext okb = new OKBDataContext()) {
     209        var soSolution = solution as DataTransfer.SingleObjectiveSolution;
     210        if (soSolution != null) {
     211          okb.SingleObjectiveSolutions.DeleteOnSubmit(okb.SingleObjectiveSolutions.Single(x => x.Id == soSolution.Id));
     212          okb.SubmitChanges();
     213        }
     214      }
     215    }
     216
    128217    public void AddRun(DataTransfer.Run run) {
    129218      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
     
    135224      }
    136225    }
     226
     227    public IEnumerable<DataTransfer.Value> GetCharacteristicValues(long problemId) {
     228      using (OKBDataContext okb = new OKBDataContext()) {
     229        var prob = okb.Problems.SingleOrDefault(x => x.Id == problemId);
     230        if (prob == null) return Enumerable.Empty<DataTransfer.Value>();
     231        return prob.CharacteristicValues.Select(Convert.ToDto).ToArray();
     232      }
     233    }
     234
     235    public void SetCharacteristicValue(long problemId, DataTransfer.Value value) {
     236      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
     237
     238      using (OKBDataContext okb = new OKBDataContext()) {
     239        var problem = okb.Problems.SingleOrDefault(x => x.Id == problemId);
     240        if (problem == null) throw new FaultException<MissingProblem>(new MissingProblem(problemId));
     241
     242        DoSetCharacteristicValue(okb, problem, value);
     243        okb.SubmitChanges();
     244      }
     245    }
     246
     247    public void SetCharacteristicValues(long problemId, DataTransfer.Value[] values) {
     248      roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);
     249
     250      using (OKBDataContext okb = new OKBDataContext()) {
     251        var problem = okb.Problems.SingleOrDefault(x => x.Id == problemId);
     252        if (problem == null) throw new FaultException<MissingProblem>(new MissingProblem(problemId));
     253
     254        foreach (var v in values) {
     255          DoSetCharacteristicValue(okb, problem, v);
     256        }
     257        okb.SubmitChanges();
     258      }
     259    }
     260
     261    private void DoSetCharacteristicValue(OKBDataContext okb, Problem problem, DataTransfer.Value value) {
     262      CharacteristicType characteristicType;
     263      try {
     264        characteristicType = GetCharacteristicType(value);
     265      } catch (ArgumentException ex) {
     266        throw new FaultException<UnknownCharacteristicType>(new UnknownCharacteristicType(ex.Message));
     267      }
     268
     269      var entity = problem.CharacteristicValues.SingleOrDefault(x => x.Characteristic.Name == value.Name && x.Characteristic.Type == characteristicType);
     270      if (entity != null) {
     271        // Update
     272        switch (characteristicType) {
     273          case CharacteristicType.Bool: entity.BoolValue = ((DataTransfer.BoolValue)value).Value; break;
     274          case CharacteristicType.Int: entity.IntValue = ((DataTransfer.IntValue)value).Value; break;
     275          case CharacteristicType.Long: entity.LongValue = ((DataTransfer.LongValue)value).Value; break;
     276          case CharacteristicType.Float: entity.FloatValue = ((DataTransfer.FloatValue)value).Value; break;
     277          case CharacteristicType.Double: entity.DoubleValue = ((DataTransfer.DoubleValue)value).Value; break;
     278          case CharacteristicType.Percent: entity.DoubleValue = ((DataTransfer.PercentValue)value).Value; break;
     279          case CharacteristicType.String: entity.StringValue = ((DataTransfer.StringValue)value).Value; break;
     280          case CharacteristicType.TimeSpan: entity.LongValue = ((DataTransfer.TimeSpanValue)value).Value; break;
     281        }
     282      } else {
     283        // Insert
     284        entity = Convert.ToEntity(value, okb, problem, characteristicType);
     285        okb.CharacteristicValues.InsertOnSubmit(entity);
     286      }
     287
     288    }
     289
     290    private CharacteristicType GetCharacteristicType(DataTransfer.Value source) {
     291      if (source is DataTransfer.BoolValue) {
     292        return CharacteristicType.Bool;
     293      } else if (source is DataTransfer.IntValue) {
     294        return CharacteristicType.Int;
     295      } else if (source is DataTransfer.TimeSpanValue) {
     296        return CharacteristicType.TimeSpan;
     297      } else if (source is DataTransfer.LongValue) {
     298        return CharacteristicType.Long;
     299      } else if (source is DataTransfer.FloatValue) {
     300        return CharacteristicType.Float;
     301      } else if (source is DataTransfer.DoubleValue) {
     302        return CharacteristicType.Double;
     303      } else if (source is DataTransfer.PercentValue) {
     304        return CharacteristicType.Percent;
     305      } else if (source is DataTransfer.StringValue) {
     306        return CharacteristicType.String;
     307      } else {
     308        throw new ArgumentException("Unknown characteristic type.", "source");
     309      }
     310    }
    137311  }
    138312}
Note: See TracChangeset for help on using the changeset viewer.