← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15005: Add new class, DateIntervalType (YEAR, MONTH, WEEK), replaces old toIsoInterval methods.

 

------------------------------------------------------------
revno: 15005
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2014-04-27 08:06:23 +0545
message:
  Add new class, DateIntervalType (YEAR, MONTH, WEEK), replaces old toIsoInterval methods.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/Calendar.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/ChronologyBasedCalendar.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateInterval.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/impl/NepaliCalendar.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/calendar/Calendar.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/Calendar.java	2014-04-26 07:16:19 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/Calendar.java	2014-04-27 02:21:23 +0000
@@ -79,21 +79,13 @@
     DateUnit fromIso( DateUnit dateUnit );
 
     /**
-     * Gets this local year as a ISO 8601 interval
-     * @param year Local year
+     * Gets interval of type based on DateUnit
+     * @param dateUnit DateUnit representing local year, month, day
      * @return ISO 8601 interval for year
      * @see <a href="http://en.wikipedia.org/wiki/ISO_8601";>http://en.wikipedia.org/wiki/ISO_8601</a>
-     */
-    DateInterval toIsoInterval( int year );
-
-    /**
-     * Gets this local year/month as a ISO 8601 interval
-     * @param year  Local year
-     * @param month Local month
-     * @return ISO 8601 interval for year/month
-     * @see <a href="http://en.wikipedia.org/wiki/ISO_8601";>http://en.wikipedia.org/wiki/ISO_8601</a>
-     */
-    DateInterval toIsoInterval( int year, int month );
+     * @see DateInterval.DateIntervalType
+     */
+    DateInterval toIsoInterval( DateUnit dateUnit, DateInterval.DateIntervalType type );
 
     /**
      * Gets current date as local DateUnit

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/ChronologyBasedCalendar.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/ChronologyBasedCalendar.java	2014-04-25 13:14:41 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/ChronologyBasedCalendar.java	2014-04-27 02:21:23 +0000
@@ -62,27 +62,51 @@
     }
 
     @Override
-    public DateInterval toIsoInterval( int year )
-    {
-        DateUnit from = new DateUnit( year, 1, 1 );
-        DateUnit to = new DateUnit( year, monthsInYear(), daysInMonth( year, monthsInYear() ) );
-
-        from.setDayOfWeek( isoWeekday( from ) );
-        to.setDayOfWeek( isoWeekday( to ) );
-
-        return new DateInterval( from, to );
-    }
-
-    @Override
-    public DateInterval toIsoInterval( int year, int month )
-    {
-        DateUnit from = new DateUnit( year, month, 1 );
-        DateUnit to = new DateUnit( year, month, daysInMonth( year, month ) );
-
-        from.setDayOfWeek( isoWeekday( from ) );
-        to.setDayOfWeek( isoWeekday( to ) );
-
-        return new DateInterval( from, to );
+    public DateInterval toIsoInterval( DateUnit dateUnit, DateInterval.DateIntervalType type )
+    {
+        switch ( type )
+        {
+            case YEAR:
+                return toYearIsoInterval( dateUnit );
+            case MONTH:
+                return toMonthIsoInterval( dateUnit );
+            case WEEK:
+                return toWeekIsoInterval( dateUnit );
+        }
+
+        return null;
+    }
+
+    private DateInterval toYearIsoInterval( DateUnit dateUnit )
+    {
+        DateUnit from = new DateUnit( dateUnit.getYear(), 1, 1 );
+        DateUnit to = new DateUnit( dateUnit.getYear(), monthsInYear(), daysInMonth( dateUnit.getYear(), monthsInYear() ) );
+
+        from.setDayOfWeek( isoWeekday( from ) );
+        to.setDayOfWeek( isoWeekday( to ) );
+
+        return new DateInterval( from, to );
+    }
+
+    private DateInterval toMonthIsoInterval( DateUnit dateUnit )
+    {
+        DateUnit from = new DateUnit( dateUnit.getYear(), dateUnit.getMonth(), 1 );
+        DateUnit to = new DateUnit( dateUnit.getYear(), dateUnit.getMonth(), daysInMonth( dateUnit.getYear(), dateUnit.getMonth() ) );
+
+        from.setDayOfWeek( isoWeekday( from ) );
+        to.setDayOfWeek( isoWeekday( to ) );
+
+        return new DateInterval( from, to );
+    }
+
+    private DateInterval toWeekIsoInterval( DateUnit dateUnit )
+    {
+        DateTime dateTime = new DateTime( dateUnit.getYear(), dateUnit.getMonth(), dateUnit.getDay(), 0, 0, chronology );
+
+        DateTime from = dateTime.weekOfWeekyear().toInterval().getStart();
+        DateTime to = dateTime.weekOfWeekyear().toInterval().getEnd().minusDays( 1 );
+
+        return new DateInterval( DateUnit.fromDateTime( from ), DateUnit.fromDateTime( to ) );
     }
 
     @Override

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateInterval.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateInterval.java	2014-04-26 06:29:49 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateInterval.java	2014-04-27 02:21:23 +0000
@@ -38,6 +38,11 @@
  */
 public class DateInterval
 {
+    public enum DateIntervalType
+    {
+        YEAR, MONTH, WEEK
+    }
+
     /**
      * Start of interval. Required.
      */

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/impl/NepaliCalendar.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/impl/NepaliCalendar.java	2014-04-25 12:25:39 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/impl/NepaliCalendar.java	2014-04-27 02:21:23 +0000
@@ -127,27 +127,51 @@
     }
 
     @Override
-    public DateInterval toIsoInterval( int year )
-    {
-        DateUnit from = new DateUnit( year, 1, 1 );
-        DateUnit to = new DateUnit( year, monthsInYear(), daysInMonth( year, monthsInYear() ) );
-
-        from = toIso( from );
-        to = toIso( to );
-
-        return new DateInterval( from, to );
-    }
-
-    @Override
-    public DateInterval toIsoInterval( int year, int month )
-    {
-        DateUnit from = new DateUnit( year, month, 1 );
-        DateUnit to = new DateUnit( year, month, daysInMonth( year, month ) );
-
-        from = toIso( from );
-        to = toIso( to );
-
-        return new DateInterval( from, to );
+    public DateInterval toIsoInterval( DateUnit dateUnit, DateInterval.DateIntervalType type )
+    {
+        switch ( type )
+        {
+            case YEAR:
+                return toYearIsoInterval( dateUnit );
+            case MONTH:
+                return toMonthIsoInterval( dateUnit );
+            case WEEK:
+                return toWeekIsoInterval( dateUnit );
+        }
+
+        return null;
+    }
+
+    private DateInterval toYearIsoInterval( DateUnit dateUnit )
+    {
+        DateUnit from = new DateUnit( dateUnit.getYear(), 1, 1 );
+        DateUnit to = new DateUnit( dateUnit.getYear(), monthsInYear(), daysInMonth( dateUnit.getYear(), monthsInYear() ) );
+
+        from = toIso( from );
+        to = toIso( to );
+
+        return new DateInterval( from, to );
+    }
+
+    private DateInterval toMonthIsoInterval( DateUnit dateUnit )
+    {
+        DateUnit from = new DateUnit( dateUnit.getYear(), dateUnit.getMonth(), 1 );
+        DateUnit to = new DateUnit( dateUnit.getYear(), dateUnit.getMonth(), daysInMonth( dateUnit.getYear(), dateUnit.getMonth() ) );
+
+        from = toIso( from );
+        to = toIso( to );
+
+        return new DateInterval( from, to );
+    }
+
+    private DateInterval toWeekIsoInterval( DateUnit dateUnit )
+    {
+        DateTime dateTime = toIso( dateUnit ).toDateTime();
+
+        DateTime from = dateTime.weekOfWeekyear().toInterval().getStart();
+        DateTime to = dateTime.weekOfWeekyear().toInterval().getEnd().minusDays( 1 );
+
+        return new DateInterval( DateUnit.fromDateTime( from ), DateUnit.fromDateTime( to ) );
     }
 
     @Override