From 883e540f89c2885f8ed058e6ec66d3d773ded850 Mon Sep 17 00:00:00 2001 From: Enrico Date: Thu, 12 Oct 2023 15:48:08 +0200 Subject: [PATCH 1/3] chore: Upgrade to next snapshot for legacy branch --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index d7c89a4b..0d7f136d 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.hisp.dhis.rules rule-engine - 2.1.8.0-SNAPSHOT + 2.1.8.1-SNAPSHOT jar rule-engine From f80e832ea926334fd4f36d9052833a91f72ddace Mon Sep 17 00:00:00 2001 From: Enrico Date: Tue, 15 Oct 2024 15:59:43 +0200 Subject: [PATCH 2/3] fix: Add createdDate to RuleEvent [DHIS2-18089] --- .../hisp/dhis/rules/RuleVariableValue.java | 4 +- .../rules/RuleVariableValueMapBuilder.java | 17 +- src/main/java/org/hisp/dhis/rules/Utils.java | 33 +++- .../rules/models/RuleDataValueHistory.java | 31 ++++ .../org/hisp/dhis/rules/models/RuleEvent.java | 13 +- .../hisp/dhis/rules/models/RuleVariable.java | 2 +- .../rules/models/RuleVariableAttribute.java | 2 +- .../models/RuleVariableCalculatedValue.java | 2 +- .../models/RuleVariableCurrentEvent.java | 2 +- .../rules/models/RuleVariableNewestEvent.java | 11 +- .../models/RuleVariableNewestStageEvent.java | 19 ++- .../models/RuleVariablePreviousEvent.java | 16 +- .../hisp/dhis/rules/ConstantsValueTest.java | 1 + .../dhis/rules/ProgramRuleVariableTest.java | 1 + .../dhis/rules/RuleEngineEffectTypesTest.java | 1 + .../dhis/rules/RuleEngineFunctionTest.java | 154 ++++++++--------- .../org/hisp/dhis/rules/RuleEngineTest.java | 2 +- .../dhis/rules/RuleEngineValueTypesTest.java | 6 +- .../rules/RuleEngineVariableNameTest.java | 70 ++++---- .../rules/RuleVariablePreviousEventTest.java | 160 ++++++++++++++++++ .../RuleVariableValueMapBuilderTest.java | 56 +++--- .../dhis/rules/VariableValueTypeTest.java | 4 +- .../rules/models/CalculatedValueTest.java | 3 + .../hisp/dhis/rules/models/RuleEventTest.java | 28 +-- .../models/RuleVariablePreviousEventTest.java | 43 ----- .../hisp/dhis/rules/util/MockRuleEvent.java | 6 + .../dhis/rules/util/MockRuleVariable.java | 3 +- 27 files changed, 451 insertions(+), 239 deletions(-) create mode 100644 src/main/java/org/hisp/dhis/rules/models/RuleDataValueHistory.java create mode 100644 src/test/java/org/hisp/dhis/rules/RuleVariablePreviousEventTest.java delete mode 100644 src/test/java/org/hisp/dhis/rules/models/RuleVariablePreviousEventTest.java diff --git a/src/main/java/org/hisp/dhis/rules/RuleVariableValue.java b/src/main/java/org/hisp/dhis/rules/RuleVariableValue.java index 44671123..4baba7f6 100644 --- a/src/main/java/org/hisp/dhis/rules/RuleVariableValue.java +++ b/src/main/java/org/hisp/dhis/rules/RuleVariableValue.java @@ -22,7 +22,7 @@ public abstract class RuleVariableValue implements VariableValue @Nonnull public static RuleVariableValue create( @Nonnull RuleValueType ruleValueType ) { - return new AutoValue_RuleVariableValue( null, ruleValueType, List.of(), getFormattedDate( new Date() ) ); + return new AutoValue_RuleVariableValue( null, ruleValueType, List.of(), null ); } @Nonnull @@ -30,7 +30,7 @@ public static RuleVariableValue create( @Nonnull String value, @Nonnull RuleValueType ruleValueType ) { return new AutoValue_RuleVariableValue( value, ruleValueType, - List.of(), getFormattedDate( new Date() ) ); + List.of(), null ); } @Nonnull diff --git a/src/main/java/org/hisp/dhis/rules/RuleVariableValueMapBuilder.java b/src/main/java/org/hisp/dhis/rules/RuleVariableValueMapBuilder.java index 43c96023..ddef4220 100644 --- a/src/main/java/org/hisp/dhis/rules/RuleVariableValueMapBuilder.java +++ b/src/main/java/org/hisp/dhis/rules/RuleVariableValueMapBuilder.java @@ -220,9 +220,9 @@ private Map buildCurrentEnrollmentValues() return currentEnrollmentValues; } - private Map> buildAllEventValues() + private Map> buildAllEventValues() { - Map> allEventsValues = new HashMap<>(); + Map> allEventsValues = new HashMap<>(); List events = new ArrayList<>( ruleEvents ); if ( ruleEvent != null ) @@ -242,17 +242,22 @@ private Map> buildAllEventValues() for ( int j = 0; j < ruleEvent.dataValues().size(); j++ ) { - RuleDataValue ruleDataValue = ruleEvent.dataValues().get( j ); + RuleDataValue ruleDataValue = ruleEvent.dataValues().get(j); + RuleDataValueHistory ruleDataValueHistory = new RuleDataValueHistory( + ruleDataValue.value(), + ruleEvent.eventDate(), + ruleEvent.createdDate(), + ruleEvent.programStage() ); // push new list if it is not there for the given data element if ( !allEventsValues.containsKey( ruleDataValue.dataElement() ) ) { allEventsValues.put( ruleDataValue.dataElement(), - new ArrayList( events.size() ) ); //NOPMD + new ArrayList( events.size() ) ); //NOPMD } // append data value to the list - allEventsValues.get( ruleDataValue.dataElement() ).add( ruleDataValue ); + allEventsValues.get( ruleDataValue.dataElement() ).add( ruleDataValueHistory ); } } @@ -377,7 +382,7 @@ private Map buildRuleVariableValues() Map valueMap = new HashMap<>(); // map data values within all events to data elements - Map> allEventValues = buildAllEventValues(); + Map> allEventValues = buildAllEventValues(); // map tracked entity attributes to values from enrollment Map currentEnrollmentValues = buildCurrentEnrollmentValues(); diff --git a/src/main/java/org/hisp/dhis/rules/Utils.java b/src/main/java/org/hisp/dhis/rules/Utils.java index ae9708db..580a5e28 100644 --- a/src/main/java/org/hisp/dhis/rules/Utils.java +++ b/src/main/java/org/hisp/dhis/rules/Utils.java @@ -1,6 +1,7 @@ package org.hisp.dhis.rules; import org.hisp.dhis.rules.models.RuleDataValue; +import org.hisp.dhis.rules.models.RuleDataValueHistory; import org.hisp.dhis.rules.models.RuleEvent; import java.text.SimpleDateFormat; @@ -32,14 +33,26 @@ public static List values( List ruleDataValues ) return Collections.unmodifiableList( values ); } - public static String getLastUpdateDateForPrevious( List ruleDataValues, + public static List valuesForHistory( List ruleDataValues ) + { + List values = new ArrayList<>( ruleDataValues.size() ); + for ( RuleDataValueHistory ruleDataValue : ruleDataValues ) + { + values.add( ruleDataValue.getValue() ); + } + return Collections.unmodifiableList( values ); + } + + public static String getLastUpdateDateForPrevious( List ruleDataValues, RuleEvent ruleEvent ) { List dates = new ArrayList<>(); - for ( RuleDataValue date : ruleDataValues ) + for ( RuleDataValueHistory date : ruleDataValues ) { - Date d = date.eventDate(); - if ( d.before( ruleEvent.eventDate() ) ) + Date d = date.getEventDate(); + if ( d.before( ruleEvent.eventDate() ) || + (ruleEvent.eventDate().equals(d) && ruleEvent.createdDate().after(date.getCreatedDate())) + ) { dates.add( d ); } @@ -48,6 +61,18 @@ public static String getLastUpdateDateForPrevious( List ruleDataV return dateFormat.format( Collections.max( dates ) ); } + public static String getLastUpdateDateForHistory( List ruleDataValues ) + { + List dates = new ArrayList<>(); + for ( RuleDataValueHistory date : ruleDataValues ) + { + Date d = date.getEventDate(); + dates.add( d ); + } + + return dateFormat.format( Collections.max( dates ) ); + } + public static String getLastUpdateDate( List ruleDataValues ) { List dates = new ArrayList<>(); diff --git a/src/main/java/org/hisp/dhis/rules/models/RuleDataValueHistory.java b/src/main/java/org/hisp/dhis/rules/models/RuleDataValueHistory.java new file mode 100644 index 00000000..cbde5de6 --- /dev/null +++ b/src/main/java/org/hisp/dhis/rules/models/RuleDataValueHistory.java @@ -0,0 +1,31 @@ +package org.hisp.dhis.rules.models; + +import java.util.Date; + +public class RuleDataValueHistory { + private final String value; + private final Date eventDate; + private final Date createdDate; + private final String programStage; + public RuleDataValueHistory( String value, Date eventDate, Date createdDate, String programStage) { + this.value = value; + this.eventDate = eventDate; + this.createdDate = createdDate; + this.programStage = programStage; + } + + public String getValue() { + return value; + } + + public Date getEventDate() { + return eventDate; + } + + public Date getCreatedDate() { + return createdDate; + } + + public String getProgramStage() { + return programStage;} +} \ No newline at end of file diff --git a/src/main/java/org/hisp/dhis/rules/models/RuleEvent.java b/src/main/java/org/hisp/dhis/rules/models/RuleEvent.java index 05ac9540..92c99581 100644 --- a/src/main/java/org/hisp/dhis/rules/models/RuleEvent.java +++ b/src/main/java/org/hisp/dhis/rules/models/RuleEvent.java @@ -22,6 +22,7 @@ public static RuleEvent create( @Nonnull String programStage, @Nonnull Status status, @Nonnull Date eventDate, + @Nonnull Date createdDate, @Nonnull Date dueDate, @Nonnull String organisationUnit, @Nullable String organisationUnitCode, @@ -35,6 +36,7 @@ public static RuleEvent create( .programStageName( programStageName ) .status( status ) .eventDate( eventDate ) + .createdDate( createdDate ) .dueDate( dueDate ) .organisationUnit( organisationUnit ) .organisationUnitCode( organisationUnitCode ) @@ -63,6 +65,9 @@ public static Builder builder() @Nonnull public abstract Date eventDate(); + @Nonnull + public abstract Date createdDate(); + @Nullable public abstract Date dueDate(); @@ -98,6 +103,8 @@ public static abstract class Builder public abstract Builder dueDate( Date dueDate ); + public abstract Builder createdDate( Date createdDate ); + public abstract Builder completedDate( Date completedDate ); public abstract Builder organisationUnit( String organisationUnit ); @@ -117,7 +124,11 @@ private static class EventDateComparator @Override public int compare( RuleEvent first, RuleEvent second ) { - return second.eventDate().compareTo( first.eventDate() ); + int compare = second.eventDate().compareTo(first.eventDate()); + if (compare == 0){ + return second.createdDate().compareTo(first.createdDate()); + } + return compare; } } diff --git a/src/main/java/org/hisp/dhis/rules/models/RuleVariable.java b/src/main/java/org/hisp/dhis/rules/models/RuleVariable.java index dbfdf457..8582faa6 100644 --- a/src/main/java/org/hisp/dhis/rules/models/RuleVariable.java +++ b/src/main/java/org/hisp/dhis/rules/models/RuleVariable.java @@ -25,7 +25,7 @@ public abstract class RuleVariable public abstract List