public static EntityDescription Create(Type entityType, EntityDescriptionAttribute attr) { var entityDescription = new EntityDescription(); entityDescription.Elements = new List <FormField>(); entityDescription.ListColumns = new List <ListColumn>(); var properties = entityType.GetRuntimeProperties(); foreach (var property in properties) { var fieldAttributes = property.GetCustomAttributes <FormFieldAttribute>(); if (fieldAttributes.Any()) { entityDescription.Elements.Add(FormField.Create(property.Name, fieldAttributes.First(), property)); } var listAttributes = property.GetCustomAttributes <ListColumnAttribute>(); if (listAttributes.Any()) { entityDescription.ListColumns.Add(ListColumn.Create(property.Name, listAttributes.First())); } } entityDescription.Name = entityType.Name; var titleProperty = attr.ResourceType.GetTypeInfo().GetDeclaredProperty(attr.TitleResource); if (titleProperty != null) { entityDescription.Title = titleProperty.GetValue(titleProperty.DeclaringType, null) as String; } else { entityDescription.Title = entityType.Name; } var descriptionProperty = attr.ResourceType.GetTypeInfo().GetDeclaredProperty(attr.DescriptionResource); if (descriptionProperty != null) { entityDescription.Description = descriptionProperty.GetValue(descriptionProperty.DeclaringType, null) as String; } var userHelpProperty = attr.ResourceType.GetTypeInfo().GetDeclaredProperty(attr.UserHelpResource); if (userHelpProperty != null) { entityDescription.UserHelp = userHelpProperty.GetValue(userHelpProperty.DeclaringType, null) as String; } entityDescription.DomainName = attr.Domain; entityDescription.EntityType = attr.EntityType; return(entityDescription); }
public static DetailResponse <TModel> Create(TModel model) { var response = new DetailResponse <TModel>(); response.Model = model; response.FormFields = new List <string>(); var viewItems = new Dictionary <string, FormField>(); var attr = typeof(TModel).GetTypeInfo().GetCustomAttributes <EntityDescriptionAttribute>().FirstOrDefault(); var entity = EntityDescription.Create(typeof(TModel), attr); if (model is IFormDescriptor) { response.FormFields = (model as IFormDescriptor).GetFormFields(); } response.Title = entity.Title; response.Help = entity.UserHelp; var properties = typeof(TModel).GetRuntimeProperties(); foreach (var property in properties) { var fieldAttributes = property.GetCustomAttributes <FormFieldAttribute>(); if (fieldAttributes.Any()) { var camelCaseName = property.Name.Substring(0, 1).ToLower() + property.Name.Substring(1); var field = FormField.Create(camelCaseName, fieldAttributes.First()); var defaultValue = property.GetValue(model); if (defaultValue != null) { field.DefaultValue = defaultValue.ToString(); } viewItems.Add(camelCaseName, field); } } response.View = viewItems; return(response); }
public static FormField Create(String name, FormFieldAttribute attr, PropertyInfo property) { var field = new FormField(); field.Name = name; field.FieldType = attr.FieldType.ToString(); if (!String.IsNullOrEmpty(attr.LabelDisplayResource)) { if (attr.ResourceType == null) { throw new Exception($"Building Metadata - label is defined, but Resource Type is not defined on {name} {attr.LabelDisplayResource}"); } var labelProperty = attr.ResourceType.GetTypeInfo().GetDeclaredProperty(attr.LabelDisplayResource); field.Label = (string)labelProperty.GetValue(labelProperty.DeclaringType, null); } if (field.FieldType == FormField.FieldType_ChildView) { field.FormFields = new Dictionary <string, FormField>(); var childProperties = property.PropertyType.GetRuntimeProperties(); foreach (var childProperty in childProperties) { var fieldAttributes = childProperty.GetCustomAttributes <FormFieldAttribute>(); if (fieldAttributes.Any()) { var camelCaseName = childProperty.Name.Substring(0, 1).ToLower() + childProperty.Name.Substring(1); var childField = FormField.Create(camelCaseName, fieldAttributes.First(), childProperty); field.FormFields.Add(camelCaseName, childField); } } return(field); } field.IsRequired = attr.IsRequired; if (field.IsRequired) { if (!String.IsNullOrEmpty(attr.RequiredMessageResource)) { if (attr.ResourceType == null) { throw new Exception($"Building Metadata - required message is defined, but Resource Type is not defined on {name} {attr.LabelDisplayResource}"); } var validationProperty = attr.ResourceType.GetTypeInfo().GetDeclaredProperty(attr.RequiredMessageResource); field.RequiredMessage = (string)validationProperty.GetValue(validationProperty.DeclaringType, null); } else { field.RequiredMessage = ValidationResource.PropertyIsRequired.Replace(Tokens.PROPERTY_LABEL, field.Label); } } field.IsUserEditable = attr.IsUserEditable; field.MinLength = attr.MinLength; field.MaxLength = attr.MaxLength; field.IsEnabled = true; field.IsMarkDown = attr.IsMarkDown; field.Options = new List <EnumDescription>(); if (attr.EnumType != null) { var options = new List <EnumDescription>(); var values = Enum.GetValues(attr.EnumType); for (var idx = 0; idx < values.GetLength(0); ++idx) { var value = values.GetValue(idx).ToString(); var enumMember = attr.EnumType.GetTypeInfo().DeclaredMembers.Where(mbr => mbr.Name == value.ToString()).FirstOrDefault(); var enumAttr = enumMember.GetCustomAttribute <EnumLabelAttribute>(); if (enumAttr.IsActive) { options.Add(EnumDescription.Create(enumAttr, value, idx)); } } field.Options = options.OrderBy(opt => opt.SortOrder).ToList(); } if (attr.FieldType == FieldTypes.NameSpace) { field.RegEx = @"^[a-z][a-z0-9]{5,30}$"; field.RegExMessage = ValidationResource.Validation_RegEx_Namespace; } else if (attr.FieldType == FieldTypes.Key) { field.RegEx = @"^[a-z][a-z0-9]{2,50}$"; field.RegExMessage = ValidationResource.Common_Key_Validation; } else { field.RegEx = attr.RegExValidation; if (!String.IsNullOrEmpty(attr.RegExValidationMessageResource)) { if (attr.ResourceType == null) { throw new Exception($"Building Metadata - Reg Ex Validation has a resource nae, but no resource type on {name} {attr.LabelDisplayResource}"); } var validationProperty = attr.ResourceType.GetTypeInfo().GetDeclaredProperty(attr.RegExValidationMessageResource); field.RegExMessage = (string)validationProperty.GetValue(validationProperty.DeclaringType, null); } } if (!String.IsNullOrEmpty(attr.WaterMark)) { if (attr.ResourceType == null) { throw new Exception($"Building Metadata - watermark is defined, but Resource Type is not defined on {name} {attr.WaterMark}"); } var placeholderProperty = attr.ResourceType.GetTypeInfo().GetDeclaredProperty(attr.WaterMark); field.Watermark = placeholderProperty == null ? String.Empty : (string)placeholderProperty.GetValue(placeholderProperty.DeclaringType, null); } if (!String.IsNullOrEmpty(attr.HelpResource)) { if (attr.ResourceType == null) { throw new Exception($"Building Metaata - watermark is defined, but Resource Type is not defined on {name} {attr.HelpResource}"); } var helpProperty = attr.ResourceType.GetTypeInfo().GetDeclaredProperty(attr.HelpResource); field.Help = helpProperty == null ? String.Empty : (string)helpProperty.GetValue(helpProperty.DeclaringType, null); } field.IsVisible = true; return(field); }