Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/12/16 16:08:38 (9 years ago)
Author:
abeham
Message:

#2560: added service methods

Location:
trunk/sources/HeuristicLab.Services.OKB/3.3/RunCreation
Files:
2 added
3 edited

Legend:

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

    r12012 r13501  
    6868        entity.Values.Add(Convert.ToEntity(value, entity, DA.ValueNameCategory.Result, okb, binCache));
    6969      return entity;
     70    }
     71
     72    public static DA.CharacteristicValue ToEntity(DT.Value source, DA.OKBDataContext okb, DA.Problem problem, string characteristicName, DA.CharacteristicType type) {
     73      if (okb == null || problem == null || string.IsNullOrEmpty(characteristicName) || source == null) throw new ArgumentNullException();
     74      var entity = new DA.CharacteristicValue();
     75      entity.Problem = problem;
     76      entity.DataType = Convert.ToEntity(source.DataType, okb);
     77      entity.Characteristic = Convert.ToEntity(characteristicName, type, okb);
     78      if (source is DT.BoolValue) {
     79        entity.BoolValue = ((DT.BoolValue)source).Value;
     80      } else if (source is DT.IntValue) {
     81        entity.IntValue = ((DT.IntValue)source).Value;
     82      } else if (source is DT.TimeSpanValue) {
     83        entity.LongValue = ((DT.TimeSpanValue)source).Value;
     84      } else if (source is DT.LongValue) {
     85        entity.LongValue = ((DT.LongValue)source).Value;
     86      } else if (source is DT.FloatValue) {
     87        entity.FloatValue = ((DT.FloatValue)source).Value;
     88      } else if (source is DT.DoubleValue) {
     89        entity.DoubleValue = ((DT.DoubleValue)source).Value;
     90      } else if (source is DT.PercentValue) {
     91        entity.DoubleValue = ((DT.PercentValue)source).Value;
     92      } else if (source is DT.StringValue) {
     93        entity.StringValue = ((DT.StringValue)source).Value;
     94      } else {
     95        throw new ArgumentException("Unknown characteristic type.", "source");
     96      }
     97      return entity;
     98    }
     99
     100    private static DA.Characteristic ToEntity(string name, DA.CharacteristicType type, DA.OKBDataContext okb) {
     101      if (string.IsNullOrEmpty(name)) return null;
     102      var entity = okb.Characteristics.FirstOrDefault(x => (x.Name == name) && (x.Type == type));
     103      return entity ?? new DA.Characteristic() { Id = 0, Name = name, Type = type };
    70104    }
    71105
  • trunk/sources/HeuristicLab.Services.OKB/3.3/RunCreation/IRunCreationService.cs

    r12012 r13501  
    4545    [OperationContract]
    4646    void AddRun(Run run);
     47
     48    [OperationContract]
     49    [FaultContract(typeof(MissingProblem))]
     50    [FaultContract(typeof(UnknownCharacteristicType))]
     51    void SetCharacteristicValue(long problemId, string characteristicName, Value value);
    4752  }
    4853}
  • trunk/sources/HeuristicLab.Services.OKB/3.3/RunCreation/RunCreationService.cs

    r12012 r13501  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Data.Linq;
     
    135136      }
    136137    }
     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    }
    137196  }
    138197}
Note: See TracChangeset for help on using the changeset viewer.