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

#2560: added service methods

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.