Search in sources :

Example 1 with JacksonXmlElementWrapper

use of com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper in project dhis2-core by dhis2.

the class Jackson2PropertyIntrospectorService method scanClass.

@Override
protected Map<String, Property> scanClass(Class<?> clazz) {
    Map<String, Property> propertyMap = Maps.newHashMap();
    Map<String, Property> hibernatePropertyMap = getPropertiesFromHibernate(clazz);
    List<String> classFieldNames = ReflectionUtils.getAllFieldNames(clazz);
    // TODO this is quite nasty, should find a better way of exposing properties at class-level
    if (AnnotationUtils.isAnnotationPresent(clazz, JacksonXmlRootElement.class)) {
        Property property = new Property();
        JacksonXmlRootElement jacksonXmlRootElement = AnnotationUtils.getAnnotation(clazz, JacksonXmlRootElement.class);
        if (!StringUtils.isEmpty(jacksonXmlRootElement.localName())) {
            property.setName(jacksonXmlRootElement.localName());
        }
        if (!StringUtils.isEmpty(jacksonXmlRootElement.namespace())) {
            property.setNamespace(jacksonXmlRootElement.namespace());
        }
        propertyMap.put("__self__", property);
    }
    List<Property> properties = collectProperties(clazz);
    for (Property property : properties) {
        Method getterMethod = property.getGetterMethod();
        JsonProperty jsonProperty = AnnotationUtils.getAnnotation(getterMethod, JsonProperty.class);
        String fieldName = getFieldName(getterMethod);
        property.setName(!StringUtils.isEmpty(jsonProperty.value()) ? jsonProperty.value() : fieldName);
        if (property.getGetterMethod() != null) {
            property.setReadable(true);
        }
        if (property.getSetterMethod() != null) {
            property.setWritable(true);
        }
        if (classFieldNames.contains(fieldName)) {
            property.setFieldName(fieldName);
        }
        if (hibernatePropertyMap.containsKey(fieldName)) {
            Property hibernateProperty = hibernatePropertyMap.get(fieldName);
            property.setPersisted(true);
            property.setWritable(true);
            property.setUnique(hibernateProperty.isUnique());
            property.setRequired(hibernateProperty.isRequired());
            property.setLength(hibernateProperty.getLength());
            property.setMax(hibernateProperty.getMax());
            property.setMin(hibernateProperty.getMin());
            property.setCollection(hibernateProperty.isCollection());
            property.setCascade(hibernateProperty.getCascade());
            property.setOwner(hibernateProperty.isOwner());
            property.setManyToMany(hibernateProperty.isManyToMany());
            property.setOneToOne(hibernateProperty.isOneToOne());
            property.setManyToOne(hibernateProperty.isManyToOne());
            property.setOwningRole(hibernateProperty.getOwningRole());
            property.setInverseRole(hibernateProperty.getInverseRole());
            property.setGetterMethod(hibernateProperty.getGetterMethod());
            property.setSetterMethod(hibernateProperty.getSetterMethod());
        }
        if (AnnotationUtils.isAnnotationPresent(property.getGetterMethod(), Description.class)) {
            Description description = AnnotationUtils.getAnnotation(property.getGetterMethod(), Description.class);
            property.setDescription(description.value());
        }
        if (AnnotationUtils.isAnnotationPresent(property.getGetterMethod(), JacksonXmlProperty.class)) {
            JacksonXmlProperty jacksonXmlProperty = AnnotationUtils.getAnnotation(getterMethod, JacksonXmlProperty.class);
            if (StringUtils.isEmpty(jacksonXmlProperty.localName())) {
                property.setName(property.getName());
            } else {
                property.setName(jacksonXmlProperty.localName());
            }
            if (!StringUtils.isEmpty(jacksonXmlProperty.namespace())) {
                property.setNamespace(jacksonXmlProperty.namespace());
            }
            property.setAttribute(jacksonXmlProperty.isAttribute());
        }
        Class<?> returnType = property.getGetterMethod().getReturnType();
        property.setKlass(Primitives.wrap(returnType));
        if (Collection.class.isAssignableFrom(returnType)) {
            property.setCollection(true);
            property.setCollectionName(property.getName());
            property.setOrdered(List.class.isAssignableFrom(returnType));
            Type type = property.getGetterMethod().getGenericReturnType();
            if (ParameterizedType.class.isInstance(type)) {
                ParameterizedType parameterizedType = (ParameterizedType) type;
                Class<?> klass = (Class<?>) parameterizedType.getActualTypeArguments()[0];
                property.setItemKlass(Primitives.wrap(klass));
                if (collectProperties(klass).isEmpty()) {
                    property.setSimple(true);
                }
                property.setIdentifiableObject(IdentifiableObject.class.isAssignableFrom(klass));
                property.setNameableObject(NameableObject.class.isAssignableFrom(klass));
                property.setEmbeddedObject(EmbeddedObject.class.isAssignableFrom(klass));
            }
        } else {
            if (collectProperties(returnType).isEmpty()) {
                property.setSimple(true);
            }
        }
        if (property.isCollection()) {
            if (AnnotationUtils.isAnnotationPresent(property.getGetterMethod(), JacksonXmlElementWrapper.class)) {
                JacksonXmlElementWrapper jacksonXmlElementWrapper = AnnotationUtils.getAnnotation(getterMethod, JacksonXmlElementWrapper.class);
                property.setCollectionWrapping(jacksonXmlElementWrapper.useWrapping());
                // TODO what if element-wrapper have different namespace?
                if (!StringUtils.isEmpty(jacksonXmlElementWrapper.localName())) {
                    property.setCollectionName(jacksonXmlElementWrapper.localName());
                }
            }
            propertyMap.put(property.getCollectionName(), property);
        } else {
            propertyMap.put(property.getName(), property);
        }
        if (Enum.class.isAssignableFrom(property.getKlass())) {
            Object[] enumConstants = property.getKlass().getEnumConstants();
            List<String> enumValues = new ArrayList<>();
            for (Object value : enumConstants) {
                enumValues.add(value.toString());
            }
            property.setConstants(enumValues);
        }
        SchemaUtils.updatePropertyTypes(property);
    }
    return propertyMap;
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Description(org.hisp.dhis.common.annotation.Description) JacksonXmlRootElement(com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement) ArrayList(java.util.ArrayList) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) Method(java.lang.reflect.Method) JacksonXmlElementWrapper(com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper) JacksonXmlProperty(com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) NameableObject(org.hisp.dhis.common.NameableObject) ArrayList(java.util.ArrayList) List(java.util.List) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) NameableObject(org.hisp.dhis.common.NameableObject) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) JacksonXmlProperty(com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty)

Aggregations

JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 JacksonXmlElementWrapper (com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper)1 JacksonXmlProperty (com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty)1 JacksonXmlRootElement (com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 EmbeddedObject (org.hisp.dhis.common.EmbeddedObject)1 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)1 NameableObject (org.hisp.dhis.common.NameableObject)1 Description (org.hisp.dhis.common.annotation.Description)1