public static DA.CharacteristicValue ToEntity(DT.Value source, DA.OKBDataContext okb, DA.Problem problem, DA.CharacteristicType type)
        {
            if (okb == null || problem == null || source == null || string.IsNullOrEmpty(source.Name))
            {
                throw new ArgumentNullException();
            }
            var entity = new DA.CharacteristicValue();

            entity.Problem        = problem;
            entity.DataType       = Convert.ToEntity(source.DataType, okb);
            entity.Characteristic = Convert.ToEntity(source.Name, type, okb);
            if (source is DT.BoolValue)
            {
                entity.BoolValue = ((DT.BoolValue)source).Value;
            }
            else if (source is DT.IntValue)
            {
                entity.IntValue = ((DT.IntValue)source).Value;
            }
            else if (source is DT.TimeSpanValue)
            {
                entity.LongValue = ((DT.TimeSpanValue)source).Value;
            }
            else if (source is DT.LongValue)
            {
                entity.LongValue = ((DT.LongValue)source).Value;
            }
            else if (source is DT.FloatValue)
            {
                entity.FloatValue = ((DT.FloatValue)source).Value;
            }
            else if (source is DT.DoubleValue)
            {
                entity.DoubleValue = ((DT.DoubleValue)source).Value;
            }
            else if (source is DT.PercentValue)
            {
                entity.DoubleValue = ((DT.PercentValue)source).Value;
            }
            else if (source is DT.StringValue)
            {
                entity.StringValue = ((DT.StringValue)source).Value;
            }
            else
            {
                throw new ArgumentException("Unknown characteristic type.", "source");
            }
            return(entity);
        }
        public void SetCharacteristicValue(long problemId, DataTransfer.Value value)
        {
            roleVerifier.AuthenticateForAnyRole(OKBRoles.OKBAdministrator, OKBRoles.OKBUser);

            using (OKBDataContext okb = new OKBDataContext()) {
                var problem = okb.Problems.SingleOrDefault(x => x.Id == problemId);
                if (problem == null)
                {
                    throw new FaultException <MissingProblem>(new MissingProblem(problemId));
                }

                DoSetCharacteristicValue(okb, problem, value);
                okb.SubmitChanges();
            }
        }
        private void DoSetCharacteristicValue(OKBDataContext okb, Problem problem, DataTransfer.Value value)
        {
            CharacteristicType characteristicType;

            try {
                characteristicType = GetCharacteristicType(value);
            } catch (ArgumentException ex) {
                throw new FaultException <UnknownCharacteristicType>(new UnknownCharacteristicType(ex.Message));
            }

            var entity = problem.CharacteristicValues.SingleOrDefault(x => x.Characteristic.Name == value.Name && x.Characteristic.Type == characteristicType);

            if (entity != null)
            {
                // Update
                switch (characteristicType)
                {
                case CharacteristicType.Bool: entity.BoolValue = ((DataTransfer.BoolValue)value).Value; break;

                case CharacteristicType.Int: entity.IntValue = ((DataTransfer.IntValue)value).Value; break;

                case CharacteristicType.Long: entity.LongValue = ((DataTransfer.LongValue)value).Value; break;

                case CharacteristicType.Float: entity.FloatValue = ((DataTransfer.FloatValue)value).Value; break;

                case CharacteristicType.Double: entity.DoubleValue = ((DataTransfer.DoubleValue)value).Value; break;

                case CharacteristicType.Percent: entity.DoubleValue = ((DataTransfer.PercentValue)value).Value; break;

                case CharacteristicType.String: entity.StringValue = ((DataTransfer.StringValue)value).Value; break;

                case CharacteristicType.TimeSpan: entity.LongValue = ((DataTransfer.TimeSpanValue)value).Value; break;
                }
            }
            else
            {
                // Insert
                entity = Convert.ToEntity(value, okb, problem, characteristicType);
                okb.CharacteristicValues.InsertOnSubmit(entity);
            }
        }
 private CharacteristicType GetCharacteristicType(DataTransfer.Value source)
 {
     if (source is DataTransfer.BoolValue)
     {
         return(CharacteristicType.Bool);
     }
     else if (source is DataTransfer.IntValue)
     {
         return(CharacteristicType.Int);
     }
     else if (source is DataTransfer.TimeSpanValue)
     {
         return(CharacteristicType.TimeSpan);
     }
     else if (source is DataTransfer.LongValue)
     {
         return(CharacteristicType.Long);
     }
     else if (source is DataTransfer.FloatValue)
     {
         return(CharacteristicType.Float);
     }
     else if (source is DataTransfer.DoubleValue)
     {
         return(CharacteristicType.Double);
     }
     else if (source is DataTransfer.PercentValue)
     {
         return(CharacteristicType.Percent);
     }
     else if (source is DataTransfer.StringValue)
     {
         return(CharacteristicType.String);
     }
     else
     {
         throw new ArgumentException("Unknown characteristic type.", "source");
     }
 }
        private static DA.Value ToEntity(DT.Value source, DA.Run run, DA.ValueNameCategory category, DA.OKBDataContext okb, List <DA.BinaryData> binCache)
        {
            if (source == null)
            {
                return(null);
            }
            var entity = new DA.Value();

            entity.Run      = run;
            entity.DataType = Convert.ToEntity(source.DataType, okb);
            if (source is DT.BoolValue)
            {
                entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Bool, okb);
                entity.BoolValue = ((DT.BoolValue)source).Value;
            }
            else if (source is DT.IntValue)
            {
                entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Int, okb);
                entity.IntValue  = ((DT.IntValue)source).Value;
            }
            else if (source is DT.TimeSpanValue)
            {
                entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.TimeSpan, okb);
                entity.LongValue = ((DT.TimeSpanValue)source).Value;
            }
            else if (source is DT.LongValue)
            {
                entity.ValueName = Convert.ToEntity(source.Name, category, DA.ValueNameType.Long, okb);
                entity.LongValue = ((DT.LongValue)source).Value;
            }
            else if (source is DT.FloatValue)
            {
                entity.ValueName  = Convert.ToEntity(source.Name, category, DA.ValueNameType.Float, okb);
                entity.FloatValue = ((DT.FloatValue)source).Value;
            }
            else if (source is DT.DoubleValue)
            {
                entity.ValueName   = Convert.ToEntity(source.Name, category, DA.ValueNameType.Double, okb);
                entity.DoubleValue = ((DT.DoubleValue)source).Value;
            }
            else if (source is DT.PercentValue)
            {
                entity.ValueName   = Convert.ToEntity(source.Name, category, DA.ValueNameType.Percent, okb);
                entity.DoubleValue = ((DT.PercentValue)source).Value;
            }
            else if (source is DT.StringValue)
            {
                entity.ValueName   = Convert.ToEntity(source.Name, category, DA.ValueNameType.String, okb);
                entity.StringValue = ((DT.StringValue)source).Value;
            }
            else if (source is DT.BinaryValue)
            {
                entity.ValueName  = Convert.ToEntity(source.Name, category, DA.ValueNameType.Binary, okb);
                entity.BinaryData = Convert.ToEntity(((DT.BinaryValue)source).Value, okb, binCache);
            }
            else
            {
                throw new ArgumentException("Unknown value type.", "source");
            }
            return(entity);
        }