Search in sources :

Example 16 with Property

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

the class EmbeddedObjectObjectBundleHook method preUpdate.

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

Example 17 with Property

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

the class EmbeddedObjectObjectBundleHook method validate.

@Override
public void validate(IdentifiableObject object, ObjectBundle bundle, Consumer<ErrorReport> addReports) {
    Class<? extends IdentifiableObject> klass = object.getClass();
    Schema schema = schemaService.getDynamicSchema(klass);
    schema.getEmbeddedObjectProperties().keySet().stream().forEach(propertyName -> {
        Property property = schema.getEmbeddedObjectProperties().get(propertyName);
        Object propertyObject = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
        if (property.getPropertyType().equals(PropertyType.COMPLEX)) {
            schemaValidator.validateEmbeddedObject(propertyObject, klass).forEach(unformattedError -> addReports.accept(formatEmbeddedErrorReport(unformattedError, propertyName)));
        } else if (property.getPropertyType().equals(PropertyType.COLLECTION)) {
            Collection<?> collection = (Collection<?>) propertyObject;
            for (Object item : collection) {
                schemaValidator.validateEmbeddedObject(property.getItemKlass().cast(item), klass).forEach(unformattedError -> addReports.accept(formatEmbeddedErrorReport(unformattedError, propertyName)));
            }
        }
    });
}
Also used : PropertyType(org.hisp.dhis.schema.PropertyType) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) ErrorReport(org.hisp.dhis.feedback.ErrorReport) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) BaseAnalyticalObject(org.hisp.dhis.common.BaseAnalyticalObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) Collection(java.util.Collection) Session(org.hibernate.Session) Property(org.hisp.dhis.schema.Property) DefaultAnalyticalObjectImportHandler(org.hisp.dhis.dxf2.metadata.DefaultAnalyticalObjectImportHandler) Consumer(java.util.function.Consumer) SchemaValidator(org.hisp.dhis.schema.validation.SchemaValidator) Component(org.springframework.stereotype.Component) PeriodType(org.hisp.dhis.period.PeriodType) Schema(org.hisp.dhis.schema.Schema) AllArgsConstructor(lombok.AllArgsConstructor) Schema(org.hisp.dhis.schema.Schema) 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)

Example 18 with Property

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

the class EmbeddedObjectObjectBundleHook method handleProperty.

private void handleProperty(Object object, ObjectBundle bundle, Property property) {
    if (object == null || bundle == null || property == null) {
        return;
    }
    if (property.isIdentifiableObject()) {
        ((BaseIdentifiableObject) object).setAutoFields();
    }
    Schema embeddedSchema = schemaService.getDynamicSchema(HibernateProxyUtils.getRealClass(object));
    for (Property embeddedProperty : embeddedSchema.getPropertyMap().values()) {
        if (PeriodType.class.isAssignableFrom(embeddedProperty.getKlass())) {
            PeriodType periodType = ReflectionUtils.invokeMethod(object, embeddedProperty.getGetterMethod());
            if (periodType != null) {
                periodType = bundle.getPreheat().getPeriodTypeMap().get(periodType.getName());
                ReflectionUtils.invokeMethod(object, embeddedProperty.getSetterMethod(), periodType);
            }
        }
    }
    preheatService.connectReferences(object, bundle.getPreheat(), bundle.getPreheatIdentifier());
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) Schema(org.hisp.dhis.schema.Schema) Property(org.hisp.dhis.schema.Property)

Example 19 with Property

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

the class UniquenessCheck method checkUniqueness.

private List<ErrorReport> checkUniqueness(IdentifiableObject object, PreheatIdentifier identifier, Map<String, Map<Object, String>> uniquenessMap, List<Property> uniqueProperties) {
    List<ErrorReport> errorReports = new ArrayList<>();
    uniqueProperties.forEach(property -> {
        Object value = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
        if (value != null) {
            String objectIdentifier = uniquenessMap.computeIfAbsent(property.getName(), key -> new HashMap<>()).get(value);
            if (objectIdentifier != null) {
                if (!identifier.getIdentifier(object).equals(objectIdentifier)) {
                    String identifiersWithName = identifier.getIdentifiersWithName(object);
                    ErrorReport errorReport = new ErrorReport(HibernateProxyUtils.getRealClass(object), ErrorCode.E5003, property.getName(), value, identifiersWithName, objectIdentifier);
                    errorReports.add(errorReport.setMainId(objectIdentifier).setErrorProperty(property.getName()));
                }
            } else {
                uniquenessMap.get(property.getName()).put(value, identifier.getIdentifier(object));
            }
        }
    });
    return errorReports;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) ErrorReport(org.hisp.dhis.feedback.ErrorReport) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) Collections.emptyList(java.util.Collections.emptyList) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier) Preheat(org.hisp.dhis.preheat.Preheat) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Property(org.hisp.dhis.schema.Property) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) ValidationUtils.createObjectReport(org.hisp.dhis.dxf2.metadata.objectbundle.validation.ValidationUtils.createObjectReport) List(java.util.List) Component(org.springframework.stereotype.Component) Map(java.util.Map) ErrorCode(org.hisp.dhis.feedback.ErrorCode) Schema(org.hisp.dhis.schema.Schema) ObjectReport(org.hisp.dhis.feedback.ObjectReport) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 20 with Property

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

the class UniquenessCheck method checkUniqueness.

private List<ErrorReport> checkUniqueness(IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier, ValidationContext ctx) {
    if (object == null || preheat.isDefault(object)) {
        return emptyList();
    }
    @SuppressWarnings("unchecked") Class<? extends IdentifiableObject> objType = HibernateProxyUtils.getRealClass(object);
    Map<String, Map<Object, String>> uniquenessMap = preheat.getUniquenessMap().computeIfAbsent(objType, key -> new HashMap<>());
    Schema schema = ctx.getSchemaService().getDynamicSchema(objType);
    List<Property> uniqueProperties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && p.isUnique() && p.isSimple()).collect(Collectors.toList());
    if (uniqueProperties.isEmpty()) {
        return emptyList();
    }
    return checkUniqueness(object, identifier, uniquenessMap, uniqueProperties);
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) ErrorReport(org.hisp.dhis.feedback.ErrorReport) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) Collections.emptyList(java.util.Collections.emptyList) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier) Preheat(org.hisp.dhis.preheat.Preheat) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Property(org.hisp.dhis.schema.Property) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) ValidationUtils.createObjectReport(org.hisp.dhis.dxf2.metadata.objectbundle.validation.ValidationUtils.createObjectReport) List(java.util.List) Component(org.springframework.stereotype.Component) Map(java.util.Map) ErrorCode(org.hisp.dhis.feedback.ErrorCode) Schema(org.hisp.dhis.schema.Schema) ObjectReport(org.hisp.dhis.feedback.ObjectReport) Schema(org.hisp.dhis.schema.Schema) 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