Skip to content

Commit

Permalink
fix: Use createdAt as secundary sorting param [DHIS2-18093]
Browse files Browse the repository at this point in the history
  • Loading branch information
enricocolasante committed Oct 3, 2024
1 parent 0ee87ec commit 524b69a
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ public Enrollment fromForRuleEngine(
TrackerPreheat preheat,
org.hisp.dhis.tracker.imports.domain.Enrollment enrollment,
UserDetails user) {
return from(preheat, enrollment, null, user);
Enrollment from = from(preheat, enrollment, null, user);
Enrollment preheatEnrollment = preheat.getEnrollment(enrollment.getUid());
if (preheatEnrollment != null) {
from.setCreated(preheatEnrollment.getCreated());
}
return from;
}

private Enrollment from(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ public Event fromForRuleEngine(
Event result = from(preheat, event, null, user);
// merge data values from DB
result.getEventDataValues().addAll(getDataValues(preheat, event));
Event preheatEvent = preheat.getEvent(event.getUid());
if (preheatEvent != null) {
result.setCreated(preheatEvent.getCreated());
}
return result;
}

Expand Down Expand Up @@ -209,8 +213,8 @@ private Event from(
result.setCreated(now);
result.setStoredBy(event.getStoredBy());
result.setCreatedByUserInfo(UserInfoSnapshot.from(user));
result.setCreatedAtClient(DateUtils.fromInstant(event.getCreatedAtClient()));
}
result.setCreatedAtClient(DateUtils.fromInstant(event.getCreatedAtClient()));
result.setLastUpdatedByUserInfo(UserInfoSnapshot.from(user));
result.setLastUpdated(now);
result.setDeleted(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public RuleValidationResult getDescription(String condition, UID programUid)
return ruleEngine.validate(
condition,
programRuleEntityMapperService.getItemStore(
programRuleVariableService.getProgramRuleVariable(program)));
programRuleVariableService.getProgramRuleVariable(program), constantService.getAllConstants()));
}

@Override
Expand All @@ -145,7 +145,7 @@ public RuleValidationResult getDataExpressionDescription(String dataExpression,
return ruleEngine.validateDataFieldExpression(
dataExpression,
programRuleEntityMapperService.getItemStore(
programRuleVariableService.getProgramRuleVariable(program)));
programRuleVariableService.getProgramRuleVariable(program), constantService.getAllConstants()));
}

private List<RuleEffects> evaluateProgramRulesForMultipleTrackerObjects(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.hisp.dhis.common.ValueType;
import org.hisp.dhis.constant.Constant;
import org.hisp.dhis.constant.ConstantService;
import org.hisp.dhis.i18n.I18nManager;
import org.hisp.dhis.program.Enrollment;
import org.hisp.dhis.program.Event;
import org.hisp.dhis.programrule.ProgramRule;
import org.hisp.dhis.programrule.ProgramRuleAction;
import org.hisp.dhis.programrule.ProgramRuleVariable;
import org.hisp.dhis.programrule.ProgramRuleVariableService;
import org.hisp.dhis.rules.api.DataItem;
import org.hisp.dhis.rules.api.EnvironmentVariables;
import org.hisp.dhis.rules.api.ItemValueType;
Expand Down Expand Up @@ -96,7 +96,6 @@
@RequiredArgsConstructor
@Service("org.hisp.dhis.tracker.imports.programrule.engine.ProgramRuleEntityMapperService")
public class DefaultProgramRuleEntityMapperService implements ProgramRuleEntityMapperService {
private final ProgramRuleVariableService programRuleVariableService;

private final ConstantService constantService;

Expand All @@ -110,14 +109,6 @@ public List<Rule> toMappedProgramRules(List<ProgramRule> programRules) {
.toList();
}

@Override
public List<RuleVariable> toMappedProgramRuleVariables() {
List<ProgramRuleVariable> programRuleVariables =
programRuleVariableService.getAllProgramRuleVariable();

return toMappedProgramRuleVariables(programRuleVariables);
}

@Override
public List<RuleVariable> toMappedProgramRuleVariables(
List<ProgramRuleVariable> programRuleVariables) {
Expand All @@ -128,7 +119,8 @@ public List<RuleVariable> toMappedProgramRuleVariables(
}

@Override
public Map<String, DataItem> getItemStore(List<ProgramRuleVariable> programRuleVariables) {
public Map<String, DataItem> getItemStore(
List<ProgramRuleVariable> programRuleVariables, List<Constant> constants) {
Map<String, DataItem> itemStore = new HashMap<>();

// program rule variables
Expand All @@ -138,19 +130,16 @@ public Map<String, DataItem> getItemStore(List<ProgramRuleVariable> programRuleV
ObjectUtils.firstNonNull(prv.getName(), prv.getDisplayName()),
getDescription(prv)));

// constants
constantService
.getAllConstants()
.forEach(
constant ->
itemStore.put(
constant.getUid(),
new DataItem(
ObjectUtils.firstNonNull(
constant.getDisplayName(),
constant.getDisplayFormName(),
constant.getName()),
ItemValueType.NUMBER)));
constants.forEach(
constant ->
itemStore.put(
constant.getUid(),
new DataItem(
ObjectUtils.firstNonNull(
constant.getDisplayName(),
constant.getDisplayFormName(),
constant.getName()),
ItemValueType.NUMBER)));

// program variables
EnvironmentVariables.INSTANCE
Expand Down Expand Up @@ -222,6 +211,7 @@ public RuleEvent toMappedRuleEvent(@Nonnull Event eventToEvaluate) {
eventToEvaluate.getOccurredDate() != null
? Instant.Companion.fromEpochMilliseconds(eventToEvaluate.getOccurredDate().getTime())
: Instant.Companion.fromEpochMilliseconds(eventToEvaluate.getScheduledDate().getTime()),
Instant.Companion.fromEpochMilliseconds(eventToEvaluate.getCreated().getTime()),
eventToEvaluate.getScheduledDate() == null
? null
: LocalDateTime.Formats.INSTANCE
Expand All @@ -239,17 +229,7 @@ public RuleEvent toMappedRuleEvent(@Nonnull Event eventToEvaluate) {
eventToEvaluate.getEventDataValues().stream()
.filter(Objects::nonNull)
.filter(dv -> dv.getValue() != null)
.map(
dv ->
new RuleDataValue(
eventToEvaluate.getOccurredDate() != null
? Instant.Companion.fromEpochMilliseconds(
eventToEvaluate.getOccurredDate().getTime())
: Instant.Companion.fromEpochMilliseconds(
eventToEvaluate.getScheduledDate().getTime()),
eventToEvaluate.getProgramStage().getUid(),
dv.getDataElement(),
dv.getValue()))
.map(dv -> new RuleDataValue(dv.getDataElement(), dv.getValue()))
.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hisp.dhis.constant.Constant;
import org.hisp.dhis.program.Enrollment;
import org.hisp.dhis.program.Event;
import org.hisp.dhis.programrule.ProgramRule;
Expand All @@ -43,7 +44,7 @@

/**
* RuleEngine has its own domain model. This service is responsible for converting DHIS domain
* objects to RuleEngine domain objects and vice versa.
* objects to RuleEngine domain objects.
*
* <p>Created by [email protected] on 19.10.17.
*/
Expand All @@ -54,11 +55,6 @@ public interface ProgramRuleEntityMapperService {
*/
List<Rule> toMappedProgramRules(List<ProgramRule> programRules);

/***
* @return A list of mapped RuleVariables for all programs.
*/
List<RuleVariable> toMappedProgramRuleVariables();

/**
* @param programRuleVariables The list of ProgramRuleVariable to be mapped.
* @return A list of mapped RuleVariables for list of programs.
Expand Down Expand Up @@ -88,5 +84,6 @@ RuleEnrollment toMappedRuleEnrollment(
*
* @return map containing item description
*/
Map<String, DataItem> getItemStore(List<ProgramRuleVariable> programRuleVariables);
Map<String, DataItem> getItemStore(
List<ProgramRuleVariable> programRuleVariables, List<Constant> constants);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
import org.hisp.dhis.common.CodeGenerator;
import org.hisp.dhis.common.ValueType;
import org.hisp.dhis.constant.Constant;
import org.hisp.dhis.constant.ConstantService;
import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.eventdatavalue.EventDataValue;
import org.hisp.dhis.i18n.I18n;
Expand All @@ -78,7 +77,6 @@
import org.hisp.dhis.programrule.ProgramRuleAction;
import org.hisp.dhis.programrule.ProgramRuleActionType;
import org.hisp.dhis.programrule.ProgramRuleVariable;
import org.hisp.dhis.programrule.ProgramRuleVariableService;
import org.hisp.dhis.programrule.ProgramRuleVariableSourceType;
import org.hisp.dhis.rules.api.DataItem;
import org.hisp.dhis.rules.models.AttributeType;
Expand All @@ -87,9 +85,6 @@
import org.hisp.dhis.rules.models.RuleDataValue;
import org.hisp.dhis.rules.models.RuleEnrollment;
import org.hisp.dhis.rules.models.RuleEvent;
import org.hisp.dhis.rules.models.RuleVariable;
import org.hisp.dhis.rules.models.RuleVariableAttribute;
import org.hisp.dhis.rules.models.RuleVariableCalculatedValue;
import org.hisp.dhis.test.TestBase;
import org.hisp.dhis.trackedentity.TrackedEntity;
import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
Expand Down Expand Up @@ -136,10 +131,6 @@ class ProgramRuleEntityMapperServiceTest extends TestBase {

private DataElement dataElement;

@Mock private ProgramRuleVariableService programRuleVariableService;

@Mock private ConstantService constantService;

@Mock private I18nManager i18nManager;

@Mock private I18n i18n;
Expand Down Expand Up @@ -239,31 +230,6 @@ void shouldMapProgramRules() {
}
}

@Test
void shouldToMapProgramRuleVariables() {
when(programRuleVariableService.getAllProgramRuleVariable()).thenReturn(programRuleVariables);
RuleVariableAttribute ruleVariableAttribute;
RuleVariableCalculatedValue ruleVariableCalculatedValue;

List<RuleVariable> ruleVariables = mapper.toMappedProgramRuleVariables();

assertEquals(ruleVariables.size(), programRuleVariables.size());

for (RuleVariable variable : ruleVariables) {
if (variable instanceof RuleVariableAttribute) {
ruleVariableAttribute = (RuleVariableAttribute) variable;
assertEquals(
ruleVariableAttribute.getField(), programRuleVariableB.getAttribute().getUid());
assertEquals(ruleVariableAttribute.getName(), programRuleVariableB.getName());
}

if (variable instanceof RuleVariableCalculatedValue) {
ruleVariableCalculatedValue = (RuleVariableCalculatedValue) variable;
assertEquals(ruleVariableCalculatedValue.getName(), programRuleVariableA.getName());
}
}
}

@Test
void shouldFailToMapEventWhenProgramStageIsNull() {
Event event = event();
Expand Down Expand Up @@ -355,15 +321,14 @@ void testGetItemStore() {
constant.setValue(7.8);
constant.setAutoFields();
constant.setName("Gravity");
List<Constant> constants = List.of(constant);

when(constantService.getAllConstants()).thenReturn(constants);
when(i18nManager.getI18n()).thenReturn(i18n);
when(i18n.getString(anyString())).thenReturn(envVariable);

Map<String, DataItem> itemStore =
mapper.getItemStore(
List.of(programRuleVariableA, programRuleVariableB, programRuleVariableC));
List.of(programRuleVariableA, programRuleVariableB, programRuleVariableC),
List.of(constant));

assertNotNull(itemStore);
assertTrue(itemStore.containsKey(programRuleVariableA.getName()));
Expand Down Expand Up @@ -559,6 +524,7 @@ private void assertEvent(Event event, RuleEvent ruleEvent) {
assertDates(event.getOccurredDate(), ruleEvent.getEventDate());
assertNull(ruleEvent.getCompletedDate());
assertEquals(event.getOrganisationUnit().getUid(), ruleEvent.getOrganisationUnit());
assertDates(event.getCreated(), ruleEvent.getCreatedDate());
assertEquals(event.getOrganisationUnit().getCode(), ruleEvent.getOrganisationUnitCode());
assertDataValue(event.getEventDataValues().iterator().next(), ruleEvent.getDataValues().get(0));
}
Expand Down
Loading

0 comments on commit 524b69a

Please sign in to comment.