← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19017: Program indicators, added support for new variable V{value_count}

 

------------------------------------------------------------
revno: 19017
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2015-04-24 15:39:16 +0200
message:
  Program indicators, added support for new variable V{value_count}
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramIndicator.java
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramIndicatorService.java
  dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/program/ProgramIndicatorServiceTest.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/resources/org/hisp/dhis/trackedentity/i18n_module.properties
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/programIndicatorForm.vm


--
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-api/src/main/java/org/hisp/dhis/program/ProgramIndicator.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramIndicator.java	2015-03-29 20:22:47 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramIndicator.java	2015-04-24 13:39:16 +0000
@@ -59,6 +59,7 @@
     public static final String INCIDENT_DATE = "incident_date";
     public static final String ENROLLMENT_DATE = "enrollment_date";
     public static final String CURRENT_DATE = "current_date";
+    public static final String VALUE_COUNT = "value_count";
     public static final String VALUE_TYPE_DATE = "date";
     public static final String VALUE_TYPE_INT = "int";
     
@@ -70,6 +71,7 @@
     public static final Pattern EXPRESSION_PATTERN = Pattern.compile( EXPRESSION_REGEXP );    
     public static final Pattern DATAELEMENT_PATTERN = Pattern.compile( KEY_DATAELEMENT + "\\{(\\w{11})" + SEPARATOR_ID + "(\\w{11})\\}" );
     public static final Pattern ATTRIBUTE_PATTERN = Pattern.compile( KEY_ATTRIBUTE + "\\{(\\w{11})\\}" );
+    public static final Pattern VALUECOUNT_PATTERN = Pattern.compile( "V\\{value_count\\}" );
     
     public static final String VALID = "valid";
 

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramIndicatorService.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramIndicatorService.java	2015-04-24 13:11:10 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramIndicatorService.java	2015-04-24 13:39:16 +0000
@@ -45,6 +45,7 @@
 import org.hisp.dhis.i18n.I18nService;
 import org.hisp.dhis.system.util.DateUtils;
 import org.hisp.dhis.system.util.MathUtils;
+import org.hisp.dhis.system.util.TextUtils;
 import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
 import org.hisp.dhis.trackedentity.TrackedEntityAttributeService;
 import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue;
@@ -259,7 +260,6 @@
                     matcher.appendReplacement( description, programStageName + ProgramIndicator.SEPARATOR_ID + dataelementName );
                 }
             }
-
             else if ( ProgramIndicator.KEY_ATTRIBUTE.equals( key ) )
             {
                 TrackedEntityAttribute attribute = attributeService.getTrackedEntityAttribute( uid );
@@ -292,13 +292,16 @@
                 {
                     matcher.appendReplacement( description, "Incident date" );
                 }
+                else if ( uid.equals( ProgramIndicator.VALUE_COUNT ) )
+                {
+                    matcher.appendReplacement( description, "Value count" );
+                }
             }
         }
 
         matcher.appendTail( description );
 
         return description.toString();
-
     }
 
     @Override
@@ -329,7 +332,6 @@
                     return ProgramIndicator.EXPRESSION_NOT_WELL_FORMED;
                 }
             }
-
             else if ( ProgramIndicator.KEY_ATTRIBUTE.equals( key ) )
             {
                 TrackedEntityAttribute attribute = attributeService.getTrackedEntityAttribute( uid );
@@ -430,8 +432,12 @@
     {
         StringBuffer buffer = new StringBuffer();
 
-        Matcher matcher = ProgramIndicator.EXPRESSION_PATTERN.matcher( indicator.getExpression() );
+        String expression = indicator.getExpression();
+        
+        Matcher matcher = ProgramIndicator.EXPRESSION_PATTERN.matcher( expression );
 
+        int valueCount = 0;
+        
         while ( matcher.find() )
         {
             String key = matcher.group( 1 );
@@ -465,6 +471,7 @@
 
                     matcher.appendReplacement( buffer, value );
                     
+                    valueCount++;                    
                 }
                 else
                 {
@@ -483,11 +490,15 @@
                     if ( attributeValue != null )
                     {
                         String value = attributeValue.getValue();
+                        
                         if( attribute.getValueType().equals( TrackedEntityAttribute.TYPE_DATE ))
                         {
                             value = DateUtils.daysBetween( new Date(), DateUtils.getDefaultDate( value ) ) + " ";
                         }
+                        
                         matcher.appendReplacement( buffer, value );
+                        
+                        valueCount++;
                     }
                     else
                     {
@@ -529,7 +540,7 @@
                 {
                     date = currentDate;
                 }
-
+                
                 if ( date != null )
                 {
                     matcher.appendReplacement( buffer, DateUtils.daysBetween( currentDate, date ) + "" );
@@ -537,8 +548,22 @@
             }
         }
 
-        matcher.appendTail( buffer );
-
-        return MathUtils.calculateExpression( buffer.toString() );
+        expression = TextUtils.appendTail( matcher, buffer );
+
+        // ---------------------------------------------------------------------
+        // Value count variable
+        // ---------------------------------------------------------------------
+
+        buffer = new StringBuffer();
+        matcher = ProgramIndicator.VALUECOUNT_PATTERN.matcher( expression );
+        
+        while ( matcher.find() )
+        {
+            matcher.appendReplacement( buffer, String.valueOf( valueCount ) );
+        }
+        
+        expression = TextUtils.appendTail( matcher, buffer );
+        
+        return MathUtils.calculateExpression( expression );
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/program/ProgramIndicatorServiceTest.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/program/ProgramIndicatorServiceTest.java	2015-03-29 20:22:47 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/program/ProgramIndicatorServiceTest.java	2015-04-24 13:39:16 +0000
@@ -470,7 +470,7 @@
         programIndicatorService.addProgramIndicator( indicatorI );
         programIndicatorService.addProgramIndicator( indicatorJ );
 
-        String valueINT = programIndicatorService.getProgramIndicatorValue( programInstance, indicatorA);
+        String valueINT = programIndicatorService.getProgramIndicatorValue( programInstance, indicatorA );
         assertEquals( "10.0", valueINT );
 
         String valueDATE = programIndicatorService.getProgramIndicatorValue( programInstance,  indicatorB  );

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java	2015-02-19 09:18:17 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java	2015-04-24 13:39:16 +0000
@@ -461,4 +461,18 @@
     {
         return object != null ? object.toString() : null;
     }
+
+    /**
+     * Invokes append tail on matcher with the given string buffer, and returns
+     * the string buffer as a string.
+     * 
+     * @param matcher the matcher.
+     * @param sb the string buffer.
+     * @return a string.
+     */
+    public static String appendTail( Matcher matcher, StringBuffer sb )
+    {
+        matcher.appendTail( sb );
+        return sb.toString();
+    }    
 }

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/resources/org/hisp/dhis/trackedentity/i18n_module.properties'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/resources/org/hisp/dhis/trackedentity/i18n_module.properties	2015-04-11 14:34:49 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/resources/org/hisp/dhis/trackedentity/i18n_module.properties	2015-04-24 13:39:16 +0000
@@ -78,6 +78,7 @@
 attribute_options=Attribute options
 add_more_option=Add Option
 mandatory=Mandatory
+value_count=Value count
 tracked_entity_attribute_group_management=Tracked entity attribute group management
 move_selected=Move selected items
 remove_selected=Remove selected items

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/programIndicatorForm.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/programIndicatorForm.vm	2015-04-23 16:39:41 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/programIndicatorForm.vm	2015-04-24 13:39:16 +0000
@@ -39,6 +39,7 @@
 						<option value="V{incident_date}">$i18n.getString( "incident_date" )</option>
 						<option value="V{enrollment_date}">$i18n.getString( "date_of_enrollment" )</option>
 						<option value="V{current_date}">$i18n.getString( "current_date" )</option>
+						<option value="V{value_count}">$i18n.getString( "value_count" )</option>
 					</select>
 				</td>
 			</tr>