dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #30069
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15258: Added PeriodType.getCalendar, will return system calendar if calendarService is available, if not...
------------------------------------------------------------
revno: 15258
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-05-13 18:00:50 +0200
message:
Added PeriodType.getCalendar, will return system calendar if calendarService is available, if not, default to iso 8601 impl. Added new method to Calendar, toIntervals, allows for generating multiple intervals.
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/period/MonthlyPeriodType.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.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 18:53:24 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/AbstractCalendar.java 2014-05-13 16:00:50 +0000
@@ -28,11 +28,14 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+import com.google.common.collect.Lists;
import org.joda.time.DateTime;
import org.joda.time.chrono.ISOChronology;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
+import java.util.List;
+
/**
* @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
*/
@@ -162,6 +165,19 @@
}
@Override
+ public List<DateInterval> toIntervals( DateUnit dateUnit, DateIntervalType type, int offset, int length, int periods )
+ {
+ List<DateInterval> dateIntervals = Lists.newArrayList();
+
+ for ( int i = offset; i <= (offset + periods - 1); i++ )
+ {
+ dateIntervals.add( toInterval( dateUnit, type, i, length ) );
+ }
+
+ return dateIntervals;
+ }
+
+ @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-12 07:52:08 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/Calendar.java 2014-05-13 16:00:50 +0000
@@ -28,6 +28,8 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+import java.util.List;
+
/**
* Generic interface for representing a Calendar.
* @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -154,6 +156,18 @@
DateInterval toInterval( DateIntervalType type, int offset, int length );
/**
+ * 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
+ * @param periods How many periods to generate
+ * @return Interval for interval type based on dateUnit
+ * @see DateIntervalType
+ */
+ List<DateInterval> toIntervals( DateUnit dateUnit, DateIntervalType type, int offset, int length, int periods );
+
+ /**
* Gets current date as local DateUnit
* @return Today date as local DateUnit
*/
=== 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 2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java 2014-05-13 16:00:50 +0000
@@ -39,7 +39,6 @@
* PeriodType for monthly Periods. A valid monthly Period has startDate set to
* the first day of a calendar month, and endDate set to the last day of the
* same month.
- *
* @author Torgeir Lorange Ostby
* @version $Id: MonthlyPeriodType.java 2971 2007-03-03 18:54:56Z torgeilo $
*/
@@ -65,7 +64,7 @@
// -------------------------------------------------------------------------
// PeriodType functionality
// -------------------------------------------------------------------------
-
+
@Override
public String getName()
{
@@ -90,7 +89,7 @@
cal.set( Calendar.DAY_OF_MONTH, 1 );
Date startDate = cal.getTime();
cal.set( Calendar.DAY_OF_MONTH, cal.getActualMaximum( Calendar.DAY_OF_MONTH ) );
-
+
return new Period( this, startDate, cal.getTime() );
}
@@ -103,7 +102,7 @@
// -------------------------------------------------------------------------
// CalendarPeriodType functionality
// -------------------------------------------------------------------------
-
+
@Override
public Period getNextPeriod( Period period )
{
@@ -130,7 +129,7 @@
Calendar cal = createCalendarInstance( date );
cal.set( Calendar.DAY_OF_YEAR, 1 );
- int year = cal.get( Calendar.YEAR );
+ int year = cal.get( Calendar.YEAR );
ArrayList<Period> periods = new ArrayList<Period>();
while ( cal.get( Calendar.YEAR ) == year )
@@ -143,7 +142,7 @@
}
/**
- * Generates the last 12 months where the last one is the month which the
+ * Generates the last 12 months where the last one is the month which the
* given date is inside.
*/
@Override
@@ -154,16 +153,16 @@
cal.add( Calendar.MONTH, -11 );
ArrayList<Period> periods = new ArrayList<Period>();
-
+
for ( int i = 0; i < 12; i++ )
{
periods.add( createPeriod( cal ) );
cal.add( Calendar.MONTH, 1 );
}
-
+
return periods;
}
-
+
@Override
public String getIsoDate( Period period )
{
@@ -182,7 +181,7 @@
{
// Ignore and try alternative format
}
-
+
try
{
Date date = new SimpleDateFormat( ALTERNATIVE_ISO_FORMAT ).parse( isoDate );
@@ -199,14 +198,14 @@
{
return ISO_FORMAT;
}
-
+
@Override
public Date getRewindedDate( Date date, Integer rewindedPeriods )
{
- date = date != null ? date : new Date();
+ date = date != null ? date : new Date();
rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1;
- Calendar cal = createCalendarInstance( date );
+ Calendar cal = createCalendarInstance( date );
cal.add( Calendar.MONTH, (rewindedPeriods * -1) );
return cal.getTime();
=== 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 2014-05-12 16:41:54 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java 2014-05-13 16:00:50 +0000
@@ -30,6 +30,7 @@
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import org.hisp.dhis.calendar.CalendarService;
+import org.hisp.dhis.calendar.impl.Iso8601Calendar;
import org.hisp.dhis.common.DxfNamespaces;
import org.springframework.beans.factory.annotation.Autowired;
@@ -65,6 +66,16 @@
this.calendarService = calendarService;
}
+ public org.hisp.dhis.calendar.Calendar getCalendar()
+ {
+ if ( calendarService != null )
+ {
+ return calendarService.getSystemCalendar();
+ }
+
+ return Iso8601Calendar.getInstance();
+ }
+
// -------------------------------------------------------------------------
// Available PeriodTypes
// -------------------------------------------------------------------------