← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2846: Added support for creating periods from iso format (needs cleanup)

 

------------------------------------------------------------
revno: 2846
committer: Jo Størset <storset@xxxxxxxxx>
branch nick: trunk
timestamp: Tue 2011-02-15 11:52:24 +0530
message:
  Added support for creating periods from iso format (needs cleanup)
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/DailyPeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/FinancialAprilPeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/OnChangePeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/QuarterlyPeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/SixMonthlyPeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/SurveyPeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/TwoYearlyPeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/WeeklyPeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/YearlyPeriodType.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/utils/PeriodUtil.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/period/DailyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/DailyPeriodType.java	2010-12-22 09:16:14 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/DailyPeriodType.java	2011-02-15 06:22:24 +0000
@@ -27,6 +27,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
@@ -43,6 +44,8 @@
 public class DailyPeriodType
     extends CalendarPeriodType
 {
+    private static final String ISO_FORMAT = "yyyyMMdd";
+
     /**
      * The name of the DailyPeriodType, which is "Daily".
      */
@@ -136,7 +139,30 @@
     @Override
     public String getIsoDate( Period period )
     {
-        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
+        SimpleDateFormat format = new SimpleDateFormat(ISO_FORMAT);
         return format.format( period.getStartDate());
     }
+
+    @Override
+    public Period createPeriod( String isoDate )
+    {
+        SimpleDateFormat format = new SimpleDateFormat(ISO_FORMAT);
+        try
+        {
+            Date date = format.parse( isoDate );
+            return createPeriod( date );
+        }
+        catch ( ParseException e )
+        {
+            throw new RuntimeException( e );
+        }
+    }
+
+    @Override
+    public String getIsoFormat()
+    {
+        return ISO_FORMAT;
+    }
+    
+    
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/FinancialAprilPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/FinancialAprilPeriodType.java	2010-12-20 12:43:44 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/FinancialAprilPeriodType.java	2011-02-15 06:22:24 +0000
@@ -150,4 +150,16 @@
     {
         throw new UnsupportedOperationException( "Not supported yet." );
     }
+
+    @Override
+    public Period createPeriod( String isoDate )
+    {
+        throw new UnsupportedOperationException( "Not supported yet." );
+    }
+
+    @Override
+    public String getIsoFormat()
+    {
+        throw new UnsupportedOperationException( "Not supported yet." );
+    }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java	2010-12-20 12:43:44 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java	2011-02-15 06:22:24 +0000
@@ -27,6 +27,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
@@ -45,6 +46,8 @@
     extends CalendarPeriodType
 {
 
+    private static final String ISO_FORMAT = "yyyyMM";
+
     /**
      * The name of the MonthlyPeriodType, which is "Monthly".
      */
@@ -149,7 +152,29 @@
     @Override
     public String getIsoDate( Period period )
     {
-        SimpleDateFormat format = new SimpleDateFormat( "yyyyMM" );
+        SimpleDateFormat format = new SimpleDateFormat( ISO_FORMAT );
         return format.format( period.getStartDate() );
     }
+
+    @Override
+    public Period createPeriod( String isoDate )
+    {
+        SimpleDateFormat format = new SimpleDateFormat( ISO_FORMAT );
+        try
+        {
+            Date date = format.parse( isoDate );
+            return createPeriod(date);
+        }
+        catch ( ParseException e )
+        {
+            throw new RuntimeException( e );
+        }
+    }
+
+    @Override
+    public String getIsoFormat()
+    {
+        return ISO_FORMAT;
+    }
+
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/OnChangePeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/OnChangePeriodType.java	2010-12-20 12:43:44 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/OnChangePeriodType.java	2011-02-15 06:22:24 +0000
@@ -120,4 +120,16 @@
     {
         throw new UnsupportedOperationException( "Not supported yet." );
     }
+
+    @Override
+    public Period createPeriod( String isoDate )
+    {
+        throw new UnsupportedOperationException( "Not supported yet." );
+    }
+
+    @Override
+    public String getIsoFormat()
+    {
+        throw new UnsupportedOperationException( "Not supported yet." );
+    }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java	2010-12-29 16:17:28 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java	2011-02-15 06:22:24 +0000
@@ -221,6 +221,10 @@
      */
     public abstract String getIsoDate(Period period);
 
+    public abstract Period createPeriod(String isoDate);
+
+    public abstract String getIsoFormat();
+    
     // -------------------------------------------------------------------------
     // hashCode and equals
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/QuarterlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/QuarterlyPeriodType.java	2010-12-20 12:43:44 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/QuarterlyPeriodType.java	2011-02-15 06:22:24 +0000
@@ -124,7 +124,7 @@
 
         return new Period( this, startDate, cal.getTime() );
     }
-    
+
     /**
      * Generates quarterly Periods for the whole year in which the given
      * Period's startDate exists.
@@ -155,29 +155,62 @@
     public String getIsoDate( Period period )
     {
         Calendar cal = createCalendarInstance( period.getStartDate() );
-        int year = cal.get( Calendar.YEAR);
-        int month = cal.get( Calendar.MONTH);
-
-        String periodString = null;
-
-        switch (month) {
+        int year = cal.get( Calendar.YEAR );
+        int month = cal.get( Calendar.MONTH );
+
+        return year + Quarter.getByMonth( month ).name();
+    }
+
+    @Override
+    public Period createPeriod( String isoDate )
+    {
+        int year = Integer.parseInt( isoDate.substring( 0, 4 ) );
+        int month = Quarter.valueOf( isoDate.substring( 4, 6 ) ).getMonth();
+        
+        Calendar cal = createCalendarInstance();
+        cal.set( year, month, 1 );
+        return createPeriod( cal );
+    }
+
+    @Override
+    public String getIsoFormat()
+    {
+        return "yyyyQn (n: 1-4)";
+    }
+
+    public enum Quarter
+    {
+        Q1( Calendar.JANUARY ), Q2( Calendar.APRIL ), Q3( Calendar.JULY ), Q4( Calendar.OCTOBER );
+
+        private final int month;
+
+        Quarter( int month )
+        {
+            this.month = month;
+        }
+
+        public int getMonth()
+        {
+            return month;
+        }
+
+        public static Quarter getByMonth( int month )
+        {
+            switch ( month )
+            {
             case Calendar.JANUARY:
-                periodString = year + "Q1";
-                break;
+                return Q1;
             case Calendar.APRIL:
-                periodString = year + "Q2";
-                break;
+                return Q2;
             case Calendar.JULY:
-                periodString = year + "Q3";
-                break;
+                return Q3;
             case Calendar.OCTOBER:
-                periodString = year + "Q1";
-                break;
+                return Q4;
             default:
-                throw new RuntimeException("Not a valid quarterly period");
+                throw new IllegalArgumentException( "Not a valid quarterly starting month" );
+            }
+
         }
-
-        return periodString;
     }
 
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/SixMonthlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/SixMonthlyPeriodType.java	2010-12-20 12:43:44 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/SixMonthlyPeriodType.java	2011-02-15 06:22:24 +0000
@@ -47,7 +47,7 @@
      * The name of the SixMonthlyPeriodType, which is "SixMonthly".
      */
     public static final String NAME = "SixMonthly";
-    
+
     public static final int FREQUENCY_ORDER = 182;
 
     // -------------------------------------------------------------------------
@@ -126,8 +126,8 @@
     }
 
     /**
-     * Generates six-monthly Periods for the whole year in which the given Period's
-     * startDate exists.
+     * Generates six-monthly Periods for the whole year in which the given
+     * Period's startDate exists.
      */
     @Override
     public List<Period> generatePeriods( Date date )
@@ -155,23 +155,59 @@
     public String getIsoDate( Period period )
     {
         Calendar cal = createCalendarInstance( period.getStartDate() );
-        int year = cal.get( Calendar.YEAR);
-        int month = cal.get( Calendar.MONTH);
-
-        String periodString = null;
-
-        switch (month) {
+        int year = cal.get( Calendar.YEAR );
+        int month = cal.get( Calendar.MONTH );
+
+        return year + Semester.getByMonth( month ).name();
+    }
+
+    @Override
+    public Period createPeriod( String isoDate )
+    {
+        int year = Integer.parseInt( isoDate.substring( 0, 4 ) );
+        int month = Semester.valueOf( isoDate.substring( 4, 6 ) ).getMonth();
+        
+        Calendar cal = createCalendarInstance();
+        cal.set( year, month, 1 );
+        return createPeriod( cal );
+
+    }
+
+    @Override
+    public String getIsoFormat()
+    {
+        return "yyyySn (n: 1-2)";
+    }
+
+    public enum Semester
+    {
+        S1( Calendar.JANUARY ), S2( Calendar.JULY );
+
+        private final int month;
+
+        Semester( int month )
+        {
+            this.month = month;
+        }
+
+        public int getMonth()
+        {
+            return month;
+        }
+
+        public static Semester getByMonth( int month )
+        {
+            switch ( month )
+            {
             case Calendar.JANUARY:
-                periodString = year + "S1";
-                break;
+                return S1;
             case Calendar.JULY:
-                periodString = year + "S2";
-                break;
+                return S2;
             default:
-                throw new RuntimeException("Not a valid six-monthly period");
+                throw new IllegalArgumentException( "Not a valid six-monthly starting month" );
+            }
+
         }
-
-        return periodString;
     }
 
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/SurveyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/SurveyPeriodType.java	2010-12-20 12:43:44 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/SurveyPeriodType.java	2011-02-15 06:22:24 +0000
@@ -81,4 +81,16 @@
     {
         throw new UnsupportedOperationException( "Not supported yet." );
     }
+
+    @Override
+    public Period createPeriod( String isoDate )
+    {
+        throw new UnsupportedOperationException( "Not supported yet." );
+    }
+
+    @Override
+    public String getIsoFormat()
+    {
+        throw new UnsupportedOperationException( "Not supported yet." );
+    }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/TwoYearlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/TwoYearlyPeriodType.java	2010-12-20 12:43:44 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/TwoYearlyPeriodType.java	2011-02-15 06:22:24 +0000
@@ -154,4 +154,16 @@
     {
         throw new UnsupportedOperationException( "Not supported yet." );
     }
+
+    @Override
+    public Period createPeriod( String isoDate )
+    {
+        throw new UnsupportedOperationException( "Not supported yet." );
+    }
+
+    @Override
+    public String getIsoFormat()
+    {
+        return "Not supported yet.";
+    }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/WeeklyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/WeeklyPeriodType.java	2010-12-20 12:43:44 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/WeeklyPeriodType.java	2011-02-15 06:22:24 +0000
@@ -214,4 +214,25 @@
         return periodString;
     }
 
+    @Override
+    public Period createPeriod( String isoDate )
+    {
+        int year = Integer.parseInt( isoDate.substring( 0, 4 ) );
+        int week = Integer.parseInt( isoDate.substring( 5 ) );
+        
+        Calendar cal = Calendar.getInstance();
+        cal.set( Calendar.YEAR, year );
+        cal.set( Calendar.WEEK_OF_YEAR, week );
+        cal.setFirstDayOfWeek( Calendar.MONDAY );
+
+        return createPeriod( cal.getTime() );
+
+    }
+
+    @Override
+    public String getIsoFormat()
+    {
+        return "yyyyWn (n: week number)";
+    }
+
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/YearlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/YearlyPeriodType.java	2010-12-20 12:43:44 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/YearlyPeriodType.java	2011-02-15 06:22:24 +0000
@@ -147,12 +147,25 @@
     public String getIsoDate( Period period )
     {
         Calendar cal = createCalendarInstance( period.getStartDate() );
-        int year = cal.get( Calendar.YEAR);
+        int year = cal.get( Calendar.YEAR );
 
         String periodString = String.valueOf( year );
 
         return periodString;
     }
 
+    @Override
+    public Period createPeriod( String isoDate )
+    {
+        Calendar cal = createCalendarInstance();
+        cal.set( Calendar.YEAR, Integer.parseInt( isoDate ) );
+        return createPeriod( cal );
+    }
+
+    @Override
+    public String getIsoFormat()
+    {
+        return "yyyy";
+    }
 
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/utils/PeriodUtil.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/utils/PeriodUtil.java	2011-01-12 09:13:36 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/utils/PeriodUtil.java	2011-02-15 06:22:24 +0000
@@ -42,6 +42,7 @@
 
 public class PeriodUtil
 {
+    
     public static Period getPeriod( String periodName, PeriodType periodType )
         throws IllegalArgumentException
     {
@@ -77,13 +78,7 @@
             int week = Integer.parseInt( periodName.substring( 0, dashIndex ) );
             int year = Integer.parseInt( periodName.substring( dashIndex + 1, periodName.length() ) );
 
-            Calendar cal = Calendar.getInstance();
-            cal.set( Calendar.YEAR, year );
-            cal.set( Calendar.WEEK_OF_YEAR, week );
-            cal.setFirstDayOfWeek( Calendar.MONDAY );
-
-            WeeklyPeriodType weeklyPeriodType = new WeeklyPeriodType();
-            return weeklyPeriodType.createPeriod( cal.getTime() );
+            return periodType.createPeriod(year + "W" + week);
         }
 
         if ( periodType instanceof MonthlyPeriodType )