=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java	2010-06-08 19:47:40 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java	2010-11-15 13:52:55 +0000
@@ -264,4 +264,6 @@
     List<OrganisationUnitLevel> getFilledOrganisationUnitLevels();
     
     int getNumberOfOrganisationUnits();
+
+    int getMaxOrgUnitLevels();
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitStore.java	2010-06-08 19:47:40 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitStore.java	2010-11-15 13:43:36 +0000
@@ -166,4 +166,6 @@
     OrganisationUnitLevel getOrganisationUnitLevelByName( String name );
     
     int getNumberOfOrganisationUnits();
+    int getMaxOrgUnitLevels();
+
 }

=== modified file 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/databrowser/jdbc/StatementManagerDataBrowserStore.java'
--- dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/databrowser/jdbc/StatementManagerDataBrowserStore.java	2010-09-18 12:12:39 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/databrowser/jdbc/StatementManagerDataBrowserStore.java	2010-11-15 19:40:44 +0000
@@ -17,6 +17,7 @@
  * @author joakibj, martinwa, briane, eivinhb
  * @version $Id StatementManagerDataBrowserStore.java 2010-04-06 Jason Pickering, Dang Duy Hieu$
  */
+
 public class StatementManagerDataBrowserStore
     implements DataBrowserStore
 {
@@ -521,7 +522,7 @@
 
         return numResults;
     }
-
+    @Override
     public Integer setCountOrgUnitsBetweenPeriods( DataBrowserTable table, Integer orgUnitParent,
         List<Integer> betweenPeriodIds )
     {
@@ -529,14 +530,27 @@
 
         Integer numResults = 0;
         StringBuffer sqlsbDescentdants = new StringBuffer();
-
-        dropView( "view_count_descentdants" );
-
+        Boolean dropViewSuccess = false;
+        Integer maxLevel = organisationUnitService.getMaxOrgUnitLevels();
+        //Try and drop the view
+        try
+        {
+         dropViewSuccess = dropView( "view_count_descentdants" );
+        }
+        catch (Exception e)
+        {
+          throw new RuntimeException( "Failed to delete view_count_descentadants", e );
+        }
+        //Only try this part if we could drop the view
+        if  ( dropViewSuccess )
+                 {
         sqlsbDescentdants.append( "CREATE VIEW view_count_descentdants AS " );
-        setUpQueryForDrillDownDescendants( sqlsbDescentdants, orgUnitParent, betweenPeriodIds );
+        setUpQueryForDrillDownDescendants( sqlsbDescentdants, orgUnitParent, betweenPeriodIds, maxLevel );
 
-        try
+        try 
         {
+
+
             TimeUtils.start();
             
             holder.getStatement().executeUpdate( sqlsbDescentdants.toString() );
@@ -553,7 +567,7 @@
             
             TimeUtils.stop();
         }
-        catch ( SQLException e )
+        catch ( Exception e )
         {
             throw new RuntimeException( "Failed to get aggregated data value", e );
         }
@@ -562,6 +576,8 @@
             holder.close();
         }
 
+        }
+
         return numResults;
     }
 
@@ -581,7 +597,6 @@
             sqlsb.append( "(SELECT de.dataelementid, de.name AS DataElement, Count(dv.value) AS counts_of_aggregated_values, p.periodid AS PeriodId, p.startDate AS ColumnHeader " );
             sqlsb.append( "FROM dataelement AS de " );
             sqlsb.append( "INNER JOIN datavalue AS dv ON (de.dataelementid = dv.dataelementid) " );
-            sqlsb.append( "INNER JOIN datasetmembers AS dsm ON (de.dataelementid = dsm.dataelementid) " );
             sqlsb.append( "INNER JOIN organisationunit As o ON (dv.sourceid = o.organisationunitid) " );
             sqlsb.append( "JOIN period p ON (dv.periodid = p.periodid) " );
             sqlsb.append( "WHERE o.organisationunitid = '" + orgUnitId + "' " );
@@ -660,66 +675,83 @@
         return stm.getResultSet();
     }
 
-    private void setUpQueryForDrillDownDescendants( StringBuffer sb, Integer orgUnitSelected,
-        List<Integer> betweenPeriodIds )
+    private String setUpQueryForDrillDownDescendants( StringBuffer sb, Integer orgUnitSelected,
+        List<Integer> betweenPeriodIds, Integer maxLevel )
+
     {
-        int i = 0;
-        int loopSize = betweenPeriodIds.size();
-        int curLevel = organisationUnitService.getLevelOfOrganisationUnit( orgUnitSelected );
-        int maxLevel = organisationUnitService.getNumberOfOrganisationalLevels();
+        if ( maxLevel == null )
+             {
+            maxLevel = organisationUnitService.getMaxOrgUnitLevels();
+        }
+            int curLevel = organisationUnitService.getLevelOfOrganisationUnit( orgUnitSelected );
+  
         int diffLevel = maxLevel - curLevel;
-        String orgIndex = this.getTableIndexByDiffLevel( diffLevel );
-
-        for ( Integer periodid : betweenPeriodIds )
-        {
-            i++;
-
-            /**
-             * The current organization unit
-             */
-            sb.append( "SELECT DISTINCT o.organisationunitid AS parentid, o.name AS OrganisationUnit, COUNT(value) as countdv_descendants, p.periodid AS PeriodId, p.startDate AS ColumnHeader " );
-            sb.append( "FROM organisationunit o " );
-            sb.append( "JOIN datavalue dv ON ( dv.sourceid = o.organisationunitid ) " );
-            sb.append( "JOIN period p ON ( p.periodid = dv.periodid ) " );
-            sb.append( "WHERE o.parentid = '" + orgUnitSelected + "' " );
-            sb.append( "AND dv.periodid = '" + periodid + "' " );
-            sb.append( "GROUP BY o.organisationunitid, OrganisationUnit, p.periodid, p.startdate " );
-            sb.append( "UNION " );
-
-            /**
-             * All descendant levels of selected organization unit
-             */
-            sb.append( "SELECT DISTINCT ou" + orgIndex + ".organisationunitid AS parentid, ou" + orgIndex + ".name AS OrganisationUnit, COUNT(value) as countdv_descendants, p.periodid AS PeriodId, p.startDate AS ColumnHeader " );
-            sb.append( "FROM datavalue dv " );
-            sb.append( "JOIN organisationunit ou ON ( ou.organisationunitid = dv.sourceid ) " );
-            this.setUpQueryForJOINTable( sb, diffLevel );
-            sb.append( "JOIN period p ON ( p.periodid = dv.periodid ) " );
-            sb.append( "WHERE dv.periodid = '" + periodid + "' " );
-            sb.append( "AND dv.sourceid IN " );
-            sb.append( "( " );
+        
+            /**
+             * Get all descendant level data for all orgunits under the selected, grouped
+             * by the next immediate children of the selected orgunit
+             */
+            sb.append("SELECT a.parentid, ou.name as organisationunit, COUNT(dv.value) as countdv_descendants, p.periodid, p.startdate as columnheader ");
+            sb.append(" FROM datavalue dv ");
+            sb.append(" INNER JOIN ( ");
             sb.append( this.setUpQueryGetDescendants( curLevel, maxLevel, orgUnitSelected ) );
-            sb.append( " ) " );
-            sb.append( "GROUP BY ou" + orgIndex + ".organisationunitid, OrganisationUnit, p.periodid, p.startDate " );
-            
-            sb.append( i < loopSize ? "UNION " : "" );
-           
-        }
+            sb.append(" ) a");
+            sb.append(" on a.childid = dv.sourceid ");
+            sb.append(" INNER JOIN organisationunit ou ON a.parentid = ou.organisationunitid ");
+            sb.append(" INNER JOIN (SELECT periodid, startdate from period where periodid in ");
+            sb.append( splitListHelper( betweenPeriodIds ) );
+            sb.append(" ) p on p.periodid = dv.periodid");
+            sb.append(" WHERE dv.periodid IN  ");
+            sb.append( splitListHelper( betweenPeriodIds ) );
+            sb.append(" GROUP BY a.parentid, ou.name, p.periodid, p.startdate ");
+            return sb.toString();
     }
 
     private String setUpQueryGetDescendants( int curLevel, int maxLevel, Integer orgUnitSelected )
     {
-        int j = curLevel;
-
-        String oldSQL = "SELECT DISTINCT idlevel" + (j + 1) + " FROM _orgunitstructure os WHERE os.idlevel" + (j)
-            + " = '" + orgUnitSelected + "'";
-
-        for ( j++; j < (maxLevel); j++ )
+       
+        Integer childLevel = curLevel + 1;
+        Integer diffLevel = maxLevel - curLevel;
+        Integer j = 0;
+
+        //The immediate child level
+        //can probably be combined into the for loop
+        //but we need to clarify whether the selected unit should be present, and if so, how?
+        StringBuilder desc_query = new StringBuilder( "SELECT DISTINCT idlevel");
+        desc_query.append( (childLevel) );
+        desc_query.append( "  as parentid, idlevel");
+        desc_query.append( childLevel );
+        desc_query.append( " as childid from _orgunitstructure where idlevel");
+        desc_query.append( curLevel );
+        desc_query.append( " = '");
+        desc_query.append(orgUnitSelected.toString());
+        desc_query.append("'");
+        desc_query.append (" AND idlevel");
+        desc_query.append ( childLevel);
+        desc_query.append ( " <> 0");
+        //loop through each of the descendants
+        //until the diff level is reached
+        for ( j=1 ; j < diffLevel ; j++ )
+            
         {
-            oldSQL = "SELECT DISTINCT idlevel" + (j + 1) + " as descendant FROM _orgunitstructure os WHERE idlevel"
-                + (j) + " IN ( " + oldSQL + " ) ";
+            
+            desc_query.append( " UNION " );
+            desc_query.append( "SELECT DISTINCT idlevel");
+            desc_query.append( (childLevel) );
+            desc_query.append( "  as parentid, idlevel");
+            desc_query.append( childLevel + j );
+            desc_query.append( " as childid from _orgunitstructure where idlevel");
+            desc_query.append( curLevel );
+            desc_query.append( " = '");
+            desc_query.append(orgUnitSelected.toString());
+            desc_query.append("'");
+            desc_query.append (" AND idlevel");
+            desc_query.append( childLevel + j );
+            desc_query.append( " <> 0");
+            
         }
-
-        return oldSQL;
+         
+        return desc_query.toString();
     }
 
     private void setUpQueryForDrillDownViewTable( StringBuffer sb )
@@ -753,21 +785,29 @@
         return (index == 0) ? "" : index + "";
     }
 
-    private void dropView( String view )
+    private boolean dropView( String view )
     {
+        boolean return_value = false;
+
         final StatementHolder holder = statementManager.getHolder();
 
         try
         {
             holder.getStatement().executeUpdate( "DROP VIEW IF EXISTS " + view );
+            return_value = true;
         }
-        catch ( SQLException ex )
+        catch ( Exception ex )
         {
+            return_value = false;
             throw new RuntimeException( "Failed to drop view: " + view, ex );
+            
         }
         finally
+
         {
             holder.close();
         }
+        return return_value;
     }
+
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2010-09-14 09:39:30 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2010-11-15 13:53:38 +0000
@@ -541,4 +541,10 @@
     {
         return organisationUnitStore.getNumberOfOrganisationUnits();
     }
+      @Override
+    public int getMaxOrgUnitLevels()
+    {
+        return organisationUnitStore.getMaxOrgUnitLevels();
+    }
+    
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/hibernate/HibernateOrganisationUnitStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/hibernate/HibernateOrganisationUnitStore.java	2010-09-18 12:12:39 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/hibernate/HibernateOrganisationUnitStore.java	2010-11-15 13:43:36 +0000
@@ -219,4 +219,11 @@
 
         return jdbcTemplate.queryForInt( sql );
     }
+    @Override
+        public int getMaxOrgUnitLevels()
+    {
+        final String sql = "SELECT count(level) FROM orgunitlevel";
+
+        return jdbcTemplate.queryForInt( sql );
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/resources/org/hisp/dhis/dataadmin/i18n_module.properties'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/resources/org/hisp/dhis/dataadmin/i18n_module.properties	2010-11-12 09:55:41 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/resources/org/hisp/dhis/dataadmin/i18n_module.properties	2010-11-15 15:59:07 +0000
@@ -87,8 +87,8 @@
 browse																= Browse
 back																= Back
 select_period_type													= Select Period Type
-data_integrity														= Data Integrity
-data_elements_without_data_set										= Data elements without data set
+organisation_unit_groups                                                                                                = Organisation Unit Groups
+data_integrity														= Data Integrity Groups
 data_elements_without_groups										= Data elements without groups
 data_sets_not_assigned_to_organisation_units						= Data sets not assigned to organisation units
 indicators_with_blank_formulas										= Indicators with blank formulas

=== modified file 'dhis-2/pom.xml'
--- dhis-2/pom.xml	2010-11-11 14:35:21 +0000
+++ dhis-2/pom.xml	2010-11-12 06:02:36 +0000
@@ -4,7 +4,7 @@
   <artifactId>dhis</artifactId>
     <version>2.0.5</version>
   <packaging>pom</packaging>
-  <name>DHIS 2</name>
+  <name>DHIS 2 2.0.5</name>
   <url>http://dhis2.org</url>
   
   <description>

=== modified file 'dhis2-live-installer/build.bat'
--- dhis2-live-installer/build.bat	2010-03-02 06:52:28 +0000
+++ dhis2-live-installer/build.bat	2010-11-12 06:06:34 +0000
@@ -1,45 +1,45 @@
-
-
-REM Adjust these enviornment variables to suit your system.
-REM They are needed by this build script as well as maven during the build.
-REM Path to the Java SDK
-JAVA_HOME="C:\Program Files\Java\jdk1.6.0_10\"
-REM Be sure maven is in your path
-PATH=%PATH%;"C:\apache-maven-2.2.1\bin\"
-REM Be sure that the Bitrock installer is in your path
-BITROCK_HOME="C:\Program Files\BitRock InstallBuilder Professional 6.2.7"
-REM If you are including Birt in the build, be sure it is available here
-BIRT_WAR="C:\apache-tomcat-6.0.18\webapps\birt\"
-REM Path to the root of the documentation branch source tree
-DHIS2_DOCS="C:\src\dhis2-docbook-docs"
-REM Path to the root of the DHIS2 branch source tree
-DHIS2_SRC="C:\src\dhis2"
-REM Path to the root of the DHIS2 Live installer source tree
-DHIS2_INSTALL="C:\src\dhis2-live-installer"
-
-SET %JAVA_HOME%
-SET %PATH%
-SET %BITROCK_HOME%
-SET %BIRT_WAR%
-SET %DHIS2_DOCS%
-SET %DHIS2_SRC%
-SET %DHIS2_INSTALL%
-
-echo "Building DHIS 2 Core..."
-cd %DHIS2_SRC%\dhis-2
-mvn clean install -Dtest=skip -DfailIfNoTests=false
-echo "Building DHIS 2 Web..."
-cd dhis-web
-mvn clean install -Dtest=skip -DfailIfNoTests=false
-echo "Packing DHIS 2 Web Portal..."
-cd dhis-web-portal
-mvn clean install -Dtest=skip -DfailIfNoTests=false
-echo "Builidng DHIS2 Live Package"
-cd %DHIS2_SRC%\dhis-live
-mvn clean package -Dtest=skip -DfailIfNoTests=false
-echo "Building documentation"
-cd %DHIS2_DOCS%
-mvn package
-echo "Building installer"
-cd %DHIS2_INSTALL%
-mvn exec:exec
+
+
+REM Adjust these enviornment variables to suit your system.
+REM They are needed by this build script as well as maven during the build.
+REM Path to the Java SDK
+JAVA_HOME=C:\Program Files\Java\jdk1.6.0_22\
+REM Be sure maven is in your path
+PATH=%PATH%;C:\users\Jason\apache-maven-2.2.1\bin\
+REM Be sure that the Bitrock installer is in your path
+BITROCK_HOME=C:\Program Files\BitRock InstallBuilder Professional 6.5.6
+REM If you are including Birt in the build, be sure it is available here
+REM BIRT_WAR="C:\apache-tomcat-6.0.18\webapps\birt\"
+REM Path to the root of the documentation branch source tree
+DHIS2_DOCS=C:\src\dhis2-docbook-docs
+REM Path to the root of the DHIS2 branch source tree
+DHIS2_SRC=C:\dhis2-2.0.5\2.0.5\
+REM Path to the root of the DHIS2 Live installer source tree
+DHIS2_INSTALL=C:\dhis2-2.0.5\2.0.5\dhis2-live-installer:
+
+REM SET %JAVA_HOME%
+SET %PATH%
+SET %BITROCK_HOME%
+REM SET %BIRT_WAR%
+SET %DHIS2_DOCS%
+SET %DHIS2_SRC%
+SET %DHIS2_INSTALL%
+
+echo "Building DHIS 2 Core..."
+cd %DHIS2_SRC%\dhis-2
+mvn clean install -Dtest=skip -DfailIfNoTests=false
+echo "Building DHIS 2 Web..."
+cd dhis-web
+mvn clean install -Dtest=skip -DfailIfNoTests=false
+echo "Packing DHIS 2 Web Portal..."
+cd dhis-web-portal
+mvn clean install -Dtest=skip -DfailIfNoTests=false
+echo "Builidng DHIS2 Live Package"
+cd %DHIS2_SRC%\dhis-live
+REM mvn clean package -Dtest=skip -DfailIfNoTests=false
+REM echo "Building documentation"
+REM cd %DHIS2_DOCS%
+REM mvn package
+echo "Building installer"
+cd %DHIS2_INSTALL%
+mvn exec:exec

