Search in sources :

Example 1 with GrailsDomainClassProperty

use of grails.core.GrailsDomainClassProperty in project grails-core by grails.

the class DefaultConstraintEvaluator method evaluate.

@Override
public Map<String, Constrained> evaluate(Class<?> cls, GrailsDomainClassProperty[] properties) {
    Map<String, ConstrainedProperty> evaluated = resolveDelegate().evaluate(cls);
    Map<String, Constrained> adapted = adaptConstraints(evaluated);
    for (GrailsDomainClassProperty property : properties) {
        String name = property.getName();
        if (!adapted.containsKey(name)) {
            adapted.remove(name);
        }
    }
    return adapted;
}
Also used : Constrained(grails.validation.Constrained) GrailsDomainClassProperty(grails.core.GrailsDomainClassProperty) ConstrainedProperty(grails.gorm.validation.ConstrainedProperty)

Example 2 with GrailsDomainClassProperty

use of grails.core.GrailsDomainClassProperty in project grails-core by grails.

the class DomainClassPropertyComparator method compare.

public int compare(Object o1, Object o2) {
    if (o1.equals(domainClass.getIdentifier())) {
        return -1;
    }
    if (o2.equals(domainClass.getIdentifier())) {
        return 1;
    }
    GrailsDomainClassProperty prop1 = (GrailsDomainClassProperty) o1;
    GrailsDomainClassProperty prop2 = (GrailsDomainClassProperty) o2;
    Constrained cp1 = (Constrained) constrainedProperties.get(prop1.getName());
    Constrained cp2 = (Constrained) constrainedProperties.get(prop2.getName());
    if (cp1 == null & cp2 == null) {
        return prop1.getName().compareTo(prop2.getName());
    }
    if (cp1 == null) {
        return 1;
    }
    if (cp2 == null) {
        return -1;
    }
    if (cp1.getOrder() > cp2.getOrder()) {
        return 1;
    }
    if (cp1.getOrder() < cp2.getOrder()) {
        return -1;
    }
    return 0;
}
Also used : Constrained(grails.validation.Constrained) GrailsDomainClassProperty(grails.core.GrailsDomainClassProperty)

Example 3 with GrailsDomainClassProperty

use of grails.core.GrailsDomainClassProperty in project grails-core by grails.

the class GrailsDomainClassValidator method validate.

/**
     * @see CascadingValidator#validate(Object, org.springframework.validation.Errors, boolean)
     */
@SuppressWarnings({ "unchecked", "rawtypes" })
public void validate(Object obj, Errors errors, boolean cascade) {
    if (obj == null) {
        throw new IllegalArgumentException("Argument [" + obj + "] is not an instance of [" + domainClass.getClazz() + "] which this validator is configured for");
    }
    BeanWrapper bean = new BeanWrapperImpl(obj);
    Map constrainedProperties = domainClass.getConstrainedProperties();
    Set<String> constrainedPropertyNames = new HashSet(constrainedProperties.keySet());
    for (Object key : constrainedProperties.keySet()) {
        String propertyName = (String) key;
        validatePropertyWithConstraint(propertyName, obj, errors, bean, constrainedProperties);
    }
    GrailsDomainClassProperty[] persistentProperties = domainClass.getPersistentProperties();
    for (GrailsDomainClassProperty persistentProperty : persistentProperties) {
        String propertyName = persistentProperty.getName();
        if ((persistentProperty.isAssociation() || persistentProperty.isEmbedded()) && cascade) {
            cascadeToAssociativeProperty(errors, bean, persistentProperty);
        }
        // Remove this property from the set of constrained property
        // names because we have already processed it.
        constrainedPropertyNames.remove(propertyName);
    }
    postValidate(obj, errors);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) GrailsDomainClassProperty(grails.core.GrailsDomainClassProperty) GString(groovy.lang.GString) Map(java.util.Map) HashSet(java.util.HashSet)

Example 4 with GrailsDomainClassProperty

use of grails.core.GrailsDomainClassProperty in project grails-core by grails.

the class GrailsDomainClassValidator method cascadeValidationToOne.

/**
     * Cascades validation to a one-to-one or many-to-one property.
     *
     * @param errors The Errors instance
     * @param bean The original BeanWrapper
     * @param associatedObject The associated object's current value
     * @param persistentProperty The GrailsDomainClassProperty instance
     * @param propertyName The name of the property
     * @param indexOrKey
     */
@SuppressWarnings("rawtypes")
protected void cascadeValidationToOne(Errors errors, BeanWrapper bean, Object associatedObject, GrailsDomainClassProperty persistentProperty, String propertyName, Object indexOrKey) {
    if (associatedObject == null) {
        return;
    }
    GrailsDomainClass associatedDomainClass = getAssociatedDomainClass(associatedObject, persistentProperty);
    if (associatedDomainClass == null || !isOwningInstance(bean, associatedDomainClass) && !persistentProperty.isExplicitSaveUpdateCascade()) {
        return;
    }
    GrailsDomainClassProperty otherSide = null;
    if (persistentProperty.isBidirectional()) {
        otherSide = persistentProperty.getOtherSide();
    }
    Map associatedConstraintedProperties = associatedDomainClass.getConstrainedProperties();
    GrailsDomainClassProperty[] associatedPersistentProperties = associatedDomainClass.getPersistentProperties();
    String nestedPath = errors.getNestedPath();
    try {
        errors.setNestedPath(buildNestedPath(nestedPath, propertyName, indexOrKey));
        for (GrailsDomainClassProperty associatedPersistentProperty : associatedPersistentProperties) {
            if (persistentProperty.isEmbedded() && EMBEDDED_EXCLUDES.contains(associatedPersistentProperty.getName())) {
                continue;
            }
            String associatedPropertyName = associatedPersistentProperty.getName();
            if (associatedConstraintedProperties.containsKey(associatedPropertyName)) {
                validatePropertyWithConstraint(errors.getNestedPath() + associatedPropertyName, associatedObject, errors, new BeanWrapperImpl(associatedObject), associatedConstraintedProperties);
            }
            if (associatedPersistentProperty.equals(otherSide))
                continue;
            if (associatedPersistentProperty.isAssociation()) {
                cascadeToAssociativeProperty(errors, new BeanWrapperImpl(associatedObject), associatedPersistentProperty);
            }
        }
    } finally {
        errors.setNestedPath(nestedPath);
    }
}
Also used : BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) GrailsDomainClass(grails.core.GrailsDomainClass) GrailsDomainClassProperty(grails.core.GrailsDomainClassProperty) GString(groovy.lang.GString) Map(java.util.Map)

Example 5 with GrailsDomainClassProperty

use of grails.core.GrailsDomainClassProperty in project grails-core by grails.

the class DefaultGrailsDomainClass method initializePersistentProperties.

private void initializePersistentProperties() {
    if (!propertiesInitialized) {
        verifyContextIsInitialized();
        propertyMap = new LinkedHashMap<>();
        PersistentProperty identity = persistentEntity.getIdentity();
        if (identity != null) {
            identifier = new DefaultGrailsDomainClassProperty(this, persistentEntity, identity);
        }
        // First go through the properties of the class and create domain properties
        // populating into a map
        populateDomainClassProperties();
        // set properties from map values
        persistentProperties = propertyMap.values().toArray(new GrailsDomainClassProperty[propertyMap.size()]);
        // if no identifier property throw exception
        if (identifier == null) {
            throw new GrailsDomainException("Identity property not found, but required in domain class [" + getFullName() + "]");
        } else {
            propertyMap.put(identifier.getName(), identifier);
        }
        // if no version property throw exception
        if (version != null) {
            propertyMap.put(version.getName(), version);
        }
        List<MetaProperty> properties = getMetaClass().getProperties();
        for (MetaProperty property : properties) {
            String name = property.getName();
            if (!propertyMap.containsKey(name) && !NameUtils.isConfigurational(name) && !Modifier.isStatic(property.getModifiers())) {
                propertyMap.put(name, new MetaGrailsDomainClassProperty(this, property));
            }
        }
        this.properties = propertyMap.values().toArray(new GrailsDomainClassProperty[propertyMap.size()]);
    }
    propertiesInitialized = true;
}
Also used : GrailsDomainClassProperty(grails.core.GrailsDomainClassProperty) PersistentProperty(org.grails.datastore.mapping.model.PersistentProperty) MetaProperty(groovy.lang.MetaProperty) GrailsDomainException(org.grails.core.exceptions.GrailsDomainException)

Aggregations

GrailsDomainClassProperty (grails.core.GrailsDomainClassProperty)11 GrailsDomainClass (grails.core.GrailsDomainClass)4 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)4 BeanWrapper (org.springframework.beans.BeanWrapper)3 Constrained (grails.validation.Constrained)2 GString (groovy.lang.GString)2 Map (java.util.Map)2 IncludeExcludeSupport (org.grails.core.util.IncludeExcludeSupport)2 GrailsClass (grails.core.GrailsClass)1 ConstrainedProperty (grails.gorm.validation.ConstrainedProperty)1 GroovyObject (groovy.lang.GroovyObject)1 MetaClass (groovy.lang.MetaClass)1 MetaProperty (groovy.lang.MetaProperty)1 HashSet (java.util.HashSet)1 GrailsDomainException (org.grails.core.exceptions.GrailsDomainException)1 PersistentProperty (org.grails.datastore.mapping.model.PersistentProperty)1 Association (org.grails.datastore.mapping.model.types.Association)1 InvalidRequestBodyException (org.grails.web.databinding.bindingsource.InvalidRequestBodyException)1 JSONWriter (org.grails.web.json.JSONWriter)1