=== 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-04-29 07:28:26 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/AbstractCalendar.java 2014-05-12 09:56:39 +0000
@@ -144,6 +144,24 @@
}
@Override
+ public DateInterval toInterval( DateIntervalType type )
+ {
+ return toInterval( today(), type );
+ }
+
+ @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-04-29 07:28:26 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/Calendar.java 2014-05-12 09:56:39 +0000
@@ -118,12 +118,42 @@
/**
* 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
*/
DateInterval toInterval( DateUnit dateUnit, DateIntervalType type );
/**
+ * Gets interval of type based on today's 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
*/
@@ -221,4 +251,68 @@
* @see http://en.wikipedia.org/wiki/Internationalization_and_localization
*/
String shortNameOfDay( int day );
+
+ /**
+ * Returns a new dateUnit with specified number of days added
+ * @param dateUnit DateUnit representing local year, month, day
+ * @param days Days to add
+ * @return dateUnit + days
+ */
+ DateUnit plusDays( DateUnit dateUnit, int days );
+
+ /**
+ * Returns a new dateUnit with specified number of days subtracted
+ * @param dateUnit DateUnit representing local year, month, day
+ * @param days Days to subtract
+ * @return dateUnit - days
+ */
+ DateUnit minusDays( DateUnit dateUnit, int days );
+
+ /**
+ * Returns a new dateUnit with specified number of weeks added
+ * @param dateUnit DateUnit representing local year, month, day
+ * @param weeks Weeks to add
+ * @return dateUnit + weeks
+ */
+ DateUnit plusWeeks( DateUnit dateUnit, int weeks );
+
+ /**
+ * Returns a new dateUnit with specified number of weeks subtracted
+ * @param dateUnit DateUnit representing local year, month, day
+ * @param weeks Weeks to subtract
+ * @return dateUnit - weeks
+ */
+ DateUnit minusWeeks( DateUnit dateUnit, int weeks );
+
+ /**
+ * Returns a new dateUnit with specified number of months added
+ * @param dateUnit DateUnit representing local year, month, day
+ * @param months Months to add
+ * @return dateUnit + months
+ */
+ DateUnit plusMonths( DateUnit dateUnit, int months );
+
+ /**
+ * Returns a new dateUnit with specified number of months subtracted
+ * @param dateUnit DateUnit representing local year, month, day
+ * @param months Months to subtract
+ * @return dateUnit - months
+ */
+ DateUnit minusMonths( DateUnit dateUnit, int months );
+
+ /**
+ * Returns a new dateUnit with specified number of years added
+ * @param dateUnit DateUnit representing local year, month, day
+ * @param years Years to add
+ * @return dateUnit + years
+ */
+ DateUnit plusYears( DateUnit dateUnit, int years );
+
+ /**
+ * Returns a new dateUnit with specified number of years subtracted
+ * @param dateUnit DateUnit representing local year, month, day
+ * @param years Years to subtract
+ * @return dateUnit - years
+ */
+ DateUnit minusYears( DateUnit dateUnit, int years );
}
=== 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-27 02:45:02 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/ChronologyBasedCalendar.java 2014-05-12 09:56:39 +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( from, 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( from, 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 );
+
+ 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 );
+
+ 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 );
+
+ 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
@@ -164,4 +234,60 @@
DateTime dateTime = dateUnit.toDateTime( chronology );
return dateTime.getDayOfWeek();
}
+
+ @Override
+ public DateUnit plusDays( DateUnit dateUnit, int days )
+ {
+ DateTime dateTime = dateUnit.toDateTime( chronology );
+ return DateUnit.fromDateTime( dateTime.plusDays( days ) );
+ }
+
+ @Override
+ public DateUnit minusDays( DateUnit dateUnit, int days )
+ {
+ DateTime dateTime = dateUnit.toDateTime( chronology );
+ return DateUnit.fromDateTime( dateTime.minusDays( days ) );
+ }
+
+ @Override
+ public DateUnit plusWeeks( DateUnit dateUnit, int weeks )
+ {
+ DateTime dateTime = dateUnit.toDateTime( chronology );
+ return DateUnit.fromDateTime( dateTime.plusWeeks( weeks ) );
+ }
+
+ @Override
+ public DateUnit minusWeeks( DateUnit dateUnit, int weeks )
+ {
+ DateTime dateTime = dateUnit.toDateTime( chronology );
+ return DateUnit.fromDateTime( dateTime.minusWeeks( weeks ) );
+ }
+
+ @Override
+ public DateUnit plusMonths( DateUnit dateUnit, int months )
+ {
+ DateTime dateTime = dateUnit.toDateTime( chronology );
+ return DateUnit.fromDateTime( dateTime.plusMonths( months ) );
+ }
+
+ @Override
+ public DateUnit minusMonths( DateUnit dateUnit, int months )
+ {
+ DateTime dateTime = dateUnit.toDateTime( chronology );
+ return DateUnit.fromDateTime( dateTime.minusMonths( months ) );
+ }
+
+ @Override
+ public DateUnit plusYears( DateUnit dateUnit, int years )
+ {
+ DateTime dateTime = dateUnit.toDateTime( chronology );
+ return DateUnit.fromDateTime( dateTime.plusYears( years ) );
+ }
+
+ @Override
+ public DateUnit minusYears( DateUnit dateUnit, int years )
+ {
+ DateTime dateTime = dateUnit.toDateTime( chronology );
+ return DateUnit.fromDateTime( dateTime.minusYears( years ) );
+ }
}
=== 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-12 09:56:39 +0000
@@ -37,5 +37,5 @@
* Gives interval for a year, month or week based on ISO 8601.
* @see http://en.wikipedia.org/wiki/ISO_8601
*/
- 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/DateUnit.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateUnit.java 2014-05-05 08:08:38 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateUnit.java 2014-05-12 09:56:39 +0000
@@ -70,6 +70,14 @@
{
}
+ public DateUnit( DateUnit dateUnit )
+ {
+ this.year = dateUnit.getYear();
+ this.month = dateUnit.getMonth();
+ this.day = dateUnit.getDay();
+ this.dayOfWeek = dateUnit.getDayOfWeek();
+ }
+
public DateUnit( int year, int month, int day )
{
this.year = year;
=== 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-12 09:56:39 +0000
@@ -94,65 +94,43 @@
DateTime start = startIso.toDateTime();
DateTime end = dateUnit.toDateTime();
- int days = Days.daysBetween( start, end ).getDays();
-
- int curYear = startNepal.getYear();
- int curMonth = startNepal.getMonth();
- int curDay = startNepal.getDay();
- int dayOfWeek = startNepal.getDayOfWeek();
-
- while ( days != 0 )
- {
- // days in month
- int dm = conversionMap.get( curYear )[curMonth];
-
- curDay++;
-
- if ( curDay > dm )
- {
- curMonth++;
- curDay = 1;
- }
-
- if ( curMonth > 12 )
- {
- curYear++;
- curMonth = 1;
- }
-
- dayOfWeek++;
-
- if ( dayOfWeek > 7 )
- {
- dayOfWeek = 1;
- }
-
- days--;
- }
-
- return new DateUnit( curYear, curMonth, curDay, dayOfWeek );
+ return plusDays( startNepal, Days.daysBetween( start, end ).getDays() );
}
@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() ) );
+ DateUnit from = new DateUnit( dateUnit );
+
+ if ( offset > 0 )
+ {
+ from = plusYears( from, offset );
+ }
+ else if ( offset < 0 )
+ {
+ from = minusYears( from, -offset );
+ }
+
+ DateUnit to = new DateUnit( from );
+ to = plusYears( to, length );
+ to = minusDays( to, length );
from = toIso( from );
to = toIso( to );
@@ -160,10 +138,22 @@
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() ) );
+ DateUnit from = new DateUnit( dateUnit );
+
+ if ( offset > 0 )
+ {
+ from = plusMonths( from, offset );
+ }
+ else if ( offset < 0 )
+ {
+ from = minusMonths( from, -offset );
+ }
+
+ DateUnit to = new DateUnit( from );
+ to = plusMonths( to, length );
+ to = minusDays( to, 1 );
from = toIso( from );
to = toIso( to );
@@ -171,14 +161,49 @@
return new DateInterval( from, to, DateIntervalType.ISO8601_MONTH );
}
- 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 ), DateIntervalType.ISO8601_WEEK );
+ private DateInterval toWeekIsoInterval( DateUnit dateUnit, int offset, int length )
+ {
+ DateUnit from = new DateUnit( dateUnit );
+
+ if ( offset > 0 )
+ {
+ from = plusWeeks( from, offset );
+ }
+ else if ( offset < 0 )
+ {
+ from = minusWeeks( from, -offset );
+ }
+
+ DateUnit to = new DateUnit( from );
+ to = plusWeeks( to, length );
+ to = minusDays( to, 1 );
+
+ from = toIso( from );
+ to = toIso( to );
+
+ return new DateInterval( from, to, DateIntervalType.ISO8601_WEEK );
+ }
+
+ private DateInterval toDayIsoInterval( DateUnit dateUnit, int offset, int length )
+ {
+ DateUnit from = new DateUnit( dateUnit );
+
+ if ( offset > 0 )
+ {
+ from = plusDays( from, offset );
+ }
+ else if ( offset < 0 )
+ {
+ from = minusDays( from, -offset );
+ }
+
+ DateUnit to = new DateUnit( from );
+ to = plusDays( to, length );
+
+ from = toIso( from );
+ to = toIso( to );
+
+ return new DateInterval( from, to, DateIntervalType.ISO8601_DAY );
}
@Override
@@ -284,6 +309,174 @@
return conversionMap.get( year )[0];
}
+ @Override
+ public DateUnit minusYears( DateUnit dateUnit, int years )
+ {
+ DateUnit result = new DateUnit( dateUnit.getYear() - years, dateUnit.getMonth(), dateUnit.getDay(), dateUnit.getDayOfWeek() );
+ updateDateUnit( result );
+
+ return result;
+ }
+
+ @Override
+ public DateUnit minusMonths( DateUnit dateUnit, int months )
+ {
+ DateUnit result = new DateUnit( dateUnit );
+
+ while ( months != 0 )
+ {
+ result.setMonth( result.getMonth() - 1 );
+
+ if ( result.getMonth() < 1 )
+ {
+ result.setMonth( monthsInYear() );
+ result.setYear( result.getYear() - 1 );
+ }
+
+ months--;
+ }
+
+ updateDateUnit( result );
+
+ return result;
+ }
+
+ @Override
+ public DateUnit minusWeeks( DateUnit dateUnit, int weeks )
+ {
+ return minusDays( dateUnit, weeks * daysInWeek() );
+ }
+
+ @Override
+ public DateUnit minusDays( DateUnit dateUnit, int days )
+ {
+ int curYear = dateUnit.getYear();
+ int curMonth = dateUnit.getMonth();
+ int curDay = dateUnit.getDay();
+ int dayOfWeek = dateUnit.getDayOfWeek();
+
+ while ( days != 0 )
+ {
+ curDay--;
+
+ if ( curDay == 0 )
+ {
+ curMonth--;
+
+ if ( curMonth == 0 )
+ {
+ curYear--;
+ curMonth = 12;
+ }
+
+ curDay = conversionMap.get( curYear )[curMonth];
+ }
+
+ dayOfWeek--;
+
+ if ( dayOfWeek == 0 )
+ {
+ dayOfWeek = 7;
+ }
+
+ days--;
+ }
+
+ return new DateUnit( curYear, curMonth, curDay, dayOfWeek );
+ }
+
+ @Override
+ public DateUnit plusYears( DateUnit dateUnit, int years )
+ {
+ DateUnit result = new DateUnit( dateUnit.getYear() + years, dateUnit.getMonth(), dateUnit.getDay(), dateUnit.getDayOfWeek() );
+ updateDateUnit( result );
+
+ return result;
+ }
+
+ @Override
+ public DateUnit plusMonths( DateUnit dateUnit, int months )
+ {
+ DateUnit result = new DateUnit( dateUnit );
+
+ while ( months != 0 )
+ {
+ result.setMonth( result.getMonth() + 1 );
+
+ if ( result.getMonth() > monthsInYear() )
+ {
+ result.setMonth( 1 );
+ result.setYear( result.getYear() + 1 );
+ }
+
+ months--;
+ }
+
+ updateDateUnit( result );
+
+ return result;
+ }
+
+ @Override
+ public DateUnit plusWeeks( DateUnit dateUnit, int weeks )
+ {
+ return plusDays( dateUnit, weeks * daysInWeek() );
+ }
+
+ @Override
+ public DateUnit plusDays( DateUnit dateUnit, int days )
+ {
+ int curYear = dateUnit.getYear();
+ int curMonth = dateUnit.getMonth();
+ int curDay = dateUnit.getDay();
+ int dayOfWeek = dateUnit.getDayOfWeek();
+
+ while ( days != 0 )
+ {
+ // days in month
+ int dm = conversionMap.get( curYear )[curMonth];
+
+ curDay++;
+
+ if ( curDay > dm )
+ {
+ curMonth++;
+ curDay = 1;
+ }
+
+ if ( curMonth > 12 )
+ {
+ curYear++;
+ curMonth = 1;
+ }
+
+ dayOfWeek++;
+
+ if ( dayOfWeek > 7 )
+ {
+ dayOfWeek = 1;
+ }
+
+ days--;
+ }
+
+ return new DateUnit( curYear, curMonth, curDay, dayOfWeek );
+ }
+
+ // check if day is more than current maximum for month, don't overflow, just set to maximum
+ // set day of week
+ private void updateDateUnit( DateUnit result )
+ {
+ int dm = conversionMap.get( result.getYear() )[result.getMonth()];
+
+ if ( result.getDay() > dm )
+ {
+ result.setDay( dm );
+ }
+
+ result.setDayOfWeek( weekday( result ) );
+ }
+
//------------------------------------------------------------------------------------------------------------
// Conversion map for Nepali calendar
//
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryFilter.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryFilter.java 2014-04-21 11:05:22 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryFilter.java 2014-05-12 09:56:39 +0000
@@ -1,8 +1,5 @@
package org.hisp.dhis.common;
-import java.util.HashMap;
-import java.util.Map;
-
/*
* Copyright (c) 2004-2014, University of Oslo
* All rights reserved.
@@ -31,6 +28,9 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+import java.util.HashMap;
+import java.util.Map;
+
/**
* @author Lars Helge Overland
*/
@@ -38,18 +38,18 @@
{
public static final String OPTION_SEP = ";";
- public static final Map OPERATOR_MAP = new HashMap() { {
- put( "eq", "=" );
- put( "gt", ">" );
- put( "ge", ">=" );
- put( "lt", "<" );
- put( "le", "<=" );
- put( "ne", "!=" );
- put( "like", "like" );
- put( "in", "in" );
+ public static final Map OPERATOR_MAP = new HashMap() { {
+ put( QueryOperator.EQ, "=" );
+ put( QueryOperator.GT, ">" );
+ put( QueryOperator.GE, ">=" );
+ put( QueryOperator.LT, "<" );
+ put( QueryOperator.LE, "<=" );
+ put( QueryOperator.NE, "!=" );
+ put( QueryOperator.LIKE, "like" );
+ put( QueryOperator.IN, "in" );
} };
- protected String operator;
+ protected QueryOperator operator;
protected String filter;
@@ -61,7 +61,7 @@
{
}
- public QueryFilter( String operator, String filter )
+ public QueryFilter( QueryOperator operator, String filter )
{
this.operator = operator;
this.filter = filter;
@@ -73,7 +73,7 @@
public boolean isFilter()
{
- return operator != null && !operator.isEmpty() && filter != null && !filter.isEmpty();
+ return operator != null && filter != null && !filter.isEmpty();
}
public String getSqlOperator()
@@ -83,7 +83,7 @@
return null;
}
- return OPERATOR_MAP.get( operator.toLowerCase() );
+ return OPERATOR_MAP.get( operator );
}
public String getSqlFilter( String encodedFilter )
@@ -92,12 +92,12 @@
{
return null;
}
-
- if ( operator.equalsIgnoreCase( "like" ) )
+
+ if ( QueryOperator.LIKE.equals( operator ) )
{
return "'%" + encodedFilter + "%'";
}
- else if ( operator.equalsIgnoreCase( "in" ) )
+ else if ( QueryOperator.IN.equals( operator ) )
{
String[] split = encodedFilter.split( OPTION_SEP );
@@ -185,12 +185,12 @@
// Getters and setters
// -------------------------------------------------------------------------
- public String getOperator()
+ public QueryOperator getOperator()
{
return operator;
}
- public void setOperator( String operator )
+ public void setOperator( QueryOperator operator )
{
this.operator = operator;
}
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryItem.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryItem.java 2014-04-21 11:05:22 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryItem.java 2014-05-12 09:56:39 +0000
@@ -61,7 +61,7 @@
this.numeric = numeric;
}
- public QueryItem( IdentifiableObject item, String operator, String filter, boolean numeric )
+ public QueryItem( IdentifiableObject item, QueryOperator operator, String filter, boolean numeric )
{
this.item = item;
this.numeric = numeric;
=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryOperator.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryOperator.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryOperator.java 2014-05-12 09:56:39 +0000
@@ -0,0 +1,16 @@
+package org.hisp.dhis.common;
+
+public enum QueryOperator
+{
+ EQ, GT, GE, LT, LE, NE, LIKE, IN;
+
+ public static final QueryOperator fromString( String string )
+ {
+ if ( string == null || string.isEmpty() )
+ {
+ return null;
+ }
+
+ return valueOf( string.toUpperCase() );
+ }
+}
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStage.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStage.java 2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStage.java 2014-05-12 09:56:39 +0000
@@ -180,6 +180,9 @@
this.reminders = reminders;
}
+ @JsonProperty
+ @JsonView( { DetailedView.class, ExportView.class } )
+ @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
public DataEntryForm getDataEntryForm()
{
return dataEntryForm;
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceService.java 2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceService.java 2014-05-12 09:56:39 +0000
@@ -146,25 +146,6 @@
List getProgramStageInstances( TrackedEntityInstance entityInstance, Boolean completed );
/**
- * Get an event report of program instance
- *
- * @param programInstance ProgramInstance
- * @param format I18nFormat object
- * @param i18n I18n object
- *
- * @return List of grids. Each grid is included all information of a event
- */
- List getProgramStageInstancesReport( ProgramInstance programInstance, I18nFormat format, I18n i18n );
-
- /**
- * Remove events without any data values
- *
- * @param programStage Empty events belong to this program stage are removed
- * @param organisationUnit Specify an orgunit where empty events belong to
- */
- void removeEmptyEvents( ProgramStage programStage, OrganisationUnit organisationUnit );
-
- /**
* Create relationship between an OutboundSms with many events.
*
* @param programStageInstances Event list
@@ -337,11 +318,4 @@
*/
ProgramStageInstance createProgramStageInstance( TrackedEntityInstance entityInstance, Program program, Date executionDate,
OrganisationUnit organisationUnit );
-
- Grid searchEvents( ProgramStage programStage, List columns,
- Collection organisationUnits, Date startDate, Date endDate, Boolean completed, Integer min,
- Integer max, I18n i18n );
-
- int searchEventsCount( ProgramStage programStage, List columns,
- Collection organisationUnits, Boolean completed, Date startDate, Date endDate );
}
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceStore.java 2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceStore.java 2014-05-12 09:56:39 +0000
@@ -35,7 +35,6 @@
import org.hisp.dhis.common.GenericIdentifiableObjectStore;
import org.hisp.dhis.common.Grid;
import org.hisp.dhis.i18n.I18n;
-import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.sms.outbound.OutboundSms;
import org.hisp.dhis.trackedentity.TrackedEntityInstance;
@@ -102,14 +101,6 @@
List get( TrackedEntityInstance entityInstance, Boolean completed );
/**
- * Remove events without any data values
- *
- * @param programStage Empty events belong to this program stage are removed
- * @param organisationUnit Specify an orgunit where empty events belong to
- */
- void removeEmptyEvents( ProgramStage programStage, OrganisationUnit organisationUnit );
-
- /**
* Create relationship between an OutboundSms with many events.
*
* @param programStageInstances Event list
@@ -245,41 +236,4 @@
* @return Grid
*/
Grid getCompleteness( Collection orgunitIds, Program program, String startDate, String endDate, I18n i18n );
-
- /**
- * Search {@link ProgramStageInstance} by criteria with result limited
- *
- * @param programStage {@link ProgramStage} needs for searching events
- * @param orgUnits List of {@link OrganisationUnit} ids
- * @param columns The criteria for searching
- * @param startDate Report date the instance should be on or after.
- * @param endDate Report date the instance should be on or before.
- * @param completed The status of events. There are three status values,
- * NULL for retrieving all events, false for retrieving events
- * uncompleted and true for retrieving events completed.
- * @param min
- * @param max
- * @param i18n
- *
- * @return A grid
- */
- Grid searchEvent( ProgramStage programStage, Collection orgUnits, List columns,
- Date startDate, Date endDate, Boolean completed, Integer min, Integer max, I18n i18n );
-
- /**
- * Search {@link ProgramStageInstance} by criteria
- *
- * @param programStage {@link ProgramStage} needs for searching events
- * @param orgUnits List of {@link OrganisationUnit} ids
- * @param columns The criteria for searching
- * @param startDate Report date the instance should be on or after.
- * @param endDate Report date the instance should be on or before.
- * @param completed The status of events. There are three status values,
- * NULL for retrieving all events, false for retrieving events
- * uncompleted and true for retrieving events completed.
- *
- * @return The number of events
- */
- int searchEventsCount( ProgramStage programStage, List columns,
- Collection organisationUnits, Date startDate, Date endDate, Boolean completed );
}
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java 2014-04-21 11:05:22 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java 2014-05-12 09:56:39 +0000
@@ -159,8 +159,30 @@
// -------------------------------------------------------------------------
/**
- * //TODO allow attributes only once and allow multiple filters per item
- *
+ * Adds a query item as attribute to the parameters.
+ */
+ public void addAttribute( QueryItem attribute )
+ {
+ this.attributes.add( attribute );
+ }
+
+ /**
+ * Adds a query item as filter to the parameters.
+ */
+ public void addFilter( QueryItem filter )
+ {
+ this.filters.add( filter );
+ }
+
+ /**
+ * Adds an organisation unit to the parameters.
+ */
+ public void addOrganisationUnit( OrganisationUnit unit )
+ {
+ this.organisationUnits.add( unit );
+ }
+
+ /**
* Performs a set of operations on this params.
*
*
@@ -210,8 +232,35 @@
return setMap;
}
+
+ /**
+ * Add the given attributes to this params if they are not already present.
+ */
+ public void addAttributesIfNotExist( List attrs )
+ {
+ for ( QueryItem attr : attrs )
+ {
+ if ( attributes != null && !attributes.contains( attr ) )
+ {
+ attributes.add( attr );
+ }
+ }
+ }
/**
+ * Adds the given filters to this params if they are not already present.
+ */
+ public void addFiltersIfNotExist( List filtrs )
+ {
+ for ( QueryItem filter : filtrs )
+ {
+ if ( filters != null && !filters.contains( filter ) )
+ {
+ filters.add( filter );
+ }
+ }
+ }
+ /**
* Indicates whether this is a logical OR query, meaning that a query string
* is specified and instances which matches this query on one or more attributes
* should be included in the response. The opposite is an item-specific query,
@@ -278,35 +327,7 @@
return duplicates;
}
-
- /**
- * Add the given attributes to this params if they are not already present.
- */
- public void addAttributesIfNotExist( List attrs )
- {
- for ( QueryItem attr : attrs )
- {
- if ( attributes != null && !attributes.contains( attr ) )
- {
- attributes.add( attr );
- }
- }
- }
-
- /**
- * Adds the given filters to this params if they are not already present.
- */
- public void addFiltersIfNotExist( List filtrs )
- {
- for ( QueryItem filter : filtrs )
- {
- if ( filters != null && !filters.contains( filter ) )
- {
- filters.add( filter );
- }
- }
- }
-
+
/**
* Indicates whether this params specifies any attributes and/or filters.
*/
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java 2014-05-04 14:04:41 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java 2014-05-12 09:56:39 +0000
@@ -45,6 +45,49 @@
import org.hisp.dhis.validation.ValidationCriteria;
/**
+ *
This interface is responsible for retrieving tracked entity instances (TEI).
+ * The query methods accepts a TrackedEntityInstanceQueryParams object which
+ * encapsulates all arguments.
+ *
+ *
The TEIs are returned as a Grid object, which is a two-dimensional list with
+ * headers. The TEI attribute values are returned in the same order as specified
+ * in the arguments. The grid has a set of columns which are always present
+ * starting at index 0, followed by attributes specified for the query. All
+ * values in the grid are of type String. The order is:
+ *
+ *
+ *
0: Tracked entity instance UID
+ *
1: Created time stamp
+ *
2: Last updated time stamp
+ *
3: Organisation unit UID
+ *
4: Tracked entity UID
+ *
+ *
+ *
Attributes specified in the query follows on the next column indexes.
+ * Example usage for retrieving TEIs with two attributes using one attribute as
+ * filter: