← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15207: refactored code for generating intervals, now supports YEAR, MONTH, WEEK, DAY intervals with opti...

 

------------------------------------------------------------
revno: 15207
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2014-05-09 20:53:24 +0200
message:
  refactored code for generating intervals, now supports YEAR, MONTH, WEEK, DAY intervals with optional negative/positive offset, and length of interval, wip (nepali calendar not updated yet)
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/AbstractCalendar.java
  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/DateIntervalType.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/AbstractCalendar.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/AbstractCalendar.java	2014-05-09 12:19:31 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/AbstractCalendar.java	2014-05-09 18:53:24 +0000
@@ -150,6 +150,18 @@
     }
 
     @Override
+    public DateInterval toInterval( DateUnit dateUnit, DateIntervalType type )
+    {
+        return toInterval( dateUnit, type, 0, 1 );
+    }
+
+    @Override
+    public DateInterval toInterval( DateIntervalType type, int offset, int length )
+    {
+        return toInterval( today(), type, offset, length );
+    }
+
+    @Override
     public DateUnit today()
     {
         DateTime dateTime = DateTime.now( ISOChronology.getInstance() );

=== 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-05-09 12:19:31 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/Calendar.java	2014-05-09 18:53:24 +0000
@@ -118,6 +118,18 @@
     /**
      * Gets interval of type based on DateUnit
      * @param dateUnit DateUnit representing local year, month, day
+     * @param type     Interval type to get
+     * @param offset   Offset to start at, can be negative of positive
+     * @param length   How many periods to asks for, i.e. type = MONTH, length = 2, two months
+     * @return Interval for interval type based on dateUnit
+     * @see DateIntervalType
+     */
+    DateInterval toInterval( DateUnit dateUnit, DateIntervalType type, int offset, int length );
+
+    /**
+     * Gets interval of type based on DateUnit using default options, 0 for offset, 1 for length
+     * @param dateUnit DateUnit representing local year, month, day
+     * @param type     Interval type to get
      * @return Interval for interval type based on dateUnit
      * @see DateIntervalType
      */
@@ -125,12 +137,23 @@
 
     /**
      * Gets interval of type based on todays date
+     * @param type     Interval type to get
      * @return Interval for interval type based on dateUnit
      * @see DateIntervalType
      */
     DateInterval toInterval( DateIntervalType type );
 
     /**
+     * Gets interval of type based on today's date
+     * @param type     Interval type to get
+     * @param offset Offset to start at, can be negative of positive
+     * @param length How many periods to asks for, i.e. type = MONTH, length = 2, two months
+     * @return Interval for interval type based on dateUnit
+     * @see DateIntervalType
+     */
+    DateInterval toInterval( DateIntervalType type, int offset, int length );
+
+    /**
      * Gets current date as local DateUnit
      * @return Today 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-05-09 12:19:31 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/ChronologyBasedCalendar.java	2014-05-09 18:53:24 +0000
@@ -62,51 +62,121 @@
     }
 
     @Override
-    public DateInterval toInterval( DateUnit dateUnit, DateIntervalType type )
+    public DateInterval toInterval( DateUnit dateUnit, DateIntervalType type, int offset, int length )
     {
         switch ( type )
         {
             case ISO8601_YEAR:
-                return toYearIsoInterval( dateUnit );
+                return toYearIsoInterval( dateUnit, offset, length );
             case ISO8601_MONTH:
-                return toMonthIsoInterval( dateUnit );
+                return toMonthIsoInterval( dateUnit, offset, length );
             case ISO8601_WEEK:
-                return toWeekIsoInterval( dateUnit );
+                return toWeekIsoInterval( dateUnit, offset, length );
+            case ISO8601_DAY:
+                return toDayIsoInterval( dateUnit, offset, length );
         }
 
         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( toIso( from ), toIso( to ), DateIntervalType.ISO8601_YEAR );
-    }
-
-    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( toIso( from ), toIso( to ), DateIntervalType.ISO8601_MONTH );
-    }
-
-    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 ), DateIntervalType.ISO8601_WEEK );
+    private DateInterval toYearIsoInterval( DateUnit dateUnit, int offset, int length )
+    {
+        DateTime from = dateUnit.toDateTime( chronology ).withMonthOfYear( 1 ).withDayOfMonth( 1 );
+
+        if ( offset > 0 )
+        {
+            from = from.plusYears( offset );
+        }
+        else if ( offset < 0 )
+        {
+            from = from.minusYears( -offset );
+        }
+
+        DateTime to = new DateTime( from ).plusYears( length ).minusDays( 1 );
+
+        DateUnit fromDateUnit = DateUnit.fromDateTime( from );
+        DateUnit toDateUnit = DateUnit.fromDateTime( to );
+
+        fromDateUnit.setDayOfWeek( isoWeekday( fromDateUnit ) );
+        toDateUnit.setDayOfWeek( isoWeekday( toDateUnit ) );
+
+        return new DateInterval( toIso( fromDateUnit ), toIso( toDateUnit ),
+            DateIntervalType.ISO8601_YEAR );
+    }
+
+    private DateInterval toMonthIsoInterval( DateUnit dateUnit, int offset, int length )
+    {
+        DateTime from = dateUnit.toDateTime( chronology ).withDayOfMonth( 1 );
+
+        if ( offset > 0 )
+        {
+            from = from.plusMonths( offset );
+        }
+        else if ( offset < 0 )
+        {
+            from = from.minusMonths( -offset );
+        }
+
+        DateTime to = new DateTime( from ).plusMonths( length ).minusDays( 1 );
+
+        DateUnit fromDateUnit = DateUnit.fromDateTime( from );
+        DateUnit toDateUnit = DateUnit.fromDateTime( to );
+
+        fromDateUnit.setDayOfWeek( isoWeekday( fromDateUnit ) );
+        toDateUnit.setDayOfWeek( isoWeekday( toDateUnit ) );
+
+        return new DateInterval( toIso( fromDateUnit ), toIso( toDateUnit ),
+            DateIntervalType.ISO8601_MONTH );
+    }
+
+    private DateInterval toWeekIsoInterval( DateUnit dateUnit, int offset, int length )
+    {
+        DateTime from = dateUnit.toDateTime( chronology ).withDayOfWeek( 1 );
+
+        if ( offset > 0 )
+        {
+            from = from.plusWeeks( offset );
+        }
+        else if ( offset < 0 )
+        {
+            from = from.minusWeeks( -offset );
+        }
+
+        DateTime to = new DateTime( from ).plusWeeks( length ).minusDays( 1 );
+
+        DateUnit fromDateUnit = DateUnit.fromDateTime( from );
+        DateUnit toDateUnit = DateUnit.fromDateTime( to );
+
+        fromDateUnit.setDayOfWeek( isoWeekday( fromDateUnit ) );
+        toDateUnit.setDayOfWeek( isoWeekday( toDateUnit ) );
+
+        return new DateInterval( toIso( fromDateUnit ), toIso( toDateUnit ),
+            DateIntervalType.ISO8601_WEEK );
+    }
+
+    private DateInterval toDayIsoInterval( DateUnit dateUnit, int offset, int length )
+    {
+        DateTime from = dateUnit.toDateTime( chronology );
+
+        if ( offset > 0 )
+        {
+            from = from.plusDays( offset );
+        }
+        else if ( offset < 0 )
+        {
+            from = from.minusDays( -offset );
+        }
+
+        DateTime to = new DateTime( from ).plusDays( length );
+
+        DateUnit fromDateUnit = DateUnit.fromDateTime( from );
+        DateUnit toDateUnit = DateUnit.fromDateTime( to );
+
+        fromDateUnit.setDayOfWeek( isoWeekday( fromDateUnit ) );
+        toDateUnit.setDayOfWeek( isoWeekday( toDateUnit ) );
+
+        return new DateInterval( toIso( fromDateUnit ), toIso( toDateUnit ),
+            DateIntervalType.ISO8601_DAY );
     }
 
     @Override

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateIntervalType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateIntervalType.java	2014-04-27 02:45:02 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateIntervalType.java	2014-05-09 18:53:24 +0000
@@ -37,5 +37,5 @@
       * Gives interval for a year, month or week based on ISO 8601.
       * @see <a href="http://en.wikipedia.org/wiki/ISO_8601";>http://en.wikipedia.org/wiki/ISO_8601</a>
       */
-     ISO8601_YEAR, ISO8601_MONTH, ISO8601_WEEK
+     ISO8601_YEAR, ISO8601_MONTH, ISO8601_WEEK, ISO8601_DAY
 }

=== 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-29 07:28:26 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/impl/NepaliCalendar.java	2014-05-09 18:53:24 +0000
@@ -134,22 +134,24 @@
     }
 
     @Override
-    public DateInterval toInterval( DateUnit dateUnit, DateIntervalType type )
+    public DateInterval toInterval( DateUnit dateUnit, DateIntervalType type, int offset, int length )
     {
         switch ( type )
         {
             case ISO8601_YEAR:
-                return toYearIsoInterval( dateUnit );
+                return toYearIsoInterval( dateUnit, offset, length );
             case ISO8601_MONTH:
-                return toMonthIsoInterval( dateUnit );
+                return toMonthIsoInterval( dateUnit, offset, length );
             case ISO8601_WEEK:
-                return toWeekIsoInterval( dateUnit );
+                return toWeekIsoInterval( dateUnit, offset, length );
+            case ISO8601_DAY:
+                return toDayIsoInterval( dateUnit, offset, length );
         }
 
         return null;
     }
 
-    private DateInterval toYearIsoInterval( DateUnit dateUnit )
+    private DateInterval toYearIsoInterval( DateUnit dateUnit, int offset, int length )
     {
         DateUnit from = new DateUnit( dateUnit.getYear(), 1, 1 );
         DateUnit to = new DateUnit( dateUnit.getYear(), monthsInYear(), daysInMonth( dateUnit.getYear(), monthsInYear() ) );
@@ -160,7 +162,7 @@
         return new DateInterval( from, to, DateIntervalType.ISO8601_YEAR );
     }
 
-    private DateInterval toMonthIsoInterval( DateUnit dateUnit )
+    private DateInterval toMonthIsoInterval( DateUnit dateUnit, int offset, int length )
     {
         DateUnit from = new DateUnit( dateUnit.getYear(), dateUnit.getMonth(), 1 );
         DateUnit to = new DateUnit( dateUnit.getYear(), dateUnit.getMonth(), daysInMonth( dateUnit.getYear(), dateUnit.getMonth() ) );
@@ -171,7 +173,7 @@
         return new DateInterval( from, to, DateIntervalType.ISO8601_MONTH );
     }
 
-    private DateInterval toWeekIsoInterval( DateUnit dateUnit )
+    private DateInterval toWeekIsoInterval( DateUnit dateUnit, int offset, int length )
     {
         DateTime dateTime = toIso( dateUnit ).toDateTime();
 
@@ -181,6 +183,11 @@
         return new DateInterval( DateUnit.fromDateTime( from ), DateUnit.fromDateTime( to ), DateIntervalType.ISO8601_WEEK );
     }
 
+    private DateInterval toDayIsoInterval( DateUnit dateUnit, int offset, int length )
+    {
+        return null;
+    }
+
     @Override
     public int daysInYear( int year )
     {