← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2026: Improved handling of relative periods in RelativePeriods class + added tests

 

------------------------------------------------------------
revno: 2026
committer: Lars <larshelg@larshelg-laptop>
branch nick: trunk
timestamp: Mon 2010-06-21 21:11:47 +0200
message:
  Improved handling of relative periods in RelativePeriods class + added tests
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PastPeriodPredicate.java
  dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/RelativePeriodTest.java
modified:
  dhis-2/dhis-api/pom.xml
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/RelativePeriods.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/pom.xml'
--- dhis-2/dhis-api/pom.xml	2010-05-26 15:58:31 +0000
+++ dhis-2/dhis-api/pom.xml	2010-06-21 19:11:47 +0000
@@ -21,8 +21,11 @@
 	<dependency>
 	  <groupId>commons-lang</groupId>
 	  <artifactId>commons-lang</artifactId>
-	  <version>2.0</version>
 	</dependency>
+	<dependency>
+	  <groupId>commons-collections</groupId>
+      <artifactId>commons-collections</artifactId>
+    </dependency>
   </dependencies>
   
   <properties>

=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PastPeriodPredicate.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PastPeriodPredicate.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PastPeriodPredicate.java	2010-06-21 19:11:47 +0000
@@ -0,0 +1,29 @@
+package org.hisp.dhis.period;
+
+import java.util.Calendar;
+import java.util.Date;
+
+import org.apache.commons.collections.Predicate;
+
+public class PastPeriodPredicate
+    implements Predicate
+{
+    private Date date;
+    
+    protected PastPeriodPredicate()
+    {
+    }
+    
+    public PastPeriodPredicate( Date d )
+    {
+        Calendar cal = PeriodType.createCalendarInstance( d );
+        cal.set( Calendar.DAY_OF_MONTH, cal.getActualMaximum( Calendar.DAY_OF_MONTH ) );
+        this.date = cal.getTime();
+    }
+
+    @Override
+    public boolean evaluate( Object o )
+    {
+        return ((Period) o).getEndDate().compareTo( date ) <= 0;
+    }
+}

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/RelativePeriods.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/RelativePeriods.java	2010-06-21 14:17:13 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/RelativePeriods.java	2010-06-21 19:11:47 +0000
@@ -28,6 +28,13 @@
  */
 
 import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+
+import org.apache.commons.collections.CollectionUtils;
 
 /**
  * @author Lars Helge Overland
@@ -139,6 +146,104 @@
     // Logic
     // -------------------------------------------------------------------------
 
+    /**
+     * Gets a list of Periods based on the given input and the state of this RelativePeriods.
+     * 
+     * @param months the number of months back in time representing the current month.
+     * @param format the i18n format.
+     * @return a list of relative Periods.
+     */
+    public List<Period> getRelativePeriods( int months )
+    {
+        return getRelativePeriods( months, null );
+    }
+    
+    /**
+     * Gets a list of Periods based on the given input and the state of this RelativePeriods.
+     * 
+     * @param months the number of months back in time representing the current month.
+     * @param format the i18n format.
+     * @param date the date representing now (for testing purposes).
+     * @return a list of relative Periods.
+     */
+    protected List<Period> getRelativePeriods( int months, Date date )
+    {
+        List<Period> periods = new ArrayList<Period>();
+        
+        Date current = getDate( months, date );
+        
+        if ( isReportingMonth() )
+        {
+            periods.add( new MonthlyPeriodType().createPeriod( getDate( months, date ) ) );
+        }
+        if ( isLast3Months() )
+        {
+            periods.add( new QuarterlyPeriodType().createPeriod( getDate( months + 2, date ) ) );
+        }
+        if ( isLast6Months() )
+        {
+            periods.add( new SixMonthlyPeriodType().createPeriod( getDate( months + 5, date ) ) );
+        }
+        if ( isLast12Months() )
+        {
+            periods.add( new YearlyPeriodType().createPeriod( getDate( months + 11, date ) ) );
+        }
+        if ( isLast3To6Months() )
+        {
+            periods.add( new QuarterlyPeriodType().createPeriod( getDate( months + 5, date ) ) );
+        }
+        if ( isLast6To9Months() )
+        {
+            periods.add( new QuarterlyPeriodType().createPeriod( getDate( months + 8, date ) ) );
+        }
+        if ( isLast9To12Months() )
+        {
+            periods.add( new QuarterlyPeriodType().createPeriod( getDate( months + 11, date ) ) );
+        }
+        if ( isLast12IndividualMonths() )
+        {
+            for ( int i = 11; i >= 0; i-- )
+            {
+                periods.add( new MonthlyPeriodType().createPeriod( getDate( months + i, date ) ) );
+            }
+        }
+        if ( isIndividualMonthsThisYear() )
+        {
+            Collection<Period> individualMonths = new MonthlyPeriodType().generatePeriods( new MonthlyPeriodType().createPeriod( current ) );            
+            CollectionUtils.filter( individualMonths, new PastPeriodPredicate( current ) );            
+            periods.addAll( individualMonths );
+        }
+        if ( isIndividualQuartersThisYear() )
+        {
+            Collection<Period> individualQuarters = new QuarterlyPeriodType().generatePeriods( new QuarterlyPeriodType().createPeriod( current ) );
+            CollectionUtils.filter( individualQuarters, new PastPeriodPredicate( current ) );
+            periods.addAll( individualQuarters );
+        }
+            
+        return periods;
+    }
+
+    /**
+     * Returns a date.
+     * 
+     * @param months the number of months to subtract from the current date.
+     * @param now the date representing now, ignored if null.
+     * @return a date.
+     */
+    private Date getDate( int months, Date now )
+    {
+        Calendar cal = PeriodType.createCalendarInstance();
+        
+        if ( now != null ) // For testing purposes
+        {
+            cal.setTime( now );
+        }
+        
+        cal.add( Calendar.MONTH, ( months * -1 ) );        
+        
+        return cal.getTime();
+    }
+    
     public boolean isReportingMonth()
     {
         return reportingMonth != null && reportingMonth;

=== added file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/RelativePeriodTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/RelativePeriodTest.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/RelativePeriodTest.java	2010-06-21 19:11:47 +0000
@@ -0,0 +1,161 @@
+package org.hisp.dhis.period;
+
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertTrue;
+
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Date;
+
+import org.junit.Test;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class RelativePeriodTest
+{
+    private static Date getDate( int year, int month, int day )
+    {
+        final Calendar calendar = Calendar.getInstance();
+
+        calendar.clear();
+        calendar.set( year, month - 1, day );
+
+        return calendar.getTime();
+    }
+    
+    @Test
+    public void getRelativePeriodsA()
+    {        
+        RelativePeriods periods = new RelativePeriods( true, true, true, true, true, true, true, true, true, true, true, true, true );
+        
+        Collection<Period> relatives = periods.getRelativePeriods( 1, getDate( 2001, 1, 1 ) );
+        
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 12, 1 ), getDate( 2000, 12, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 2000, 10, 1 ), getDate( 2000, 12, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new SixMonthlyPeriodType(), getDate( 2000, 7, 1 ), getDate( 2000, 12, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new YearlyPeriodType(), getDate( 2000, 1, 1 ), getDate( 2000, 12, 31 ) ) ) );
+        
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 2000, 7, 1 ), getDate( 2000, 9, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 2000, 4, 1 ), getDate( 2000, 6, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 2000, 1, 1 ), getDate( 2000, 3, 31 ) ) ) );
+        
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 1, 1 ), getDate( 2000, 1, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 2, 1 ), getDate( 2000, 2, 29 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 3, 1 ), getDate( 2000, 3, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 4, 1 ), getDate( 2000, 4, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 5, 1 ), getDate( 2000, 5, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 6, 1 ), getDate( 2000, 6, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 7, 1 ), getDate( 2000, 7, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 8, 1 ), getDate( 2000, 8, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 9, 1 ), getDate( 2000, 9, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 10, 1 ), getDate( 2000, 10, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 11, 1 ), getDate( 2000, 11, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 12, 1 ), getDate( 2000, 12, 31 ) ) ) );        
+
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 2000, 1, 1 ), getDate( 2000, 3, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 2000, 4, 1 ), getDate( 2000, 6, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 2000, 7, 1 ), getDate( 2000, 9, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 2000, 10, 1 ), getDate( 2000, 12, 31 ) ) ) );
+        
+        assertEquals( 35, relatives.size() );
+    }
+    
+    @Test
+    public void getRelativePeriodsB()
+    {        
+        RelativePeriods periods = new RelativePeriods( true, true, true, true, true, true, true, true, true, true, true, true, true );
+        
+        Collection<Period> relatives = periods.getRelativePeriods( 2, getDate( 2001, 1, 1 ) );
+
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 11, 1 ), getDate( 2000, 11, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 2000, 7, 1 ), getDate( 2000, 9, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new SixMonthlyPeriodType(), getDate( 2000, 1, 1 ), getDate( 2000, 6, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new YearlyPeriodType(), getDate( 1999, 1, 1 ), getDate( 1999, 12, 31 ) ) ) );
+        
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 2000, 4, 1 ), getDate( 2000, 6, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 2000, 1, 1 ), getDate( 2000, 3, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 1999, 10, 1 ), getDate( 1999, 12, 31 ) ) ) );
+
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 1999, 12, 1 ), getDate( 1999, 12, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 1, 1 ), getDate( 2000, 1, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 2, 1 ), getDate( 2000, 2, 29 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 3, 1 ), getDate( 2000, 3, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 4, 1 ), getDate( 2000, 4, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 5, 1 ), getDate( 2000, 5, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 6, 1 ), getDate( 2000, 6, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 7, 1 ), getDate( 2000, 7, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 8, 1 ), getDate( 2000, 8, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 9, 1 ), getDate( 2000, 9, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 10, 1 ), getDate( 2000, 10, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 11, 1 ), getDate( 2000, 11, 30 ) ) ) );      
+
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 2000, 1, 1 ), getDate( 2000, 3, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 2000, 4, 1 ), getDate( 2000, 6, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 2000, 7, 1 ), getDate( 2000, 9, 30 ) ) ) );
+        
+        assertEquals( 33, relatives.size() );
+    }
+
+    @Test
+    public void getRelativePeriodsC()
+    {        
+        RelativePeriods periods = new RelativePeriods( true, true, true, true, true, true, true, true, true, true, true, true, true );
+        
+        Collection<Period> relatives = periods.getRelativePeriods( 5, getDate( 2001, 1, 1 ) );
+
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 8, 1 ), getDate( 2000, 8, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 2000, 4, 1 ), getDate( 2000, 6, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new SixMonthlyPeriodType(), getDate( 2000, 1, 1 ), getDate( 2000, 6, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new YearlyPeriodType(), getDate( 1999, 1, 1 ), getDate( 1999, 12, 31 ) ) ) );
+        
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 2000, 1, 1 ), getDate( 2000, 3, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 1999, 10, 1 ), getDate( 1999, 12, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 1999, 7, 1 ), getDate( 1999, 9, 30 ) ) ) );
+
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 1999, 9, 1 ), getDate( 1999, 9, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 1999, 10, 1 ), getDate( 1999, 10, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 1999, 11, 1 ), getDate( 1999, 11, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 1999, 12, 1 ), getDate( 1999, 12, 31 ) ) ) );        
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 1, 1 ), getDate( 2000, 1, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 2, 1 ), getDate( 2000, 2, 29 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 3, 1 ), getDate( 2000, 3, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 4, 1 ), getDate( 2000, 4, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 5, 1 ), getDate( 2000, 5, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 6, 1 ), getDate( 2000, 6, 30 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 7, 1 ), getDate( 2000, 7, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new MonthlyPeriodType(), getDate( 2000, 8, 1 ), getDate( 2000, 8, 31 ) ) ) );  
+
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 2000, 1, 1 ), getDate( 2000, 3, 31 ) ) ) );
+        assertTrue( relatives.contains( new Period( new QuarterlyPeriodType(), getDate( 2000, 4, 1 ), getDate( 2000, 6, 30 ) ) ) );
+        
+        assertEquals( 29, relatives.size() );
+    }
+}