← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16587: Improve function of showing the details of aggregate query builder result in Manual Aggregate.

 

------------------------------------------------------------
revno: 16587
committer: Tran Chau<tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2014-08-30 01:08:10 +0700
message:
  Improve function of showing the details of aggregate query builder result in Manual Aggregate.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/caseaggregation/CaseAggregationConditionStore.java
  dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/DefaultCaseAggregationConditionService.java
  dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/hibernate/HibernateCaseAggregationConditionStore.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/org/hisp/dhis/caseentry/i18n_module.properties


--
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/CaseAggregationConditionStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/caseaggregation/CaseAggregationConditionStore.java	2014-08-03 16:27:47 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/caseaggregation/CaseAggregationConditionStore.java	2014-08-29 18:08:10 +0000
@@ -105,11 +105,12 @@
      * @param caseAggregationCondition CaseAggregationCondition
      * @param orgunitIds The list of {@link OrganisationUnit} ids
      * @param period {@link Period}
+     * @param nonRegistrationProgram Specify if the expression of aggregationCondition has any single event without registration program
      * @param format I18nFormat
      * @param i18n I18n
      */
     Grid getAggregateValueDetails( CaseAggregationCondition aggregationCondition, OrganisationUnit orgunit,
-        Period period, I18nFormat format, I18n i18n );
+        Period period, boolean nonRegistrationProgram, I18nFormat format, I18n i18n );
 
     /**
      * Insert data values into database directly
@@ -162,10 +163,11 @@
      *        MIN and MAX
      * @param orgunitIds The id of {@link OrganisationUnit}
      * @param period The period for retrieving data
+     * @Param nonRegistrationProgram Specify if the expression of aggregationCondition has any single event without registration program
      * 
      * @return SQL
      */
-    String parseExpressionDetailsToSql( String caseExpression, String operator, Integer orgunitId, Period period );
+    String parseExpressionDetailsToSql( String caseExpression, String operator, Integer orgunitId, Period period, boolean nonRegistrationProgram );
 
     /**
      * Get list of {@link TrackedEntityInstance} ids from SQL

=== modified file 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/DefaultCaseAggregationConditionService.java'
--- dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/DefaultCaseAggregationConditionService.java	2014-08-15 07:40:20 +0000
+++ dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/DefaultCaseAggregationConditionService.java	2014-08-29 18:08:10 +0000
@@ -441,7 +441,8 @@
     {
         periodService.reloadPeriod( period );
 
-        return aggregationConditionStore.getAggregateValueDetails( aggregationCondition, orgunit, period, format, i18n );
+        boolean nonRegistrationProgram = singleEventWithoutRegistrationProgram( aggregationCondition.getAggregationExpression() );
+        return aggregationConditionStore.getAggregateValueDetails( aggregationCondition, orgunit, period, nonRegistrationProgram, format, i18n  );
     }
 
     public void insertAggregateValue( CaseAggregationCondition caseAggregationCondition,
@@ -464,7 +465,8 @@
     {
         periodService.reloadPeriod( period );
 
-        return aggregationConditionStore.parseExpressionDetailsToSql( caseExpression, operator, orgunitId, period );
+        boolean nonRegistrationProgram = singleEventWithoutRegistrationProgram( caseExpression );
+        return aggregationConditionStore.parseExpressionDetailsToSql( caseExpression, operator, orgunitId, period, nonRegistrationProgram );
     }
 
     @Override
@@ -522,4 +524,64 @@
         return entityInstanceIds.size();
     }
 
+    private boolean singleEventWithoutRegistrationProgram( String expression )
+    {
+        Pattern patternCondition = Pattern.compile( CaseAggregationCondition.regExp );
+
+        Matcher matcher = patternCondition.matcher( expression );
+
+        while ( matcher.find() )
+        {
+            String match = matcher.group();
+            match = match.replaceAll( "[\\[\\]]", "" );
+
+            String[] info = match.split( SEPARATOR_OBJECT );
+
+            if ( info[0].equalsIgnoreCase( OBJECT_PROGRAM_STAGE_DATAELEMENT ) )
+            {
+                String[] ids = info[1].split( SEPARATOR_ID );
+                
+                int programId = Integer.parseInt( ids[0] );
+                Program program = programService.getProgram( programId );
+
+                if ( program != null && !program.isRegistration() )
+                {
+                    return true;
+                }
+            }
+            else
+            {
+                String[] ids = info[1].split( SEPARATOR_ID );
+
+                if ( info[0].equalsIgnoreCase( OBJECT_TRACKED_ENTITY_ATTRIBUTE ) )
+                {
+                    return false;
+                }
+                else if ( info[0].equalsIgnoreCase( OBJECT_PROGRAM ) )
+                {
+                    int objectId = Integer.parseInt( ids[0] );
+
+                    Program program = programService.getProgram( objectId );
+
+                    if ( program != null && !program.isRegistration() )
+                    {
+                        return true;
+                    }
+                }
+                else if ( info[0].equalsIgnoreCase( OBJECT_PROGRAM_STAGE )
+                    || info[0].equalsIgnoreCase( OBJECT_ORGUNIT_COMPLETE_PROGRAM_STAGE ) )
+                {
+                    int objectId = Integer.parseInt( ids[0] );
+                    ProgramStage programStage = programStageService.getProgramStage( objectId );
+                    if (programStage!=null && !programStage.getProgram().isRegistration() )
+                    {
+                        return true;
+                    }
+                }
+            }
+
+        }
+System.out.println("\n\n\n ==== \n false ");
+        return false;
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/hibernate/HibernateCaseAggregationConditionStore.java'
--- dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/hibernate/HibernateCaseAggregationConditionStore.java	2014-08-29 11:02:02 +0000
+++ dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/hibernate/HibernateCaseAggregationConditionStore.java	2014-08-29 18:08:10 +0000
@@ -231,14 +231,14 @@
 
     @Override
     public Grid getAggregateValueDetails( CaseAggregationCondition aggregationCondition, OrganisationUnit orgunit,
-        Period period, I18nFormat format, I18n i18n )
+        Period period, boolean nonRegistrationProgram, I18nFormat format, I18n i18n )
     {
         Grid grid = new ListGrid();
         grid.setTitle( orgunit.getName() + " - " + aggregationCondition.getDisplayName() );
         grid.setSubtitle( format.formatPeriod( period ) );
 
         String sql = parseExpressionDetailsToSql( aggregationCondition.getAggregationExpression(),
-            aggregationCondition.getOperator(), orgunit.getId(), period );
+            aggregationCondition.getOperator(), orgunit.getId(), period, nonRegistrationProgram );
 
         SqlRowSet rs = jdbcTemplate.queryForRowSet( sql );
 
@@ -1016,12 +1016,11 @@
     }
 
     @Override
-    public String parseExpressionDetailsToSql( String caseExpression, String operator, Integer orgunitId, Period period )
+    public String parseExpressionDetailsToSql( String caseExpression, String operator, Integer orgunitId,
+        Period period, boolean nonRegistrationProgram )
     {
         String sql = "SELECT ";
-
-        boolean hasDataelement = hasDataelementCriteria( caseExpression );
-        boolean hasEntityInstance = hasEntityInstanceCriteria( caseExpression );
+        
         Collection<Integer> orgunitIds = new HashSet<>();
         orgunitIds.add( orgunitId );
 
@@ -1032,41 +1031,34 @@
                     DateUtils.getMediumDateString( period.getStartDate() ),
                     DateUtils.getMediumDateString( period.getEndDate() ) );
         }
-        else  if ( hasDataelement )
-        {
-            sql += "pdv.programstageinstanceid as event, pdv.value,pgs.name as program_stage, psi.executiondate as report_date,";
-        }
-        else
-        {
-            sql += "p.trackedentityinstanceid,ou.name,";
-        }
-
-        sql = sql.substring( 0, sql.length() - 1 );
-        sql += " FROM ";
-
-        if( hasEntityInstance )
-        {
-            sql += " programinstance as pi INNER JOIN trackedentityinstance p on p.trackedentityinstanceid=pi.trackedentityinstanceid";
-            sql += " INNER JOIN organisationunit ou ON ou.organisationunitid=p.organisationunitid ";
-        }
-        
-        if ( hasDataelement )
-        {
-            if( hasEntityInstance )
-            {
-                sql += " INNER JOIN programinstance as pi on pi.trackedentityinstanceid=p.trackedentityinstanceid ";
-            }
-            else
-            {
-                sql += " programinstance as pi ";
-            }
-            sql += " INNER JOIN programstageinstance psi ON pi.programinstanceid=psi.programinstanceid ";
+        else if ( nonRegistrationProgram )
+        {
+            sql += " pdv.programstageinstanceid as event, pgs.name as program_stage, de.name as data_element, pdv.value, psi.executiondate as report_date ";
+            sql += " FROM programstageinstance psi inner join programinstance pi on pi.programinstanceid=psi.programinstanceid ";
             sql += " INNER JOIN organisationunit ou ON ou.organisationunitid=psi.organisationunitid ";
             sql += " INNER JOIN trackedentitydatavalue pdv ON pdv.programstageinstanceid=psi.programstageinstanceid ";
-            sql += " INNER JOIN program pg ON pg.programid=pi.programid ";
+            sql += " INNER JOIN dataelement de ON de.dataelementid=pdv.dataelementid ";
+            sql += " INNER JOIN program pg on pg.programid=pi.programid ";
             sql += " INNER JOIN programstage pgs ON pgs.programid=pg.programid ";
         }
-        
+        else
+        {
+            sql += " p.trackedentityinstanceid as tracked_entity_instance, tea.name as attribute, ";
+            sql += "teav.value as value, pg.name as program ";
+            sql += "FROM trackedentityinstance p ";
+            sql += "INNER JOIN trackedentityattributevalue teav on p.trackedentityinstanceid=teav.trackedentityinstanceid  ";
+            sql += "INNER JOIN trackedentityattribute tea on tea.trackedentityattributeid=teav.trackedentityattributeid  ";
+            sql += "INNER JOIN programinstance as pi on p.trackedentityinstanceid=pi.trackedentityinstanceid  ";
+            sql += "INNER JOIN program pg on pg.programid=pi.programid  ";
+            sql += "INNER JOIN programstage pgs on pgs.programid=pg.programid  ";
+            
+            if( hasDataelementCriteria( caseExpression ) )
+            {
+                sql += " INNER JOIN programstageinstance psi on pi.programinstanceid=psi.programinstanceid ";
+                sql += " INNER JOIN organisationunit ou on ou.organisationunitid=psi.organisationunitid ";
+                sql += " INNER JOIN trackedentitydatavalue pdv on pdv.programstageinstanceid=psi.programstageinstanceid ";
+            }
+        }       
 
         sql += " WHERE "
             + createSQL( caseExpression, operator, orgunitIds, DateUtils.getMediumDateString( period.getStartDate() ),
@@ -1074,6 +1066,15 @@
 
         sql = sql.replaceAll( "COMBINE", "" );
 
+        if ( nonRegistrationProgram )
+        {
+            sql += " ORDER BY pdv.programstageinstanceid";
+        }
+        else
+        {
+            sql += " ORDER BY  p.trackedentityinstanceid ";
+        }
+
         return sql;
     }
 
@@ -1207,10 +1208,10 @@
         return false;
     }
 
-    private boolean hasDataelementCriteria( String expresstion )
+    private boolean hasDataelementCriteria( String expression )
     {
         Pattern pattern = Pattern.compile( CaseAggregationCondition.regExp );
-        Matcher matcher = pattern.matcher( expresstion );
+        Matcher matcher = pattern.matcher( expression );
         while ( matcher.find() )
         {
             String match = matcher.group();
@@ -1230,5 +1231,6 @@
 
         return false;
     }
+    
 
 }

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/org/hisp/dhis/caseentry/i18n_module.properties'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/org/hisp/dhis/caseentry/i18n_module.properties	2014-08-24 12:31:24 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/org/hisp/dhis/caseentry/i18n_module.properties	2014-08-29 18:08:10 +0000
@@ -695,4 +695,5 @@
 message_is_sent = Message is sent
 messsage = Message
 please_select_an_orgunit = Please select an organisation unit
-event=Event
\ No newline at end of file
+event=Event
+data_element = Data element
\ No newline at end of file