Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: (2.41) Enrollment/Event notification in tracker app [DHIS2-18633] #19550

Open
wants to merge 1 commit into
base: 2.41
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading