Ejemplo n.º 1
0
        private object GetAttributeValue(IExtendable extendable, CustomAttributeConfiguration config)
        {
            switch (config.CustomAttributeType)
            {
            case CustomAttributeType.Numeric:
                return((extendable == null || extendable.GetAttributeValue(config.AttributeKey) == null)
                        ? default(decimal)
                        : extendable.GetAttributeValue(config.AttributeKey));

            case CustomAttributeType.String:
                return((extendable == null || extendable.GetAttributeValue(config.AttributeKey) == null)
                        ? " "// Not a big fan of this but Razor does not recognise empty strings and ends up not rendering a control for the attribute.
                        : extendable.GetAttributeValue(config.AttributeKey));

            case CustomAttributeType.Selection:
                return((extendable == null || extendable.GetAttributeValue(config.AttributeKey) == null)
                        ? default(int)
                        : extendable.GetAttributeValue(config.AttributeKey));

            case CustomAttributeType.DateTime:
                return((extendable == null || extendable.GetAttributeValue(config.AttributeKey) == null)
                        ? default(DateTime)
                        : extendable.GetAttributeValue(config.AttributeKey));

            default:
                throw new CustomAttributeException("Unknown AttributeType for AttributeKey: {0}", config.AttributeKey);
            }
        }
Ejemplo n.º 2
0
        public void ValidateAndSetAttributeValue <T>(CustomAttributeConfiguration attributeConfig, T attributeValue, string updatedByUser)
        {
            if (attributeConfig.IsRequired && attributeValue == null)
            {
                throw new CustomAttributeValidationException(string.Format("{0} is required", attributeConfig.AttributeKey));
            }

            if (attributeValue.GetType() == typeof(string) && attributeConfig.StringMaxLength.HasValue)
            {
                if (attributeValue.ToString().Length > attributeConfig.StringMaxLength.Value)
                {
                    throw new CustomAttributeValidationException("Length of {0} may not exceed {1} characters", attributeConfig.AttributeKey, attributeConfig.StringMaxLength.Value);
                }
            }

            if (attributeValue.GetType() == typeof(decimal) && (attributeConfig.NumericMinValue.HasValue || attributeConfig.NumericMaxValue.HasValue))
            {
                var decimalValue = Convert.ToDecimal(attributeValue);

                if (attributeConfig.NumericMinValue.HasValue && decimalValue < attributeConfig.NumericMinValue.Value)
                {
                    throw new CustomAttributeValidationException("Value of {0} may not be lower than {1}", attributeConfig.AttributeKey, attributeConfig.NumericMinValue.Value);
                }

                if (attributeConfig.NumericMaxValue.HasValue && decimalValue > attributeConfig.NumericMaxValue.Value)
                {
                    throw new CustomAttributeValidationException("Value of {0} may not be higher than {1}", attributeConfig.AttributeKey, attributeConfig.NumericMaxValue.Value);
                }
            }

            if (attributeValue.GetType() == typeof(DateTime) && (attributeConfig.PastDateOnly || attributeConfig.FutureDateOnly))
            {
                var dateTimeValue = Convert.ToDateTime(attributeValue);

                if (attributeConfig.PastDateOnly && dateTimeValue.Date > DateTime.Now.Date)
                {
                    throw new CustomAttributeValidationException("{0} may only be in the past", attributeConfig.AttributeKey);
                }

                if (attributeConfig.FutureDateOnly && dateTimeValue.Date < DateTime.Now.Date)
                {
                    throw new CustomAttributeValidationException("{0} may only be in the future", attributeConfig.AttributeKey);
                }
            }

            SetAttributeValue(attributeConfig.AttributeKey, attributeValue, updatedByUser);
        }
Ejemplo n.º 3
0
 public void ValidateAndSetAttributeValue <T>(VPS.CustomAttributes.CustomAttributeConfiguration attributeConfig, T attributeValue, string updatedByUser)
 {
     customAttributes.ValidateAndSetAttributeValue(attributeConfig, attributeValue, updatedByUser);
 }