Search in sources :

Example 6 with GrailsDomainClassProperty

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

the class DefaultGrailsDomainClass method getAssociations.

/**
     * Retrieves a list of associations
     *
     * @deprecated Use {@link org.grails.datastore.mapping.model.MappingContext} API instead
     */
@Deprecated
public List<GrailsDomainClassProperty> getAssociations() {
    verifyContextIsInitialized();
    List<GrailsDomainClassProperty> associations = new ArrayList<>();
    List<String> associationNames = new ArrayList<>();
    for (Association a : persistentEntity.getAssociations()) {
        associationNames.add(a.getName());
    }
    for (GrailsDomainClassProperty p : persistentProperties) {
        if (associationNames.contains(p.getName())) {
            associations.add(p);
        }
    }
    return associations;
}
Also used : Association(org.grails.datastore.mapping.model.types.Association) GrailsDomainClassProperty(grails.core.GrailsDomainClassProperty)

Example 7 with GrailsDomainClassProperty

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

the class DefaultGrailsDomainClassProperty method equals.

/**
     * Overridden equals to take into account inherited properties
     * e.g. childClass.propertyName is equal to parentClass.propertyName if the types match and
     * childClass.property.isInherited
     *
     * @param o the Object to compare this property to
     * @return boolean indicating equality of the two objects
     */
@Override
public boolean equals(Object o) {
    if (o == null) {
        return false;
    }
    if (o instanceof GrailsDomainClassProperty) {
        if (!super.equals(o)) {
            GrailsDomainClassProperty otherProp = (GrailsDomainClassProperty) o;
            Class<?> myActualClass = getDomainClass().getClazz();
            Class<?> otherActualClass = otherProp.getDomainClass().getClazz();
            return otherProp.getName().equals(getName()) && otherProp.getReferencedPropertyType().equals(getReferencedPropertyType()) && (otherActualClass.isAssignableFrom(myActualClass) || myActualClass.isAssignableFrom(otherActualClass));
        }
        return true;
    }
    return false;
}
Also used : GrailsDomainClassProperty(grails.core.GrailsDomainClassProperty)

Example 8 with GrailsDomainClassProperty

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

the class GrailsDomainConfigurationUtil method configureDomainClassRelationships.

/**
     * Configures the relationships between domain classes after they have been all loaded.
     *
     * @param domainClasses The domain classes to configure relationships for
     * @param domainMap     The domain class map
     */
public static void configureDomainClassRelationships(GrailsClass[] domainClasses, Map<?, ?> domainMap) {
    // and configure how domain class properties reference each other
    for (GrailsClass grailsClass : domainClasses) {
        GrailsDomainClass domainClass = (GrailsDomainClass) grailsClass;
        if (!domainClass.isRoot()) {
            Class<?> superClass = grailsClass.getClazz().getSuperclass();
            while (!superClass.equals(Object.class) && !superClass.equals(GroovyObject.class)) {
                GrailsDomainClass gdc = (GrailsDomainClass) domainMap.get(superClass.getName());
                if (gdc == null || gdc.getSubClasses() == null) {
                    break;
                }
                gdc.getSubClasses().add((GrailsDomainClass) grailsClass);
                superClass = superClass.getSuperclass();
            }
        }
        GrailsDomainClassProperty[] props = domainClass.getPersistentProperties();
        for (GrailsDomainClassProperty prop : props) {
            if (prop != null && prop.isAssociation()) {
                GrailsDomainClass referencedGrailsDomainClass = (GrailsDomainClass) domainMap.get(prop.getReferencedPropertyType().getName());
                prop.setReferencedDomainClass(referencedGrailsDomainClass);
            }
        }
    }
    // now configure so that the 'other side' of a property can be resolved by the property itself
    for (GrailsClass domainClass1 : domainClasses) {
        GrailsDomainClass domainClass = (GrailsDomainClass) domainClass1;
        GrailsDomainClassProperty[] props = domainClass.getPersistentProperties();
        for (GrailsDomainClassProperty prop : props) {
            if (prop == null || !prop.isAssociation()) {
                continue;
            }
            GrailsDomainClass referenced = prop.getReferencedDomainClass();
            if (referenced == null) {
                continue;
            }
            boolean isOwnedBy = referenced.isOwningClass(domainClass.getClazz());
            prop.setOwningSide(isOwnedBy);
            String refPropertyName = null;
            try {
                refPropertyName = prop.getReferencedPropertyName();
            } catch (UnsupportedOperationException e) {
            // ignore (to support Hibernate entities)
            }
            if (!StringUtils.hasText(refPropertyName)) {
                GrailsDomainClassProperty[] referencedProperties = referenced.getPersistentProperties();
                for (GrailsDomainClassProperty referencedProp : referencedProperties) {
                    // to be equal to self
                    if (prop.equals(referencedProp) && prop.isBidirectional()) {
                        continue;
                    }
                    if (isCandidateForOtherSide(domainClass, prop, referencedProp)) {
                        prop.setOtherSide(referencedProp);
                        break;
                    }
                }
            } else {
                GrailsDomainClassProperty otherSide = referenced.getPropertyByName(refPropertyName);
                prop.setOtherSide(otherSide);
                otherSide.setOtherSide(prop);
            }
        }
    }
}
Also used : GrailsClass(grails.core.GrailsClass) GrailsDomainClass(grails.core.GrailsDomainClass) GrailsDomainClassProperty(grails.core.GrailsDomainClassProperty)

Example 9 with GrailsDomainClassProperty

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

the class DomainClassMarshaller method marshalObject.

@SuppressWarnings({ "unchecked", "rawtypes" })
public void marshalObject(Object value, XML xml) throws ConverterException {
    Class clazz = value.getClass();
    List<String> excludes = xml.getExcludes(clazz);
    List<String> includes = xml.getIncludes(clazz);
    IncludeExcludeSupport<String> includeExcludeSupport = new IncludeExcludeSupport<String>();
    GrailsDomainClass domainClass = (GrailsDomainClass) application.getArtefact(DomainClassArtefactHandler.TYPE, ConverterUtil.trimProxySuffix(clazz.getName()));
    BeanWrapper beanWrapper = new BeanWrapperImpl(value);
    GrailsDomainClassProperty id = domainClass.getIdentifier();
    if (shouldInclude(includeExcludeSupport, includes, excludes, value, id.getName())) {
        Object idValue = beanWrapper.getPropertyValue(id.getName());
        if (idValue != null)
            xml.attribute("id", String.valueOf(idValue));
    }
    if (shouldInclude(includeExcludeSupport, includes, excludes, value, GrailsDomainClassProperty.VERSION) && includeVersion) {
        Object versionValue = beanWrapper.getPropertyValue(domainClass.getVersion().getName());
        xml.attribute("version", String.valueOf(versionValue));
    }
    GrailsDomainClassProperty[] properties = domainClass.getPersistentProperties();
    for (GrailsDomainClassProperty property : properties) {
        String propertyName = property.getName();
        if (!shouldInclude(includeExcludeSupport, includes, excludes, value, property.getName()))
            continue;
        xml.startNode(propertyName);
        if (!property.isAssociation()) {
            // Write non-relation property
            Object val = beanWrapper.getPropertyValue(propertyName);
            xml.convertAnother(val);
        } else {
            if (isRenderDomainClassRelations()) {
                Object referenceObject = beanWrapper.getPropertyValue(propertyName);
                if (referenceObject != null && shouldInitializeProxy(referenceObject)) {
                    referenceObject = proxyHandler.unwrapIfProxy(referenceObject);
                    if (referenceObject instanceof SortedMap) {
                        referenceObject = new TreeMap((SortedMap) referenceObject);
                    } else if (referenceObject instanceof SortedSet) {
                        referenceObject = new TreeSet((SortedSet) referenceObject);
                    } else if (referenceObject instanceof Set) {
                        referenceObject = new LinkedHashSet((Set) referenceObject);
                    } else if (referenceObject instanceof Map) {
                        referenceObject = new LinkedHashMap((Map) referenceObject);
                    } else if (referenceObject instanceof Collection) {
                        referenceObject = new ArrayList((Collection) referenceObject);
                    }
                    xml.convertAnother(referenceObject);
                }
            } else {
                Object referenceObject = beanWrapper.getPropertyValue(propertyName);
                if (referenceObject != null) {
                    GrailsDomainClass referencedDomainClass = property.getReferencedDomainClass();
                    // Embedded are now always fully rendered
                    if (referencedDomainClass == null || property.isEmbedded() || property.getType().isEnum()) {
                        xml.convertAnother(referenceObject);
                    } else if (property.isOneToOne() || property.isManyToOne() || property.isEmbedded()) {
                        asShortObject(referenceObject, xml, referencedDomainClass.getIdentifier(), referencedDomainClass);
                    } else {
                        GrailsDomainClassProperty referencedIdProperty = referencedDomainClass.getIdentifier();
                        @SuppressWarnings("unused") String refPropertyName = referencedDomainClass.getPropertyName();
                        if (referenceObject instanceof Collection) {
                            Collection o = (Collection) referenceObject;
                            for (Object el : o) {
                                xml.startNode(xml.getElementName(el));
                                asShortObject(el, xml, referencedIdProperty, referencedDomainClass);
                                xml.end();
                            }
                        } else if (referenceObject instanceof Map) {
                            Map<Object, Object> map = (Map<Object, Object>) referenceObject;
                            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                                String key = String.valueOf(entry.getKey());
                                Object o = entry.getValue();
                                xml.startNode("entry").attribute("key", key);
                                asShortObject(o, xml, referencedIdProperty, referencedDomainClass);
                                xml.end();
                            }
                        }
                    }
                }
            }
        }
        xml.end();
    }
}
Also used : BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) GrailsDomainClass(grails.core.GrailsDomainClass) GrailsDomainClassProperty(grails.core.GrailsDomainClassProperty) IncludeExcludeSupport(org.grails.core.util.IncludeExcludeSupport) BeanWrapper(org.springframework.beans.BeanWrapper) GrailsDomainClass(grails.core.GrailsDomainClass)

Example 10 with GrailsDomainClassProperty

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

the class DomainClassMarshaller method marshalObject.

@SuppressWarnings({ "unchecked", "rawtypes" })
public void marshalObject(Object value, JSON json) throws ConverterException {
    JSONWriter writer = json.getWriter();
    value = proxyHandler.unwrapIfProxy(value);
    Class<?> clazz = value.getClass();
    List<String> excludes = json.getExcludes(clazz);
    List<String> includes = json.getIncludes(clazz);
    IncludeExcludeSupport<String> includeExcludeSupport = new IncludeExcludeSupport<String>();
    GrailsDomainClass domainClass = (GrailsDomainClass) application.getArtefact(DomainClassArtefactHandler.TYPE, ConverterUtil.trimProxySuffix(clazz.getName()));
    BeanWrapper beanWrapper = new BeanWrapperImpl(value);
    writer.object();
    if (includeClass && shouldInclude(includeExcludeSupport, includes, excludes, value, "class")) {
        writer.key("class").value(domainClass.getClazz().getName());
    }
    GrailsDomainClassProperty id = domainClass.getIdentifier();
    if (shouldInclude(includeExcludeSupport, includes, excludes, value, id.getName())) {
        Object idValue = extractValue(value, id);
        if (idValue != null) {
            json.property(GrailsDomainClassProperty.IDENTITY, idValue);
        }
    }
    if (shouldInclude(includeExcludeSupport, includes, excludes, value, GrailsDomainClassProperty.VERSION) && isIncludeVersion()) {
        GrailsDomainClassProperty versionProperty = domainClass.getVersion();
        Object version = extractValue(value, versionProperty);
        if (version != null) {
            json.property(GrailsDomainClassProperty.VERSION, version);
        }
    }
    GrailsDomainClassProperty[] properties = domainClass.getPersistentProperties();
    for (GrailsDomainClassProperty property : properties) {
        if (!shouldInclude(includeExcludeSupport, includes, excludes, value, property.getName()))
            continue;
        writer.key(property.getName());
        if (!property.isAssociation()) {
            // Write non-relation property
            Object val = beanWrapper.getPropertyValue(property.getName());
            json.convertAnother(val);
        } else {
            Object referenceObject = beanWrapper.getPropertyValue(property.getName());
            if (isRenderDomainClassRelations()) {
                if (referenceObject == null) {
                    writer.valueNull();
                } else {
                    referenceObject = proxyHandler.unwrapIfProxy(referenceObject);
                    if (referenceObject instanceof SortedMap) {
                        referenceObject = new TreeMap((SortedMap) referenceObject);
                    } else if (referenceObject instanceof SortedSet) {
                        referenceObject = new TreeSet((SortedSet) referenceObject);
                    } else if (referenceObject instanceof Set) {
                        referenceObject = new LinkedHashSet((Set) referenceObject);
                    } else if (referenceObject instanceof Map) {
                        referenceObject = new LinkedHashMap((Map) referenceObject);
                    } else if (referenceObject instanceof Collection) {
                        referenceObject = new ArrayList((Collection) referenceObject);
                    }
                    json.convertAnother(referenceObject);
                }
            } else {
                if (referenceObject == null) {
                    json.value(null);
                } else {
                    GrailsDomainClass referencedDomainClass = property.getReferencedDomainClass();
                    // Embedded are now always fully rendered
                    if (referencedDomainClass == null || property.isEmbedded() || property.getType().isEnum()) {
                        json.convertAnother(referenceObject);
                    } else if (property.isOneToOne() || property.isManyToOne() || property.isEmbedded()) {
                        asShortObject(referenceObject, json, referencedDomainClass.getIdentifier(), referencedDomainClass);
                    } else {
                        GrailsDomainClassProperty referencedIdProperty = referencedDomainClass.getIdentifier();
                        @SuppressWarnings("unused") String refPropertyName = referencedDomainClass.getPropertyName();
                        if (referenceObject instanceof Collection) {
                            Collection o = (Collection) referenceObject;
                            writer.array();
                            for (Object el : o) {
                                asShortObject(el, json, referencedIdProperty, referencedDomainClass);
                            }
                            writer.endArray();
                        } else if (referenceObject instanceof Map) {
                            Map<Object, Object> map = (Map<Object, Object>) referenceObject;
                            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                                String key = String.valueOf(entry.getKey());
                                Object o = entry.getValue();
                                writer.object();
                                writer.key(key);
                                asShortObject(o, json, referencedIdProperty, referencedDomainClass);
                                writer.endObject();
                            }
                        }
                    }
                }
            }
        }
    }
    writer.endObject();
}
Also used : JSONWriter(org.grails.web.json.JSONWriter) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) GrailsDomainClass(grails.core.GrailsDomainClass) GrailsDomainClassProperty(grails.core.GrailsDomainClassProperty) IncludeExcludeSupport(org.grails.core.util.IncludeExcludeSupport) BeanWrapper(org.springframework.beans.BeanWrapper) GroovyObject(groovy.lang.GroovyObject)

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