Beispiel #1
0
        /// <summary>
        /// The create.
        /// </summary>
        /// <returns>
        /// The <see cref="ResultFieldOptions"/>.
        /// </returns>
        public static SampleFieldOptions Create(PropertyInfo property, IEditableRoot model)
        {
            if (property == null)
            {
                throw new ArgumentNullException("property");
            }

            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            var result = new SampleFieldOptions();

            var settingsString = model.GetValueByPropertyName(string.Format(CultureInfo.InvariantCulture, "{0}{1}", property.Name, Constants.SampleSettingPostfix));
            if (!string.IsNullOrEmpty(settingsString))
            {
                var sampleSettings = XElement.Parse(settingsString);
                SetProperties(result, sampleSettings, model);
            }

            result.IsNewItem = model.Id == 0;

            return result;
        }
Beispiel #2
0
        private static void SetProperties(SampleFieldOptions options, XElement settings, IEditableRoot model)
        {
            if (settings == null || !settings.HasAttributes)
            {
                return;
            }

            var lowerSpecLimitField = settings.Attribute("LowerSpecFieldName").Value;
            var upperSpecLimitField = settings.Attribute("UpperSpecFieldName").Value;
            options.SampleTypeFieldName = settings.Attribute("SampleTypeFieldName").Value;
            var sampleSizeTypeStr = settings.Attribute("SampleSizeType").Value;
            var sampleSizeFieldName = settings.Attribute("SampleSizeFieldName").Value;
            var samplingTechniqueFieldName = (string)settings.Attribute("SamplingTechniqueFieldName") ?? string.Empty;

            if (!string.IsNullOrEmpty(options.SampleTypeFieldName))
            {
                if (model != null)
                {
                    var sampleType = model.GetValueByPropertyName(options.SampleTypeFieldName);
                    if (!string.IsNullOrEmpty(sampleType))
                    {
                        options.SampleType = (SampleTypes)Enum.Parse(typeof(SampleTypes), sampleType, false);
                    }
                    else
                    {
                        options.SampleType = SampleTypes.Number;
                    }

                    options.TrueFalseLabels = new List<string>(2)
                    {
                        model.GetValueByPropertyName(options.SampleTypeFieldName + Constants.SampleTrueLabel),
                        model.GetValueByPropertyName(options.SampleTypeFieldName + Constants.SampleFalseLabel)
                    };

                    if (string.IsNullOrEmpty(options.TrueFalseLabels[0]))
                    {
                        options.TrueFalseLabels[0] = LanguageService.Translate("Item_Pass");
                    }

                    if (string.IsNullOrEmpty(options.TrueFalseLabels[1]))
                    {
                        options.TrueFalseLabels[1] = LanguageService.Translate("Item_Fail");
                    }
                }
            }

            var sampleSizeType = SampleSizeTypes.Fixed;
            if (!string.IsNullOrEmpty(samplingTechniqueFieldName) && model != null &&
                Enum.TryParse(model.GetValueByPropertyName(samplingTechniqueFieldName), true, out sampleSizeType))
            {
                options.SampleSizeType = sampleSizeType;
            }
            else if (!string.IsNullOrEmpty(sampleSizeTypeStr))
            {
                if (Enum.TryParse(sampleSizeTypeStr, true, out sampleSizeType))
                {
                    options.SampleSizeType = sampleSizeType;
                }
            }

            options.NumberOfDecimalsFieldName = settings.Attribute("NumberOfDecimalsFieldName").Value;
            options.Mask = "n0";
            if (!string.IsNullOrEmpty(options.NumberOfDecimalsFieldName) && model != null)
            {
                var numberOfDecimals = model.GetValueByPropertyName(options.NumberOfDecimalsFieldName);
                if (SafeTypeConverter.Convert<int>(numberOfDecimals) > 0)
                {
                    options.NumberOfDecimals = numberOfDecimals < 15 ? (int)numberOfDecimals : 15;
                    options.Mask = string.Format(CultureInfo.InvariantCulture, "n{0}", numberOfDecimals < 15 ? (int)numberOfDecimals : 15);
                }
            }

            if (!string.IsNullOrEmpty(lowerSpecLimitField))
            {
                if (model != null)
                {
                    double? limit;
                    SafeTypeConverter.TryToConvert<double?>(model.GetValueByPropertyName(lowerSpecLimitField), out limit);
                    options.LowerSpecLimit = limit;
                }
            }

            if (!string.IsNullOrEmpty(upperSpecLimitField))
            {
                if (model != null)
                {
                    double? limit;
                    SafeTypeConverter.TryToConvert<double?>(model.GetValueByPropertyName(upperSpecLimitField), out limit);
                    options.UpperSpecLimit = limit;
                }
            }

            if (!string.IsNullOrEmpty(sampleSizeFieldName))
            {
                if (model != null)
                {
                    double? size;
                    SafeTypeConverter.TryToConvert<double?>(model.GetValueByPropertyName(sampleSizeFieldName), out size);
                    if (size.HasValue && size < 0)
                    {
                        options.InfoText = "Invalid number of samples";
                    }
                }
            }
        }