← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6283: Impl time aggregation for on-time reporting rates

 

------------------------------------------------------------
revno: 6283
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2012-03-16 17:49:58 +0100
message:
  Impl time aggregation for on-time reporting rates
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/completeness/DataSetCompletenessResult.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/completeness/DataSetCompletenessStore.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/AbstractDataSetCompletenessService.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/CompulsoryDataSetCompletenessService.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/RatioDataSetCompletenessService.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/RegistrationDataSetCompletenessService.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/jdbc/JDBCDataSetCompletenessStore.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-services/dhis-service-reporting/src/test/java/org/hisp/dhis/completeness/DataSetCompletenessServiceTest.java
  dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java
  dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/DerbyStatementBuilder.java
  dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/H2StatementBuilder.java
  dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/MySQLStatementBuilder.java
  dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/PostgreSQLStatementBuilder.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-api/src/main/java/org/hisp/dhis/completeness/DataSetCompletenessResult.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/completeness/DataSetCompletenessResult.java	2012-02-27 14:12:54 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/completeness/DataSetCompletenessResult.java	2012-03-16 16:49:58 +0000
@@ -180,8 +180,7 @@
     @Override
     public String toString()
     {
-        String toString = "[Name: " + name + ", Reg: " + registrations + ", On time: " + registrationsOnTime
-            + ", Src: " + sources + "]";
+        String toString = "[Name: " + name + ", sources: " + sources+ ", registrations: " + registrations + ", on time: " + registrationsOnTime + "]";
 
         return toString;
     }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/completeness/DataSetCompletenessStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/completeness/DataSetCompletenessStore.java	2012-03-15 16:04:20 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/completeness/DataSetCompletenessStore.java	2012-03-16 16:49:58 +0000
@@ -28,7 +28,6 @@
  */
 
 import java.util.Collection;
-import java.util.Date;
 
 import org.hisp.dhis.dataset.DataSet;
 
@@ -42,13 +41,13 @@
     
     Integer getCompleteDataSetRegistrations( DataSet dataSet, Collection<Integer> periods, Collection<Integer> relevantSources );
     
-    Integer getCompleteDataSetRegistrations( DataSet dataSet, Collection<Integer> periods, Collection<Integer> relevantSources, Date deadline );
+    Integer getCompleteDataSetRegistrations( DataSet dataSet, Collection<Integer> periods, Collection<Integer> relevantSources, int completenessOffset );
 
     Integer getCompulsoryDataElementRegistrations( DataSet dataSet, Collection<Integer> children, Collection<Integer> periods );
     
-    Integer getCompulsoryDataElementRegistrations( DataSet dataSet, Collection<Integer> children, Collection<Integer> periods, Date deadline );
+    Integer getCompulsoryDataElementRegistrations( DataSet dataSet, Collection<Integer> children, Collection<Integer> periods, int completenessOffset );
 
-    Integer getNumberOfValues( DataSet dataSet, Collection<Integer> children, Collection<Integer> periods, Date deadline );
+    Integer getNumberOfValues( DataSet dataSet, Collection<Integer> children, Collection<Integer> periods );
     
     Collection<DataSet> getDataSetsWithRegistrations( Collection<DataSet> dataSets );
     

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/AbstractDataSetCompletenessService.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/AbstractDataSetCompletenessService.java	2012-03-16 12:05:01 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/AbstractDataSetCompletenessService.java	2012-03-16 16:49:58 +0000
@@ -29,12 +29,10 @@
 
 import static org.hisp.dhis.setting.SystemSettingManager.DEFAULT_COMPLETENESS_OFFSET;
 import static org.hisp.dhis.setting.SystemSettingManager.KEY_COMPLETENESS_OFFSET;
-import static org.hisp.dhis.system.util.ConversionUtils.*;
+import static org.hisp.dhis.system.util.ConversionUtils.getIdentifiers;
 
 import java.util.ArrayList;
-import java.util.Calendar;
 import java.util.Collection;
-import java.util.Date;
 
 import org.amplecode.quick.BatchHandler;
 import org.amplecode.quick.BatchHandlerFactory;
@@ -49,13 +47,13 @@
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.dataset.DataSetService;
 import org.hisp.dhis.jdbc.batchhandler.DataSetCompletenessResultBatchHandler;
-import org.hisp.dhis.setting.SystemSettingManager;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitHierarchy;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodService;
 import org.hisp.dhis.period.RelativePeriods;
+import org.hisp.dhis.setting.SystemSettingManager;
 import org.hisp.dhis.system.util.ConversionUtils;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -140,7 +138,7 @@
     public abstract int getRegistrations( DataSet dataSet, Collection<Integer> relevantSources, Collection<Integer> periods );
 
     public abstract int getRegistrationsOnTime( DataSet dataSet, Collection<Integer> relevantSources, Collection<Integer> periods,
-        Date deadline );
+        int completenessOffset );
 
     public abstract int getSources( DataSet dataSet, Collection<Integer> relevantSources, Period period );
 
@@ -204,13 +202,11 @@
                 {
                     if ( period.getPeriodType() != null && period.getPeriodType().getFrequencyOrder() >= dataSetFrequencyOrder )
                     {
-                        final Date deadline = getDeadline( period, days );
-
                         final Collection<Integer> periodsBetweenDates = 
                             aggregationCache.getPeriodsBetweenDatesPeriodType( dataSet.getPeriodType(), period.getStartDate(), period.getEndDate() );
                         
                         final DataSetCompletenessResult result = getDataSetCompleteness( period, 
-                            periodsBetweenDates, deadline, unit, relevantSources, dataSet );
+                            periodsBetweenDates, days, unit, relevantSources, dataSet );
 
                         if ( result.getSources() > 0 )
                         {
@@ -240,7 +236,6 @@
 
         int days = (Integer) systemSettingManager.getSystemSetting( KEY_COMPLETENESS_OFFSET,
             DEFAULT_COMPLETENESS_OFFSET );
-        Date deadline = getDeadline( period, days );
 
         final Collection<Integer> children = organisationUnitService.getOrganisationUnitHierarchy().getChildren(
             organisationUnitId );
@@ -264,8 +259,7 @@
             {
                 result.setName( dataSet.getName() );
                 result.setRegistrations( getRegistrations( dataSet, relevantSources, periodsBetweenDates ) );
-                result.setRegistrationsOnTime( deadline != null ? getRegistrationsOnTime( dataSet, relevantSources,
-                    periodsBetweenDates, deadline ) : 0 );
+                result.setRegistrationsOnTime( getRegistrationsOnTime( dataSet, relevantSources, periodsBetweenDates, days ) );
 
                 result.setDataSetId( dataSet.getId() );
                 result.setPeriodId( periodId );
@@ -289,7 +283,6 @@
 
         int days = (Integer) systemSettingManager.getSystemSetting( KEY_COMPLETENESS_OFFSET,
             DEFAULT_COMPLETENESS_OFFSET );
-        Date deadline = getDeadline( period, days );
 
         final Collection<DataSetCompletenessResult> results = new ArrayList<DataSetCompletenessResult>();
 
@@ -302,7 +295,7 @@
 
             final Collection<Integer> relevantSources = getRelevantSources( dataSet, children );
 
-            final DataSetCompletenessResult result = getDataSetCompleteness( period, periodsBetweenDates, deadline, unit, relevantSources, dataSet );
+            final DataSetCompletenessResult result = getDataSetCompleteness( period, periodsBetweenDates, days, unit, relevantSources, dataSet );
 
             if ( result.getSources() > 0 )
             {
@@ -336,7 +329,7 @@
     // Supportive methods
     // -------------------------------------------------------------------------
 
-    private DataSetCompletenessResult getDataSetCompleteness( Period period, Collection<Integer> periodsBetweenDates, Date deadline, OrganisationUnit unit,
+    private DataSetCompletenessResult getDataSetCompleteness( Period period, Collection<Integer> periodsBetweenDates, int completenessOffset, OrganisationUnit unit,
         Collection<Integer> relevantSources, DataSet dataSet )
     {
         final DataSetCompletenessResult result = new DataSetCompletenessResult();
@@ -347,8 +340,7 @@
         if ( result.getSources() > 0 )
         {
             result.setRegistrations( getRegistrations( dataSet, relevantSources, periodsBetweenDates ) );
-            result.setRegistrationsOnTime( deadline != null ? getRegistrationsOnTime( dataSet, relevantSources, periodsBetweenDates,
-                deadline ) : 0 );
+            result.setRegistrationsOnTime( getRegistrationsOnTime( dataSet, relevantSources, periodsBetweenDates, completenessOffset ) );
 
             result.setDataSetId( dataSet.getId() );
             result.setPeriodId( period.getId() );
@@ -367,22 +359,4 @@
 
         return CollectionUtils.intersection( dataSetSources, sources );
     }
-
-    private Date getDeadline( Period period, int days )
-    {
-        Calendar cal = Calendar.getInstance();
-
-        Date date = null;
-
-        if ( period != null )
-        {
-            cal.clear();
-            cal.setTime( period.getEndDate() );
-            cal.add( Calendar.DAY_OF_MONTH, days );
-
-            date = cal.getTime();
-        }
-
-        return date;
-    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/CompulsoryDataSetCompletenessService.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/CompulsoryDataSetCompletenessService.java	2012-03-16 12:05:01 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/CompulsoryDataSetCompletenessService.java	2012-03-16 16:49:58 +0000
@@ -28,7 +28,6 @@
  */
 
 import java.util.Collection;
-import java.util.Date;
 
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.period.Period;
@@ -46,9 +45,9 @@
     }
 
     @Override
-    public int getRegistrationsOnTime( DataSet dataSet, Collection<Integer> relevantSources, Collection<Integer> periods, Date deadline )
+    public int getRegistrationsOnTime( DataSet dataSet, Collection<Integer> relevantSources, Collection<Integer> periods, int completenessOffset )
     {
-        return completenessStore.getCompulsoryDataElementRegistrations( dataSet, relevantSources, periods, deadline );
+        return completenessStore.getCompulsoryDataElementRegistrations( dataSet, relevantSources, periods, completenessOffset );
     }
 
     @Override

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/RatioDataSetCompletenessService.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/RatioDataSetCompletenessService.java	2012-03-16 12:05:01 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/RatioDataSetCompletenessService.java	2012-03-16 16:49:58 +0000
@@ -28,7 +28,6 @@
  */
 
 import java.util.Collection;
-import java.util.Date;
 
 import org.hisp.dhis.dataelement.DataElementOperand;
 import org.hisp.dhis.dataset.DataSet;
@@ -44,13 +43,13 @@
     @Override
     public int getRegistrations( DataSet dataSet, Collection<Integer> relevantSources, Collection<Integer> periods )
     {
-        return completenessStore.getNumberOfValues( dataSet, relevantSources, periods, null );
+        return completenessStore.getNumberOfValues( dataSet, relevantSources, periods );
     }
 
     @Override
-    public int getRegistrationsOnTime( DataSet dataSet, Collection<Integer> relevantSources, Collection<Integer> periods, Date deadline )
+    public int getRegistrationsOnTime( DataSet dataSet, Collection<Integer> relevantSources, Collection<Integer> periods, int completenessOffset )
     {
-        return completenessStore.getNumberOfValues( dataSet, relevantSources, periods, null );
+        return completenessStore.getNumberOfValues( dataSet, relevantSources, periods );
     }
 
     @Override

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/RegistrationDataSetCompletenessService.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/RegistrationDataSetCompletenessService.java	2012-03-16 12:05:01 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/impl/RegistrationDataSetCompletenessService.java	2012-03-16 16:49:58 +0000
@@ -28,7 +28,6 @@
  */
 
 import java.util.Collection;
-import java.util.Date;
 
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.period.Period;
@@ -46,9 +45,9 @@
     }
 
     @Override
-    public int getRegistrationsOnTime( DataSet dataSet, Collection<Integer> relevantSources, Collection<Integer> periods, Date deadline )
+    public int getRegistrationsOnTime( DataSet dataSet, Collection<Integer> relevantSources, Collection<Integer> periods, int completenessOffset )
     {
-        return completenessStore.getCompleteDataSetRegistrations( dataSet, periods, relevantSources, deadline );
+        return completenessStore.getCompleteDataSetRegistrations( dataSet, periods, relevantSources, completenessOffset );
     }
 
     @Override

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/jdbc/JDBCDataSetCompletenessStore.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/jdbc/JDBCDataSetCompletenessStore.java	2012-03-15 16:04:20 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/completeness/jdbc/JDBCDataSetCompletenessStore.java	2012-03-16 16:49:58 +0000
@@ -27,20 +27,17 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import static org.hisp.dhis.system.util.DateUtils.getMediumDateString;
 import static org.hisp.dhis.system.util.TextUtils.getCommaDelimitedString;
 
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Date;
 
 import org.amplecode.quick.StatementManager;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.completeness.DataSetCompletenessStore;
 import org.hisp.dhis.dataset.DataSet;
-import org.hisp.dhis.period.Period;
-import org.hisp.dhis.system.util.DateUtils;
+import org.hisp.dhis.jdbc.StatementBuilder;
 import org.hisp.dhis.system.util.TextUtils;
 
 /**
@@ -62,6 +59,13 @@
     {
         this.statementManager = statementManager;
     }
+    
+    private StatementBuilder statementBuilder;
+
+    public void setStatementBuilder( StatementBuilder statementBuilder )
+    {
+        this.statementBuilder = statementBuilder;
+    }
 
     // -------------------------------------------------------------------------
     // DataSetCompletenessStore
@@ -80,15 +84,15 @@
         
         final String sql =
             "SELECT COUNT(*) " +
-            "FROM completedatasetregistration " +
-            "WHERE datasetid = " + dataSet.getId() + " " +
-            "AND periodid IN ( " + getCommaDelimitedString( periods ) + " ) " +
-            "AND sourceid IN ( " + getCommaDelimitedString( relevantSources ) + " )";
+            "FROM completedatasetregistration cr " +
+            "WHERE cr.datasetid = " + dataSet.getId() + " " +
+            "AND cr.periodid IN ( " + getCommaDelimitedString( periods ) + " ) " +
+            "AND cr.sourceid IN ( " + getCommaDelimitedString( relevantSources ) + " )";
         
         return statementManager.getHolder().queryForInteger( sql );
     }
 
-    public Integer getCompleteDataSetRegistrations( DataSet dataSet, Collection<Integer> periods, Collection<Integer> relevantSources, Date deadline )
+    public Integer getCompleteDataSetRegistrations( DataSet dataSet, Collection<Integer> periods, Collection<Integer> relevantSources, int completenessOffset )
     {
         if ( relevantSources == null || relevantSources.size() == 0 )
         {
@@ -97,11 +101,12 @@
         
         final String sql =
             "SELECT COUNT(*) " +
-            "FROM completedatasetregistration " +
-            "WHERE datasetid = " + dataSet.getId() + " " +
-            "AND periodid IN ( " + getCommaDelimitedString( periods ) + " ) " +
-            "AND sourceid IN ( " + getCommaDelimitedString( relevantSources ) + " ) " +
-            "AND date <= '" + getMediumDateString( deadline ) + "'";
+            "FROM completedatasetregistration cr " +
+            "JOIN period p ON (cr.periodid = p.periodid) " +
+            "WHERE cr.datasetid = " + dataSet.getId() + " " +
+            "AND cr.periodid IN ( " + getCommaDelimitedString( periods ) + " ) " +
+            "AND cr.sourceid IN ( " + getCommaDelimitedString( relevantSources ) + " ) " +
+            "AND cr.date <= " + statementBuilder.getAddDate( "p.enddate", completenessOffset );
         
         return statementManager.getHolder().queryForInteger( sql );
     }
@@ -112,13 +117,13 @@
 
     public Integer getCompulsoryDataElementRegistrations( DataSet dataSet, Collection<Integer> children, Collection<Integer> periods )
     {
-        return getCompulsoryDataElementRegistrations( dataSet, children, periods, null );
+        return getCompulsoryDataElementRegistrations( dataSet, children, periods, 0 );
     }
     
-    public Integer getCompulsoryDataElementRegistrations( DataSet dataSet, Collection<Integer> children, Collection<Integer> periods, Date deadline )
+    public Integer getCompulsoryDataElementRegistrations( DataSet dataSet, Collection<Integer> children, Collection<Integer> periods, int completenessOffset )
     {           
         final int compulsoryElements = dataSet.getCompulsoryDataElementOperands().size();
-        final String deadlineCriteria = deadline != null ? "AND lastupdated < '" + DateUtils.getMediumDateString( deadline ) + "' " : "";
+        final String deadlineCriteria = ""; //FIXME //deadline != null ? "AND lastupdated < '" + DateUtils.getMediumDateString( deadline ) + "' " : "";
         
         final String sql = 
             "SELECT COUNT(completed) FROM ( " +
@@ -140,16 +145,15 @@
     // Based on number of data values
     // -------------------------------------------------------------------------
 
-    public Integer getNumberOfValues( DataSet dataSet, Collection<Integer> children, Collection<Integer> periods, Date deadline )
+    public Integer getNumberOfValues( DataSet dataSet, Collection<Integer> children, Collection<Integer> periods )
     {
         final String childrenIds = TextUtils.getCommaDelimitedString( children );
-        final String deadlineCriteria = deadline != null ? "AND lastupdated < '" + DateUtils.getMediumDateString( deadline ) + "' " : "";
         
         final String sql =
             "SELECT count(*) FROM datavalue dv " +
             "JOIN datasetmembers dsm ON dv.dataelementid=dsm.dataelementid " +
             "JOIN dataset ds ON dsm.datasetid=ds.datasetid " +
-            "WHERE ds.datasetid = " + dataSet.getId() + " " + deadlineCriteria +
+            "WHERE ds.datasetid = " + dataSet.getId() + " " +
             "AND periodid IN ( " + getCommaDelimitedString( periods ) + " ) " +
             "AND sourceid IN (" + childrenIds + ")";
 

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml	2012-03-15 16:04:20 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml	2012-03-16 16:49:58 +0000
@@ -118,6 +118,7 @@
 
   <bean id="org.hisp.dhis.completeness.DataSetCompletenessStore" class="org.hisp.dhis.completeness.jdbc.JDBCDataSetCompletenessStore">
     <property name="statementManager" ref="statementManager" />
+	<property name="statementBuilder" ref="statementBuilder" />
   </bean>
 
   <!-- PivotTable -->

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/test/java/org/hisp/dhis/completeness/DataSetCompletenessServiceTest.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/test/java/org/hisp/dhis/completeness/DataSetCompletenessServiceTest.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/test/java/org/hisp/dhis/completeness/DataSetCompletenessServiceTest.java	2012-03-16 16:49:58 +0000
@@ -43,9 +43,11 @@
 import org.hisp.dhis.external.location.LocationManager;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.period.MonthlyPeriodType;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodService;
 import org.hisp.dhis.period.PeriodType;
+import org.hisp.dhis.period.QuarterlyPeriodType;
 import org.junit.Test;
 
 /**
@@ -65,8 +67,10 @@
     
     private Period periodA;
     private Period periodB;
+    private Period periodC;
     
     private int periodIdA;
+    private int periodIdC;
     
     private OrganisationUnit unitA;
     private OrganisationUnit unitB;
@@ -92,6 +96,7 @@
 
     private Date onTimeA;
     private Date tooLateA;
+    private Date onTimeB;
     private Date tooLateB;
 
     // -------------------------------------------------------------------------
@@ -115,13 +120,15 @@
         
         completenessService = (DataSetCompletenessService) getBean( "registrationDataCompletenessService" );
 
-        periodType = periodService.getAllPeriodTypes().iterator().next();
+        periodType = new MonthlyPeriodType();
         
         periodA = createPeriod( periodType, getDate( 2000, 1, 1 ), getDate( 2000, 1, 31 ) );
         periodB = createPeriod( periodType, getDate( 2000, 2, 1 ), getDate( 2000, 2, 28 ) );
+        periodC = createPeriod( new QuarterlyPeriodType(), getDate( 2000, 1, 1 ), getDate( 2000, 3, 31 ) );        
         
         periodIdA = periodService.addPeriod( periodA );
         periodService.addPeriod( periodB );
+        periodIdC = periodService.addPeriod( periodC );
 
         unitA = createOrganisationUnit( 'A' );
         unitB = createOrganisationUnit( 'B' );
@@ -169,6 +176,7 @@
 
         onTimeA = getDate( 2000, 2, 10 );
         tooLateA = getDate( 2000, 2, 25 );
+        onTimeB = getDate( 2000, 3, 10 );
         tooLateB = getDate( 2000, 3, 25 );
     }
 
@@ -213,26 +221,31 @@
         
         registrationService.saveCompleteDataSetRegistration( new CompleteDataSetRegistration( dataSetA, periodA, unitA, tooLateA, "" ) );
         registrationService.saveCompleteDataSetRegistration( new CompleteDataSetRegistration( dataSetA, periodA, unitB, tooLateA, "" ) );
+        registrationService.saveCompleteDataSetRegistration( new CompleteDataSetRegistration( dataSetA, periodB, unitA, tooLateB, "" ) );
+        registrationService.saveCompleteDataSetRegistration( new CompleteDataSetRegistration( dataSetA, periodA, unitD, tooLateA, "" ) );
         
         registrationService.saveCompleteDataSetRegistration( new CompleteDataSetRegistration( dataSetB, periodA, unitA, tooLateA, "" ) );
-        registrationService.saveCompleteDataSetRegistration( new CompleteDataSetRegistration( dataSetB, periodA, unitC, tooLateA, "" ) );        
+        registrationService.saveCompleteDataSetRegistration( new CompleteDataSetRegistration( dataSetB, periodA, unitC, tooLateA, "" ) );
+        registrationService.saveCompleteDataSetRegistration( new CompleteDataSetRegistration( dataSetB, periodB, unitB, onTimeB, "" ) );
 
         registrationService.saveCompleteDataSetRegistration( new CompleteDataSetRegistration( dataSetC, periodA, unitC, tooLateA, "" ) );
-
-        registrationService.saveCompleteDataSetRegistration( new CompleteDataSetRegistration( dataSetA, periodB, unitA, tooLateB, "" ) );
-        registrationService.saveCompleteDataSetRegistration( new CompleteDataSetRegistration( dataSetA, periodA, unitD, tooLateA, "" ) );
+        registrationService.saveCompleteDataSetRegistration( new CompleteDataSetRegistration( dataSetC, periodB, unitA, tooLateB, "" ) );
         
         Collection<DataSetCompletenessResult> results = completenessService.getDataSetCompleteness( periodIdA, unitIdA );
         
-        DataSetCompletenessResult resultA = new DataSetCompletenessResult( dataSetA.getName(), 2, 2, 0 );
-        DataSetCompletenessResult resultB = new DataSetCompletenessResult( dataSetB.getName(), 2, 1, 0 );
-        DataSetCompletenessResult resultC = new DataSetCompletenessResult( dataSetC.getName(), 2, 0, 0 );        
-        
         assertNotNull( results );
         assertEquals( 3, results.size() );        
-        assertTrue( results.contains( resultA ) );
-        assertTrue( results.contains( resultB ) );
-        assertTrue( results.contains( resultC ) );        
+        assertTrue( results.contains( new DataSetCompletenessResult( dataSetA.getName(), 2, 2, 0 ) ) );
+        assertTrue( results.contains( new DataSetCompletenessResult( dataSetB.getName(), 2, 1, 0 ) ) );
+        assertTrue( results.contains( new DataSetCompletenessResult( dataSetC.getName(), 2, 0, 0 ) ) );  
+
+        results = completenessService.getDataSetCompleteness( periodIdC, unitIdA );
+   
+        assertNotNull( results );
+        assertEquals( 3, results.size() );
+        assertTrue( results.contains( new DataSetCompletenessResult( dataSetA.getName(), 6, 3, 0 ) ) );
+        assertTrue( results.contains( new DataSetCompletenessResult( dataSetB.getName(), 6, 2, 1 ) ) );
+        assertTrue( results.contains( new DataSetCompletenessResult( dataSetC.getName(), 6, 1, 0 ) ) );            
     }
 
     @Test

=== modified file 'dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java'
--- dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java	2012-01-03 03:50:58 +0000
+++ dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java	2012-03-16 16:49:58 +0000
@@ -167,4 +167,6 @@
     String getActivityPlan( int orgunitId, int min, int max );
     
     String limitRecord( int min, int max );
+    
+    String getAddDate( String dateField, int days );
 }

=== modified file 'dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/DerbyStatementBuilder.java'
--- dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/DerbyStatementBuilder.java	2012-03-15 09:25:30 +0000
+++ dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/DerbyStatementBuilder.java	2012-03-16 16:49:58 +0000
@@ -347,4 +347,9 @@
     {
         return " LIMIT " + max + " OFFSET " + min;
     }
+
+    public String getAddDate( String dateField, int days )
+    {
+        return "DATEADD('DAY'," + days + "," + dateField + ")";
+    }
 }

=== modified file 'dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/H2StatementBuilder.java'
--- dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/H2StatementBuilder.java	2012-03-15 09:25:30 +0000
+++ dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/H2StatementBuilder.java	2012-03-16 16:49:58 +0000
@@ -368,4 +368,9 @@
     {
         return " LIMIT " + max + " OFFSET " + min;
     }
+    
+    public String getAddDate( String dateField, int days )
+    {
+        return "DATEADD('DAY'," + days + "," + dateField + ")";
+    }
 }

=== modified file 'dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/MySQLStatementBuilder.java'
--- dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/MySQLStatementBuilder.java	2012-03-15 09:25:30 +0000
+++ dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/MySQLStatementBuilder.java	2012-03-16 16:49:58 +0000
@@ -365,4 +365,9 @@
     {
         return " LIMIT " + min + " ," + max;
     }
+
+    public String getAddDate( String dateField, int days )
+    {
+        return "ADDDATE(" + dateField + "," + days + ")";
+    }
 }

=== modified file 'dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/PostgreSQLStatementBuilder.java'
--- dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/PostgreSQLStatementBuilder.java	2012-03-15 09:25:30 +0000
+++ dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/PostgreSQLStatementBuilder.java	2012-03-16 16:49:58 +0000
@@ -70,7 +70,7 @@
 
     public String getDropDatasetForeignKeyForDataEntryFormTable()
     {
-        return  "ALTER TABLE dataentryform DROP CONSTRAINT fk_dataentryform_datasetid;" ;
+        return "ALTER TABLE dataentryform DROP CONSTRAINT fk_dataentryform_datasetid;" ;
     }
 
     @Override
@@ -365,4 +365,9 @@
     {
         return " LIMIT " + max + " OFFSET " + min;
     }
+    
+    public String getAddDate( String dateField, int days )
+    {
+        return "(" + dateField + "+" + days + ")";
+    }
 }