← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6828: (patient) Sort result by orgunits in Tabular report.

 

------------------------------------------------------------
revno: 6828
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2012-05-02 15:15:45 +0700
message:
  (patient) Sort result by orgunits in Tabular report.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceStore.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramInstanceService.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramStageInstanceService.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/report/GenerateTabularReportAction.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/jsonTabularReport.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/ProgramStageInstanceService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceService.java	2012-04-18 10:28:39 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceService.java	2012-05-02 08:15:45 +0000
@@ -89,11 +89,11 @@
 
     List<ProgramStageInstance> getProgramStageInstances( Patient patient, Boolean completed );
             
-    List<ProgramStageInstance> searchProgramStageInstances( ProgramStage programStage, Map<Integer,String> searchingIdenKeys, Map<Integer,String> searchingAttrKeys, Map<Integer,String> searchingDEKeys, Collection<Integer> orgunitIds, Date startDate, Date endDate, boolean orderByOrgunitAsc, boolean orderByExecutionDateByAsc, int min, int max );
+    List<ProgramStageInstance> searchProgramStageInstances( ProgramStage programStage, Map<Integer,String> searchingIdenKeys, Map<Integer,String> searchingAttrKeys, Map<Integer,String> searchingDEKeys, Collection<Integer> upperOrgunitIds, Collection<Integer> bottomOrgunitIds, Date startDate, Date endDate, boolean orderByOrgunitAsc, boolean orderByExecutionDateByAsc, int min, int max );
 
-    Grid getTabularReport( ProgramStage programStage, List<Boolean> hiddenCols, List<PatientIdentifierType> idens, List<String> fixedAttributes, List<PatientAttribute> attributes, List<DataElement> dataElements, Map<Integer,String> searchingIdenKeys, Map<Integer,String> searchingAttrKeys, Map<Integer,String> searchingDEKeys, Collection<Integer> orgunitIds, int level, Date startDate, Date endDate, boolean orderByOrgunitAsc, boolean orderByExecutionDateByAsc, int min, int max, I18nFormat format, I18n i18n );
+    Grid getTabularReport( ProgramStage programStage, List<Boolean> hiddenCols, List<PatientIdentifierType> idens, List<String> fixedAttributes, List<PatientAttribute> attributes, List<DataElement> dataElements, Map<Integer,String> searchingIdenKeys, Map<Integer,String> searchingAttrKeys, Map<Integer,String> searchingDEKeys, Collection<Integer> upperOrgunitIds, Collection<Integer> bottomOrgunitIds, int level, Date startDate, Date endDate, boolean orderByOrgunitAsc, boolean orderByExecutionDateByAsc, int min, int max, I18nFormat format, I18n i18n );
     
-    Grid getTabularReport( ProgramStage programStage, List<Boolean> hiddenCols, List<PatientIdentifierType> idens, List<String> fixedAttributes, List<PatientAttribute> attributes, List<DataElement> dataElements, Map<Integer,String> searchingIdenKeys, Map<Integer,String> searchingAttrKeys, Map<Integer,String> searchingDEKeys, Collection<Integer> orgunitIds, int level, Date startDate, Date endDate, boolean orderByOrgunitAsc, boolean orderByExecutionDateByAsc, I18nFormat format, I18n i18n );
+    Grid getTabularReport( ProgramStage programStage, List<Boolean> hiddenCols, List<PatientIdentifierType> idens, List<String> fixedAttributes, List<PatientAttribute> attributes, List<DataElement> dataElements, Map<Integer,String> searchingIdenKeys, Map<Integer,String> searchingAttrKeys, Map<Integer,String> searchingDEKeys, Collection<Integer> upperOrgunitIds, Collection<Integer> bottomOrgunitIds, int level, Date startDate, Date endDate, boolean orderByOrgunitAsc, boolean orderByExecutionDateByAsc, I18nFormat format, I18n i18n );
 
     int countProgramStageInstances( ProgramStage programStage, Map<Integer,String> searchingIdenKeys, Map<Integer,String> searchingAttrKeys, Map<Integer,String> searchingDEKeys, Collection<Integer> orgunitIds, Date startDate, Date endDate );
     

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceStore.java	2012-04-18 04:03:50 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceStore.java	2012-05-02 08:15:45 +0000
@@ -71,9 +71,9 @@
     
     List<ProgramStageInstance> get( ProgramStage programStage, OrganisationUnit orgunit, Date startDate, Date endDate, int min, int max );
     
-    List<ProgramStageInstance> get( ProgramStage programStage, Map<Integer,String> searchingIdenKeys, Map<Integer,String> searchingAttrKeys, Map<Integer,String> searchingDEKeys, Collection<Integer> orgunitIds, Date startDate, Date endDate, boolean orderByOrgunitAsc, boolean orderByExecutionDateByAsc, int min, int max );
+    List<ProgramStageInstance> get( ProgramStage programStage, Map<Integer,String> searchingIdenKeys, Map<Integer,String> searchingAttrKeys, Map<Integer,String> searchingDEKeys, Collection<Integer> upperOrgunitIds, Collection<Integer> bottomOrgunitIds, Date startDate, Date endDate, boolean orderByOrgunitAsc, boolean orderByExecutionDateByAsc, int min, int max );
     
-    List<ProgramStageInstance> get( ProgramStage programStage, Map<Integer,String> searchingIdenKeys, Map<Integer,String> searchingAttrKeys, Map<Integer,String> searchingDEKeys, Collection<Integer> orgunitIds, Date startDate, Date endDate, boolean orderByOrgunitAsc, boolean orderByExecutionDateByAsc );
+    List<ProgramStageInstance> get( ProgramStage programStage, Map<Integer,String> searchingIdenKeys, Map<Integer,String> searchingAttrKeys, Map<Integer,String> searchingDEKeys, Collection<Integer> upperOrgunitIds, Collection<Integer> bottomOrgunitIds, Date startDate, Date endDate, boolean orderByOrgunitAsc, boolean orderByExecutionDateByAsc );
 
     int count( ProgramStage programStage, Map<Integer,String> searchingIdenKeys, Map<Integer,String> searchingAttrKeys, Map<Integer,String> searchingKeys, Collection<Integer> orgunitIds, Date startDate, Date endDate );
     

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramInstanceService.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramInstanceService.java	2012-04-28 08:03:14 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramInstanceService.java	2012-05-02 08:15:45 +0000
@@ -222,7 +222,7 @@
 
         attrGrid.addRow();
         attrGrid.addValue( i18n.getString( "blood_group" ) );
-        attrGrid.addValue( (patient.getBloodGroup().isEmpty() ) ? PatientAttributeValue.UNKNOWN : patient.getBloodGroup() );
+        attrGrid.addValue( ( patient.getBloodGroup() == null || patient.getBloodGroup().isEmpty() ) ? PatientAttributeValue.UNKNOWN : patient.getBloodGroup() );
 
         attrGrid.addRow();
         attrGrid.addValue( i18n.getString( "phoneNumber" ) );

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramStageInstanceService.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramStageInstanceService.java	2012-04-23 12:19:27 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramStageInstanceService.java	2012-05-02 08:15:45 +0000
@@ -217,30 +217,30 @@
 
     public List<ProgramStageInstance> searchProgramStageInstances( ProgramStage programStage,
         Map<Integer, String> searchingIdenKeys, Map<Integer, String> searchingAttrKeys,
-        Map<Integer, String> searchingDEKeys, Collection<Integer> orgunitIds, Date startDate, Date endDate,
+        Map<Integer, String> searchingDEKeys, Collection<Integer> upperOrgunitIds, Collection<Integer> bottomOrgunitIds, Date startDate, Date endDate,
         boolean orderByOrgunitAsc, boolean orderByExecutionDateByAsc, int min, int max )
     {
         return programStageInstanceStore.get( programStage, searchingIdenKeys, searchingAttrKeys, searchingDEKeys,
-            orgunitIds, startDate, endDate, orderByOrgunitAsc, orderByExecutionDateByAsc, min, max );
+            upperOrgunitIds, bottomOrgunitIds, startDate, endDate, orderByOrgunitAsc, orderByExecutionDateByAsc, min, max );
     }
 
     public List<ProgramStageInstance> searchProgramStageInstances( ProgramStage programStage,
         Map<Integer, String> searchingIdenKeys, Map<Integer, String> searchingAttrKeys,
-        Map<Integer, String> searchingDEKeys, Collection<Integer> orgunitIds, Date startDate, Date endDate,
+        Map<Integer, String> searchingDEKeys, Collection<Integer> upperOrgunitIds, Collection<Integer> bottomOrgunitIds, Date startDate, Date endDate,
         boolean orderByOrgunitAsc, boolean orderByExecutionDateByAsc )
     {
         return programStageInstanceStore.get( programStage, searchingIdenKeys, searchingAttrKeys, searchingDEKeys,
-            orgunitIds, startDate, endDate, orderByOrgunitAsc, orderByExecutionDateByAsc );
+            upperOrgunitIds, bottomOrgunitIds, startDate, endDate, orderByOrgunitAsc, orderByExecutionDateByAsc );
     }
 
     public Grid getTabularReport( ProgramStage programStage, List<Boolean> hiddenCols,
         List<PatientIdentifierType> idens, List<String> fixedAttributes, List<PatientAttribute> attributes,
         List<DataElement> dataElements, Map<Integer, String> searchingIdenKeys, Map<Integer, String> searchingAttrKeys,
-        Map<Integer, String> searchingDEKeys, Collection<Integer> orgunitIds, int level, Date startDate, Date endDate,
+        Map<Integer, String> searchingDEKeys, Collection<Integer> upperOrgunitIds, Collection<Integer> bottomOrgunitIds, int level, Date startDate, Date endDate,
         boolean orderByOrgunitAsc, boolean orderByExecutionDateByAsc, int min, int max, I18nFormat format, I18n i18n )
     {
         List<ProgramStageInstance> programStageInstances = searchProgramStageInstances( programStage,
-            searchingIdenKeys, searchingAttrKeys, searchingDEKeys, orgunitIds, startDate, endDate, orderByOrgunitAsc,
+            searchingIdenKeys, searchingAttrKeys, searchingDEKeys, upperOrgunitIds, bottomOrgunitIds, startDate, endDate, orderByOrgunitAsc,
             orderByExecutionDateByAsc, min, max );
 
         return createTabularGrid( level, hiddenCols, programStage, programStageInstances, idens, fixedAttributes,
@@ -250,11 +250,11 @@
     public Grid getTabularReport( ProgramStage programStage, List<Boolean> hiddenCols,
         List<PatientIdentifierType> idens, List<String> fixedAttributes, List<PatientAttribute> attributes,
         List<DataElement> dataElements, Map<Integer, String> searchingIdenKeys, Map<Integer, String> searchingAttrKeys,
-        Map<Integer, String> searchingDEKeys, Collection<Integer> orgunitIds, int level, Date startDate, Date endDate,
+        Map<Integer, String> searchingDEKeys, Collection<Integer> upperOrgunitIds, Collection<Integer> bottomOrgunitIds, int level, Date startDate, Date endDate,
         boolean orderByOrgunitAsc, boolean orderByExecutionDateByAsc, I18nFormat format, I18n i18n )
     {
         List<ProgramStageInstance> programStageInstances = searchProgramStageInstances( programStage,
-            searchingIdenKeys, searchingAttrKeys, searchingDEKeys, orgunitIds, startDate, endDate, orderByOrgunitAsc,
+            searchingIdenKeys, searchingAttrKeys, searchingDEKeys, upperOrgunitIds, bottomOrgunitIds, startDate, endDate, orderByOrgunitAsc,
             orderByExecutionDateByAsc );
 
         return createTabularGrid( level, hiddenCols, programStage, programStageInstances, idens, fixedAttributes,
@@ -378,7 +378,7 @@
             if ( !hasMetaData )
             {
                 // Organisation units
-                for ( int i = level; i < maxLevel; i++ )
+                for ( int i = level; i <= maxLevel; i++ )
                 {
                     grid.addHeader( new GridHeader( organisationUnitService.getOrganisationUnitLevelByLevel( i )
                         .getName(), false, true ) );
@@ -394,7 +394,7 @@
             }
             else
             {
-                for ( int i = level; i < maxLevel; i++ )
+                for ( int i = level; i <= maxLevel; i++ )
                 {
                     grid.addHeader( new GridHeader( organisationUnitService.getOrganisationUnitLevelByLevel( i )
                         .getName(), hiddenCols.get( index ), true ) );
@@ -462,7 +462,7 @@
                 Map<Integer, String> hierarchyOrgunit = getHierarchyOrgunit(
                     programStageInstance.getOrganisationUnit(), level );
 
-                for ( int i = level; i < maxLevel; i++ )
+                for ( int i = level; i <= maxLevel; i++ )
                 {
                     if ( hierarchyOrgunit.get( i ) != null )
                     {
@@ -562,7 +562,8 @@
     private Map<Integer, String> getHierarchyOrgunit( OrganisationUnit orgunit, int level )
     {
         Map<Integer, String> hierarchyOrgunit = new HashMap<Integer, String>();
-        hierarchyOrgunit.put( level, orgunit.getName() );
+        
+        hierarchyOrgunit.put( organisationUnitService.getLevelOfOrganisationUnit( orgunit.getId() ), orgunit.getName() );
 
         orgunit = orgunit.getParent();
 

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java	2012-04-27 13:36:22 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java	2012-05-02 08:15:45 +0000
@@ -186,12 +186,20 @@
     }
 
     public List<ProgramStageInstance> get( ProgramStage programStage, Map<Integer, String> searchingIdenKeys,
-        Map<Integer, String> searchingAttrKeys, Map<Integer, String> searchingDEKeys, Collection<Integer> orgunitIds,
+        Map<Integer, String> searchingAttrKeys, Map<Integer, String> searchingDEKeys, Collection<Integer> upperOrgunitIds, Collection<Integer> bottomOrgunitIds,
         Date startDate, Date endDate, boolean orderByOrgunitAsc, boolean orderByExecutionDateByAsc, int min, int max )
     {
         String sql = getTabularReportStatement( false, programStage, searchingIdenKeys, searchingAttrKeys,
-            searchingDEKeys, orgunitIds, startDate, endDate, orderByOrgunitAsc, orderByExecutionDateByAsc )
-            + statementBuilder.limitRecord( min, max );
+            searchingDEKeys, upperOrgunitIds, startDate, endDate, orderByOrgunitAsc, orderByExecutionDateByAsc );
+        
+        if( bottomOrgunitIds.size() > 0 )
+        {
+            String sqlBottom = getTabularReportStatement( false, programStage, searchingIdenKeys, searchingAttrKeys,
+                searchingDEKeys, bottomOrgunitIds, startDate, endDate, orderByOrgunitAsc, orderByExecutionDateByAsc );
+            sql = "( "+ sqlBottom + ") union all ( " + sql + " ) ";
+        }
+        
+        sql += statementBuilder.limitRecord( min, max );
 
         List<Integer> ids = executeSQL( sql );
 
@@ -206,12 +214,19 @@
     }
 
     public List<ProgramStageInstance> get( ProgramStage programStage, Map<Integer, String> searchingIdenKeys,
-        Map<Integer, String> searchingAttrKeys, Map<Integer, String> searchingDEKeys, Collection<Integer> orgunitIds,
+        Map<Integer, String> searchingAttrKeys, Map<Integer, String> searchingDEKeys, Collection<Integer> upperOrgunitIds, Collection<Integer> bottomOrgunitIds,
         Date startDate, Date endDate, boolean orderByOrgunitAsc, boolean orderByExecutionDateByAsc )
     {
         String sql = getTabularReportStatement( false, programStage, searchingIdenKeys, searchingAttrKeys,
-            searchingDEKeys, orgunitIds, startDate, endDate, orderByOrgunitAsc, orderByExecutionDateByAsc );
-
+            searchingDEKeys, upperOrgunitIds, startDate, endDate, orderByOrgunitAsc, orderByExecutionDateByAsc );
+        
+        if( bottomOrgunitIds.size() > 0 )
+        {
+            String sqlBottom = getTabularReportStatement( false, programStage, searchingIdenKeys, searchingAttrKeys,
+                searchingDEKeys, bottomOrgunitIds, startDate, endDate, orderByOrgunitAsc, orderByExecutionDateByAsc );
+            sql = "( "+ sqlBottom + ") union all ( " + sql + " ) ";
+        }
+        
         List<Integer> ids = executeSQL( sql );
 
         List<ProgramStageInstance> programStageInstances = new ArrayList<ProgramStageInstance>();
@@ -244,18 +259,23 @@
         Map<Integer, String> searchingDEKeys, Collection<Integer> orgunitIds, Date startDate, Date endDate,
         boolean orderByOrgunitAsc, boolean orderByExecutionDateByAsc )
     {
-        String select = "SELECT distinct psi.programstageinstanceid, psi.organisationunitid , psi.executiondate ";
+        String select = "SELECT distinct psi.programstageinstanceid, ogu.name , psi.executiondate ";
+        
         String sqlID = " select distinct psi.programstageinstanceid from patientdatavalue pdv "
             + "inner join programstageinstance psi on pdv.programstageinstanceid=psi.programstageinstanceid "
             + "INNER JOIN patientidentifier as pid ON pid.patientid = p.patientid "
             + "INNER JOIN patientidentifiertype as pit ON pid.patientidentifiertypeid = pit.patientidentifiertypeid ";
+        
         String sqlATTR = " select distinct psi.programstageinstanceid from patientdatavalue pdv "
             + "inner join programstageinstance psi on pdv.programstageinstanceid=psi.programstageinstanceid ";
+        
         String sqlDE = " select distinct psi.programstageinstanceid from patientdatavalue pdv "
             + "inner join programstageinstance psi on pdv.programstageinstanceid=psi.programstageinstanceid ";
+        
         String condition = "FROM patientdatavalue pdv "
             + "INNER JOIN programstageinstance psi ON pdv.programstageinstanceid=psi.programstageinstanceid "
-            + "INNER JOIN programinstance pi ON pi.programinstanceid=psi.programinstanceid ";
+            + "INNER JOIN programinstance pi ON pi.programinstanceid=psi.programinstanceid " 
+            + "INNER JOIN organisationunit ogu ON ogu.organisationunitid=psi.organisationunitid ";
 
         condition += " WHERE psi.executiondate >= '" + DateUtils.getMediumDateString( startDate )
             + "' AND psi.executiondate <= '" + DateUtils.getMediumDateString( endDate ) + "' "
@@ -363,14 +383,14 @@
             return "select count( distinct psi.programstageinstanceid ) " + condition;
         }
 
-        condition += " ORDER BY psi.organisationunitid ";
+        condition += " ORDER BY ogu.name ";
         condition += orderByOrgunitAsc ? "asc" : "desc";
         condition += ", psi.executiondate ";
         condition += orderByExecutionDateByAsc ? "asc" : "desc";
 
         return select + condition;
     }
-
+    
     private List<Integer> executeSQL( String sql )
     {
         StatementHolder holder = statementManager.getHolder();

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/report/GenerateTabularReportAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/report/GenerateTabularReportAction.java	2012-04-20 10:24:33 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/report/GenerateTabularReportAction.java	2012-05-02 08:15:45 +0000
@@ -28,6 +28,7 @@
 package org.hisp.dhis.caseentry.action.report;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -290,24 +291,35 @@
 
         OrganisationUnit selectedOrgunit = organisationUnitService.getOrganisationUnit( orgunitId );
 
-        Set<Integer> orgunitIds = new HashSet<Integer>();
+        Set<Integer> upperOrgunitIds = new HashSet<Integer>();
+
+        Set<Integer> bottomOrgunitIds = new HashSet<Integer>();
 
         if ( facilityLB.equals( "selected" ) )
         {
-            orgunitIds.add( orgunitId );
+            upperOrgunitIds.add( orgunitId );
+        }
+        else if ( facilityLB.equals( "childrenOnly" ) )
+        {
+            Set<OrganisationUnit> children = selectedOrgunit.getChildren();
+
+            for ( OrganisationUnit child : children )
+            {
+                upperOrgunitIds.add( child.getId() );
+            }
         }
         else
         {
             OrganisationUnitHierarchy hierarchy = organisationUnitService.getOrganisationUnitHierarchy();
 
             Set<Integer> childOrgUnitIdentifiers = hierarchy.getChildren( selectedOrgunit.getId() );
-
-            orgunitIds.addAll( childOrgUnitIdentifiers );
-
-            if ( facilityLB.equals( "childrenOnly" ) )
-            {
-                orgunitIds.remove( selectedOrgunit.getId() );
-            }
+            upperOrgunitIds.add( orgunitId );
+            upperOrgunitIds.addAll( childOrgUnitIdentifiers );
+            
+            // Get bottom orgunit
+            int maxLevel = organisationUnitService.getMaxOfOrganisationUnitLevels();
+            bottomOrgunitIds = getOrganisationUnitsAtLevel( selectedOrgunit, maxLevel - 1 );
+            upperOrgunitIds.removeAll( bottomOrgunitIds );
         }
 
         // ---------------------------------------------------------------------
@@ -332,6 +344,10 @@
 
         if ( type == null )
         {
+            Set<Integer> orgunitIds = new HashSet<Integer>();
+            orgunitIds.addAll( upperOrgunitIds );
+            orgunitIds.addAll( bottomOrgunitIds );
+
             int totalRecords = programStageInstanceService.countProgramStageInstances( programStage, searchingIdenKeys,
                 searchingAttrKeys, searchingDEKeys, orgunitIds, startValue, endValue );
 
@@ -341,15 +357,16 @@
 
             grid = programStageInstanceService.getTabularReport( programStage, hiddenCols, identifierTypes,
                 fixedAttributes, patientAttributes, dataElements, searchingIdenKeys, searchingAttrKeys,
-                searchingDEKeys, orgunitIds, level, startValue, endValue, orderByOrgunitAsc, orderByExecutionDateByAsc,
-                paging.getStartPos(), paging.getPageSize(), format, i18n );
+                searchingDEKeys, upperOrgunitIds, bottomOrgunitIds, level, startValue, endValue, orderByOrgunitAsc,
+                orderByExecutionDateByAsc, paging.getStartPos(), paging.getPageSize(), format, i18n );
 
             return SUCCESS;
         }
 
         grid = programStageInstanceService.getTabularReport( programStage, hiddenCols, identifierTypes,
             fixedAttributes, patientAttributes, dataElements, searchingIdenKeys, searchingAttrKeys, searchingDEKeys,
-            orgunitIds, level, startValue, endValue, orderByOrgunitAsc, orderByExecutionDateByAsc, format, i18n );
+            upperOrgunitIds, bottomOrgunitIds, level, startValue, endValue, orderByOrgunitAsc,
+            orderByExecutionDateByAsc, format, i18n );
 
         return type;
     }
@@ -498,4 +515,47 @@
         return values;
     }
 
+    private Set<Integer> getOrganisationUnitsAtLevel( OrganisationUnit orgunit, int level )
+    {
+        Set<Integer> result = new HashSet<Integer>();
+
+        if ( level < 1 )
+        {
+            throw new IllegalArgumentException( "Level must be greater than zero" );
+        }
+
+        if ( level == 1 )
+        {
+            result.add( orgunit.getId() );
+            return result;
+        }
+
+        for ( OrganisationUnit root : orgunit.getChildren() )
+        {
+            addOrganisationUnitChildrenAtLevel( root, 2, level, result );
+        }
+
+        return result;
+    }
+
+    private void addOrganisationUnitChildrenAtLevel( OrganisationUnit parent, int currentLevel, int targetLevel,
+        Set<Integer> result )
+    {
+        if ( currentLevel == targetLevel )
+        {
+            Collection<OrganisationUnit> orgunits = parent.getChildren();
+            for ( OrganisationUnit orgunit : orgunits )
+            {
+                result.add( orgunit.getId() );
+            }
+        }
+        else
+        {
+            for ( OrganisationUnit child : parent.getChildren() )
+            {
+                addOrganisationUnitChildrenAtLevel( child, currentLevel + 1, targetLevel, result );
+            }
+        }
+    }
+
 }

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/jsonTabularReport.vm'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/jsonTabularReport.vm	2012-04-20 03:52:46 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/jsonTabularReport.vm	2012-05-02 08:15:45 +0000
@@ -62,7 +62,7 @@
 	'items':[
 		{
 			"id":"$i18n.getString( 'filter' )",
-			#foreach( $col in $grid.getVisibleHeaders() )
+			#foreach( $col in $grid.getHeaders() )
 				#if ($velocityCount <= $metaData )
 					"col$velocityCount": "",
 				#end