Search in sources :

Example 11 with Property

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

the class EmbeddedObjectObjectBundleHook method preUpdate.

@Override
public <T extends IdentifiableObject> void preUpdate(T object, T persistedObject, ObjectBundle bundle) {
    Schema schema = schemaService.getDynamicSchema(object.getClass());
    if (schema == null || schema.getEmbeddedObjectProperties().isEmpty()) {
        return;
    }
    Collection<Property> properties = schema.getEmbeddedObjectProperties().values();
    clearEmbeddedObjects(persistedObject, properties);
    handleEmbeddedObjects(object, bundle, properties);
}
Also used : Schema(org.hisp.dhis.schema.Schema) Property(org.hisp.dhis.schema.Property)

Example 12 with Property

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

the class PeriodTypeObjectBundleHook method preUpdate.

@Override
public <T extends IdentifiableObject> void preUpdate(T object, T persistedObject, ObjectBundle bundle) {
    Schema schema = schemaService.getDynamicSchema(object.getClass());
    for (Property property : schema.getPropertyMap().values()) {
        if (PeriodType.class.isAssignableFrom(property.getKlass())) {
            PeriodType periodType = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
            if (periodType != null) {
                periodType = bundle.getPreheat().getPeriodTypeMap().get(periodType.getName());
                ReflectionUtils.invokeMethod(object, property.getSetterMethod(), periodType);
            }
        }
    }
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) Schema(org.hisp.dhis.schema.Schema) Property(org.hisp.dhis.schema.Property)

Example 13 with Property

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

the class PeriodTypeObjectBundleHook method preCreate.

@Override
public <T extends IdentifiableObject> void preCreate(T object, ObjectBundle bundle) {
    Schema schema = schemaService.getDynamicSchema(object.getClass());
    for (Property property : schema.getPropertyMap().values()) {
        if (PeriodType.class.isAssignableFrom(property.getKlass())) {
            PeriodType periodType = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
            if (periodType != null) {
                periodType = bundle.getPreheat().getPeriodTypeMap().get(periodType.getName());
                ReflectionUtils.invokeMethod(object, property.getSetterMethod(), periodType);
            }
        }
    }
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) Schema(org.hisp.dhis.schema.Schema) Property(org.hisp.dhis.schema.Property)

Example 14 with Property

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

the class DefaultSchemaValidator method validate.

@Override
public List<ErrorReport> validate(Object object, boolean persisted) {
    List<ErrorReport> errorReports = new ArrayList<>();
    if (object == null) {
        return errorReports;
    }
    Schema schema = schemaService.getDynamicSchema(object.getClass());
    if (schema == null) {
        return errorReports;
    }
    Class<?> klass = object.getClass();
    for (Property property : schema.getProperties()) {
        if (persisted && !property.isPersisted()) {
            continue;
        }
        Object value = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
        if (value == null) {
            if (property.isRequired() && !Preheat.isDefaultClass(property.getKlass())) {
                errorReports.add(new ErrorReport(klass, ErrorCode.E4000, property.getName()).setErrorKlass(property.getKlass()).setErrorProperty(property.getName()));
            }
            continue;
        }
        errorReports.addAll(validateString(klass, value, property));
        errorReports.addAll(validateCollection(klass, value, property));
        errorReports.addAll(validateInteger(klass, value, property));
        errorReports.addAll(validateFloat(klass, value, property));
        errorReports.addAll(validateDouble(klass, value, property));
    }
    if (User.class.isInstance(object)) {
        User user = (User) object;
        if (user.getUserCredentials() != null) {
            errorReports.addAll(validate(user.getUserCredentials(), persisted));
        }
    }
    return errorReports;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) User(org.hisp.dhis.user.User) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) Property(org.hisp.dhis.schema.Property)

Example 15 with Property

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

the class EmbeddedObjectObjectBundleHook method handleEmbeddedObjects.

private void handleEmbeddedObjects(IdentifiableObject object, ObjectBundle bundle, Collection<Property> properties) {
    for (Property property : properties) {
        Object propertyObject = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
        if (property.isCollection()) {
            Collection<?> objects = (Collection<?>) propertyObject;
            objects.forEach(itemPropertyObject -> {
                handleProperty(itemPropertyObject, bundle, property);
                handleEmbeddedAnalyticalProperty(itemPropertyObject, bundle, property);
            });
        } else {
            handleProperty(propertyObject, bundle, property);
            handleEmbeddedAnalyticalProperty(propertyObject, bundle, property);
        }
    }
}
Also used : Collection(java.util.Collection) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) BaseAnalyticalObject(org.hisp.dhis.common.BaseAnalyticalObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) 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