mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
ALF-9156 Calendar monthly repeating
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@29189 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -360,6 +360,248 @@ public class CalendarHelpersTest
|
|||||||
assertEquals("2011-07-25", dateFmt.format(dates.get(0)));
|
assertEquals("2011-07-25", dateFmt.format(dates.get(0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* eg on the 2nd of the month
|
||||||
|
*/
|
||||||
|
@Test public void monthlyRecurrenceByDateInMonth()
|
||||||
|
{
|
||||||
|
List<Date> dates = new ArrayList<Date>();
|
||||||
|
Calendar currentDate = Calendar.getInstance();
|
||||||
|
|
||||||
|
Map<String,String> params = new HashMap<String, String>();
|
||||||
|
params.put("BYMONTHDAY", "2");
|
||||||
|
|
||||||
|
|
||||||
|
// Dates in the past, get nothing
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,19,10,30);
|
||||||
|
RecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params,
|
||||||
|
date(2011,7,10), date(2011,7,15),
|
||||||
|
true, 1
|
||||||
|
);
|
||||||
|
assertEquals(0, dates.size());
|
||||||
|
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,19,10,30);
|
||||||
|
RecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params,
|
||||||
|
date(2011,7,10), date(2011,7,15),
|
||||||
|
false, 1
|
||||||
|
);
|
||||||
|
assertEquals(0, dates.size());
|
||||||
|
|
||||||
|
|
||||||
|
// With this month
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,1,10,30);
|
||||||
|
RecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params,
|
||||||
|
date(2011,7,1), date(2011,7,26),
|
||||||
|
true, 1
|
||||||
|
);
|
||||||
|
assertEquals(1, dates.size());
|
||||||
|
assertEquals("2011-07-02", dateFmt.format(dates.get(0)));
|
||||||
|
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,1,10,30);
|
||||||
|
RecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params,
|
||||||
|
date(2011,7,1), date(2011,7,26),
|
||||||
|
false, 1
|
||||||
|
);
|
||||||
|
assertEquals(1, dates.size());
|
||||||
|
assertEquals("2011-07-02", dateFmt.format(dates.get(0)));
|
||||||
|
|
||||||
|
|
||||||
|
// From the day of the month
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,2,10,30);
|
||||||
|
RecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params,
|
||||||
|
date(2011,7,2), date(2011,7,26),
|
||||||
|
true, 1
|
||||||
|
);
|
||||||
|
assertEquals(1, dates.size());
|
||||||
|
assertEquals("2011-07-02", dateFmt.format(dates.get(0)));
|
||||||
|
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,2,10,30);
|
||||||
|
RecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params,
|
||||||
|
date(2011,7,2), date(2011,7,26),
|
||||||
|
false, 1
|
||||||
|
);
|
||||||
|
assertEquals(1, dates.size());
|
||||||
|
assertEquals("2011-07-02", dateFmt.format(dates.get(0)));
|
||||||
|
|
||||||
|
|
||||||
|
// Dates in the future, goes from then
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,19,10,30);
|
||||||
|
RecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params,
|
||||||
|
date(2011,7,20), date(2011,9,20),
|
||||||
|
true, 1
|
||||||
|
);
|
||||||
|
assertEquals(1, dates.size());
|
||||||
|
assertEquals("2011-08-02", dateFmt.format(dates.get(0)));
|
||||||
|
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,19,10,30);
|
||||||
|
RecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params,
|
||||||
|
date(2011,7,20), date(2011,9,20),
|
||||||
|
false, 1
|
||||||
|
);
|
||||||
|
assertEquals(2, dates.size());
|
||||||
|
assertEquals("2011-08-02", dateFmt.format(dates.get(0)));
|
||||||
|
assertEquals("2011-09-02", dateFmt.format(dates.get(1)));
|
||||||
|
|
||||||
|
|
||||||
|
// With no end date but only first, check it behaves
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,2,10,30);
|
||||||
|
RecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params,
|
||||||
|
date(2011,7,1), null,
|
||||||
|
true, 1
|
||||||
|
);
|
||||||
|
assertEquals(1, dates.size());
|
||||||
|
assertEquals("2011-07-02", dateFmt.format(dates.get(0)));
|
||||||
|
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,19,10,30);
|
||||||
|
RecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params,
|
||||||
|
date(2011,7,19), null,
|
||||||
|
true, 1
|
||||||
|
);
|
||||||
|
assertEquals(1, dates.size());
|
||||||
|
assertEquals("2011-08-02", dateFmt.format(dates.get(0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* on the 1st Tuesday of the month
|
||||||
|
*/
|
||||||
|
@Test public void monthlyRecurrenceByDayOfWeek()
|
||||||
|
{
|
||||||
|
List<Date> dates = new ArrayList<Date>();
|
||||||
|
Calendar currentDate = Calendar.getInstance();
|
||||||
|
|
||||||
|
Map<String,String> params = new HashMap<String, String>();
|
||||||
|
params.put("BYSETPOS", "TU");
|
||||||
|
|
||||||
|
|
||||||
|
// Dates in the past, get nothing
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,19,10,30);
|
||||||
|
RecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params,
|
||||||
|
date(2011,7,10), date(2011,7,15),
|
||||||
|
true, 1
|
||||||
|
);
|
||||||
|
assertEquals(0, dates.size());
|
||||||
|
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,19,10,30);
|
||||||
|
RecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params,
|
||||||
|
date(2011,7,10), date(2011,7,15),
|
||||||
|
false, 1
|
||||||
|
);
|
||||||
|
assertEquals(0, dates.size());
|
||||||
|
|
||||||
|
|
||||||
|
// With this month
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,1,10,30);
|
||||||
|
RecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params,
|
||||||
|
date(2011,7,1), date(2011,7,26),
|
||||||
|
true, 1
|
||||||
|
);
|
||||||
|
assertEquals(1, dates.size());
|
||||||
|
assertEquals("2011-07-05", dateFmt.format(dates.get(0))); // Tuesday 5th
|
||||||
|
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,1,10,30);
|
||||||
|
RecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params,
|
||||||
|
date(2011,7,1), date(2011,7,26),
|
||||||
|
false, 1
|
||||||
|
);
|
||||||
|
assertEquals(1, dates.size());
|
||||||
|
assertEquals("2011-07-05", dateFmt.format(dates.get(0))); // Tuesday 5th
|
||||||
|
|
||||||
|
|
||||||
|
// From the day of the month
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,2,10,30);
|
||||||
|
RecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params,
|
||||||
|
date(2011,7,2), date(2011,7,26),
|
||||||
|
true, 1
|
||||||
|
);
|
||||||
|
assertEquals(1, dates.size());
|
||||||
|
assertEquals("2011-07-05", dateFmt.format(dates.get(0))); // Tuesday 5th
|
||||||
|
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,2,10,30);
|
||||||
|
RecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params,
|
||||||
|
date(2011,7,2), date(2011,7,26),
|
||||||
|
false, 1
|
||||||
|
);
|
||||||
|
assertEquals(1, dates.size());
|
||||||
|
assertEquals("2011-07-05", dateFmt.format(dates.get(0))); // Tuesday 5th
|
||||||
|
|
||||||
|
|
||||||
|
// Dates in the future, goes from then
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,19,10,30);
|
||||||
|
RecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params,
|
||||||
|
date(2011,7,20), date(2011,9,20),
|
||||||
|
true, 1
|
||||||
|
);
|
||||||
|
assertEquals(1, dates.size());
|
||||||
|
assertEquals("2011-08-02", dateFmt.format(dates.get(0))); // Tuesday 2nd
|
||||||
|
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,19,10,30);
|
||||||
|
RecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params,
|
||||||
|
date(2011,7,20), date(2011,9,20),
|
||||||
|
false, 1
|
||||||
|
);
|
||||||
|
assertEquals(2, dates.size());
|
||||||
|
assertEquals("2011-08-02", dateFmt.format(dates.get(0))); // Tuesday 2nd
|
||||||
|
assertEquals("2011-09-06", dateFmt.format(dates.get(1))); // Tuesday 6th
|
||||||
|
|
||||||
|
|
||||||
|
// With no end date but only first, check it behaves
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,2,10,30);
|
||||||
|
RecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params,
|
||||||
|
date(2011,7,1), null,
|
||||||
|
true, 1
|
||||||
|
);
|
||||||
|
assertEquals(1, dates.size());
|
||||||
|
assertEquals("2011-07-05", dateFmt.format(dates.get(0)));
|
||||||
|
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,19,10,30);
|
||||||
|
RecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params,
|
||||||
|
date(2011,7,19), null,
|
||||||
|
true, 1
|
||||||
|
);
|
||||||
|
assertEquals(1, dates.size());
|
||||||
|
assertEquals("2011-08-02", dateFmt.format(dates.get(0)));
|
||||||
|
}
|
||||||
|
|
||||||
private static class RecurrenceHelper extends CalendarRecurrenceHelper
|
private static class RecurrenceHelper extends CalendarRecurrenceHelper
|
||||||
{
|
{
|
||||||
protected static void buildDailyRecurrences(Calendar currentDate, List<Date> dates,
|
protected static void buildDailyRecurrences(Calendar currentDate, List<Date> dates,
|
||||||
@@ -375,6 +617,13 @@ public class CalendarHelpersTest
|
|||||||
CalendarRecurrenceHelper.buildWeeklyRecurrences(
|
CalendarRecurrenceHelper.buildWeeklyRecurrences(
|
||||||
currentDate, dates, params, onOrAfter, until, firstOnly, interval);
|
currentDate, dates, params, onOrAfter, until, firstOnly, interval);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static void buildMonthlyRecurrences(Calendar currentDate, List<Date> dates,
|
||||||
|
Map<String,String> params, Date onOrAfter, Date until, boolean firstOnly, int interval)
|
||||||
|
{
|
||||||
|
CalendarRecurrenceHelper.buildMonthlyRecurrences(
|
||||||
|
currentDate, dates, params, onOrAfter, until, firstOnly, interval);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Date date(int year, int month, int day)
|
private static Date date(int year, int month, int day)
|
||||||
|
@@ -333,14 +333,19 @@ public class CalendarRecurrenceHelper
|
|||||||
{
|
{
|
||||||
// eg the 15th of each month
|
// eg the 15th of each month
|
||||||
int dayOfMonth = Integer.parseInt(params.get("BYMONTHDAY"));
|
int dayOfMonth = Integer.parseInt(params.get("BYMONTHDAY"));
|
||||||
if(currentDate.get(Calendar.DAY_OF_MONTH) < dayOfMonth)
|
if(currentDate.get(Calendar.DAY_OF_MONTH) > dayOfMonth)
|
||||||
{
|
{
|
||||||
// Move forward to start
|
// Move forward to start of the next month
|
||||||
addMonthToDayOfMonth(currentDate, dayOfMonth);
|
addMonthToDayOfMonth(currentDate, dayOfMonth);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Move to that date in this month
|
||||||
|
currentDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
|
||||||
|
}
|
||||||
|
|
||||||
// Go until in the ok range
|
// Go until in the ok range
|
||||||
while(currentDate.before(onOrAfter))
|
while(currentDate.getTime().before(onOrAfter))
|
||||||
{
|
{
|
||||||
addMonthToDayOfMonth(currentDate, dayOfMonth);
|
addMonthToDayOfMonth(currentDate, dayOfMonth);
|
||||||
}
|
}
|
||||||
@@ -348,7 +353,7 @@ public class CalendarRecurrenceHelper
|
|||||||
{
|
{
|
||||||
if(until != null)
|
if(until != null)
|
||||||
{
|
{
|
||||||
if(currentDate.after(until))
|
if(currentDate.getTime().after(until))
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -377,13 +382,13 @@ public class CalendarRecurrenceHelper
|
|||||||
// Move forward to start
|
// Move forward to start
|
||||||
Date t = currentDate.getTime();
|
Date t = currentDate.getTime();
|
||||||
currentDate.set(Calendar.DAY_OF_WEEK, dayOfWeek);
|
currentDate.set(Calendar.DAY_OF_WEEK, dayOfWeek);
|
||||||
if(currentDate.before(t))
|
if(currentDate.getTime().before(t))
|
||||||
{
|
{
|
||||||
currentDate.add(Calendar.DATE, 7);
|
currentDate.add(Calendar.DATE, 7);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while(currentDate.before(onOrAfter))
|
while(currentDate.getTime().before(onOrAfter))
|
||||||
{
|
{
|
||||||
addMonthToFirstDayOfWeek(currentDate, dayOfWeek);
|
addMonthToFirstDayOfWeek(currentDate, dayOfWeek);
|
||||||
}
|
}
|
||||||
@@ -391,7 +396,7 @@ public class CalendarRecurrenceHelper
|
|||||||
{
|
{
|
||||||
if(until != null)
|
if(until != null)
|
||||||
{
|
{
|
||||||
if(currentDate.after(until))
|
if(currentDate.getTime().after(until))
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -469,8 +474,8 @@ public class CalendarRecurrenceHelper
|
|||||||
{
|
{
|
||||||
// Set it to the 1st
|
// Set it to the 1st
|
||||||
c.set(Calendar.DAY_OF_MONTH, 1);
|
c.set(Calendar.DAY_OF_MONTH, 1);
|
||||||
// Add 32 days, will be on the 1st-5th
|
// Add 33 days, will be on the 2nd-6th
|
||||||
c.add(Calendar.DATE, 32);
|
c.add(Calendar.DATE, 33);
|
||||||
// Set to the requred day in the month
|
// Set to the requred day in the month
|
||||||
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
|
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
|
||||||
}
|
}
|
||||||
@@ -483,7 +488,7 @@ public class CalendarRecurrenceHelper
|
|||||||
Date t = c.getTime();
|
Date t = c.getTime();
|
||||||
c.set(Calendar.DAY_OF_WEEK, dayOfWeek);
|
c.set(Calendar.DAY_OF_WEEK, dayOfWeek);
|
||||||
// If we went back, go forward a week
|
// If we went back, go forward a week
|
||||||
if(c.before(t))
|
if(c.getTime().before(t))
|
||||||
{
|
{
|
||||||
c.add(Calendar.DATE, 7);
|
c.add(Calendar.DATE, 7);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user