From d3205c34070d877c5ea76b2f00393fc507a69364 Mon Sep 17 00:00:00 2001 From: Zubair Asghar Date: Fri, 20 Dec 2024 14:35:57 +0500 Subject: [PATCH] Fix: Enrollment/Event notification in tracker app (#19531) --- .../ProgramNotificationMessageRenderer.java | 19 ++++++++---- ...ogramStageNotificationMessageRenderer.java | 29 ++++++++++++------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/notification/ProgramNotificationMessageRenderer.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/notification/ProgramNotificationMessageRenderer.java index 19c07616aece..ac79adb0b485 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/notification/ProgramNotificationMessageRenderer.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/notification/ProgramNotificationMessageRenderer.java @@ -33,12 +33,16 @@ import java.util.Collections; import java.util.Date; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; +import org.hisp.dhis.common.BaseIdentifiableObject; +import org.hisp.dhis.option.OptionService; import org.hisp.dhis.program.Enrollment; import org.hisp.dhis.program.notification.ProgramTemplateVariable; import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** @@ -77,6 +81,8 @@ public class ProgramNotificationMessageRenderer private static final Set SUPPORTED_EXPRESSION_TYPES = ImmutableSet.of(ExpressionType.TRACKED_ENTITY_ATTRIBUTE, ExpressionType.VARIABLE); + @Autowired private OptionService optionService; + // ------------------------------------------------------------------------- // Overrides // ------------------------------------------------------------------------- @@ -95,9 +101,7 @@ protected Map resolveTrackedEntityAttributeValues( return entity.getTrackedEntity().getTrackedEntityAttributeValues().stream() .filter(av -> attributeKeys.contains(av.getAttribute().getUid())) - .collect( - Collectors.toMap( - av -> av.getAttribute().getUid(), ProgramNotificationMessageRenderer::filterValue)); + .collect(Collectors.toMap(av -> av.getAttribute().getUid(), this::filterValue)); } @Override @@ -121,7 +125,7 @@ protected Map resolveDataElementValues( // Internal methods // ------------------------------------------------------------------------- - private static String filterValue(TrackedEntityAttributeValue av) { + private String filterValue(TrackedEntityAttributeValue av) { String value = av.getPlainValue(); if (value == null) { @@ -131,9 +135,12 @@ private static String filterValue(TrackedEntityAttributeValue av) { // If the AV has an OptionSet -> substitute value with the name of the // Option if (av.getAttribute().hasOptionSet()) { - value = av.getAttribute().getOptionSet().getOptionByCode(value).getName(); + value = + Optional.ofNullable(optionService.getOptionByCode(value)) + .map(BaseIdentifiableObject::getName) + .orElse(MISSING_VALUE_REPLACEMENT); } - return value != null ? value : MISSING_VALUE_REPLACEMENT; + return value; } } diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/notification/ProgramStageNotificationMessageRenderer.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/notification/ProgramStageNotificationMessageRenderer.java index e718f724e5d3..f07ceff67cf4 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/notification/ProgramStageNotificationMessageRenderer.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/notification/ProgramStageNotificationMessageRenderer.java @@ -33,14 +33,18 @@ import java.util.Date; import java.util.HashMap; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; +import org.hisp.dhis.common.BaseIdentifiableObject; import org.hisp.dhis.dataelement.DataElement; import org.hisp.dhis.eventdatavalue.EventDataValue; +import org.hisp.dhis.option.OptionService; import org.hisp.dhis.program.Event; import org.hisp.dhis.program.notification.ProgramStageTemplateVariable; import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** @@ -106,6 +110,8 @@ public class ProgramStageNotificationMessageRenderer ExpressionType.VARIABLE, ExpressionType.DATA_ELEMENT); + @Autowired private OptionService optionService; + // ------------------------------------------------------------------------- // Singleton instance // ------------------------------------------------------------------------- @@ -131,10 +137,7 @@ protected Map resolveTrackedEntityAttributeValues( return entity.getEnrollment().getTrackedEntity().getTrackedEntityAttributeValues().stream() .filter(av -> attributeKeys.contains(av.getAttribute().getUid())) - .collect( - Collectors.toMap( - av -> av.getAttribute().getUid(), - ProgramStageNotificationMessageRenderer::filterValue)); + .collect(Collectors.toMap(av -> av.getAttribute().getUid(), this::filterValue)); } @Override @@ -168,7 +171,7 @@ protected Set getSupportedExpressionTypes() { // Internal methods // ------------------------------------------------------------------------- - private static String filterValue(TrackedEntityAttributeValue av) { + private String filterValue(TrackedEntityAttributeValue av) { String value = av.getPlainValue(); if (value == null) { @@ -178,13 +181,16 @@ private static String filterValue(TrackedEntityAttributeValue av) { // If the AV has an OptionSet -> substitute value with the name of the // Option if (av.getAttribute().hasOptionSet()) { - value = av.getAttribute().getOptionSet().getOptionByCode(value).getName(); + value = + Optional.ofNullable(optionService.getOptionByCode(value)) + .map(BaseIdentifiableObject::getName) + .orElse(MISSING_VALUE_REPLACEMENT); } - return value != null ? value : MISSING_VALUE_REPLACEMENT; + return value; } - private static String filterValue(EventDataValue dv, DataElement dataElement) { + private String filterValue(EventDataValue dv, DataElement dataElement) { String value = dv.getValue(); if (value == null) { @@ -194,9 +200,12 @@ private static String filterValue(EventDataValue dv, DataElement dataElement) { // If the DV has an OptionSet -> substitute value with the name of the // Option if (dataElement != null && dataElement.hasOptionSet()) { - value = dataElement.getOptionSet().getOptionByCode(value).getName(); + value = + Optional.ofNullable(optionService.getOptionByCode(value)) + .map(BaseIdentifiableObject::getName) + .orElse(MISSING_VALUE_REPLACEMENT); } - return value != null ? value : MISSING_VALUE_REPLACEMENT; + return value; } }