Search in sources :

Example 1 with ValidationUtils.createObjectReport

use of org.hisp.dhis.dxf2.metadata.objectbundle.validation.ValidationUtils.createObjectReport in project dhis2-core by dhis2.

the class UniqueMultiPropertiesCheck method check.

@Override
public <T extends IdentifiableObject> void check(ObjectBundle bundle, Class<T> klass, List<T> persistedObjects, List<T> nonPersistedObjects, ImportStrategy importStrategy, ValidationContext context, Consumer<ObjectReport> addReports) {
    Schema schema = context.getSchemaService().getSchema(klass);
    Map<List<String>, List<IdentifiableObject>> propertyValuesToObjects = new HashMap<>();
    for (T identifiableObject : nonPersistedObjects) {
        for (Map.Entry<Collection<String>, Collection<Function<IdentifiableObject, String>>> entry : schema.getUniqueMultiPropertiesExctractors().entrySet()) {
            List<String> propertyValues = entry.getValue().stream().map(valueExtractor -> valueExtractor.apply(identifiableObject)).collect(Collectors.toList());
            propertyValuesToObjects.computeIfAbsent(propertyValues, key -> new ArrayList<>()).add(identifiableObject);
        }
    }
    for (Map.Entry<List<String>, List<IdentifiableObject>> entry : propertyValuesToObjects.entrySet()) {
        List<IdentifiableObject> objects = entry.getValue();
        if (objects.size() > 1) {
            for (IdentifiableObject object : objects) {
                ErrorReport errorReport = new ErrorReport(klass, E5005, String.join(", ", entry.getKey()), objects.stream().map(IdentifiableObject::getUid).collect(joining(", ")));
                addReports.accept(ValidationUtils.createObjectReport(errorReport, object, bundle));
                context.markForRemoval(object);
            }
        }
    }
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) ErrorReport(org.hisp.dhis.feedback.ErrorReport) Collection(java.util.Collection) E5005(org.hisp.dhis.feedback.ErrorCode.E5005) HashMap(java.util.HashMap) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Collectors.joining(java.util.stream.Collectors.joining) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) List(java.util.List) Component(org.springframework.stereotype.Component) Map(java.util.Map) Schema(org.hisp.dhis.schema.Schema) ObjectReport(org.hisp.dhis.feedback.ObjectReport) HashMap(java.util.HashMap) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) ErrorReport(org.hisp.dhis.feedback.ErrorReport) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Collectors.joining (java.util.stream.Collectors.joining)1 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)1 ObjectBundle (org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle)1 E5005 (org.hisp.dhis.feedback.ErrorCode.E5005)1 ErrorReport (org.hisp.dhis.feedback.ErrorReport)1 ObjectReport (org.hisp.dhis.feedback.ObjectReport)1 ImportStrategy (org.hisp.dhis.importexport.ImportStrategy)1 Schema (org.hisp.dhis.schema.Schema)1 Component (org.springframework.stereotype.Component)1