Search in sources :

Example 11 with GrailsDomainClassProperty

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

the class DataBindingUtils method assignBidirectionalAssociations.

/**
     * Associations both sides of any bidirectional relationships found in the object and source map to bind
     *
     * @param object The object
     * @param source The source map
     * @param domainClass The DomainClass for the object
     */
public static void assignBidirectionalAssociations(Object object, Map source, GrailsDomainClass domainClass) {
    if (source == null) {
        return;
    }
    for (Object key : source.keySet()) {
        String propertyName = key.toString();
        if (propertyName.indexOf('.') > -1) {
            propertyName = propertyName.substring(0, propertyName.indexOf('.'));
        }
        if (domainClass.hasPersistentProperty(propertyName)) {
            GrailsDomainClassProperty prop = domainClass.getPropertyByName(propertyName);
            if (prop != null && prop.isOneToOne() && prop.isBidirectional()) {
                Object val = source.get(key);
                GrailsDomainClassProperty otherSide = prop.getOtherSide();
                if (val != null && otherSide != null) {
                    MetaClass mc = GroovySystem.getMetaClassRegistry().getMetaClass(val.getClass());
                    try {
                        mc.setProperty(val, otherSide.getName(), object);
                    } catch (Exception e) {
                    // ignore
                    }
                }
            }
        }
    }
}
Also used : MetaClass(groovy.lang.MetaClass) GrailsDomainClassProperty(grails.core.GrailsDomainClassProperty) InvalidRequestBodyException(org.grails.web.databinding.bindingsource.InvalidRequestBodyException)

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