Search in sources :

Example 66 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class JacksonPropertyIntrospector method introspect.

@Override
public void introspect(Class<?> klass, Map<String, Property> properties) {
    Map<String, Property> persistedProperties = new HashMap<>(properties);
    properties.clear();
    Set<String> classFieldNames = ReflectionUtils.getAllFieldNames(klass);
    // properties at class-level
    if (isAnnotationPresent(klass, JacksonXmlRootElement.class) || isAnnotationPresent(klass, JsonRootName.class)) {
        properties.put(SchemaService.PROPERTY_SCHEMA, createSchemaProperty(klass));
    }
    for (Property property : collectProperties(klass)) {
        String fieldName = initFromJsonProperty(property);
        if (classFieldNames.contains(fieldName)) {
            property.setFieldName(fieldName);
        }
        if (persistedProperties.containsKey(fieldName)) {
            initFromPersistedProperty(property, persistedProperties.get(fieldName));
        }
        initFromDescription(property);
        initFromJacksonXmlProperty(property);
        initCollectionProperty(property);
        Method getterMethod = property.getGetterMethod();
        if (getterMethod != null && !property.isCollection() && !hasProperties(getterMethod.getReturnType())) {
            property.setSimple(true);
        }
        initFromJacksonXmlElementWrapper(property);
        initFromEnumConstants(property);
        properties.put(property.key(), property);
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JacksonXmlRootElement(com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement) Method(java.lang.reflect.Method) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) JacksonXmlProperty(com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty) Property(org.hisp.dhis.schema.Property) JsonRootName(com.fasterxml.jackson.annotation.JsonRootName)

Example 67 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class JacksonPropertyIntrospector method createSchemaProperty.

private static Property createSchemaProperty(Class<?> klass) {
    Property schemaProperty = new Property();
    schemaProperty.setAnnotations(getAnnotations(klass.getAnnotations()));
    if (isAnnotationPresent(klass, JsonRootName.class)) {
        JsonRootName jsonRootName = getAnnotation(klass, JsonRootName.class);
        if (!isEmpty(jsonRootName.value())) {
            schemaProperty.setName(jsonRootName.value());
        }
        if (!isEmpty(jsonRootName.namespace())) {
            schemaProperty.setNamespace(jsonRootName.namespace());
        }
    } else if (isAnnotationPresent(klass, JacksonXmlRootElement.class)) {
        JacksonXmlRootElement jacksonXmlRootElement = getAnnotation(klass, JacksonXmlRootElement.class);
        if (!isEmpty(jacksonXmlRootElement.localName())) {
            schemaProperty.setName(jacksonXmlRootElement.localName());
        }
        if (!isEmpty(jacksonXmlRootElement.namespace())) {
            schemaProperty.setNamespace(jacksonXmlRootElement.namespace());
        }
    }
    return schemaProperty;
}
Also used : JacksonXmlRootElement(com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) JacksonXmlProperty(com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty) Property(org.hisp.dhis.schema.Property) JsonRootName(com.fasterxml.jackson.annotation.JsonRootName)

Example 68 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class JacksonPropertyIntrospector method collectProperties.

private static List<Property> collectProperties(Class<?> klass) {
    boolean isPrimitiveOrWrapped = ClassUtils.isPrimitiveOrWrapper(klass);
    if (isPrimitiveOrWrapped) {
        return Collections.emptyList();
    }
    List<Field> fields = ReflectionUtils.findFields(klass, f -> f.isAnnotationPresent(JsonProperty.class));
    List<Method> methods = ReflectionUtils.findMethods(klass, m -> AnnotationUtils.findAnnotation(m, JsonProperty.class) != null && m.getParameterTypes().length == 0);
    Multimap<String, Method> multimap = ReflectionUtils.getMethodsMultimap(klass);
    Map<String, Property> propertyMap = new HashMap<>();
    for (var field : fields) {
        Property property = new Property(klass, null, null);
        property.setAnnotations(getAnnotations(field.getAnnotations()));
        JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class);
        String fieldName = field.getName();
        String name = StringUtils.isEmpty(requireNonNull(jsonProperty).value()) ? fieldName : jsonProperty.value();
        property.setName(name);
        property.setFieldName(fieldName);
        property.setSetterMethod(ReflectionUtils.findSetterMethod(fieldName, klass));
        property.setGetterMethod(ReflectionUtils.findGetterMethod(fieldName, klass));
        property.setNamespace(trimToNull(jsonProperty.namespace()));
        propertyMap.put(name, property);
    }
    for (var method : methods) {
        JsonProperty jsonProperty = AnnotationUtils.findAnnotation(method, JsonProperty.class);
        String fieldName = ReflectionUtils.getFieldName(method);
        String name = StringUtils.isEmpty(requireNonNull(jsonProperty).value()) ? fieldName : jsonProperty.value();
        if (propertyMap.containsKey(name)) {
            continue;
        }
        Property property = new Property(klass, method, null);
        property.setAnnotations(getAnnotations(method.getAnnotations()));
        property.setName(name);
        property.setFieldName(fieldName);
        property.setNamespace(trimToNull(jsonProperty.namespace()));
        propertyMap.put(name, property);
        String setterName = "set" + capitalize(fieldName);
        if (multimap.containsKey(setterName)) {
            property.setSetterMethod(multimap.get(setterName).iterator().next());
        }
        propertyMap.put(name, property);
    }
    return new ArrayList<>(propertyMap.values());
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) JacksonXmlProperty(com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty) Property(org.hisp.dhis.schema.Property)

Example 69 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class HibernatePropertyIntrospector method createProperty.

private Property createProperty(Class<?> klass, org.hibernate.mapping.Property hibernateProperty, MetamodelImplementor metamodelImplementor) {
    Property property = new Property(klass);
    property.setRequired(false);
    property.setPersisted(true);
    property.setWritable(true);
    property.setOwner(true);
    Type type = hibernateProperty.getType();
    property.setName(hibernateProperty.getName());
    property.setFieldName(hibernateProperty.getName());
    property.setCascade(hibernateProperty.getCascade());
    property.setCollection(type.isCollectionType());
    property.setSetterMethod(hibernateProperty.getSetter(klass).getMethod());
    property.setGetterMethod(hibernateProperty.getGetter(klass).getMethod());
    if (property.isCollection()) {
        initCollectionProperty(metamodelImplementor, property, (CollectionType) type);
    }
    if (type instanceof SingleColumnType || type instanceof CustomType || type instanceof ManyToOneType) {
        Column column = (Column) hibernateProperty.getColumnIterator().next();
        property.setUnique(column.isUnique());
        property.setRequired(!column.isNullable());
        property.setMin(0d);
        property.setMax((double) column.getLength());
        property.setLength(column.getLength());
        if (type instanceof TextType) {
            property.setMin(0d);
            property.setMax((double) Integer.MAX_VALUE);
            property.setLength(Integer.MAX_VALUE);
        } else if (type instanceof IntegerType) {
            property.setMin((double) Integer.MIN_VALUE);
            property.setMax((double) Integer.MAX_VALUE);
            property.setLength(Integer.MAX_VALUE);
        } else if (type instanceof LongType) {
            property.setMin((double) Long.MIN_VALUE);
            property.setMax((double) Long.MAX_VALUE);
            property.setLength(Integer.MAX_VALUE);
        } else if (type instanceof DoubleType) {
            property.setMin(-Double.MAX_VALUE);
            property.setMax(Double.MAX_VALUE);
            property.setLength(Integer.MAX_VALUE);
        }
    }
    if (type instanceof ManyToOneType) {
        property.setManyToOne(true);
        property.setRequired(property.isRequired() && !property.isCollection());
        if (property.isOwner()) {
            property.setOwningRole(klass.getName() + "." + property.getName());
        } else {
            property.setInverseRole(klass.getName() + "." + property.getName());
        }
    } else if (type instanceof OneToOneType) {
        property.setOneToOne(true);
    } else if (type instanceof OneToMany) {
        property.setOneToMany(true);
    }
    return property;
}
Also used : CustomType(org.hibernate.type.CustomType) IntegerType(org.hibernate.type.IntegerType) CustomType(org.hibernate.type.CustomType) CollectionType(org.hibernate.type.CollectionType) ManyToOneType(org.hibernate.type.ManyToOneType) IntegerType(org.hibernate.type.IntegerType) TextType(org.hibernate.type.TextType) LongType(org.hibernate.type.LongType) SingleColumnType(org.hibernate.type.SingleColumnType) OneToOneType(org.hibernate.type.OneToOneType) SetType(org.hibernate.type.SetType) DoubleType(org.hibernate.type.DoubleType) Type(org.hibernate.type.Type) LongType(org.hibernate.type.LongType) SingleColumnType(org.hibernate.type.SingleColumnType) ManyToOneType(org.hibernate.type.ManyToOneType) Column(org.hibernate.mapping.Column) DoubleType(org.hibernate.type.DoubleType) OneToMany(org.hibernate.mapping.OneToMany) Property(org.hisp.dhis.schema.Property) TextType(org.hibernate.type.TextType) OneToOneType(org.hibernate.type.OneToOneType)

Example 70 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class AbstractHibernateListener method handleNonIdentifiableCollection.

private void handleNonIdentifiableCollection(Property property, Object value, Map<String, Object> objectMap) {
    if (value == null)
        return;
    Schema schema = schemaService.getSchema(property.getItemKlass());
    if (schema == null) {
        objectMap.put(property.getFieldName(), value);
        return;
    }
    List<Map<String, Object>> listProperties = new ArrayList<>();
    List<Property> properties = schema.getProperties();
    Collection collection = (Collection) value;
    collection.forEach(item -> {
        Map<String, Object> propertyMap = new HashMap<>();
        properties.forEach(prop -> putValueToMap(prop, propertyMap, ReflectionUtils.invokeGetterMethod(prop.getFieldName(), item)));
        listProperties.add(propertyMap);
    });
    objectMap.put(property.getFieldName(), listProperties);
}
Also used : HashMap(java.util.HashMap) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) Collection(java.util.Collection) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) HashMap(java.util.HashMap) Map(java.util.Map) Property(org.hisp.dhis.schema.Property)

Aggregations

Property (org.hisp.dhis.schema.Property)126 Schema (org.hisp.dhis.schema.Schema)69 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)36 ArrayList (java.util.ArrayList)32 HashMap (java.util.HashMap)26 Collection (java.util.Collection)21 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)21 List (java.util.List)20 Map (java.util.Map)16 Test (org.junit.jupiter.api.Test)16 Attribute (org.hisp.dhis.attribute.Attribute)14 ReflectionUtils (org.hisp.dhis.system.util.ReflectionUtils)14 Collectors (java.util.stream.Collectors)13 User (org.hisp.dhis.user.User)13 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)12 EmbeddedObject (org.hisp.dhis.common.EmbeddedObject)12 SimpleNode (org.hisp.dhis.node.types.SimpleNode)12 Query (org.hisp.dhis.query.Query)12 SchemaService (org.hisp.dhis.schema.SchemaService)12 Transactional (org.springframework.transaction.annotation.Transactional)12