📄 recurrencerule.java
字号:
cal.setTime(new Date(startTime));
long nextStartTime = startTime;
while (nextStartTime < fromTime) {
// if (Debug.verboseOn()) Debug.logVerbose("[RecurrenceInfo.getNextFreq] : Updating time - " + getFrequency(), module);
switch (getFrequency()) {
case SECONDLY:
cal.add(Calendar.SECOND, getIntervalInt());
break;
case MINUTELY:
cal.add(Calendar.MINUTE, getIntervalInt());
break;
case HOURLY:
cal.add(Calendar.HOUR, getIntervalInt());
break;
case DAILY:
cal.add(Calendar.DAY_OF_MONTH, getIntervalInt());
break;
case WEEKLY:
cal.add(Calendar.WEEK_OF_YEAR, getIntervalInt());
break;
case MONTHLY:
cal.add(Calendar.MONTH, getIntervalInt());
break;
case YEARLY:
cal.add(Calendar.YEAR, getIntervalInt());
break;
default:
return null; // should never happen
}
nextStartTime = cal.getTime().getTime();
}
return new Date(nextStartTime);
}
// Checks to see if a date is valid by the byXXX rules
private boolean validByRule(Date date) {
// Build a Calendar object
Calendar cal = Calendar.getInstance();
cal.setTime(date);
// Test each byXXX rule.
if (bySecondList != null && bySecondList.size() > 0) {
if (!bySecondList.contains(new Integer(cal.get(Calendar.SECOND))))
return false;
}
if (byMinuteList != null && byMinuteList.size() > 0) {
if (!byMinuteList.contains(new Integer(cal.get(Calendar.MINUTE))))
return false;
}
if (byHourList != null && byHourList.size() > 0) {
if (!byHourList.contains(new Integer(cal.get(Calendar.HOUR))))
return false;
}
if (byDayList != null && byDayList.size() > 0) {
Iterator iter = byDayList.iterator();
boolean foundDay = false;
while (iter.hasNext() && !foundDay) {
String dayRule = (String) iter.next();
String dayString = getDailyString(dayRule);
if (Calendar.DAY_OF_WEEK == getCalendarDay(dayString)) {
if ((hasNumber(dayRule)) && (getFrequency() == MONTHLY || getFrequency() == YEARLY)) {
int modifier = getDailyNumber(dayRule);
if (modifier == 0)
foundDay = true;
if (getFrequency() == MONTHLY) {
// figure if we are the nth xDAY if this month
int currentPos = cal.get(Calendar.WEEK_OF_MONTH);
int dayPosCalc = cal.get(Calendar.DAY_OF_MONTH) - ((currentPos - 1) * 7);
if (dayPosCalc < 1)
currentPos--;
if (modifier > 0) {
if (currentPos == modifier) {
foundDay = true;
}
} else if (modifier < 0) {
int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
int firstDay = dayPosCalc > 0 ? dayPosCalc : dayPosCalc + 7;
int totalDay = ((maxDay - firstDay) / 7) + 1;
int thisDiff = (currentPos - totalDay) - 1;
if (thisDiff == modifier) {
foundDay = true;
}
}
} else if (getFrequency() == YEARLY) {
// figure if we are the nth xDAY if this year
int currentPos = cal.get(Calendar.WEEK_OF_YEAR);
int dayPosCalc = cal.get(Calendar.DAY_OF_YEAR) - ((currentPos - 1) * 7);
if (dayPosCalc < 1) {
currentPos--;
}
if (modifier > 0) {
if (currentPos == modifier) {
foundDay = true;
}
} else if (modifier < 0) {
int maxDay = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
int firstDay = dayPosCalc > 0 ? dayPosCalc : dayPosCalc + 7;
int totalDay = ((maxDay - firstDay) / 7) + 1;
int thisDiff = (currentPos - totalDay) - 1;
if (thisDiff == modifier) {
foundDay = true;
}
}
}
} else {
// we are a DOW only rule
foundDay = true;
}
}
}
if (!foundDay) {
return false;
}
}
if (byMonthDayList != null && byMonthDayList.size() > 0) {
Iterator iter = byMonthDayList.iterator();
boolean foundDay = false;
while (iter.hasNext() && !foundDay) {
int day = 0;
String dayStr = (String) iter.next();
try {
day = Integer.parseInt(dayStr);
} catch (NumberFormatException nfe) {
Debug.logError(nfe, "Error parsing day string " + dayStr + ": " + nfe.toString(), module);
}
int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
int currentDay = cal.get(Calendar.DAY_OF_MONTH);
if (day > 0 && day == currentDay) {
foundDay = true;
}
if (day < 0 && day == ((currentDay - maxDay) - 1)) {
foundDay = true;
}
}
if (!foundDay) {
return false;
}
}
if (byYearDayList != null && byYearDayList.size() > 0) {
Iterator iter = byYearDayList.iterator();
boolean foundDay = false;
while (iter.hasNext() && !foundDay) {
int day = 0;
String dayStr = (String) iter.next();
try {
day = Integer.parseInt(dayStr);
} catch (NumberFormatException nfe) {
Debug.logError(nfe, "Error parsing day string " + dayStr + ": " + nfe.toString(), module);
}
int maxDay = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
int currentDay = cal.get(Calendar.DAY_OF_YEAR);
if (day > 0 && day == currentDay)
foundDay = true;
if (day < 0 && day == ((currentDay - maxDay) - 1))
foundDay = true;
}
if (!foundDay)
return false;
}
if (byWeekNoList != null && byWeekNoList.size() > 0) {
Iterator iter = byWeekNoList.iterator();
boolean foundWeek = false;
while (iter.hasNext() && !foundWeek) {
int week = 0;
String weekStr = (String) iter.next();
try {
week = Integer.parseInt(weekStr);
} catch (NumberFormatException nfe) {
Debug.logError(nfe, "Error parsing week string " + weekStr + ": " + nfe.toString(), module);
}
int maxWeek = cal.getActualMaximum(Calendar.WEEK_OF_YEAR);
int currentWeek = cal.get(Calendar.WEEK_OF_YEAR);
if (week > 0 && week == currentWeek)
foundWeek = true;
if (week < 0 && week == ((currentWeek - maxWeek) - 1))
foundWeek = true;
}
if (!foundWeek)
return false;
}
if (byMonthList != null && byMonthList.size() > 0) {
Iterator iter = byMonthList.iterator();
boolean foundMonth = false;
while (iter.hasNext() && !foundMonth) {
int month = 0;
String monthStr = (String) iter.next();
try {
month = Integer.parseInt(monthStr);
} catch (NumberFormatException nfe) {
Debug.logError(nfe, "Error parsing month string " + monthStr + ": " + nfe.toString(), module);
}
if (month == cal.get(Calendar.MONTH)) {
foundMonth = true;
}
}
if (!foundMonth)
return false;
}
return true;
}
// Tests a string for the contents of a number at the beginning
private boolean hasNumber(String str) {
String list[] = {"+", "-", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
List numberList = Arrays.asList(list);
String firstChar = str.substring(0, 0);
if (numberList.contains(firstChar))
return true;
return false;
}
// Gets the numeric value of the number at the beginning of the string
private int getDailyNumber(String str) {
int number = 0;
StringBuffer numberBuf = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
String thisChar = str.substring(i, i);
if (hasNumber(thisChar))
numberBuf.append(thisChar);
}
String numberStr = numberBuf.toString();
if (numberStr.length() > 0 && (numberStr.length() > 1 ||
(numberStr.charAt(0) != '+' && numberStr.charAt(0) != '-'))) {
try {
number = Integer.parseInt(numberStr);
} catch (NumberFormatException nfe) {
Debug.logError(nfe, "Error parsing daily number string " + numberStr + ": " + nfe.toString(), module);
}
}
return number;
}
// Gets the string part of the combined number+string
private String getDailyString(String str) {
StringBuffer sBuf = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
String thisChar = str.substring(i, i);
if (!hasNumber(thisChar)) {
sBuf.append(thisChar);
}
}
return sBuf.toString();
}
// Returns the Calendar day of the rule day string
private int getCalendarDay(String day) {
if (day.equalsIgnoreCase("MO"))
return Calendar.MONDAY;
if (day.equalsIgnoreCase("TU"))
return Calendar.TUESDAY;
if (day.equalsIgnoreCase("WE"))
return Calendar.WEDNESDAY;
if (day.equalsIgnoreCase("TH"))
return Calendar.THURSDAY;
if (day.equalsIgnoreCase("FR"))
return Calendar.FRIDAY;
if (day.equalsIgnoreCase("SA"))
return Calendar.SATURDAY;
if (day.equalsIgnoreCase("SU"))
return Calendar.SUNDAY;
return 0;
}
public String primaryKey() {
return rule.getString("recurrenceRuleId");
}
public static RecurrenceRule makeRule(GenericDelegator delegator, int frequency, int interval, int count)
throws RecurrenceRuleException {
return makeRule(delegator, frequency, interval, count, 0);
}
public static RecurrenceRule makeRule(GenericDelegator delegator, int frequency, int interval, long endTime)
throws RecurrenceRuleException {
return makeRule(delegator, frequency, interval, -1, endTime);
}
public static RecurrenceRule makeRule(GenericDelegator delegator, int frequency, int interval, int count, long endTime)
throws RecurrenceRuleException {
String freq[] = {"", "SECONDLY", "MINUTELY", "HOURLY", "DAILY", "WEEKLY", "MONTHLY", "YEARLY"};
if (frequency < 1 || frequency > 7)
throw new RecurrenceRuleException("Invalid frequency");
if (interval < 0)
throw new RecurrenceRuleException("Invalid interval");
String freqStr = freq[frequency];
try {
String ruleId = delegator.getNextSeqId("RecurrenceRule").toString();
GenericValue value = delegator.makeValue("RecurrenceRule", UtilMisc.toMap("recurrenceRuleId", ruleId));
value.set("frequency", freqStr);
value.set("intervalNumber", new Long(interval));
value.set("countNumber", new Long(count));
if (endTime > 0) {
value.set("untilDateTime", new java.sql.Timestamp(endTime));
}
delegator.create(value);
RecurrenceRule newRule = new RecurrenceRule(value);
return newRule;
} catch (GenericEntityException ee) {
throw new RecurrenceRuleException(ee.getMessage(), ee);
} catch (RecurrenceRuleException re) {
throw re;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -