diff --git a/dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/objectbundle/validation/MandatoryAttributesCheck.java b/dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/objectbundle/validation/MandatoryAttributesCheck.java index debad2342444..31815825774a 100644 --- a/dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/objectbundle/validation/MandatoryAttributesCheck.java +++ b/dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/objectbundle/validation/MandatoryAttributesCheck.java @@ -27,15 +27,13 @@ */ package org.hisp.dhis.dxf2.metadata.objectbundle.validation; -import static java.util.Collections.emptyList; import static org.hisp.dhis.dxf2.metadata.objectbundle.validation.ValidationUtils.createObjectReport; -import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.function.Consumer; -import java.util.stream.Collectors; import org.hisp.dhis.attribute.Attribute; +import org.hisp.dhis.attribute.AttributeValue; import org.hisp.dhis.common.IdentifiableObject; import org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle; import org.hisp.dhis.feedback.ErrorCode; @@ -63,45 +61,37 @@ public void check( Schema schema = ctx.getSchemaService().getDynamicSchema(klass); List objects = selectObjects(persistedObjects, nonPersistedObjects, importStrategy); - if (objects.isEmpty() || !schema.havePersistedProperty("attributeValues")) { - return; - } + if (objects.isEmpty() || !schema.hasAttributeValues()) return; - for (T object : objects) { - List errorReports = checkMandatoryAttributes(klass, object, bundle.getPreheat()); + Preheat preheat = bundle.getPreheat(); + Set mandatoryAttributes = preheat.getMandatoryAttributes().get(klass); + if (mandatoryAttributes == null || mandatoryAttributes.isEmpty()) return; - if (!errorReports.isEmpty()) { - addReports.accept(createObjectReport(errorReports, object, bundle)); - ctx.markForRemoval(object); + for (T object : objects) { + if (object != null && !preheat.isDefault(object)) { + Set attributeValues = object.getAttributeValues(); + mandatoryAttributes.stream() + .filter(attrId -> !isDefined(attrId, attributeValues)) + .forEach( + attrId -> { + addReports.accept( + createObjectReport( + new ErrorReport(Attribute.class, ErrorCode.E4011, attrId) + .setMainId(attrId) + .setErrorProperty("value"), + object, + bundle)); + ctx.markForRemoval(object); + }); } } } - private List checkMandatoryAttributes( - Class klass, IdentifiableObject object, Preheat preheat) { - if (object == null - || preheat.isDefault(object) - || !preheat.getMandatoryAttributes().containsKey(klass)) { - return emptyList(); - } - - Set mandatoryAttributes = preheat.getMandatoryAttributes().get(klass); - if (mandatoryAttributes.isEmpty()) { - return emptyList(); - } - Set missingMandatoryAttributes = new HashSet<>(mandatoryAttributes); - object - .getAttributeValues() - .forEach( - attributeValue -> - missingMandatoryAttributes.remove(attributeValue.getAttribute().getUid())); - - return missingMandatoryAttributes.stream() - .map( - att -> - new ErrorReport(Attribute.class, ErrorCode.E4011, att) - .setMainId(att) - .setErrorProperty("value")) - .collect(Collectors.toList()); + private static boolean isDefined(String attrId, Set values) { + if (values == null || values.isEmpty()) return false; + for (AttributeValue v : values) + if (attrId.equals(v.getAttribute().getUid())) + return v.getValue() != null && !v.getValue().isEmpty(); + return false; } }