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);
}
}
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;
}
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());
}
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;
}
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);
}
Aggregations