Exemple #1
0
        /// <summary>
        /// Retourne le domaine associé à une propriété.
        /// </summary>
        /// <param name="property">Description de la propriété.</param>
        /// <returns>Null si aucun domaine n'est associé.</returns>
        public IDomainChecker GetDomain(BeanPropertyDescriptor property)
        {
            IDomainChecker domain = null;

            if (property == null)
            {
                throw new ArgumentNullException("property");
            }

            if (property.DomainName == null)
            {
                Type primitiveType = property.PrimitiveType;
                if (primitiveType != null)
                {
                    if (!_domainDictionary.TryGetValue(DomainPrefix + primitiveType.Name, out domain))
                    {
                        throw new NotSupportedException("Pas de domaine par défaut pour le type " + primitiveType.Name + " !");
                    }
                }
            }
            else
            {
                domain = GetDomainInternal(property.DomainName);
            }

            if (domain != null)
            {
                domain.CheckPropertyType(property);
            }

            return(domain);
        }
Exemple #2
0
        /// <summary>
        /// Teste si la valeur passée en paramètre est valide pour le champ.
        /// </summary>
        /// <param name="value">Valeur à tester.</param>
        /// <param name="propertyDescriptor">Propriété.</param>
        /// <exception cref="System.InvalidCastException">En cas d'erreur de type.</exception>
        /// <exception cref="ConstraintException">En cas d'erreur, le message décrit l'erreur.</exception>
        void IDomainChecker.CheckValue(object value, BeanPropertyDescriptor propertyDescriptor)
        {
            if (propertyDescriptor == null)
            {
                throw new ArgumentNullException("propertyDescriptor");
            }

            CheckValueType(value, propertyDescriptor);
            CheckValueValidation(value, propertyDescriptor);
        }
Exemple #3
0
        /// <summary>
        /// Vérifie la cohérence de la propriété
        /// avec le domaine.
        /// </summary>
        /// <param name="propertyDescriptor">Propriété.</param>
        void IDomainChecker.CheckPropertyType(BeanPropertyDescriptor propertyDescriptor)
        {
            if (propertyDescriptor == null)
            {
                throw new ArgumentNullException("propertyDescriptor");
            }

            if (!this.DataType.Equals(propertyDescriptor.PrimitiveType))
            {
                if (propertyDescriptor.PrimitiveType != null)
                {
                    throw new NotSupportedException("Invalid property type " + propertyDescriptor.PropertyType +
                                                    " for domain " + this.Name + " " + this.DataType + " expected." + propertyDescriptor.PrimitiveType);
                }

                throw new NotSupportedException("Invalid property type " + propertyDescriptor.PropertyType +
                                                " for domain " + this.Name + " " + this.DataType + " expected. PrimitiveType is null.");
            }
        }
Exemple #4
0
        /// <summary>
        /// Vérifie la cohérence de la valeur passée avec les attributs de validations du domaine.
        /// </summary>
        /// <param name="value">Valeur.</param>
        /// <param name="propertyDescriptor">Propriété.</param>
        private void CheckValueValidation(object value, BeanPropertyDescriptor propertyDescriptor)
        {
            if (this.ValidationAttributes != null)
            {
                foreach (ValidationAttribute validationAttribute in this.ValidationAttributes)
                {
                    if (!validationAttribute.IsValid(value))
                    {
                        string errorMessage;
                        try {
                            errorMessage = validationAttribute.FormatErrorMessage(propertyDescriptor.PropertyName);
                        } catch (Exception e) {
                            throw new ConstraintException(propertyDescriptor, "Impossible de formater le message d'erreur pour la propriété " + propertyDescriptor.PropertyName, e);
                        }

                        throw new ConstraintException(propertyDescriptor, errorMessage);
                    }
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// Converti une valeur en string.
        /// </summary>
        /// <param name="value">Valeur à convertir.</param>
        /// <param name="propertyDescriptor">Propriété.</param>
        /// <exception cref="System.InvalidCastException">En cas d'erreur de type.</exception>
        /// <returns>La valeur sous sa forme textuelle.</returns>
        string IDomainChecker.ConvertToString(object value, BeanPropertyDescriptor propertyDescriptor)
        {
            if (propertyDescriptor != null)
            {
                CheckValueType(value, propertyDescriptor);
            }

            if (_formatter != null)
            {
                return(_formatter.ConvertToString((T)value));
            }

            ExtendedValue extValue = value as ExtendedValue;

            if (_extendedFormatter != null && extValue != null)
            {
                return(_extendedFormatter.ConvertToString(extValue));
            }

            return(TypeDescriptor.GetConverter(typeof(T)).ConvertToString(value));
        }
Exemple #6
0
        /// <summary>
        /// Converti un string en valeur.
        /// </summary>
        /// <param name="text">Valeur à convertir.</param>
        /// <param name="propertyDescriptor">Propriété.</param>
        /// <returns>Valeur dans le type cible.</returns>
        /// <exception cref="System.FormatException">En cas d'erreur de convertion.</exception>
        object IDomainChecker.ConvertFromString(string text, BeanPropertyDescriptor propertyDescriptor)
        {
            if (propertyDescriptor == null)
            {
                throw new ArgumentNullException("propertyDescriptor");
            }

            object v = null;

            try {
                if (_formatter != null)
                {
                    v = _formatter.ConvertFromString(text);
                }
                else if (_extendedFormatter != null)
                {
                    v = _extendedFormatter.ConvertFromString(text);
                }
                else
                {
                    if (!string.IsNullOrEmpty(text))
                    {
                        v = TypeDescriptor.GetConverter(typeof(T)).ConvertFromString(text);
                    }
                }
            } catch (Exception e) {
                string message = e.Message;
                if (_propertyMessage != null)
                {
                    message = string.Format(CultureInfo.CurrentCulture, (string)_propertyMessage.GetValue(null, null), text);
                }

                throw new ConstraintException(propertyDescriptor, message, e);
            }

            CheckValueType(v, propertyDescriptor);
            CheckValueValidation(v, propertyDescriptor);
            return(v);
        }
Exemple #7
0
        /// <summary>
        /// Crée la collection des descripteurs de propriétés.
        /// </summary>
        /// <param name="properties">PropertyDescriptors.</param>
        /// <param name="defaultProperty">Propriété par défaut.</param>
        /// <param name="beanType">Type du bean.</param>
        /// <param name="metadataProperties">Métadonnées.</param>
        /// <returns>Collection.</returns>
        private static BeanPropertyDescriptorCollection CreateCollection(PropertyDescriptorCollection properties, PropertyDescriptor defaultProperty, Type beanType, PropertyDescriptorCollection metadataProperties)
        {
            BeanPropertyDescriptorCollection coll = new BeanPropertyDescriptorCollection(beanType);

            for (int i = 0; i < properties.Count; i++)
            {
                PropertyDescriptor property = properties[i];

                KeyAttribute            keyAttr          = (KeyAttribute)property.Attributes[typeof(KeyAttribute)];
                DisplayAttribute        displayAttr      = (DisplayAttribute)property.Attributes[typeof(DisplayAttribute)];
                ReferencedTypeAttribute attr             = (ReferencedTypeAttribute)property.Attributes[typeof(ReferencedTypeAttribute)];
                ColumnAttribute         colAttr          = (ColumnAttribute)property.Attributes[typeof(ColumnAttribute)];
                DomainAttribute         domainAttr       = (DomainAttribute)property.Attributes[typeof(DomainAttribute)];
                RequiredAttribute       requiredAttr     = (RequiredAttribute)property.Attributes[typeof(RequiredAttribute)];
                TranslatableAttribute   translatableAttr = (TranslatableAttribute)property.Attributes[typeof(TranslatableAttribute)];
                Type[] genericArgumentArray = beanType.GetGenericArguments();

                string display = null;
                if (displayAttr != null)
                {
                    if (displayAttr.ResourceType != null && displayAttr.Name != null)
                    {
                        Dictionary <string, PropertyInfo> resourceProperties;
                        if (!_resourceTypeMap.TryGetValue(displayAttr.ResourceType, out resourceProperties))
                        {
                            resourceProperties = new Dictionary <string, PropertyInfo>();
                            _resourceTypeMap[displayAttr.ResourceType] = resourceProperties;

                            foreach (PropertyInfo p in displayAttr.ResourceType.GetProperties(BindingFlags.Public | BindingFlags.Static))
                            {
                                resourceProperties.Add(p.Name, p);
                            }
                        }

                        display = resourceProperties[displayAttr.Name].GetValue(null, null).ToString();
                    }
                    else
                    {
                        display = displayAttr.Name;
                    }
                }

                string memberName     = (colAttr == null) ? null : colAttr.Name;
                bool   isPrimaryKey   = keyAttr != null;
                bool   isRequired     = requiredAttr != null;
                bool   isTranslatable = translatableAttr != null;
                string domainName     = (domainAttr == null) ? null : domainAttr.Name;
                bool   isDefault      = property.Equals(defaultProperty) || (DefaultPropertyDefaultName.Equals(property.Name) && defaultProperty == null);
                Type   referenceType  = attr == null ? null : attr.ReferenceType;
                //// Type dtoType = genericArgumentArray.Length > 0 ? genericArgumentArray[0] : beanType;
                bool isBrowsable = property.IsBrowsable;
                bool isReadonly  = property.IsReadOnly;

                // Traitement des métadonnées.
                if (metadataProperties != null)
                {
                    PropertyDescriptor metadata = metadataProperties[property.Name];
                    if (metadata != null)
                    {
                        if (!metadata.IsBrowsable)
                        {
                            isBrowsable = false;
                        }

                        if (metadata.Attributes[typeof(RequiredAttribute)] != null)
                        {
                            isRequired = true;
                        }

                        if (metadata.Attributes[typeof(TranslatableAttribute)] != null)
                        {
                            isTranslatable = true;
                        }

                        ReadOnlyAttribute readonlyAttr = (ReadOnlyAttribute)metadata.Attributes[typeof(ReadOnlyAttribute)];
                        if (readonlyAttr != null && readonlyAttr.IsReadOnly)
                        {
                            isReadonly = true;
                        }
                    }
                }

                BeanPropertyDescriptor description = new BeanPropertyDescriptor(
                    property.Name,
                    memberName,
                    property.PropertyType,
                    display,
                    domainName,
                    isPrimaryKey,
                    isDefault,
                    isRequired,
                    referenceType,
                    isReadonly,
                    isBrowsable,
                    isTranslatable);
                if (domainName != null)
                {
                    DomainManager.Instance.GetDomain(description);
                }

                coll.Add(description);
            }

            return(coll);
        }
Exemple #8
0
 /// <summary>
 /// Vérifie la cohérence de la valeur passée et de la propriété
 /// avec le domaine.
 /// </summary>
 /// <param name="value">Valeur.</param>
 /// <param name="propertyDescriptor">Propriété.</param>
 private static void CheckValueType(object value, BeanPropertyDescriptor propertyDescriptor)
 {
     propertyDescriptor.CheckValueType(value);
 }
Exemple #9
0
 /// <summary>
 /// Crée une nouvelle exception.
 /// </summary>
 /// <param name="property">Propriété associée à la violation de contrainte.</param>
 /// <param name="message">Description de l'exception.</param>
 public ConstraintException(BeanPropertyDescriptor property, string message)
     : base(message)
 {
     this.Property = property;
 }