Search in sources :

Example 1 with IncludeExcludeSupport

use of org.grails.core.util.IncludeExcludeSupport 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 2 with IncludeExcludeSupport

use of org.grails.core.util.IncludeExcludeSupport in project grails-core by grails.

the class GenericJavaBeanMarshaller method marshalObject.

public void marshalObject(Object o, JSON json) throws ConverterException {
    JSONWriter writer = json.getWriter();
    Class<? extends Object> clazz = o.getClass();
    List<String> excludes = json.getExcludes(clazz);
    List<String> includes = json.getIncludes(clazz);
    IncludeExcludeSupport<String> includeExcludeSupport = new IncludeExcludeSupport<String>();
    try {
        writer.object();
        for (PropertyDescriptor property : BeanUtils.getPropertyDescriptors(o.getClass())) {
            String name = property.getName();
            Method readMethod = property.getReadMethod();
            if (!shouldInclude(includeExcludeSupport, includes, excludes, o, name))
                continue;
            if (readMethod != null && !(name.equals("metaClass")) && !(name.equals("class"))) {
                if (readMethod.getAnnotation(PersistenceMethod.class) != null)
                    continue;
                if (readMethod.getAnnotation(ControllerMethod.class) != null)
                    continue;
                Object value = readMethod.invoke(o, (Object[]) null);
                writer.key(name);
                json.convertAnother(value);
            }
        }
        for (Field field : o.getClass().getDeclaredFields()) {
            int modifiers = field.getModifiers();
            if (field.isAccessible() && Modifier.isPublic(modifiers) && !(Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))) {
                String name = field.getName();
                if (!shouldInclude(includeExcludeSupport, includes, excludes, o, name))
                    continue;
                writer.key(field.getName());
                json.convertAnother(field.get(o));
            }
        }
        writer.endObject();
    } catch (ConverterException ce) {
        throw ce;
    } catch (Exception e) {
        throw new ConverterException("Error converting Bean with class " + o.getClass().getName(), e);
    }
}
Also used : JSONWriter(org.grails.web.json.JSONWriter) ConverterException(org.grails.web.converters.exceptions.ConverterException) PropertyDescriptor(java.beans.PropertyDescriptor) ControllerMethod(grails.web.controllers.ControllerMethod) PersistenceMethod(grails.persistence.PersistenceMethod) Method(java.lang.reflect.Method) IncludeExcludeSupport(org.grails.core.util.IncludeExcludeSupport) ConverterException(org.grails.web.converters.exceptions.ConverterException) Field(java.lang.reflect.Field) PersistenceMethod(grails.persistence.PersistenceMethod) ControllerMethod(grails.web.controllers.ControllerMethod)

Example 3 with IncludeExcludeSupport

use of org.grails.core.util.IncludeExcludeSupport 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)

Example 4 with IncludeExcludeSupport

use of org.grails.core.util.IncludeExcludeSupport in project grails-core by grails.

the class GroovyBeanMarshaller method marshalObject.

public void marshalObject(Object o, JSON json) throws ConverterException {
    JSONWriter writer = json.getWriter();
    Class<? extends Object> clazz = o.getClass();
    List<String> excludes = json.getExcludes(clazz);
    List<String> includes = json.getIncludes(clazz);
    IncludeExcludeSupport<String> includeExcludeSupport = new IncludeExcludeSupport<String>();
    try {
        writer.object();
        for (PropertyDescriptor property : BeanUtils.getPropertyDescriptors(clazz)) {
            Method readMethod = property.getReadMethod();
            String name = property.getName();
            if (!shouldInclude(includeExcludeSupport, includes, excludes, o, name))
                continue;
            if (readMethod != null && !(name.equals("metaClass")) && !(name.equals("class"))) {
                if (readMethod.getAnnotation(PersistenceMethod.class) != null)
                    continue;
                if (readMethod.getAnnotation(ControllerMethod.class) != null)
                    continue;
                Object value = readMethod.invoke(o, (Object[]) null);
                writer.key(name);
                json.convertAnother(value);
            }
        }
        for (Field field : clazz.getDeclaredFields()) {
            int modifiers = field.getModifiers();
            if (Modifier.isPublic(modifiers) && !(Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))) {
                String name = field.getName();
                if (!shouldInclude(includeExcludeSupport, includes, excludes, o, name))
                    continue;
                writer.key(name);
                json.convertAnother(field.get(o));
            }
        }
        writer.endObject();
    } catch (ConverterException ce) {
        throw ce;
    } catch (Exception e) {
        throw new ConverterException("Error converting Bean with class " + clazz.getName(), e);
    }
}
Also used : JSONWriter(org.grails.web.json.JSONWriter) ConverterException(org.grails.web.converters.exceptions.ConverterException) PropertyDescriptor(java.beans.PropertyDescriptor) ControllerMethod(grails.web.controllers.ControllerMethod) PersistenceMethod(grails.persistence.PersistenceMethod) Method(java.lang.reflect.Method) IncludeExcludeSupport(org.grails.core.util.IncludeExcludeSupport) ConverterException(org.grails.web.converters.exceptions.ConverterException) Field(java.lang.reflect.Field) PersistenceMethod(grails.persistence.PersistenceMethod) GroovyObject(groovy.lang.GroovyObject) ControllerMethod(grails.web.controllers.ControllerMethod)

Example 5 with IncludeExcludeSupport

use of org.grails.core.util.IncludeExcludeSupport in project grails-core by grails.

the class GroovyBeanMarshaller method marshalObject.

public void marshalObject(Object o, XML xml) throws ConverterException {
    try {
        Class<? extends Object> clazz = o.getClass();
        List<String> excludes = xml.getExcludes(clazz);
        List<String> includes = xml.getIncludes(clazz);
        IncludeExcludeSupport<String> includeExcludeSupport = new IncludeExcludeSupport<String>();
        boolean isEntity = o.getClass().getAnnotation(Entity.class) != null;
        for (PropertyDescriptor property : BeanUtils.getPropertyDescriptors(o.getClass())) {
            String name = property.getName();
            if (!shouldInclude(includeExcludeSupport, includes, excludes, o, name))
                continue;
            if (isEntity && (name.equals(GrailsDomainClassProperty.ATTACHED) || name.equals(GrailsDomainClassProperty.ERRORS)))
                continue;
            Method readMethod = property.getReadMethod();
            if (readMethod != null && !(name.equals("metaClass")) && !(name.equals("class"))) {
                if (readMethod.getAnnotation(PersistenceMethod.class) != null)
                    continue;
                if (readMethod.getAnnotation(ControllerMethod.class) != null)
                    continue;
                Object value = readMethod.invoke(o, (Object[]) null);
                xml.startNode(name);
                xml.convertAnother(value);
                xml.end();
            }
        }
        for (Field field : o.getClass().getDeclaredFields()) {
            int modifiers = field.getModifiers();
            if (Modifier.isPublic(modifiers) && !(Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))) {
                String name = field.getName();
                if (!shouldInclude(includeExcludeSupport, includes, excludes, o, name))
                    continue;
                if (isEntity && (name.equals(GrailsDomainClassProperty.ATTACHED) || name.equals(GrailsDomainClassProperty.ERRORS)))
                    continue;
                xml.startNode(name);
                xml.convertAnother(field.get(o));
                xml.end();
            }
        }
    } catch (ConverterException ce) {
        throw ce;
    } catch (Exception e) {
        throw new ConverterException("Error converting Bean with class " + o.getClass().getName(), e);
    }
}
Also used : Entity(grails.persistence.Entity) ConverterException(org.grails.web.converters.exceptions.ConverterException) PropertyDescriptor(java.beans.PropertyDescriptor) ControllerMethod(grails.web.controllers.ControllerMethod) PersistenceMethod(grails.persistence.PersistenceMethod) Method(java.lang.reflect.Method) IncludeExcludeSupport(org.grails.core.util.IncludeExcludeSupport) ConverterException(org.grails.web.converters.exceptions.ConverterException) Field(java.lang.reflect.Field) PersistenceMethod(grails.persistence.PersistenceMethod) GroovyObject(groovy.lang.GroovyObject) ControllerMethod(grails.web.controllers.ControllerMethod)

Aggregations

IncludeExcludeSupport (org.grails.core.util.IncludeExcludeSupport)5 PersistenceMethod (grails.persistence.PersistenceMethod)3 ControllerMethod (grails.web.controllers.ControllerMethod)3 GroovyObject (groovy.lang.GroovyObject)3 PropertyDescriptor (java.beans.PropertyDescriptor)3 Field (java.lang.reflect.Field)3 Method (java.lang.reflect.Method)3 ConverterException (org.grails.web.converters.exceptions.ConverterException)3 JSONWriter (org.grails.web.json.JSONWriter)3 GrailsDomainClass (grails.core.GrailsDomainClass)2 GrailsDomainClassProperty (grails.core.GrailsDomainClassProperty)2 BeanWrapper (org.springframework.beans.BeanWrapper)2 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)2 Entity (grails.persistence.Entity)1