Example #1
0
        public void SetAttributeValue <T>(string attributeKey, T attributeValue, string updatedByUser)
        {
            if (!allowedTypes.Contains(typeof(T)))
            {
                throw new CustomAttributeException("Custom attribute of Type {0} is not supported.", typeof(T).Name);
            }

            var existingType = GetCurrentAttributeTypeIfExists(attributeKey);

            switch (typeof(T).Name)
            {
            case "Int32":
                var intValue = Convert.ToInt32(attributeValue);

                var attribute = CustomSelectionAttributes.SingleOrDefault(a => a.Key == attributeKey);

                if (attribute != null)
                {
                    attribute.SetAttributeValue(intValue, updatedByUser);
                }
                else
                {
                    if (existingType != CustomAttributeType.None)
                    {
                        throw new CustomAttributeException("Custom Attribute value for {0} is of an incorrect type. Please cast the value before setting.", attributeKey);
                    }

                    CustomSelectionAttributes.Add(new CustomSelectionAttribute {
                        Key = attributeKey, Value = intValue, UpdatedByUser = updatedByUser
                    });
                }
                break;

            case "String":
                var stringValue = attributeValue.ToString();

                var stringAttribute = CustomStringAttributes.SingleOrDefault(a => a.Key == attributeKey);

                if (stringAttribute != null)
                {
                    stringAttribute.SetAttributeValue(stringValue, updatedByUser);
                }
                else
                {
                    if (existingType != CustomAttributeType.None)
                    {
                        throw new CustomAttributeException("Custom Attribute value for {0} is of an incorrect type. Please cast the value before setting.", attributeKey);
                    }

                    CustomStringAttributes.Add(new CustomStringAttribute {
                        Key = attributeKey, Value = stringValue, UpdatedByUser = updatedByUser
                    });
                }
                break;

            case "DateTime":
                var dateTimeValue = Convert.ToDateTime(attributeValue);

                var dateTimeAttribute = CustomDateTimeAttributes.SingleOrDefault(a => a.Key == attributeKey);

                if (dateTimeAttribute != null)
                {
                    dateTimeAttribute.SetAttributeValue(dateTimeValue, updatedByUser);
                }
                else
                {
                    if (existingType != CustomAttributeType.None)
                    {
                        throw new CustomAttributeException("Custom Attribute value for {0} is of an incorrect type. Please cast the value before setting.", attributeKey);
                    }

                    CustomDateTimeAttributes.Add(new CustomDateTimeAttribute {
                        Key = attributeKey, Value = dateTimeValue, UpdatedByUser = updatedByUser
                    });
                }
                break;

            case "Decimal":
                var decimalValue = Convert.ToDecimal(attributeValue);

                var decimalAttribute = CustomNumericAttributes.SingleOrDefault(a => a.Key == attributeKey);

                if (decimalAttribute != null)
                {
                    decimalAttribute.SetAttributeValue(decimalValue, updatedByUser);
                }
                else
                {
                    if (existingType != CustomAttributeType.None)
                    {
                        throw new CustomAttributeException("Custom Attribute value for {0} is of an incorrect type. Please cast the value before setting.", attributeKey);
                    }
                    CustomNumericAttributes.Add(new CustomNumericAttribute {
                        Key = attributeKey, Value = decimalValue, UpdatedByUser = updatedByUser
                    });
                }
                break;

            default:
                // This should never happen
                throw new CustomAttributeException("Unable to set custom attribute; Key: {0}, Value: {1}, Type: {2}", attributeKey, attributeValue, typeof(T).Name);
            }
        }