Skip to content

Commit

Permalink
fix: mandatory attributes cannot have empty value [DHIS2-18365] (2.41) (
Browse files Browse the repository at this point in the history
#19643) (#19646)

* fix: mandatory attributes cannot have empty value [DHIS2-18365] (#19586)

* fix: mandatory attributes cannot have empty value [DHIS2-18365]

* test: adds a test for user without mandatory attribute creation [DHIS2-18365]

* fix: use Set<AttributeValue>

* fix: revert change in DefaultMetadataImportService
  • Loading branch information
jbee authored Jan 13, 2025
1 parent d52753f commit 8c5f02e
Showing 1 changed file with 27 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,45 +61,37 @@ public <T extends IdentifiableObject> void check(
Schema schema = ctx.getSchemaService().getDynamicSchema(klass);
List<T> objects = selectObjects(persistedObjects, nonPersistedObjects, importStrategy);

if (objects.isEmpty() || !schema.havePersistedProperty("attributeValues")) {
return;
}
if (objects.isEmpty() || !schema.hasAttributeValues()) return;

for (T object : objects) {
List<ErrorReport> errorReports = checkMandatoryAttributes(klass, object, bundle.getPreheat());
Preheat preheat = bundle.getPreheat();
Set<String> 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<AttributeValue> 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<ErrorReport> checkMandatoryAttributes(
Class<? extends IdentifiableObject> klass, IdentifiableObject object, Preheat preheat) {
if (object == null
|| preheat.isDefault(object)
|| !preheat.getMandatoryAttributes().containsKey(klass)) {
return emptyList();
}

Set<String> mandatoryAttributes = preheat.getMandatoryAttributes().get(klass);
if (mandatoryAttributes.isEmpty()) {
return emptyList();
}
Set<String> 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<AttributeValue> 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;
}
}

0 comments on commit 8c5f02e

Please sign in to comment.