← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20375: Program indicator calculation, impl support for missing value replacement

 

------------------------------------------------------------
revno: 20375
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2015-09-27 11:29:50 +0200
message:
  Program indicator calculation, impl support for missing value replacement
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/DefaultProgramIndicatorService.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/program/ProgramIndicatorServiceTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/DefaultProgramIndicatorService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/DefaultProgramIndicatorService.java	2015-09-24 21:48:20 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/DefaultProgramIndicatorService.java	2015-09-27 09:29:50 +0000
@@ -38,7 +38,6 @@
 import java.util.Set;
 import java.util.regex.Matcher;
 
-import org.hisp.dhis.common.ValueType;
 import org.hisp.dhis.commons.sqlfunc.ConditionalSqlFunction;
 import org.hisp.dhis.commons.sqlfunc.DaysBetweenSqlFunction;
 import org.hisp.dhis.commons.sqlfunc.OneIfZeroOrPositiveSqlFunction;
@@ -63,6 +62,7 @@
 import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService;
 import org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue;
 import org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValueService;
+import org.hisp.dhis.util.ObjectUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -248,18 +248,17 @@
 
                     TrackedEntityDataValue dataValue = dataValueService.getTrackedEntityDataValue( psi, dataElement );
 
+                    String value = null;
+                    
                     if ( dataValue == null )
                     {
-                        return null;
+                        value = String.valueOf( ObjectUtils.firstNonNull( indicator.getMissingValueReplacement(), 0 ) );
                     }
-
-                    String value = dataValue.getValue();
-
-                    if ( ValueType.DATE == dataElement.getValueType() )
+                    else
                     {
-                        value = DateUtils.daysBetween( new Date(), DateUtils.getDefaultDate( value ) ) + " ";
+                        value = dataValue.getValue();
                     }
-
+                    
                     matcher.appendReplacement( buffer, value );
 
                     valueCount++;
@@ -279,24 +278,21 @@
                     TrackedEntityAttributeValue attributeValue = attributeValueService.getTrackedEntityAttributeValue(
                         programInstance.getEntityInstance(), attribute );
 
-                    if ( attributeValue != null )
+                    String value = null;
+                    
+                    if ( attributeValue == null )
                     {
-                        String value = attributeValue.getValue();
-
-                        if ( ValueType.DATE == attribute.getValueType() )
-                        {
-                            value = DateUtils.daysBetween( new Date(), DateUtils.getDefaultDate( value ) ) + " ";
-                        }
-
-                        matcher.appendReplacement( buffer, value );
-
-                        valueCount++;
-                        zeroPosValueCount = isZeroOrPositive( value ) ? (zeroPosValueCount + 1) : zeroPosValueCount;
+                        value = String.valueOf( ObjectUtils.firstNonNull( indicator.getMissingValueReplacement(), 0 ) );
                     }
                     else
                     {
-                        return null;
+                        value = attributeValue.getValue();
                     }
+                    
+                    matcher.appendReplacement( buffer, value );
+
+                    valueCount++;
+                    zeroPosValueCount = isZeroOrPositive( value ) ? (zeroPosValueCount + 1) : zeroPosValueCount;
                 }
                 else
                 {
@@ -367,7 +363,7 @@
         }
 
         expression = TextUtils.appendTail( matcher, buffer );
-
+        
         return MathUtils.calculateExpression( expression );
     }
 

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/program/ProgramIndicatorServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/program/ProgramIndicatorServiceTest.java	2015-09-24 21:35:11 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/program/ProgramIndicatorServiceTest.java	2015-09-27 09:29:50 +0000
@@ -110,7 +110,7 @@
     @Autowired
     private ConstantService constantService;
 
-    private Date incidenDate;
+    private Date incidentDate;
 
     private Date enrollmentDate;
 
@@ -132,10 +132,6 @@
 
     private TrackedEntityAttribute atB;
 
-    private TrackedEntityAttribute atC;
-
-    private TrackedEntityAttribute atD;
-
     private ProgramIndicator indicatorA;
 
     private ProgramIndicator indicatorB;
@@ -154,8 +150,6 @@
 
     private ProgramIndicator indicatorI;
 
-    private ProgramIndicator indicatorJ;
-
     @Override
     public void setUpTest()
     {
@@ -195,7 +189,6 @@
 
         deB = createDataElement( 'B' );
         deB.setDomainType( DataElementDomain.TRACKER );
-        deB.setValueType( ValueType.DATE );
 
         dataElementService.addDataElement( deA );
         dataElementService.addDataElement( deB );
@@ -217,17 +210,17 @@
         TrackedEntityInstance entityInstance = createTrackedEntityInstance( 'A', organisationUnit );
         entityInstanceService.addTrackedEntityInstance( entityInstance );
 
-        incidenDate = DateUtils.getMediumDate( "2014-10-22" );
-        enrollmentDate = DateUtils.getMediumDate( "2014-12-31" );
-
-        programInstance = programInstanceService.enrollTrackedEntityInstance( entityInstance, programA, enrollmentDate,
-            incidenDate, organisationUnit );
-
-        incidenDate = DateUtils.getMediumDate( "2014-10-22" );
-        enrollmentDate = DateUtils.getMediumDate( "2014-12-31" );
-
-        programInstance = programInstanceService.enrollTrackedEntityInstance( entityInstance, programA, enrollmentDate,
-            incidenDate, organisationUnit );
+        incidentDate = DateUtils.getMediumDate( "2014-10-22" );
+        enrollmentDate = DateUtils.getMediumDate( "2014-12-31" );
+
+        programInstance = programInstanceService.enrollTrackedEntityInstance( entityInstance, programA, enrollmentDate,
+            incidentDate, organisationUnit );
+
+        incidentDate = DateUtils.getMediumDate( "2014-10-22" );
+        enrollmentDate = DateUtils.getMediumDate( "2014-12-31" );
+
+        programInstance = programInstanceService.enrollTrackedEntityInstance( entityInstance, programA, enrollmentDate,
+            incidentDate, organisationUnit );
 
         // TODO enroll twice?
 
@@ -237,32 +230,24 @@
 
         atA = createTrackedEntityAttribute( 'A', ValueType.NUMBER );
         atB = createTrackedEntityAttribute( 'B', ValueType.NUMBER );
-        atC = createTrackedEntityAttribute( 'C', ValueType.DATE );
-        atD = createTrackedEntityAttribute( 'D', ValueType.DATE );
 
         attributeService.addTrackedEntityAttribute( atA );
         attributeService.addTrackedEntityAttribute( atB );
-        attributeService.addTrackedEntityAttribute( atC );
-        attributeService.addTrackedEntityAttribute( atD );
 
         TrackedEntityAttributeValue attributeValueA = new TrackedEntityAttributeValue( atA, entityInstance, "1" );
         TrackedEntityAttributeValue attributeValueB = new TrackedEntityAttributeValue( atB, entityInstance, "2" );
-        TrackedEntityAttributeValue attributeValueC = new TrackedEntityAttributeValue( atC, entityInstance, "2015-01-01" );
-        TrackedEntityAttributeValue attributeValueD = new TrackedEntityAttributeValue( atD, entityInstance, "2015-01-03" );
 
         attributeValueService.addTrackedEntityAttributeValue( attributeValueA );
         attributeValueService.addTrackedEntityAttributeValue( attributeValueB );
-        attributeValueService.addTrackedEntityAttributeValue( attributeValueC );
-        attributeValueService.addTrackedEntityAttributeValue( attributeValueD );
 
         // ---------------------------------------------------------------------
         // TrackedEntityDataValue
         // ---------------------------------------------------------------------
 
         ProgramStageInstance stageInstanceA = programStageInstanceService.createProgramStageInstance( programInstance,
-            psA, enrollmentDate, incidenDate, organisationUnit );
+            psA, enrollmentDate, incidentDate, organisationUnit );
         ProgramStageInstance stageInstanceB = programStageInstanceService.createProgramStageInstance( programInstance,
-            psB, enrollmentDate, incidenDate, organisationUnit );
+            psB, enrollmentDate, incidentDate, organisationUnit );
 
         Set<ProgramStageInstance> programStageInstances = new HashSet<>();
         programStageInstances.add( stageInstanceA );
@@ -271,9 +256,9 @@
         programInstance.setProgram( programA );
 
         TrackedEntityDataValue dataValueA = new TrackedEntityDataValue( stageInstanceA, deA, "3" );
-        TrackedEntityDataValue dataValueB = new TrackedEntityDataValue( stageInstanceA, deB, "2015-03-01" );
+        TrackedEntityDataValue dataValueB = new TrackedEntityDataValue( stageInstanceA, deB, "1" );
         TrackedEntityDataValue dataValueC = new TrackedEntityDataValue( stageInstanceB, deA, "5" );
-        TrackedEntityDataValue dataValueD = new TrackedEntityDataValue( stageInstanceB, deB, "2015-03-15" );
+        TrackedEntityDataValue dataValueD = new TrackedEntityDataValue( stageInstanceB, deB, "7" );
 
         dataValueService.saveTrackedEntityDataValue( dataValueA );
         dataValueService.saveTrackedEntityDataValue( dataValueB );
@@ -316,9 +301,9 @@
             + KEY_ATTRIBUTE + "{" + atB.getUid() + "}";
         indicatorF = createProgramIndicator( 'F', programB, expressionF, null );
 
-        String expressionG = "(" + KEY_DATAELEMENT + "{" + psB.getUid() + "." + deB.getUid() + "} - " + KEY_DATAELEMENT
+        String expressionG = "((" + KEY_DATAELEMENT + "{" + psB.getUid() + "." + deB.getUid() + "} - " + KEY_DATAELEMENT
             + "{" + psA.getUid() + "." + deB.getUid() + "} ) + " + KEY_ATTRIBUTE + "{" + atA.getUid() + "} + "
-            + KEY_ATTRIBUTE + "{" + atB.getUid() + "} * " + KEY_CONSTANT + "{" + constantA.getUid() + "}";
+            + KEY_ATTRIBUTE + "{" + atB.getUid() + "}) * " + KEY_CONSTANT + "{" + constantA.getUid() + "}";
         indicatorG = createProgramIndicator( 'G', programB, expressionG, null );
 
         String expressionH = "(" + KEY_PROGRAM_VARIABLE + "{" + ProgramIndicator.VAR_CURRENT_DATE + "} - "
@@ -330,11 +315,6 @@
             + KEY_DATAELEMENT + "{" + psA.getUid() + "." + deB.getUid() + "} ) + " + KEY_DATAELEMENT + "{"
             + psA.getUid() + "." + deA.getUid() + "}";
         indicatorI = createProgramIndicator( 'I', programB, expressionI, null );
-
-        String expressionJ = "(" + KEY_ATTRIBUTE + "{" + atC.getUid() + "}  - " + KEY_PROGRAM_VARIABLE + "{"
-            + ProgramIndicator.VAR_ENROLLMENT_DATE + "} ) + " + KEY_DATAELEMENT + "{" + psA.getUid() + "." + deA.getUid()
-            + "} * " + ProgramIndicator.KEY_CONSTANT + "{" + constantA.getUid() + "}";
-        indicatorJ = createProgramIndicator( 'J', programB, expressionJ, null );
     }
 
     // -------------------------------------------------------------------------
@@ -429,7 +409,6 @@
         programIndicatorService.addProgramIndicator( indicatorG );
         programIndicatorService.addProgramIndicator( indicatorH );
         programIndicatorService.addProgramIndicator( indicatorI );
-        programIndicatorService.addProgramIndicator( indicatorJ );
 
         Double valueINT = programIndicatorService.getProgramIndicatorValue( indicatorA, programInstance );
         assertEquals( 10.0, valueINT, 0.01 );
@@ -438,10 +417,10 @@
         assertEquals( 9.0, valueE, 0.01 );
 
         Double valueF = programIndicatorService.getProgramIndicatorValue( indicatorF, programInstance );
-        assertEquals( 17.0, valueF, 0.01 );
+        assertEquals( 9.0, valueF, 0.01 );
 
         Double valueG = programIndicatorService.getProgramIndicatorValue( indicatorG, programInstance );
-        assertEquals( 29.0, valueG, 0.01 );
+        assertEquals( 63.0, valueG, 0.01 );
     }
 
     @Test