Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/13/10 01:21:32 (14 years ago)
Author:
swagner
Message:

Worked on OKB data model and services (#1174).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Services.OKB/3.3/AdminService.cs

    r4279 r4381  
    2424using System.ServiceModel;
    2525using HeuristicLab.Services.OKB.DataAccess;
    26 using log4net;
    2726
    2827namespace HeuristicLab.Services.OKB {
    29 
    3028  /// <summary>
    3129  /// Implementation of <see cref="IAdminService"/>
    3230  /// </summary>
    33   [ServiceBehavior(
    34     InstanceContextMode = InstanceContextMode.PerSession,
    35     IncludeExceptionDetailInFaults = true)]
     31  [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    3632  public class AdminService : IAdminService {
    37 
    38     private static ILog logger = LogManager.GetLogger(typeof(AdminService));
    39 
    40     private OKBDataContext GetDataContext() {
    41       return new OKBDataContext();
    42     }
    43 
    4433    /// <summary>
    4534    /// Gets all available platforms.
    4635    /// </summary>
    4736    /// <returns>A list of <see cref="Platform"/>s.</returns>
    48     public Platform[] GetPlatformNames() {
    49       OKBDataContext okb = GetDataContext();
    50       return okb.Platforms.ToArray();
     37    public Platform[] GetPlatforms() {
     38      using (OKBDataContext okb = new OKBDataContext()) {
     39        return okb.Platforms.ToArray();
     40      }
    5141    }
    5242
     
    5444    /// Gets the complete algorithm object graph up to the following entities:
    5545    /// <list type="bullet">
    56     ///     <item>Parameter</item>
    57     ///     <item>Algorithm_Paramters.Parameter.DataType</item>
    58     ///     <item>Algorithm_Results.Result.DataType</item>
    59     ///   </list>
     46    ///   <item>Parameter</item>
     47    ///   <item>Algorithm_Paramters.Parameter.DataType</item>
     48    ///   <item>Algorithm_Results.Result.DataType</item>
     49    /// </list>
    6050    /// </summary>
    6151    /// <param name="id">The algorithm id.</param>
    6252    /// <returns>An <see cref="Algorithm"/></returns>
    6353    public Algorithm GetCompleteAlgorithm(int id) {
    64       OKBDataContext okb = GetDataContext();
    65       var dlo = new DataLoadOptions();
    66       dlo.LoadWith<Algorithm>(a => a.Platform);
    67       dlo.LoadWith<Algorithm>(a => a.Algorithm_Parameters);
    68       dlo.LoadWith<Algorithm>(a => a.Algorithm_Results);
    69       dlo.LoadWith<Algorithm_Parameter>(ap => ap.Parameter);
    70       dlo.LoadWith<Algorithm_Result>(ar => ar.Result);
    71       dlo.LoadWith<Parameter>(p => p.DataType);
    72       dlo.LoadWith<Result>(r => r.DataType);
    73       okb.LoadOptions = dlo;
    74       return okb.Algorithms.Single(a => a.Id == id);
     54      using (OKBDataContext okb = new OKBDataContext()) {
     55        var dlo = new DataLoadOptions();
     56        dlo.LoadWith<Algorithm>(a => a.AlgorithmClass);
     57        dlo.LoadWith<Algorithm>(a => a.Platform);
     58        dlo.LoadWith<Algorithm>(a => a.AlgorithmUsers);
     59        dlo.LoadWith<AlgorithmUser>(u => u.User);
     60        okb.LoadOptions = dlo;
     61        return okb.Algorithms.Single(a => a.Id == id);
     62      }
    7563    }
    7664
     
    7866    /// Gets the complete problem object graph up to the following entities:
    7967    /// <list type="bullet">
    80     ///     <item>Platform</item>
    81     ///     <item>SolutionRepresentation</item>
    82     ///     <item>Problem_Parameters.Parameter</item>
    83     ///     <item>IntProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
    84     ///     <item>FloatProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
    85     ///     <item>CharProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
    86     ///   </list>
     68    ///   <item>Platform</item>
     69    ///   <item>SolutionRepresentation</item>
     70    ///   <item>Problem_Parameters.Parameter</item>
     71    ///   <item>IntProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
     72    ///   <item>FloatProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
     73    ///   <item>CharProblemCharacteristicValues.ProblemCharacteristic.DataType</item>
     74    /// </list>
    8775    /// </summary>
    8876    /// <param name="id">The problem id.</param>
    8977    /// <returns>A <see cref="Problem"/></returns>
    9078    public Problem GetCompleteProblem(int id) {
    91       OKBDataContext okb = GetDataContext();
    92       var dlo = new DataLoadOptions();
    93       dlo.LoadWith<Problem>(p => p.Platform);
    94       dlo.LoadWith<Problem>(p => p.SolutionRepresentation);
    95       dlo.LoadWith<Problem>(p => p.Problem_Parameters);
    96       dlo.LoadWith<Problem>(p => p.IntProblemCharacteristicValues);
    97       dlo.LoadWith<Problem>(p => p.FloatProblemCharacteristicValues);
    98       dlo.LoadWith<Problem>(p => p.CharProblemCharacteristicValues);
    99       dlo.LoadWith<Problem_Parameter>(ap => ap.Parameter);
    100       dlo.LoadWith<IntProblemCharacteristicValue>(ipcv => ipcv.ProblemCharacteristic);
    101       dlo.LoadWith<FloatProblemCharacteristicValue>(fpcv => fpcv.ProblemCharacteristic);
    102       dlo.LoadWith<CharProblemCharacteristicValue>(cpcv => cpcv.ProblemCharacteristic);
    103       dlo.LoadWith<Parameter>(p => p.DataType);
    104       dlo.LoadWith<ProblemCharacteristic>(pc => pc.DataType);
    105       okb.LoadOptions = dlo;
    106       return okb.Problems.Single(p => p.Id == id);
     79      using (OKBDataContext okb = new OKBDataContext()) {
     80        var dlo = new DataLoadOptions();
     81        dlo.LoadWith<Problem>(p => p.ProblemClass);
     82        dlo.LoadWith<Problem>(p => p.SolutionRepresentation);
     83        dlo.LoadWith<Problem>(p => p.Platform);
     84        dlo.LoadWith<Problem>(p => p.ProblemUsers);
     85        dlo.LoadWith<ProblemUser>(u => u.User);
     86        dlo.LoadWith<Problem>(p => p.ProblemCharacteristicIntValues);
     87        dlo.LoadWith<Problem>(p => p.ProblemCharacteristicFloatValues);
     88        dlo.LoadWith<Problem>(p => p.ProblemCharacteristicStringValues);
     89        dlo.LoadWith<ProblemCharacteristicIntValue>(ipcv => ipcv.ProblemCharacteristic);
     90        dlo.LoadWith<ProblemCharacteristicFloatValue>(fpcv => fpcv.ProblemCharacteristic);
     91        dlo.LoadWith<ProblemCharacteristicStringValue>(cpcv => cpcv.ProblemCharacteristic);
     92        dlo.LoadWith<ProblemCharacteristic>(pc => pc.DataType);
     93        okb.LoadOptions = dlo;
     94        return okb.Problems.Single(p => p.Id == id);
     95      }
    10796    }
    10897
     
    11099    /// Updates the algorithm object graph including the following properties and linked entitites:
    111100    /// <list type="bullet">
    112     ///     <item>Name</item>
    113     ///     <item>Description</item>
    114     ///     <item>AlgorithmClassId</item>
    115     ///     <item>PlatformId</item>
    116     ///     <item>Algorithm_Parameters</item>
    117     ///     <item>Algorithm_Results</item>
    118     ///   </list>
    119     ///   <remarks>
     101    ///   <item>Name</item>
     102    ///   <item>Description</item>
     103    ///   <item>AlgorithmClassId</item>
     104    ///   <item>PlatformId</item>
     105    ///   <item>Algorithm_Parameters</item>
     106    ///   <item>Algorithm_Results</item>
     107    /// </list>
     108    /// <remarks>
    120109    /// New <see cref="Parameter"/>s or <see cref="Result"/>s will not be
    121110    /// created but have to be pre-existing.
     
    124113    /// <param name="algorithm">The algorithm.</param>
    125114    public void UpdateCompleteAlgorithm(Algorithm algorithm) {
    126       OKBDataContext okb = GetDataContext();
    127       Algorithm original = okb.Algorithms.Single(a => a.Id == algorithm.Id);
     115      using (OKBDataContext okb = new OKBDataContext()) {
     116        Algorithm original = okb.Algorithms.Single(a => a.Id == algorithm.Id);
     117        UpdateAlgorithmData(algorithm, original, okb);
     118        okb.SubmitChanges();
     119      }
     120    }
     121
     122    /// <summary>
     123    /// Updates the problem object graph including the following properties and linked entities:
     124    /// <list type="bullet">
     125    ///   <item>Name</item>
     126    ///   <item>Description</item>
     127    ///   <item>ProblemClassId</item>
     128    ///   <item>PlatformId</item>
     129    ///   <item>SolutionRepresentationId</item>
     130    ///   <item>IntProblemCharacteristicValues.Value</item>
     131    ///   <item>FloatProblemCharacteristicValues.Value</item>
     132    ///   <item>CharProblemCharacteristicValues.Value</item>
     133    ///   <item>Problem_Parameters</item>
     134    /// </list>
     135    /// <remarks>
     136    /// New <see cref="ProblemCharacteristic"/>s or <see cref="Parameter"/>s will
     137    /// not be created but have to be pre-existing.
     138    /// </remarks>
     139    /// </summary>
     140    /// <param name="problem">The problem.</param>
     141    public void UpdateCompleteProblem(Problem problem) {
     142      using (OKBDataContext okb = new OKBDataContext()) {
     143        Problem originalProblem = okb.Problems.Single(p => p.Id == problem.Id);
     144        UpdateProblemData(problem, originalProblem, okb);
     145        UpdateProblemCharacteristics(problem, originalProblem, okb);
     146        okb.SubmitChanges();
     147      }
     148    }
     149
     150    private static void UpdateAlgorithmData(Algorithm algorithm, Algorithm original, OKBDataContext okb) {
    128151      original.Name = algorithm.Name;
    129152      original.Description = algorithm.Description;
    130153      original.AlgorithmClassId = algorithm.AlgorithmClassId;
    131154      original.PlatformId = algorithm.PlatformId;
    132       okb.Algorithm_Parameters.DeleteAllOnSubmit(original.Algorithm_Parameters);
    133       foreach (var a_p in algorithm.Algorithm_Parameters)
    134         original.Algorithm_Parameters.Add(new Algorithm_Parameter() {
     155      okb.AlgorithmUsers.DeleteAllOnSubmit(original.AlgorithmUsers);
     156      original.AlgorithmUsers.Clear();
     157      foreach (var u in algorithm.AlgorithmUsers) {
     158        original.AlgorithmUsers.Add(new AlgorithmUser() {
    135159          AlgorithmId = original.Id,
    136           ParameterId = a_p.ParameterId,
    137         });
    138       okb.Algorithm_Results.DeleteAllOnSubmit(original.Algorithm_Results);
    139       foreach (var a_r in algorithm.Algorithm_Results)
    140         original.Algorithm_Results.Add(new Algorithm_Result() {
    141           AlgorithmId = original.Id,
    142           ResultId = a_r.ResultId,
    143         });
    144       logger.Info("Updating algorithm implementation for " + original.Name);
    145       okb.SubmitChanges();
    146     }
    147 
    148     /// <summary>
    149     /// Updates the problem object graph including the following properties and linked entities:
    150     /// <list type="bullet">
    151     ///     <item>Name</item>
    152     ///     <item>Description</item>
    153     ///     <item>ProblemClassId</item>
    154     ///     <item>PlatformId</item>
    155     ///     <item>SolutionRepresentationId</item>
    156     ///     <item>IntProblemCharacteristicValues.Value</item>
    157     ///     <item>FloatProblemCharacteristicValues.Value</item>
    158     ///     <item>CharProblemCharacteristicValues.Value</item>
    159     ///     <item>Problem_Parameters</item>
    160     ///   </list>
    161     ///   <remarks>
    162     /// New <see cref="ProblemCharacteristic"/>s or <see cref="Parameter"/>s will
    163     /// not be created but have to be pre-existing.
    164     /// </remarks>
    165     /// </summary>
    166     /// <param name="problem">The problem.</param>
    167     public void UpdateCompleteProblem(Problem problem) {
    168       OKBDataContext okb = GetDataContext();
    169       Problem originalProblem = okb.Problems.Single(p => p.Id == problem.Id);
    170       UpdateProblemMasterInfo(problem, originalProblem);
    171       UpdateProblemCharacteristics(problem, okb, originalProblem);
    172       UpdateProblemParameters(problem, okb, originalProblem);
    173       logger.Info("Updating problem " + originalProblem.Name);
    174       okb.SubmitChanges();
    175     }
    176 
    177     private static void UpdateProblemParameters(Problem problem, OKBDataContext okb, Problem originalProblem) {
    178       okb.Problem_Parameters.DeleteAllOnSubmit(originalProblem.Problem_Parameters);
    179       originalProblem.Problem_Parameters.Clear();
    180       foreach (var p_p in problem.Problem_Parameters) {
    181         originalProblem.Problem_Parameters.Add(new Problem_Parameter() {
    182           ProblemId = originalProblem.Id,
    183           ParameterId = p_p.ParameterId,
    184         });
    185       }
    186     }
    187 
    188     private static void UpdateProblemCharacteristics(Problem problem, OKBDataContext okb, Problem originalProblem) {
    189       okb.IntProblemCharacteristicValues.DeleteAllOnSubmit(originalProblem.IntProblemCharacteristicValues);
    190       okb.FloatProblemCharacteristicValues.DeleteAllOnSubmit(originalProblem.FloatProblemCharacteristicValues);
    191       okb.CharProblemCharacteristicValues.DeleteAllOnSubmit(originalProblem.CharProblemCharacteristicValues);
    192       originalProblem.IntProblemCharacteristicValues.Clear();
    193       originalProblem.FloatProblemCharacteristicValues.Clear();
    194       originalProblem.CharProblemCharacteristicValues.Clear();
    195       foreach (var ipc in problem.IntProblemCharacteristicValues) {
    196         originalProblem.IntProblemCharacteristicValues.Add(new IntProblemCharacteristicValue() {
    197           ProblemId = originalProblem.Id,
     160          UserId = u.UserId
     161        });
     162      }
     163    }
     164
     165    private static void UpdateProblemData(Problem problem, Problem original, OKBDataContext okb) {
     166      original.Name = problem.Name;
     167      original.Description = problem.Description;
     168      original.ProblemClassId = problem.ProblemClassId;
     169      original.SolutionRepresentationId = problem.SolutionRepresentationId;
     170      original.PlatformId = problem.PlatformId;
     171      okb.ProblemUsers.DeleteAllOnSubmit(original.ProblemUsers);
     172      original.ProblemUsers.Clear();
     173      foreach (var u in problem.ProblemUsers) {
     174        original.ProblemUsers.Add(new ProblemUser() {
     175          ProblemId = original.Id,
     176          UserId = u.UserId
     177        });
     178      }
     179    }
     180    private static void UpdateProblemCharacteristics(Problem problem, Problem original, OKBDataContext okb) {
     181      okb.ProblemCharacteristicIntValues.DeleteAllOnSubmit(original.ProblemCharacteristicIntValues);
     182      okb.ProblemCharacteristicFloatValues.DeleteAllOnSubmit(original.ProblemCharacteristicFloatValues);
     183      okb.ProblemCharacteristicStringValues.DeleteAllOnSubmit(original.ProblemCharacteristicStringValues);
     184      original.ProblemCharacteristicIntValues.Clear();
     185      original.ProblemCharacteristicFloatValues.Clear();
     186      original.ProblemCharacteristicStringValues.Clear();
     187      foreach (var ipc in problem.ProblemCharacteristicIntValues) {
     188        original.ProblemCharacteristicIntValues.Add(new ProblemCharacteristicIntValue() {
     189          ProblemId = original.Id,
    198190          ProblemCharacteristicId = ipc.ProblemCharacteristicId,
    199191          Value = ipc.Value,
    200192        });
    201193      }
    202       foreach (var fpc in problem.FloatProblemCharacteristicValues) {
    203         originalProblem.FloatProblemCharacteristicValues.Add(new FloatProblemCharacteristicValue() {
    204           ProblemId = originalProblem.Id,
     194      foreach (var fpc in problem.ProblemCharacteristicFloatValues) {
     195        original.ProblemCharacteristicFloatValues.Add(new ProblemCharacteristicFloatValue() {
     196          ProblemId = original.Id,
    205197          ProblemCharacteristicId = fpc.ProblemCharacteristicId,
    206198          Value = fpc.Value,
    207199        });
    208200      }
    209       foreach (var cpc in problem.CharProblemCharacteristicValues) {
    210         originalProblem.CharProblemCharacteristicValues.Add(new CharProblemCharacteristicValue() {
    211           ProblemId = originalProblem.Id,
     201      foreach (var cpc in problem.ProblemCharacteristicStringValues) {
     202        original.ProblemCharacteristicStringValues.Add(new ProblemCharacteristicStringValue() {
     203          ProblemId = original.Id,
    212204          ProblemCharacteristicId = cpc.ProblemCharacteristicId,
    213205          Value = cpc.Value,
     
    215207      }
    216208    }
    217 
    218     private static void UpdateProblemMasterInfo(Problem problem, Problem originalProblem) {
    219       originalProblem.Name = problem.Name;
    220       originalProblem.Description = problem.Description;
    221       originalProblem.ProblemClassId = problem.ProblemClassId;
    222       originalProblem.PlatformId = problem.PlatformId;
    223       originalProblem.SolutionRepresentationId = problem.SolutionRepresentationId;
    224     }
    225209  }
    226210}
Note: See TracChangeset for help on using the changeset viewer.