Search in sources :

Example 11 with GrailsDomainClass

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

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

the class HeirarchyDomainClassTests method testClassHeirarchy.

public void testClassHeirarchy() throws Exception {
    GroovyClassLoader gcl = new GroovyClassLoader();
    gcl.parseClass("@grails.persistence.Entity class Super { Long id;Long version;}\n" + "class Sub extends Super { }\n" + "class Sub2 extends Sub { }");
    GrailsApplication ga = new DefaultGrailsApplication(gcl.getLoadedClasses(), gcl);
    ga.initialise();
    new MappingContextBuilder(ga).build(gcl.getLoadedClasses());
    GrailsDomainClass gdc1 = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "Super");
    assertNotNull(gdc1);
    assertTrue(gdc1.isRoot());
    assertEquals(2, gdc1.getSubClasses().size());
    assertFalse(gdc1.getPropertyByName("id").isInherited());
    GrailsDomainClass gdc2 = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "Sub");
    assertFalse(gdc2.isRoot());
    assertEquals(1, gdc2.getSubClasses().size());
    assertTrue(gdc2.getPropertyByName("id").isInherited());
    GrailsDomainClass gdc3 = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "Sub2");
    assertFalse(gdc3.isRoot());
    assertEquals(0, gdc3.getSubClasses().size());
    assertTrue(gdc3.getPropertyByName("id").isInherited());
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) GrailsApplication(grails.core.GrailsApplication) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GrailsDomainClass(grails.core.GrailsDomainClass) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) MappingContextBuilder(org.grails.core.support.MappingContextBuilder)

Example 13 with GrailsDomainClass

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

the class DefaultGrailsDomainClass method getPersistentProperty.

public GrailsDomainClassProperty getPersistentProperty(String name) {
    initializePersistentProperties();
    if (propertyMap.containsKey(name)) {
        return propertyMap.get(name);
    }
    int indexOfDot = name.indexOf('.');
    if (indexOfDot > 0) {
        String basePropertyName = name.substring(0, indexOfDot);
        if (propertyMap.containsKey(basePropertyName)) {
            verifyContextIsInitialized();
            PersistentProperty prop = persistentEntity.getPropertyByName(basePropertyName);
            PersistentEntity referencedEntity = prop.getOwner();
            GrailsDomainClass referencedDomainClass = (GrailsDomainClass) grailsApplication.getArtefact(DomainClassArtefactHandler.TYPE, referencedEntity.getName());
            if (referencedDomainClass != null) {
                String restOfPropertyName = name.substring(indexOfDot + 1);
                return referencedDomainClass.getPropertyByName(restOfPropertyName);
            }
        }
    }
    return null;
}
Also used : PersistentEntity(org.grails.datastore.mapping.model.PersistentEntity) GrailsDomainClass(grails.core.GrailsDomainClass) PersistentProperty(org.grails.datastore.mapping.model.PersistentProperty)

Example 14 with GrailsDomainClass

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

the class ConstraintsEvaluatingPropertyTests method ensureConstraintsPresent.

@SuppressWarnings("rawtypes")
private void ensureConstraintsPresent(String[] classSource, int classIndexToTest, int constraintCount) throws Exception {
    // We need to do a real test here to make sure
    GroovyClassLoader gcl = new GroovyClassLoader();
    Class[] classes = new Class[classSource.length];
    for (int i = 0; i < classSource.length; i++) {
        classes[i] = gcl.parseClass(classSource[i]);
    }
    GrailsApplication ga = new DefaultGrailsApplication(classes[classIndexToTest]);
    ga.initialise();
    new MappingContextBuilder(ga).build(classes[classIndexToTest]);
    GrailsDomainClass domainClass = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, classes[classIndexToTest].getName());
    Map constraints = domainClass.getConstrainedProperties();
    grails.gorm.validation.ConstrainedProperty p = (grails.gorm.validation.ConstrainedProperty) constraints.get("name");
    Collection cons = p.getAppliedConstraints();
    assertEquals("Incorrect number of constraints extracted: " + constraints, constraintCount, cons.size());
}
Also used : GrailsDomainClass(grails.core.GrailsDomainClass) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GroovyClassLoader(groovy.lang.GroovyClassLoader) GrailsApplication(grails.core.GrailsApplication) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) Collection(java.util.Collection) GrailsDomainClass(grails.core.GrailsDomainClass) Map(java.util.Map) grails.gorm.validation(grails.gorm.validation)

Example 15 with GrailsDomainClass

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

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