Skip to content

Commit

Permalink
Fix: Enrollment/Event notification in tracker app (#19531) (#19628)
Browse files Browse the repository at this point in the history
  • Loading branch information
zubaira authored Jan 13, 2025
1 parent 9ca4f4a commit d52753f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.ProgramInstance;
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;

/**
Expand Down Expand Up @@ -78,6 +82,8 @@ public class ProgramNotificationMessageRenderer
private static final Set<ExpressionType> SUPPORTED_EXPRESSION_TYPES =
ImmutableSet.of(ExpressionType.TRACKED_ENTITY_ATTRIBUTE, ExpressionType.VARIABLE);

@Autowired private OptionService optionService;

// -------------------------------------------------------------------------
// Overrides
// -------------------------------------------------------------------------
Expand All @@ -97,9 +103,7 @@ protected Map<String, String> resolveTrackedEntityAttributeValues(

return entity.getEntityInstance().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
Expand All @@ -123,7 +127,7 @@ protected Map<String, String> resolveDataElementValues(
// Internal methods
// -------------------------------------------------------------------------

private static String filterValue(TrackedEntityAttributeValue av) {
private String filterValue(TrackedEntityAttributeValue av) {
String value = av.getPlainValue();

if (value == null) {
Expand All @@ -133,9 +137,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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.ProgramStageInstance;
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;

/**
Expand Down Expand Up @@ -110,6 +114,8 @@ public class ProgramStageNotificationMessageRenderer
ExpressionType.VARIABLE,
ExpressionType.DATA_ELEMENT);

@Autowired private OptionService optionService;

// -------------------------------------------------------------------------
// Singleton instance
// -------------------------------------------------------------------------
Expand Down Expand Up @@ -140,10 +146,7 @@ protected Map<String, String> resolveTrackedEntityAttributeValues(
.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
Expand Down Expand Up @@ -178,7 +181,7 @@ protected Set<ExpressionType> getSupportedExpressionTypes() {
// Internal methods
// -------------------------------------------------------------------------

private static String filterValue(TrackedEntityAttributeValue av) {
private String filterValue(TrackedEntityAttributeValue av) {
String value = av.getPlainValue();

if (value == null) {
Expand All @@ -188,13 +191,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) {
Expand All @@ -204,9 +210,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;
}
}

0 comments on commit d52753f

Please sign in to comment.