← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15304: Add statement to update aggregate query builder formulas from 2.13 to 2.15

 

------------------------------------------------------------
revno: 15304
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-05-19 22:53:36 +0800
message:
  Add statement to update aggregate query builder formulas from 2.13 to 2.15
modified:
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/startup/TableAlteror.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-tracker/src/main/java/org/hisp/dhis/trackedentity/startup/TableAlteror.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/startup/TableAlteror.java	2014-05-17 12:04:42 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/startup/TableAlteror.java	2014-05-19 14:53:36 +0000
@@ -36,6 +36,7 @@
 import org.amplecode.quick.StatementManager;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.hisp.dhis.caseaggregation.CaseAggregationCondition;
 import org.hisp.dhis.common.CodeGenerator;
 import org.hisp.dhis.jdbc.StatementBuilder;
 import org.hisp.dhis.system.startup.AbstractStartupRoutine;
@@ -266,12 +267,74 @@
         executeSql( "DROP TABLE orgunitgroupprograms" ); 
         
         executeSql( "UPDATE trackedentityattribute SET valuetype='combo' WHERE valuetype='optionSet'" );
+        
+        updateAggregateQueryBuilder();
     }
 
     // -------------------------------------------------------------------------
     // Supporting methods
     // -------------------------------------------------------------------------
 
+    private void updateAggregateQueryBuilder()
+    {
+        StatementHolder holder = statementManager.getHolder();
+
+        try
+        {
+            Statement statement = holder.getStatement();
+            ResultSet resultSet = statement
+                .executeQuery( "select trackedentityattributeid from trackedentityattribute where name='Age'" );
+
+            if ( resultSet.next() )
+            {
+                int id = resultSet.getInt( "trackedentityattributeid" );
+                
+                String source = "PC:DATE@executionDate#-DATE@birthDate#";
+                String target = CaseAggregationCondition.OBJECT_TRACKED_ENTITY_ATTRIBUTE
+                    + CaseAggregationCondition.SEPARATOR_OBJECT + id + ".visit";
+
+                updateFixedAttributeInCaseAggregate( source, target );
+            }
+        }
+        catch ( Exception ex )
+        {
+            ex.printStackTrace();
+        }
+    }
+
+    private void updateFixedAttributeInCaseAggregate( String source, String target )
+    {
+        StatementHolder holder = statementManager.getHolder();
+        try
+        {
+            Statement statement = holder.getStatement();
+            ResultSet resultSet = statement
+                .executeQuery( "SELECT caseaggregationconditionid, aggregationExpression FROM caseaggregationcondition where aggregationExpression like '%"
+                    + source + "%'" );
+
+            source = source.replaceAll( "@", "\\@" ).replaceAll( "#", "\\#" );
+
+            while ( resultSet.next() )
+            {
+                String id = resultSet.getString( "caseaggregationconditionid" );
+                String expression = resultSet.getString( "aggregationExpression" );
+
+                expression = expression.replaceAll( source, target );
+                expression = expression.replaceAll( "'", "\"" );
+                executeSql( "UPDATE caseaggregationcondition SET aggregationExpression='" + expression
+                    + "'  WHERE caseaggregationconditionid=" + id );
+            }
+        }
+        catch ( Exception ex )
+        {
+            log.debug( ex );
+        }
+        finally
+        {
+            holder.close();
+        }
+    }
+
     private void updateProgramInstanceStatus()
     {
         // Set active status for events