Exemple #1
0
        public static EnumDescription Create(EnumLabelAttribute attr, String name)
        {
            var enumLabel = new EnumDescription();

            enumLabel.Key  = attr.Key;
            enumLabel.Name = name;

            var labelProperty = attr.ResourceType.GetTypeInfo().GetDeclaredProperty(attr.LabelResource);

            enumLabel.Label = labelProperty.GetValue(labelProperty.DeclaringType, null) as String;

            if (!String.IsNullOrEmpty(attr.HelpResource))
            {
                var helpProperty = attr.ResourceType.GetTypeInfo().GetDeclaredProperty(attr.HelpResource);
                enumLabel.Help = helpProperty.GetValue(helpProperty.DeclaringType, null) as String;
            }

            return(enumLabel);
        }
Exemple #2
0
        public static List <EnumDescription> GetEnumOptions <Type>()
        {
            var enumType = typeof(Type);

            new List <EnumDescription>();
            var options = new List <EnumDescription>();
            var values  = Enum.GetValues(enumType);

            for (var idx = 0; idx < values.GetLength(0); ++idx)
            {
                var value = values.GetValue(idx).ToString();

                var enumMember = 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));
                }
            }
            return(options.OrderBy(opt => opt.SortOrder).ToList());
        }
Exemple #3
0
        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);
        }
Exemple #4
0
        public static FormField Create(String name, FormFieldAttribute attr)
        {
            var field = new FormField();

            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);
            }

            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.FieldType      = attr.FieldType.ToString();
            field.Name           = name;
            field.MinLength      = attr.MinLength;
            field.MaxLength      = attr.MaxLength;
            field.IsEnabled      = true;

            field.Options = new List <EnumDescription>();
            if (attr.EnumType != null)
            {
                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>();

                    field.Options.Add(EnumDescription.Create(enumAttr, value));
                }
            }

            if (attr.FieldType == FieldTypes.NameSpace)
            {
                field.RegEx        = @"^[a-z0-9]{6,30}$";
                field.RegExMessage = ValidationResource.Validation_RegEx_Namespace;
            }
            else if (attr.FieldType == FieldTypes.Key)
            {
                field.RegEx        = @"^[a-z0-9]{3,30}$";
                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);
        }