Search in sources :

Example 1 with GrailsDomainClass

use of grails.core.GrailsDomainClass 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 2 with GrailsDomainClass

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

the class DataBindingUtils method bindToCollection.

/**
     * For each DataBindingSource provided by collectionBindingSource a new instance of targetType is created,
     * data binding is imposed on that instance with the DataBindingSource and the instance is added to the end of
     * collectionToPopulate
     *
     * @param targetType The type of objects to create, must be a concrete class
     * @param collectionToPopulate A collection to populate with new instances of targetType
     * @param collectionBindingSource A CollectionDataBindingSource
     * @since 2.3
     */
public static <T> void bindToCollection(final Class<T> targetType, final Collection<T> collectionToPopulate, final CollectionDataBindingSource collectionBindingSource) throws InstantiationException, IllegalAccessException {
    final GrailsApplication application = Holders.findApplication();
    GrailsDomainClass domain = null;
    if (application != null) {
        domain = (GrailsDomainClass) application.getArtefact(DomainClassArtefactHandler.TYPE, targetType.getName());
    }
    final List<DataBindingSource> dataBindingSources = collectionBindingSource.getDataBindingSources();
    for (final DataBindingSource dataBindingSource : dataBindingSources) {
        final T newObject = targetType.newInstance();
        bindObjectToDomainInstance(domain, newObject, dataBindingSource, getBindingIncludeList(newObject), Collections.emptyList(), null);
        collectionToPopulate.add(newObject);
    }
}
Also used : GrailsApplication(grails.core.GrailsApplication) GrailsDomainClass(grails.core.GrailsDomainClass) CollectionDataBindingSource(grails.databinding.CollectionDataBindingSource) DataBindingSource(grails.databinding.DataBindingSource)

Example 3 with GrailsDomainClass

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

the class DataBindingUtils method bindObjectToInstance.

/**
     * Binds the given source object to the given target object performing type conversion if necessary
     *
     * @param object The object to bind to
     * @param source The source object
     * @param include The list of properties to include
     * @param exclude The list of properties to exclude
     * @param filter The prefix to filter by
     *
     * @return A BindingResult or null if it wasn't successful
     */
public static BindingResult bindObjectToInstance(Object object, Object source, List include, List exclude, String filter) {
    if (include == null && exclude == null) {
        include = getBindingIncludeList(object);
    }
    GrailsApplication application = Holders.findApplication();
    GrailsDomainClass domain = null;
    if (application != null) {
        domain = (GrailsDomainClass) application.getArtefact(DomainClassArtefactHandler.TYPE, object.getClass().getName());
    }
    return bindObjectToDomainInstance(domain, object, source, include, exclude, filter);
}
Also used : GrailsApplication(grails.core.GrailsApplication) GrailsDomainClass(grails.core.GrailsDomainClass)

Example 4 with GrailsDomainClass

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

the class DefaultGrailsDomainClass method getComponents.

/**
     * Retrieves a list of embedded components
     *
     * @deprecated Use {@link org.grails.datastore.mapping.model.MappingContext} API instead
     */
@Deprecated
public List<GrailsDomainClass> getComponents() {
    verifyContextIsInitialized();
    List<GrailsDomainClass> components = new ArrayList<>();
    for (Association a : persistentEntity.getAssociations()) {
        if (a.isEmbedded()) {
            components.add((GrailsDomainClass) grailsApplication.getArtefact(DomainClassArtefactHandler.TYPE, a.getAssociatedEntity().getName()));
        }
    }
    return components;
}
Also used : Association(org.grails.datastore.mapping.model.types.Association) GrailsDomainClass(grails.core.GrailsDomainClass)

Example 5 with GrailsDomainClass

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

the class DefaultGrailsDomainClass method getSubClasses.

/**
     * Obtains a Set of subclasses
     *
     * @deprecated Use {@link org.grails.datastore.mapping.model.MappingContext} API instead
     */
@Deprecated
@Override
@SuppressWarnings("unchecked")
public Set getSubClasses() {
    verifyContextIsInitialized();
    Set<GrailsDomainClass> subClasses = new LinkedHashSet<>();
    for (PersistentEntity e : mappingContext.getChildEntities(persistentEntity)) {
        subClasses.add((GrailsDomainClass) grailsApplication.getArtefact(DomainClassArtefactHandler.TYPE, e.getName()));
    }
    return subClasses;
}
Also used : PersistentEntity(org.grails.datastore.mapping.model.PersistentEntity) GrailsDomainClass(grails.core.GrailsDomainClass)

Aggregations

GrailsDomainClass (grails.core.GrailsDomainClass)17 GrailsApplication (grails.core.GrailsApplication)8 DefaultGrailsApplication (grails.core.DefaultGrailsApplication)5 GroovyClassLoader (groovy.lang.GroovyClassLoader)5 Map (java.util.Map)5 GrailsDomainClassProperty (grails.core.GrailsDomainClassProperty)4 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)3 CollectionDataBindingSource (grails.databinding.CollectionDataBindingSource)2 DataBindingSource (grails.databinding.DataBindingSource)2 GroovyObject (groovy.lang.GroovyObject)2 Collection (java.util.Collection)2 IncludeExcludeSupport (org.grails.core.util.IncludeExcludeSupport)2 PersistentEntity (org.grails.datastore.mapping.model.PersistentEntity)2 BeanWrapper (org.springframework.beans.BeanWrapper)2 GrailsClass (grails.core.GrailsClass)1 DataBinder (grails.databinding.DataBinder)1 grails.gorm.validation (grails.gorm.validation)1 Constrained (grails.gorm.validation.Constrained)1 ValidationErrors (grails.validation.ValidationErrors)1 GrailsWebDataBinder (grails.web.databinding.GrailsWebDataBinder)1