← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10750: Improve Aggregate Query Builder (WIP).

 

------------------------------------------------------------
revno: 10750
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2013-05-03 10:13:36 +0700
message:
  Improve Aggregate Query Builder (WIP).
modified:
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/caseaggregation/jdbc/JdbcCaseAggregationConditionManager.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-patient/src/main/java/org/hisp/dhis/caseaggregation/jdbc/JdbcCaseAggregationConditionManager.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/caseaggregation/jdbc/JdbcCaseAggregationConditionManager.java	2013-05-02 03:51:12 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/caseaggregation/jdbc/JdbcCaseAggregationConditionManager.java	2013-05-03 03:13:36 +0000
@@ -296,10 +296,21 @@
                     sql += operator + " (distinct(psi.programinstanceid ) ) as value ";
                 }
 
-                sql += "FROM programstageinstance as psi "
-                    + "INNER JOIN programinstance as pi ON pi.programinstanceid = psi.programinstanceid "
-                    + "INNER JOIN organisationunit ou on ou.organisationunitid=psi.organisationunitid "
-                    + "INNER JOIN patient p on p.patientid=pi.patientid WHERE "
+                sql += "FROM programstageinstance as psi ";
+                boolean hasPatients = hasPatientCriteria( caseExpression );
+                boolean hasProgramInstances = hasProgramInstanceCriteria( caseExpression );
+
+                if ( hasPatients )
+                {
+                    sql += "INNER JOIN patient p on p.patientid=pi.patientid  ";
+                    sql += "INNER JOIN programinstance as pi ON pi.programinstanceid = psi.programinstanceid ";
+                }
+                if ( (hasProgramInstances && !hasPatients)
+                    || operator.equals( CaseAggregationCondition.AGGRERATION_COUNT ) )
+                {
+                    sql += "INNER JOIN programinstance as pi ON pi.programinstanceid = psi.programinstanceid ";
+                }
+                sql += "INNER JOIN organisationunit ou on ou.organisationunitid=psi.organisationunitid WHERE "
                     + createSQL( caseExpression, operator, orgunitIds,
                         DateUtils.getMediumDateString( period.getStartDate() ),
                         DateUtils.getMediumDateString( period.getEndDate() ) );
@@ -769,8 +780,7 @@
     private String getConditionForCountProgramStage( String programStageId, String operator,
         Collection<Integer> orgunitIds, String startDate, String endDate )
     {
-        String sql = " EXISTS ( SELECT * " + "FROM programstageinstance as _psi "
-            + "INNER JOIN programinstance as _pi ON psi.programinstanceid=psi.programinstanceid "
+        String sql = " EXISTS ( SELECT * FROM programstageinstance as _psi "
             + "WHERE psi.programstageinstanceid=_psi.programstageinstanceid AND _psi.organisationunitid in ("
             + TextUtils.getCommaDelimitedString( orgunitIds ) + ") and _psi.programstageid = " + programStageId + " "
             + "AND _psi.executionDate >= '" + startDate + "' AND _psi.executionDate <= '" + endDate + "' "
@@ -869,4 +879,49 @@
         return false;
     }
 
+    public boolean hasPatientCriteria( String expresstion )
+    {
+        Pattern pattern = Pattern.compile( CaseAggregationCondition.regExp );
+        Matcher matcher = pattern.matcher( expresstion );
+        while ( matcher.find() )
+        {
+            String match = matcher.group();
+
+            match = match.replaceAll( "[\\[\\]]", "" );
+
+            String[] info = match.split( SEPARATOR_OBJECT );
+
+            if ( info[0].equalsIgnoreCase( CaseAggregationCondition.OBJECT_PATIENT )
+                || info[0].equalsIgnoreCase( CaseAggregationCondition.OBJECT_PATIENT_PROPERTY ) )
+            {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    public boolean hasProgramInstanceCriteria( String expresstion )
+    {
+        Pattern pattern = Pattern.compile( CaseAggregationCondition.regExp );
+        Matcher matcher = pattern.matcher( expresstion );
+        while ( matcher.find() )
+        {
+            String match = matcher.group();
+
+            match = match.replaceAll( "[\\[\\]]", "" );
+
+            String[] info = match.split( SEPARATOR_OBJECT );
+
+            if ( info[0].equalsIgnoreCase( CaseAggregationCondition.OBJECT_PROGRAM_PROPERTY )
+                || info[0].equalsIgnoreCase( CaseAggregationCondition.OBJECT_PROGRAM )
+                || info[0].equalsIgnoreCase( CaseAggregationCondition.OBJECT_PROGRAM_STAGE ) )
+            {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
 }