← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9768: Add avg/min/max operators in Aggregate Query Builder.

 

------------------------------------------------------------
revno: 9768
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2013-02-07 20:44:38 +0700
message:
  Add avg/min/max operators in Aggregate Query Builder.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/caseaggregation/CaseAggregationCondition.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/caseaggregation/DefaultCaseAggregationConditionService.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/startup/TableAlteror.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/caseaggregation/TestCaseAggregationConditionAction.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/resources/org/hisp/dhis/patient/i18n_module.properties
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/addCaseAggregation.vm
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/caseAggregationForm.vm
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/caseaggregation.js
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/updateCaseAggregation.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/caseaggregation/CaseAggregationCondition.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/caseaggregation/CaseAggregationCondition.java	2013-02-07 08:12:12 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/caseaggregation/CaseAggregationCondition.java	2013-02-07 13:44:38 +0000
@@ -47,9 +47,15 @@
 
     public static final String AGGRERATION_COUNT = "COUNT";
 
-    public static final String AGGRERATION_SUM = "SUM";
-    
-    public static final String AGGRERATION_SUM_VALUE = "SUM_VALUE";
+    public static final String AGGRERATION_SUM = "times";
+    
+    public static final String AGGRERATION_SUM_VALUE = "sum";
+    
+    public static final String AGGRERATION_AVG_VALUE = "avg";
+    
+    public static final String AGGRERATION_AVG_MIN = "min";
+    
+    public static final String AGGRERATION_AVG_MAX = "max";
 
     public static final String OPERATOR_AND = "AND";
 

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/caseaggregation/DefaultCaseAggregationConditionService.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/caseaggregation/DefaultCaseAggregationConditionService.java	2013-02-07 08:12:12 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/caseaggregation/DefaultCaseAggregationConditionService.java	2013-02-07 13:44:38 +0000
@@ -226,31 +226,32 @@
     public Integer parseConditition( CaseAggregationCondition aggregationCondition, OrganisationUnit orgunit,
         Period period )
     {
-        String sql = convertCondition( aggregationCondition, orgunit, period );
         String operator = aggregationCondition.getOperator();
 
         if ( operator.equals( CaseAggregationCondition.AGGRERATION_COUNT )
             || operator.equals( CaseAggregationCondition.AGGRERATION_SUM ) )
         {
+            String sql = convertCondition( aggregationCondition, orgunit, period );
             Collection<Integer> ids = aggregationConditionStore.executeSQL( sql );
             return (ids == null) ? null : ids.size();
         }
 
-        String sumSql = "SELECT sum( cast( pdv.value as DOUBLE PRECISION ) ) ";
-        sumSql += "FROM patientdatavalue pdv ";
-        sumSql += "    INNER JOIN programstageinstance psi  ";
-        sumSql += "    ON psi.programstageinstanceid = pdv.programstageinstanceid ";
-        sumSql += "WHERE executiondate >= '" + DateUtils.getMediumDateString( period.getStartDate() ) + "'  ";
-        sumSql += "    AND executiondate>='" + DateUtils.getMediumDateString( period.getStartDate() )
+        String sql = "SELECT " + operator + "( cast( pdv.value as DOUBLE PRECISION ) ) ";
+        sql += "FROM patientdatavalue pdv ";
+        sql += "    INNER JOIN programstageinstance psi  ";
+        sql += "    ON psi.programstageinstanceid = pdv.programstageinstanceid ";
+        sql += "WHERE executiondate >= '" + DateUtils.getMediumDateString( period.getStartDate() ) + "'  ";
+        sql += "    AND executiondate>='" + DateUtils.getMediumDateString( period.getEndDate() )
             + "' AND pdv.dataelementid=" + aggregationCondition.getDeSum().getId();
-       
-        if ( !sql.trim().isEmpty() )
+
+        if ( aggregationCondition.getAggregationExpression() != null
+            && !aggregationCondition.getAggregationExpression().isEmpty() )
         {
-            sql = sumSql + " AND pdv.programstageinstanceid in ( " + sql + " ) ";
+            sql = sql + " AND pdv.programstageinstanceid in ( "
+                + convertCondition( aggregationCondition, orgunit, period ) + " ) ";
         }
-        
+
         Collection<Integer> ids = aggregationConditionStore.executeSQL( sql );
-
         return (ids == null) ? null : ids.iterator().next();
     }
 
@@ -802,7 +803,7 @@
             from = "FROM programstageinstance as psi "
                 + "INNER JOIN patientdatavalue as pd ON psi.programstageinstanceid = pd.programstageinstanceid ";
         }
-        
+
         sql += from + " WHERE pd.dataelementid=" + dataElementId + "  AND psi.organisationunitid=" + orgunitId
             + "             AND psi.executionDate>='" + startDate + "' AND psi.executionDate <= '" + endDate + "'";
 
@@ -914,7 +915,7 @@
         String sql = "SELECT distinct(pi.patientid) FROM programinstance as pi "
             + "inner join patient psi on psi.patientid=pi.patientid ";
 
-        if ( operator.equals( AGGRERATION_SUM )|| operator.equals( AGGRERATION_SUM_VALUE )  )
+        if ( operator.equals( AGGRERATION_SUM ) || operator.equals( AGGRERATION_SUM_VALUE ) )
         {
             sql = "SELECT psi.programstageinstanceid FROM programinstance as pi "
                 + "INNER JOIN programstageinstance psi ON pi.programinstanceid=psi.programinstanceid ";

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/startup/TableAlteror.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/startup/TableAlteror.java	2013-01-23 04:48:39 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/startup/TableAlteror.java	2013-02-07 13:44:38 +0000
@@ -193,6 +193,8 @@
             executeSql( "ALTER TABLE patientdatavalue DROP COLUMN organisationUnitid" );
             executeSql( "ALTER TABLE patientdatavalue DROP COLUMN providedByAnotherFacility" );
             executeSql( "ALTER TABLE patientdatavalue ADD PRIMARY KEY ( programstageinstanceid, dataelementid )" );
+            
+            executeSql( "update caseaggregationcondition set \"operator\"='times' where \"operator\"='SUM'" );
         }
         catch ( Exception ex )
         {

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/caseaggregation/TestCaseAggregationConditionAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/caseaggregation/TestCaseAggregationConditionAction.java	2013-02-07 08:12:12 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/caseaggregation/TestCaseAggregationConditionAction.java	2013-02-07 13:44:38 +0000
@@ -33,6 +33,7 @@
 import org.hisp.dhis.caseaggregation.CaseAggregationCondition;
 import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.AGGRERATION_COUNT;
 import org.hisp.dhis.caseaggregation.CaseAggregationConditionService;
+import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.i18n.I18n;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.period.Period;
@@ -61,6 +62,13 @@
         this.aggregationConditionService = aggregationConditionService;
     }
 
+    private DataElementService dataElementService;
+
+    public void setDataElementService( DataElementService dataElementService )
+    {
+        this.dataElementService = dataElementService;
+    }
+
     private I18n i18n;
 
     public void setI18n( I18n i18n )
@@ -86,6 +94,13 @@
         this.operator = operator;
     }
 
+    private Integer deSumId;
+
+    public void setDeSumId( Integer deSumId )
+    {
+        this.deSumId = deSumId;
+    }
+
     private String message;
 
     public String getMessage()
@@ -101,8 +116,11 @@
     public String execute()
         throws Exception
     {
-        CaseAggregationCondition aggCondition = new CaseAggregationCondition( "", operator, condition, null,
-            null );
+        CaseAggregationCondition aggCondition = new CaseAggregationCondition( "", operator, condition, null, null );
+        if ( deSumId != null )
+        {
+            aggCondition.setDeSum( dataElementService.getDataElement( deSumId ) );
+        }
 
         Collection<Program> programs = aggregationConditionService.getProgramsInCondition( condition );
 

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/resources/META-INF/dhis/beans.xml	2013-02-05 08:50:26 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/resources/META-INF/dhis/beans.xml	2013-02-07 13:44:38 +0000
@@ -825,6 +825,9 @@
 		<property name="aggregationConditionService">
 			<ref bean="org.hisp.dhis.caseaggregation.CaseAggregationConditionService" />
 		</property>
+		<property name="dataElementService">
+			<ref bean="org.hisp.dhis.dataelement.DataElementService" />
+		</property>
 	</bean>
 
 	<bean

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/resources/org/hisp/dhis/patient/i18n_module.properties'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/resources/org/hisp/dhis/patient/i18n_module.properties	2013-02-07 08:12:12 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/resources/org/hisp/dhis/patient/i18n_module.properties	2013-02-07 13:44:38 +0000
@@ -346,5 +346,8 @@
 attributes = Attributes
 left_side_expression = Left side expression
 right_side_expression = Right side expression
-sum_dataelement_value = Sum values of data element
-data_element_for_summary = Data element for summary
\ No newline at end of file
+sum_dataelement_value = Sum of data element values
+data_element_for_sum_avg = Data element for sum/average 
+avg_dataelement_value = Average of data element values
+min_dataelement_value = Mininum of data element values
+max_dataelement_value = Maximun of data element values
\ No newline at end of file

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/addCaseAggregation.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/addCaseAggregation.vm	2013-02-07 08:12:12 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/addCaseAggregation.vm	2013-02-07 13:44:38 +0000
@@ -52,8 +52,11 @@
 		<td><label for="operator">$i18n.getString( "operator" )</label></td>
 		<td>
 			<input type="radio" id="operator" name="operator" value="COUNT" checked onchange='operatorOnchange(this.value)'>$i18n.getString('number_of_patients')<br>
-			<input type="radio" id="operator" name="operator" value="SUM" onchange='operatorOnchange(this.value)'> $i18n.getString('number_of_visits')<br>
-			<input type="radio" id="operator" name="operator" value="SUM_VALUE" onchange='operatorOnchange(this.value)'> $i18n.getString('sum_dataelement_value')
+			<input type="radio" id="operator" name="operator" value="times" onchange='operatorOnchange(this.value)'> $i18n.getString('number_of_visits')<br>
+			<input type="radio" id="operator" name="operator" value="sum" onchange='operatorOnchange(this.value)'> $i18n.getString('sum_dataelement_value')<br>
+			<input type="radio" id="operator" name="operator" value="avg" onchange='operatorOnchange(this.value)'> $i18n.getString('avg_dataelement_value')<br>
+			<input type="radio" id="operator" name="operator" value="min" onchange='operatorOnchange(this.value)'> $i18n.getString('min_dataelement_value')<br>
+			<input type="radio" id="operator" name="operator" value="max" onchange='operatorOnchange(this.value)'> $i18n.getString('max_dataelement_value')
 		</td>
 	</tr>
 	
@@ -82,7 +85,7 @@
 	
 	<tr>
 		<td>
-			<label for="deSumId">$i18n.getString( "data_element_for_summary" ) <em title="$i18n.getString( "required" )" class="required">*</em></label>
+			<label for="deSumId">$i18n.getString( "data_element_for_sum_avg" ) <em title="$i18n.getString( "required" )" class="required">*</em></label>
 		</td>
 		<td>
 			<select id="deSumId" name="deSumId" disabled class="{validate:{required:true}}"></select>

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/caseAggregationForm.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/caseAggregationForm.vm	2012-11-12 05:15:33 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/caseAggregationForm.vm	2013-02-07 13:44:38 +0000
@@ -116,7 +116,7 @@
 		<td>
 			<fieldset>
 				<legend>$i18n.getString( "condition" )</legend>
-				<textarea id="aggregationCondition" name="aggregationCondition" class="{validate:{required:true}}" onkeyup='getConditionDescription();' maxlength="254">$!caseAggregation.aggregationExpression</textarea>
+				<textarea id="aggregationCondition" name="aggregationCondition" onkeyup='getConditionDescription();' maxlength="254">$!caseAggregation.aggregationExpression</textarea>
 			</fieldset>
 		</td>
 	</tr>

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/caseaggregation.js'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/caseaggregation.js	2013-02-07 08:12:12 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/caseaggregation.js	2013-02-07 13:44:38 +0000
@@ -301,6 +301,7 @@
 	$.postUTF8( 'testCaseAggregationCondition.action', 
 		{ 
 			condition: getFieldValue('aggregationCondition'),
+			deSumId: getFieldValue('deSumId'),
 			operator: operator
 		},function (json)
 		{
@@ -396,7 +397,8 @@
 
 function operatorOnchange(operator)
 {
-	if(operator=='SUM_VALUE'){
+	if( operator=='sum' || operator=='avg' 
+		|| operator=='min' || operator=='max' ){
 		enable('deSumId');
 	}
 	else{

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/updateCaseAggregation.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/updateCaseAggregation.vm	2013-02-07 08:12:12 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/updateCaseAggregation.vm	2013-02-07 13:44:38 +0000
@@ -50,8 +50,11 @@
 		<td><label for="operator">$i18n.getString( "operator" )</label></td>
 		<td>
 			<input type="radio" id="operator" name="operator" value="COUNT" onchange='operatorOnchange(this.value)' #if($caseAggregation.operator=="COUNT") checked #end >  $i18n.getString('number_of_patients')<br>
-			<input type="radio" id="operator" name="operator" value="SUM" onchange='operatorOnchange(this.value)' #if($caseAggregation.operator=="SUM") checked #end > $i18n.getString('number_of_visits')<br>
-			<input type="radio" id="operator" name="operator" value="SUM_VALUE" onchange='operatorOnchange(this.value)' #if($caseAggregation.operator=="SUM_VALUE") checked #end > $i18n.getString('sum_dataelement_value')
+			<input type="radio" id="operator" name="operator" value="times" onchange='operatorOnchange(this.value)' #if($caseAggregation.operator=="times") checked #end > $i18n.getString('number_of_visits')<br>
+			<input type="radio" id="operator" name="operator" value="sum" onchange='operatorOnchange(this.value)' #if($caseAggregation.operator=="sum") checked #end > $i18n.getString('sum_dataelement_value')<br>
+			<input type="radio" id="operator" name="operator" value="avg" onchange='operatorOnchange(this.value)' #if($caseAggregation.operator=="avg") checked #end > $i18n.getString('avg_dataelement_value')<br>
+			<input type="radio" id="operator" name="operator" value="min" onchange='operatorOnchange(this.value)' #if($caseAggregation.operator=="min") checked #end > $i18n.getString('min_dataelement_value')<br>
+			<input type="radio" id="operator" name="operator" value="max" onchange='operatorOnchange(this.value)' #if($caseAggregation.operator=="max") checked #end > $i18n.getString('max_dataelement_value')
 		</td>
 	</tr>
 	<tr>
@@ -79,7 +82,7 @@
 	
 	<tr>
 		<td>
-			<label for="deSumId">$i18n.getString( "data_element_for_summary" )  <em title="$i18n.getString( "required" )" class="required">*</em></label>
+			<label for="deSumId">$i18n.getString( "data_element_for_sum_avg" )  <em title="$i18n.getString( "required" )" class="required">*</em></label>
 		</td>
 		<td>
 			<select id="deSumId" name="deSumId" class="{validate:{required:true}}" #if( $caseAggregation.deSum ) #else disabled #end>