public static IFieldInfo Create(PropertyInfo prop, IEditableRoot editableRoot, IField field, object rawValue, IValidationContext validationContext) { var customAttr = prop.GetCustomAttribute<CommonSettingsAttribute>(); var promptAttr = prop.GetCustomAttribute<PromptAttribute>(); var docAttr = prop.GetCustomAttribute<DocumentationAttribute>(); var fieldTypeAttr = prop.GetCustomAttribute<FieldTypeAttribute>(); var hasCalculatedAttr = prop.GetCustomAttributes(typeof(CalculatedAttribute), false).Any(); var hasDefaultExpressionAttr = prop.GetCustomAttributes(typeof(HasDefaultExpressionAttribute), false).Any(); //Set common properties var fieldInfo = CreateFieldInfo(fieldTypeAttr.ColumnType, prop, rawValue, editableRoot); fieldInfo.Id = editableRoot.Id; fieldInfo.SystemName = field.SystemName; fieldInfo.DisplayName = customAttr.Name; fieldInfo.Width = customAttr.Width; fieldInfo.CanRead = editableRoot.CanReadProperty(prop.Name); fieldInfo.IsRequired = validationContext != null && validationContext.IsRequired(editableRoot.ProcessName, field.SystemName, editableRoot.GetCurrentStateGuid()); fieldInfo.CanRead = editableRoot.CanReadProperty(prop.Name); fieldInfo.CanWrite = editableRoot.CanWriteProperty(prop.Name) && !hasCalculatedAttr; fieldInfo.BackColor = GetBackColor(editableRoot, prop); fieldInfo.Prompt = promptAttr == null ? string.Format(CultureInfo.InvariantCulture, LanguageService.Translate("Tooltip_EnterPrompt"), customAttr.Name) : promptAttr.PromptString; fieldInfo.IsDocumentationAvailable = docAttr != null; if (hasCalculatedAttr || hasDefaultExpressionAttr) { fieldInfo.ExpressionType = hasCalculatedAttr ? FieldExpressionType.Calculated : FieldExpressionType.Default; fieldInfo.Expression = GetExpression(editableRoot, prop); } // docAttr == null ? string.Empty : ClrUnicodeConverter.ToText(docAttr.DocumentationBody), return fieldInfo; }
/// <summary> /// Initializes a new instance of the <see cref="FieldReadModel"/> class. /// </summary> /// <param name="baseModel"> /// The base model. /// </param> /// <param name="property"> /// The property. /// </param> public FieldReadModel(IEditableRoot baseModel, PropertyInfo property) { if (baseModel == null) { throw new ArgumentNullException("baseModel"); } if (property == null) { throw new ArgumentNullException("property"); } Ioc.SatisfyImportsOnce(this); SystemName = property.Name; var commonSettingsAttr = property.GetCustomAttributes(typeof(CommonSettingsAttribute), false).Select(d => d).FirstOrDefault() as CommonSettingsAttribute; Name = commonSettingsAttr == null ? property.Name : commonSettingsAttr.Name; SectionName = commonSettingsAttr == null ? string.Empty : commonSettingsAttr.Section; var fieldTypeAttr = property.GetCustomAttributes(typeof(FieldTypeAttribute), false).Select(d => d).FirstOrDefault() as FieldTypeAttribute; FieldType = fieldTypeAttr == null ? ColumnTypes.String : fieldTypeAttr.ColumnType; var hasCalculatedAttr = property.GetCustomAttributes(typeof(CalculatedAttribute), false).Any(); CanWrite = baseModel.CanWriteProperty(property.Name) && !hasCalculatedAttr; CanRead = baseModel.CanReadProperty(property.Name); BackColor = GetBackColor(baseModel, property); HideFromDetails = commonSettingsAttr != null && commonSettingsAttr.HideFromDetails; bool valueObtained = false; switch (this.FieldType) { case ColumnTypes.String: var testFieldAttr = property.GetCustomAttributes(typeof(TextFieldAttribute), false).Select(d => d).FirstOrDefault() as TextFieldAttribute; var stringLengthAttr = property.GetCustomAttributes(typeof(StringLengthAttribute), false).Select(d => d).FirstOrDefault() as StringLengthAttribute; var richTextAttr = property.GetCustomAttributes(typeof(RichTextAttribute), false).Select(d => d).FirstOrDefault() as RichTextAttribute; this.Options = StringFieldOptions.Create(testFieldAttr, stringLengthAttr, richTextAttr); break; case ColumnTypes.Integer: case ColumnTypes.Numeric: case ColumnTypes.Double: var numericOptionsAttr = property.GetCustomAttributes(typeof(NumericAttribute), false).Select(d => d).FirstOrDefault() as NumericAttribute; var rangeAttr = property.GetCustomAttributes(typeof(RangeAttribute), false).Select(d => d).FirstOrDefault() as RangeAttribute; this.Options = NumericFieldOptions.Create(numericOptionsAttr, rangeAttr); break; case ColumnTypes.DateTime: var dateTimeAttr = property.GetCustomAttributes(typeof(DateTimeFormatAttribute), false).Select(d => d).FirstOrDefault() as DateTimeFormatAttribute; this.Options = DateTimeFieldOptions.Create(dateTimeAttr); if (CanRead) { Value = string.Format(CultureInfo.InvariantCulture, "{0:yyyy-MM-ddTHH:mm:ss}", property.GetValue(baseModel)); } valueObtained = true; break; case ColumnTypes.Boolean: this.Options = BooleanFieldOptions.Create(property); break; case ColumnTypes.Reference: this.Options = SingleCrossRefFieldOptions.Create(property, baseModel); break; case ColumnTypes.File:break; case ColumnTypes.DisplayList: if (CanRead) { var displayItemList = (IDisplayListItemList) baseModel.GetValueByPropertyName(property.Name); Options = DisplayListFieldOptions.Create(property, displayItemList); var displayListFieldOptions = (DisplayListFieldOptions) Options; if (displayListFieldOptions != null) { var baseType = displayItemList.GetType().BaseType; if (baseType != null) { var itemType = baseType.GetGenericArguments()[1]; var columns = displayListFieldOptions.Columns; var displayListModels = new List<DisplayListItemModel>(); foreach (var displayItem in displayItemList) { var item = new DisplayListItemModel { Id = (int) displayItem.GetType().GetProperty(Constants.IdColumnName).GetValue(displayItem) }; foreach (var column in columns) { var propertyInfo = itemType.GetPropertyByFullName(column.SystemName); if (propertyInfo == null) continue; var columnValue = FieldValueExtractor.GetPropertyStringValue(propertyInfo, propertyInfo.GetValue(displayItem), column.ColumnType); item.Values[column.ColumnName] = columnValue; } displayListModels.Add(item); } Value = displayListModels; } } } valueObtained = true; break; case ColumnTypes.MultiReference: this.Options = MultiCrossRefFieldOptions.Create(property, baseModel); if (CanRead) { if (baseModel.GetValueByPropertyName(property.Name) is ICrossRefItemList) { var list = (ICrossRefItemList)baseModel.GetValueByPropertyName(property.Name); var values = new List<MultiCrossRefItemModel>(); foreach (var l in list) { var id = (int)l.GetType().GetProperty(Constants.IdColumnName).GetValue(l); var value = SafeTypeConverter.Convert<string>(l.GetType().GetProperty(((MultiCrossRefFieldOptions)Options).FieldName).GetValue(l)); values.Add(new MultiCrossRefItemModel { Id = id, DisplayText = value, ProcessName = ((MultiCrossRefFieldOptions)Options).ProcessSystemName, FieldName = property.Name }); } Value = values; // JsonConvert.SerializeObject(values); } } valueObtained = true; break; case ColumnTypes.Image: this.Options = ImageFieldOptions.Create(property); if (CanRead) { var imageBytes = property.GetValue(baseModel) as byte[]; if (imageBytes == null || imageBytes.Length == 0) { return; } using (var sourceStream = new MemoryStream(imageBytes)) { using (var image = Image.FromStream(sourceStream)) { var size = CommonHelper.GetThumbnailSize(image); using (var thumb = image.GetThumbnailImage(size.Width, size.Height, () => false, IntPtr.Zero)) { using (var thumbStream = new MemoryStream()) { thumb.Save(thumbStream, ImageFormat.Png); Value = Convert.ToBase64String(thumbStream.GetBuffer()); } } } } } valueObtained = true; break; case ColumnTypes.Result: this.Options = ResultFieldOptions.Create(property, baseModel); break; case ColumnTypes.Checklist: this.Options = ChecklistFieldOptions.Create(property, baseModel); if (CanRead) { var checklist = property.GetValue(baseModel) as ChecklistEdit; if (checklist == null || string.IsNullOrEmpty(checklist.AnswerProcessSystemName)) { return; } var answerDetails = new List<DetailsReadModel>(); for (int index = 0; index < checklist.AnswerProcessList.Count; index++) { var answer = (IEditableRoot)checklist.AnswerProcessList[index]; answerDetails.Add(ProcessController.CreateDetailsReadModel(answer, index)); } Value = answerDetails; } valueObtained = true; break; case ColumnTypes.Sample: SampleFieldOptions sampleOptions = SampleFieldOptions.Create(property, baseModel); this.Options = sampleOptions; if (CanRead) { var value = property.GetValue(baseModel); if (value != null) { var val = (ISampleList)value; if (string.IsNullOrEmpty(val.SampleType)) { val.SampleType = SampleTypes.Number.ToString(); } foreach (ISampleEdit sample in val) { sample.LowerSpecLimit = sampleOptions.LowerSpecLimit; sample.UpperSpecLimit = sampleOptions.UpperSpecLimit; } sampleOptions.SamplesAreGenerated = val.Count > 0 || sampleOptions.SampleSizeType == SampleSizeTypes.Manual; var index = 0; Value = val.Cast<ISampleEdit>().Select(v => new SampleModel { Id = v.Id, ItemId = v.ItemId, Label = v.Label, SampleNumeric = v.SampleNumeric, SampleBoolean = v.SampleBoolean, SampleAlphanumeric = v.SampleAlphanumeric, SampleType = v.SampleType, LowerSpecLimit = v.LowerSpecLimit, UpperSpecLimit = v.UpperSpecLimit, ChildIndex = index++ }); } valueObtained = true; } break; case ColumnTypes.SampleType: this.Options = SampleTypeFieldOptions.Create(property, baseModel); if (CanRead) { var value = property.GetValue(baseModel); Value = SafeTypeConverter.Convert<string>(value); if (string.IsNullOrEmpty((string)Value)) { Value = SampleTypes.Number.ToString(); } } break; case ColumnTypes.SamplingTechnique: this.Options = SamplingTechniqueFieldOptions.Create(property, baseModel); if (CanRead) { var value = property.GetValue(baseModel); Value = SafeTypeConverter.Convert<string>(value); //ELMTSUP 2512 //if (string.IsNullOrEmpty((string)Value)) //{ // Value = SampleSizeTypes.Fixed.ToString(); //} } break; } if (!valueObtained) { if (CanRead) { var value = property.GetValue(baseModel); Value = SafeTypeConverter.Convert<string>(value); } } }