Skip to content

Commit

Permalink
Fix: Enrollment/Event notification in tracker app (#19531)
Browse files Browse the repository at this point in the history
  • Loading branch information
zubaira committed Dec 20, 2024
1 parent 011d6c2 commit d3205c3
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.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;

/**
Expand Down Expand Up @@ -77,6 +81,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 @@ -95,9 +101,7 @@ protected Map<String, String> 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
Expand All @@ -121,7 +125,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 @@ -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;
}
}
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.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;

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

@Autowired private OptionService optionService;

// -------------------------------------------------------------------------
// Singleton instance
// -------------------------------------------------------------------------
Expand All @@ -131,10 +137,7 @@ protected Map<String, String> 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
Expand Down Expand Up @@ -168,7 +171,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 @@ -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) {
Expand All @@ -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;
}
}

0 comments on commit d3205c3

Please sign in to comment.