Search in sources :

Example 1 with ConstrainedProperty

use of grails.validation.ConstrainedProperty in project grails-core by grails.

the class ConstrainedPropertyBuilder method createNode.

@SuppressWarnings("rawtypes")
@Override
protected Object createNode(Object name, Map attributes) {
    try {
        String property = (String) name;
        ConstrainedProperty cp;
        if (constrainedProperties.containsKey(property)) {
            cp = (ConstrainedProperty) constrainedProperties.get(property);
        } else {
            Class<?> propertyType = classPropertyFetcher.getPropertyType(property, true);
            if (propertyType == null) {
                throw new MissingMethodException(property, targetClass, new Object[] { attributes }, true);
            }
            cp = new ConstrainedProperty(targetClass, property, propertyType);
            cp.setOrder(order++);
            constrainedProperties.put(property, cp);
        }
        if (cp.getPropertyType() == null) {
            if (!IMPORT_FROM_CONSTRAINT.equals(name)) {
                GrailsUtil.warn("Property [" + cp.getPropertyName() + "] not found in domain class " + targetClass.getName() + "; cannot apply constraints: " + attributes);
            }
            return cp;
        }
        for (Object o : attributes.keySet()) {
            String constraintName = (String) o;
            final Object value = attributes.get(constraintName);
            if (SHARED_CONSTRAINT.equals(constraintName)) {
                if (value != null) {
                    sharedConstraints.put(property, value.toString());
                }
                continue;
            }
            if (cp.supportsContraint(constraintName)) {
                cp.applyConstraint(constraintName, value);
            } else {
                if (ConstrainedProperty.hasRegisteredConstraint(constraintName)) {
                    // constraint is registered but doesn't support this property's type
                    GrailsUtil.warn("Property [" + cp.getPropertyName() + "] of domain class " + targetClass.getName() + " has type [" + cp.getPropertyType().getName() + "] and doesn't support constraint [" + constraintName + "]. This constraint will not be checked during validation.");
                } else {
                    // in the case where the constraint is not supported we still retain meta data
                    // about the constraint in case its needed for other things
                    cp.addMetaConstraint(constraintName, value);
                }
            }
        }
        return cp;
    } catch (InvalidPropertyException ipe) {
        throw new MissingMethodException((String) name, targetClass, new Object[] { attributes });
    }
}
Also used : MissingMethodException(groovy.lang.MissingMethodException) InvalidPropertyException(org.springframework.beans.InvalidPropertyException) ConstrainedProperty(grails.validation.ConstrainedProperty)

Example 2 with ConstrainedProperty

use of grails.validation.ConstrainedProperty in project grails-core by grails.

the class GrailsDomainClassValidator method validatePropertyWithConstraint.

@SuppressWarnings("rawtypes")
private void validatePropertyWithConstraint(String propertyName, Object obj, Errors errors, BeanWrapper bean, Map constrainedProperties) {
    int i = propertyName.lastIndexOf(".");
    String constrainedPropertyName;
    if (i > -1) {
        constrainedPropertyName = propertyName.substring(i + 1, propertyName.length());
    } else {
        constrainedPropertyName = propertyName;
    }
    FieldError fieldError = errors.getFieldError(constrainedPropertyName);
    if (fieldError == null) {
        ConstrainedProperty c = (ConstrainedProperty) constrainedProperties.get(constrainedPropertyName);
        c.setMessageSource(messageSource);
        c.validate(obj, bean.getPropertyValue(constrainedPropertyName), errors);
    }
}
Also used : FieldError(org.springframework.validation.FieldError) GString(groovy.lang.GString) ConstrainedProperty(grails.validation.ConstrainedProperty)

Example 3 with ConstrainedProperty

use of grails.validation.ConstrainedProperty in project grails-core by grails.

the class ConstrainedPropertyBuilder method handleImportFrom.

@SuppressWarnings({ "unchecked", "rawtypes" })
private Object handleImportFrom(Map attributes, Class importFromClazz) {
    Map importFromConstrainedProperties = new DefaultConstraintEvaluator().evaluate(importFromClazz);
    PropertyDescriptor[] targetPropertyDescriptorArray = classPropertyFetcher.getPropertyDescriptors();
    List toBeIncludedPropertyNamesParam = (List) attributes.get("include");
    List toBeExcludedPropertyNamesParam = (List) attributes.get("exclude");
    List<String> resultingPropertyNames = new ArrayList<String>();
    for (PropertyDescriptor targetPropertyDescriptor : targetPropertyDescriptorArray) {
        String targetPropertyName = targetPropertyDescriptor.getName();
        if (toBeIncludedPropertyNamesParam == null) {
            resultingPropertyNames.add(targetPropertyName);
        } else if (isListOfRegexpsContainsString(toBeIncludedPropertyNamesParam, targetPropertyName)) {
            resultingPropertyNames.add(targetPropertyName);
        }
        if (toBeExcludedPropertyNamesParam != null && isListOfRegexpsContainsString(toBeExcludedPropertyNamesParam, targetPropertyName)) {
            resultingPropertyNames.remove(targetPropertyName);
        }
    }
    resultingPropertyNames.remove("class");
    resultingPropertyNames.remove("metaClass");
    for (String targetPropertyName : resultingPropertyNames) {
        ConstrainedProperty importFromConstrainedProperty = (ConstrainedProperty) importFromConstrainedProperties.get(targetPropertyName);
        if (importFromConstrainedProperty != null) {
            // Map importFromConstrainedPropertyAttributes = importFromConstrainedProperty.getAttributes();
            // createNode(targetPropertyName, importFromConstrainedPropertyAttributes);
            Map importFromConstrainedPropertyAttributes = new HashMap();
            for (Constraint importFromAppliedConstraint : importFromConstrainedProperty.getAppliedConstraints()) {
                String importFromAppliedConstraintName = importFromAppliedConstraint.getName();
                Object importFromAppliedConstraintParameter = importFromAppliedConstraint.getParameter();
                importFromConstrainedPropertyAttributes.put(importFromAppliedConstraintName, importFromAppliedConstraintParameter);
            }
            createNode(targetPropertyName, importFromConstrainedPropertyAttributes);
        }
    }
    return null;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Constraint(grails.validation.Constraint) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ConstrainedProperty(grails.validation.ConstrainedProperty) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

ConstrainedProperty (grails.validation.ConstrainedProperty)3 Constraint (grails.validation.Constraint)1 GString (groovy.lang.GString)1 MissingMethodException (groovy.lang.MissingMethodException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 InvalidPropertyException (org.springframework.beans.InvalidPropertyException)1 FieldError (org.springframework.validation.FieldError)1