Skip to content

Commit

Permalink
Merge pull request #115 from dhis2/legacy-update
Browse files Browse the repository at this point in the history
chore: Upgrade to next snapshot for legacy branch
  • Loading branch information
enricocolasante authored Oct 16, 2024
2 parents 4afaaa4 + 49fc450 commit ad638af
Show file tree
Hide file tree
Showing 28 changed files with 452 additions and 240 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.hisp.dhis.rules</groupId>
<artifactId>rule-engine</artifactId>
<version>2.1.8.0-SNAPSHOT</version>
<version>2.1.8.2-SNAPSHOT</version>
<packaging>jar</packaging>
<name>rule-engine</name>

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/hisp/dhis/rules/RuleVariableValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ 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
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
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/org/hisp/dhis/rules/RuleVariableValueMapBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ private Map<String, RuleAttributeValue> buildCurrentEnrollmentValues()
return currentEnrollmentValues;
}

private Map<String, List<RuleDataValue>> buildAllEventValues()
private Map<String, List<RuleDataValueHistory>> buildAllEventValues()
{
Map<String, List<RuleDataValue>> allEventsValues = new HashMap<>();
Map<String, List<RuleDataValueHistory>> allEventsValues = new HashMap<>();
List<RuleEvent> events = new ArrayList<>( ruleEvents );

if ( ruleEvent != null )
Expand All @@ -242,17 +242,22 @@ private Map<String, List<RuleDataValue>> 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<RuleDataValue>( events.size() ) ); //NOPMD
new ArrayList<RuleDataValueHistory>( events.size() ) ); //NOPMD
}

// append data value to the list
allEventsValues.get( ruleDataValue.dataElement() ).add( ruleDataValue );
allEventsValues.get( ruleDataValue.dataElement() ).add( ruleDataValueHistory );
}
}

Expand Down Expand Up @@ -377,7 +382,7 @@ private Map<String, RuleVariableValue> buildRuleVariableValues()
Map<String, RuleVariableValue> valueMap = new HashMap<>();

// map data values within all events to data elements
Map<String, List<RuleDataValue>> allEventValues = buildAllEventValues();
Map<String, List<RuleDataValueHistory>> allEventValues = buildAllEventValues();

// map tracked entity attributes to values from enrollment
Map<String, RuleAttributeValue> currentEnrollmentValues = buildCurrentEnrollmentValues();
Expand Down
33 changes: 29 additions & 4 deletions src/main/java/org/hisp/dhis/rules/Utils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -32,14 +33,26 @@ public static List<String> values( List<RuleDataValue> ruleDataValues )
return Collections.unmodifiableList( values );
}

public static String getLastUpdateDateForPrevious( List<RuleDataValue> ruleDataValues,
public static List<String> valuesForHistory( List<RuleDataValueHistory> ruleDataValues )
{
List<String> values = new ArrayList<>( ruleDataValues.size() );
for ( RuleDataValueHistory ruleDataValue : ruleDataValues )
{
values.add( ruleDataValue.getValue() );
}
return Collections.unmodifiableList( values );
}

public static String getLastUpdateDateForPrevious( List<RuleDataValueHistory> ruleDataValues,
RuleEvent ruleEvent )
{
List<Date> 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 );
}
Expand All @@ -48,6 +61,18 @@ public static String getLastUpdateDateForPrevious( List<RuleDataValue> ruleDataV
return dateFormat.format( Collections.max( dates ) );
}

public static String getLastUpdateDateForHistory( List<RuleDataValueHistory> ruleDataValues )
{
List<Date> 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<RuleDataValue> ruleDataValues )
{
List<Date> dates = new ArrayList<>();
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/org/hisp/dhis/rules/models/RuleDataValueHistory.java
Original file line number Diff line number Diff line change
@@ -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;}
}
13 changes: 12 additions & 1 deletion src/main/java/org/hisp/dhis/rules/models/RuleEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -35,6 +36,7 @@ public static RuleEvent create(
.programStageName( programStageName )
.status( status )
.eventDate( eventDate )
.createdDate( createdDate )
.dueDate( dueDate )
.organisationUnit( organisationUnit )
.organisationUnitCode( organisationUnitCode )
Expand Down Expand Up @@ -63,6 +65,9 @@ public static Builder builder()
@Nonnull
public abstract Date eventDate();

@Nonnull
public abstract Date createdDate();

@Nullable
public abstract Date dueDate();

Expand Down Expand Up @@ -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 );
Expand All @@ -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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/hisp/dhis/rules/models/RuleVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public abstract class RuleVariable
public abstract List<Option> options();

public abstract Map<String, RuleVariableValue> createValues( RuleVariableValueMapBuilder builder,
Map<String, List<RuleDataValue>> allEventValues,
Map<String, List<RuleDataValueHistory>> allEventValues,
Map<String, RuleAttributeValue> currentEnrollmentValues,
Map<String, RuleDataValue> currentEventValues );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static RuleVariableAttribute create(@Nonnull String name,

@Override
public Map<String, RuleVariableValue> createValues( RuleVariableValueMapBuilder builder,
Map<String, List<RuleDataValue>> allEventValues,
Map<String, List<RuleDataValueHistory>> allEventValues,
Map<String, RuleAttributeValue> currentEnrollmentValues,
Map<String, RuleDataValue> currentEventValues )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static RuleVariableCalculatedValue create(@Nonnull String name,

@Override
public Map<String, RuleVariableValue> createValues( RuleVariableValueMapBuilder builder,
Map<String, List<RuleDataValue>> allEventValues,
Map<String, List<RuleDataValueHistory>> allEventValues,
Map<String, RuleAttributeValue> currentEnrollmentValues,
Map<String, RuleDataValue> currentEventValues )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static RuleVariableCurrentEvent create(@Nonnull String name,

@Override
public Map<String, RuleVariableValue> createValues( RuleVariableValueMapBuilder builder,
Map<String, List<RuleDataValue>> allEventValues,
Map<String, List<RuleDataValueHistory>> allEventValues,
Map<String, RuleAttributeValue> currentEnrollmentValues,
Map<String, RuleDataValue> currentEventValues )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Map;

import static org.hisp.dhis.rules.Utils.getLastUpdateDate;
import static org.hisp.dhis.rules.Utils.getLastUpdateDateForHistory;

@AutoValue
public abstract class RuleVariableNewestEvent
Expand All @@ -27,12 +28,12 @@ public static RuleVariableNewestEvent create(@Nonnull String name,

@Override
public Map<String, RuleVariableValue> createValues( RuleVariableValueMapBuilder builder,
Map<String, List<RuleDataValue>> allEventValues,
Map<String, List<RuleDataValueHistory>> allEventValues,
Map<String, RuleAttributeValue> currentEnrollmentValues,
Map<String, RuleDataValue> currentEventValues )
{
Map<String, RuleVariableValue> valueMap = new HashMap();
List<RuleDataValue> ruleDataValues = allEventValues.get( this.dataElement() );
List<RuleDataValueHistory> ruleDataValues = allEventValues.get( this.dataElement() );

if ( ruleDataValues == null || ruleDataValues.isEmpty() )
{
Expand All @@ -42,12 +43,12 @@ public Map<String, RuleVariableValue> createValues( RuleVariableValueMapBuilder
{
RuleVariableValue variableValue;

RuleDataValue value = ruleDataValues.get( 0 );
RuleDataValueHistory value = ruleDataValues.get( 0 );

String optionValue = this.useCodeForOptionSet() ? value.value() : getOptionName( value.value() );
String optionValue = this.useCodeForOptionSet() ? value.getValue() : getOptionName( value.getValue() );

variableValue = RuleVariableValue.create( optionValue,
this.dataElementType(), Utils.values( ruleDataValues ), getLastUpdateDate( ruleDataValues ) );
this.dataElementType(), Utils.valuesForHistory( ruleDataValues ), getLastUpdateDateForHistory( ruleDataValues ) );


valueMap.put( this.name(), variableValue );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Map;

import static org.hisp.dhis.rules.Utils.getLastUpdateDate;
import static org.hisp.dhis.rules.Utils.getLastUpdateDateForHistory;

@AutoValue
public abstract class RuleVariableNewestStageEvent
Expand All @@ -32,21 +33,21 @@ public static RuleVariableNewestStageEvent create(@Nonnull String name, @Nonnull

@Override
public Map<String, RuleVariableValue> createValues( RuleVariableValueMapBuilder builder,
Map<String, List<RuleDataValue>> allEventValues,
Map<String, List<RuleDataValueHistory>> allEventValues,
Map<String, RuleAttributeValue> currentEnrollmentValues,
Map<String, RuleDataValue> currentEventValues )
{
Map<String, RuleVariableValue> valueMap = new HashMap();
List<RuleDataValue> stageRuleDataValues = new ArrayList<>();
List<RuleDataValue> sourceRuleDataValues = allEventValues.get( this.dataElement() );
List<RuleDataValueHistory> stageRuleDataValues = new ArrayList<>();
List<RuleDataValueHistory> sourceRuleDataValues = allEventValues.get( this.dataElement() );
if ( sourceRuleDataValues != null && !sourceRuleDataValues.isEmpty() )
{

// filter data values based on program stage
for ( int i = 0; i < sourceRuleDataValues.size(); i++ )
{
RuleDataValue ruleDataValue = sourceRuleDataValues.get( i );
if ( this.programStage().equals( ruleDataValue.programStage() ) )
RuleDataValueHistory ruleDataValue = sourceRuleDataValues.get( i );
if ( this.programStage().equals( ruleDataValue.getProgramStage() ) )
{
stageRuleDataValues.add( ruleDataValue );
}
Expand All @@ -61,13 +62,13 @@ public Map<String, RuleVariableValue> createValues( RuleVariableValueMapBuilder
{
RuleVariableValue variableValue;

RuleDataValue value = stageRuleDataValues.get( 0 );
RuleDataValueHistory value = stageRuleDataValues.get( 0 );

String optionValue = this.useCodeForOptionSet() ? value.value() : getOptionName( value.value() );
String optionValue = this.useCodeForOptionSet() ? value.getValue() : getOptionName( value.getValue() );

variableValue = RuleVariableValue.create( optionValue,
this.dataElementType(), Utils.values( stageRuleDataValues ),
getLastUpdateDate( stageRuleDataValues ) );
this.dataElementType(), Utils.valuesForHistory( stageRuleDataValues ),
getLastUpdateDateForHistory( stageRuleDataValues ) );

valueMap.put( this.name(), variableValue );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,30 @@ public static RuleVariablePreviousEvent create(@Nonnull String name,

@Override
public Map<String, RuleVariableValue> createValues( RuleVariableValueMapBuilder builder,
Map<String, List<RuleDataValue>> allEventValues,
Map<String, List<RuleDataValueHistory>> allEventValues,
Map<String, RuleAttributeValue> currentEnrollmentValues,
Map<String, RuleDataValue> currentEventValues )
{
Map<String, RuleVariableValue> valueMap = new HashMap();

RuleVariableValue variableValue = null;
List<RuleDataValue> ruleDataValues = allEventValues.get( this.dataElement() );
List<RuleDataValueHistory> ruleDataValues = allEventValues.get( this.dataElement() );
if ( builder.ruleEvent != null && ruleDataValues != null && !ruleDataValues.isEmpty() )
{
for ( RuleDataValue ruleDataValue : ruleDataValues )
for ( RuleDataValueHistory ruleDataValue : ruleDataValues )
{
// We found preceding value to the current currentEventValues,
// which is assumed to be best candidate.
if ( builder.ruleEvent.eventDate().compareTo( ruleDataValue.eventDate() ) > 0 )
if ( builder.ruleEvent.eventDate().compareTo( ruleDataValue.getEventDate() ) > 0
|| (
builder.ruleEvent.eventDate().compareTo( ruleDataValue.getEventDate() ) == 0 &&
builder.ruleEvent.createdDate().compareTo( ruleDataValue.getCreatedDate() ) > 0
))
{
String optionValue = this.useCodeForOptionSet() ? ruleDataValue.value() : getOptionName( ruleDataValue.value() );
String optionValue = this.useCodeForOptionSet() ? ruleDataValue.getValue() : getOptionName( ruleDataValue.getValue() );

variableValue = RuleVariableValue.create( optionValue, this.dataElementType(),
Utils.values( ruleDataValues ),
Utils.valuesForHistory( ruleDataValues ),
getLastUpdateDateForPrevious( ruleDataValues, builder.ruleEvent ) );
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/hisp/dhis/rules/ConstantsValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public void assignConstantValueFromAssignActionInEvent()
.programStageName( "" )
.status( RuleEvent.Status.ACTIVE )
.eventDate( new Date() )
.createdDate( new Date() )
.dueDate( new Date() )
.organisationUnit( "" )
.organisationUnitCode( "" )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ private List<RuleEffect> callEventRuleEngine( Rule rule )
.programStageName( PROGRAM_STAGE_NAME )
.status( RULE_EVENT_STATUS )
.eventDate( EVENT_DATE )
.createdDate( new Date() )
.dueDate( DUE_DATE )
.organisationUnit( ORGANISATION_UNIT )
.organisationUnitCode( ORGANISATION_UNIT_CODE )
Expand Down
Loading

0 comments on commit ad638af

Please sign in to comment.