Improved: General refactoring and code improvements, package

org.apache.ofbiz.service.calendar.
(OFBIZ-9946)

Thanks Dennis Balkir for reporting and providing the patches.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1818481 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Brohl
2017-12-17 11:48:06 +00:00
parent 0a63482c06
commit 55d3ee92cb
5 changed files with 156 additions and 87 deletions
@@ -50,16 +50,18 @@ public class RecurrenceInfo {
/** Creates new RecurrenceInfo */
public RecurrenceInfo(GenericValue info) throws RecurrenceInfoException {
this.info = info;
if (!"RecurrenceInfo".equals(info.getEntityName()))
if (!"RecurrenceInfo".equals(info.getEntityName())) {
throw new RecurrenceInfoException("Invalid RecurrenceInfo Value object.");
}
init();
}
/** Initializes the rules for this RecurrenceInfo object. */
public void init() throws RecurrenceInfoException {
if (info.get("startDateTime") == null)
if (info.get("startDateTime") == null) {
throw new RecurrenceInfoException("Recurrence startDateTime cannot be null.");
}
// Get start date
long startTime = info.getTimestamp("startDateTime").getTime();
@@ -144,8 +146,9 @@ public class RecurrenceInfo {
/** Returns the current count of this recurrence. */
public long getCurrentCount() {
if (info.get("recurrenceCount") != null)
if (info.get("recurrenceCount") != null) {
return info.getLong("recurrenceCount").longValue();
}
return 0;
}
@@ -170,13 +173,12 @@ public class RecurrenceInfo {
rulesList.addAll(eRulesList);
try {
for (RecurrenceRule rule: rulesList)
for (RecurrenceRule rule: rulesList) {
rule.remove();
}
info.remove();
} catch (RecurrenceRuleException rre) {
throw new RecurrenceInfoException(rre.getMessage(), rre);
} catch (GenericEntityException gee) {
throw new RecurrenceInfoException(gee.getMessage(), gee);
} catch (RecurrenceRuleException | GenericEntityException e) {
throw new RecurrenceInfoException(e.getMessage(), e);
}
}
@@ -262,7 +264,9 @@ public class RecurrenceInfo {
private long getNextTime(RecurrenceRule rule, long fromTime) {
long nextTime = rule.next(getStartTime(), fromTime, getCurrentCount());
if (Debug.verboseOn()) Debug.logVerbose("Next Time Before Date Check: " + nextTime, module);
if (Debug.verboseOn()) {
Debug.logVerbose("Next Time Before Date Check: " + nextTime, module);
}
return checkDateList(rDateList, nextTime, fromTime);
}
@@ -271,10 +275,11 @@ public class RecurrenceInfo {
if (UtilValidate.isNotEmpty(dateList)) {
for (Date thisDate: dateList) {
if (nextTime > 0 && thisDate.getTime() < nextTime && thisDate.getTime() > fromTime)
if (nextTime > 0 && thisDate.getTime() < nextTime && thisDate.getTime() > fromTime) {
nextTime = thisDate.getTime();
else if (nextTime == 0 && thisDate.getTime() > fromTime)
} else if (nextTime == 0 && thisDate.getTime() > fromTime) {
nextTime = thisDate.getTime();
}
}
}
return nextTime;
@@ -286,8 +291,9 @@ public class RecurrenceInfo {
while (exceptRulesIterator.hasNext()) {
RecurrenceRule except = exceptRulesIterator.next();
if (except.isValid(getStartTime(), time) || eDateList.contains(new Date(time)))
if (except.isValid(getStartTime(), time) || eDateList.contains(new Date(time))) {
return false;
}
}
return true;
}
@@ -319,10 +325,8 @@ public class RecurrenceInfo {
RecurrenceInfo newInfo = new RecurrenceInfo(value);
return newInfo;
} catch (RecurrenceRuleException re) {
throw new RecurrenceInfoException(re.getMessage(), re);
} catch (GenericEntityException ee) {
throw new RecurrenceInfoException(ee.getMessage(), ee);
} catch (RecurrenceRuleException | GenericEntityException e) {
throw new RecurrenceInfoException(e.getMessage(), e);
} catch (RecurrenceInfoException rie) {
throw rie;
}
@@ -106,8 +106,9 @@ public class RecurrenceRule {
*/
public RecurrenceRule(GenericValue rule) throws RecurrenceRuleException {
this.rule = rule;
if (!"RecurrenceRule".equals(rule.getEntityName()))
if (!"RecurrenceRule".equals(rule.getEntityName())) {
throw new RecurrenceRuleException("Invalid RecurrenceRule Value object.");
}
init();
}
@@ -119,10 +120,12 @@ public class RecurrenceRule {
// Check the validity of the rule
String freq = rule.getString("frequency");
if (!checkFreq(freq))
if (!checkFreq(freq)) {
throw new RecurrenceRuleException("Recurrence FREQUENCY is a required parameter.");
if (rule.getLong("intervalNumber").longValue() < 1)
}
if (rule.getLong("intervalNumber").longValue() < 1) {
throw new RecurrenceRuleException("Recurrence INTERVAL must be a positive integer.");
}
// Initialize the byXXX lists
bySecondList = StringUtil.split(rule.getString("bySecondList"), ",");
@@ -138,22 +141,30 @@ public class RecurrenceRule {
// Checks for a valid frequency property.
private boolean checkFreq(String freq) {
if (freq == null)
if (freq == null) {
return false;
if ("SECONDLY".equalsIgnoreCase(freq))
}
if ("SECONDLY".equalsIgnoreCase(freq)) {
return true;
if ("MINUTELY".equalsIgnoreCase(freq))
}
if ("MINUTELY".equalsIgnoreCase(freq)) {
return true;
if ("HOURLY".equalsIgnoreCase(freq))
}
if ("HOURLY".equalsIgnoreCase(freq)) {
return true;
if ("DAILY".equalsIgnoreCase(freq))
}
if ("DAILY".equalsIgnoreCase(freq)) {
return true;
if ("WEEKLY".equalsIgnoreCase(freq))
}
if ("WEEKLY".equalsIgnoreCase(freq)) {
return true;
if ("MONTHLY".equalsIgnoreCase(freq))
}
if ("MONTHLY".equalsIgnoreCase(freq)) {
return true;
if ("YEARLY".equalsIgnoreCase(freq))
}
if ("YEARLY".equalsIgnoreCase(freq)) {
return true;
}
return false;
}
@@ -186,8 +197,9 @@ public class RecurrenceRule {
*@return long The number of time this recurrence will run.
*/
public long getCount() {
if (rule.get("countNumber") != null)
if (rule.get("countNumber") != null) {
return rule.getLong("countNumber").longValue();
}
return 0;
}
@@ -206,22 +218,30 @@ public class RecurrenceRule {
public int getFrequency() {
String freq = rule.getString("frequency");
if (freq == null)
if (freq == null) {
return 0;
if ("SECONDLY".equalsIgnoreCase(freq))
}
if ("SECONDLY".equalsIgnoreCase(freq)) {
return SECONDLY;
if ("MINUTELY".equalsIgnoreCase(freq))
}
if ("MINUTELY".equalsIgnoreCase(freq)) {
return MINUTELY;
if ("HOURLY".equalsIgnoreCase(freq))
}
if ("HOURLY".equalsIgnoreCase(freq)) {
return HOURLY;
if ("DAILY".equalsIgnoreCase(freq))
}
if ("DAILY".equalsIgnoreCase(freq)) {
return DAILY;
if ("WEEKLY".equalsIgnoreCase(freq))
}
if ("WEEKLY".equalsIgnoreCase(freq)) {
return WEEKLY;
if ("MONTHLY".equalsIgnoreCase(freq))
}
if ("MONTHLY".equalsIgnoreCase(freq)) {
return MONTHLY;
if ("YEARLY".equalsIgnoreCase(freq))
}
if ("YEARLY".equalsIgnoreCase(freq)) {
return YEARLY;
}
return 0;
}
@@ -230,8 +250,9 @@ public class RecurrenceRule {
*@return long Interval value
*/
public long getInterval() {
if (rule.get("intervalNumber") == null)
if (rule.get("intervalNumber") == null) {
return 1;
}
return rule.getLong("intervalNumber").longValue();
}
@@ -240,7 +261,6 @@ public class RecurrenceRule {
*@return The interval of this frequency as an integer.
*/
public int getIntervalInt() {
// if (Debug.verboseOn()) Debug.logVerbose("[RecurrenceInfo.getInterval] : " + getInterval(), module);
return (int) getInterval();
}
@@ -253,20 +273,28 @@ public class RecurrenceRule {
*/
public long next(long startTime, long fromTime, long currentCount) {
// Set up the values
if (startTime == 0)
if (startTime == 0) {
startTime = RecurrenceUtil.now();
if (fromTime == 0)
}
if (fromTime == 0) {
fromTime = startTime;
}
// Test the end time of the recurrence.
if (getEndTime() != 0 && getEndTime() <= RecurrenceUtil.now())
if (getEndTime() != 0 && getEndTime() <= RecurrenceUtil.now()) {
return 0;
if (Debug.verboseOn()) Debug.logVerbose("Rule NOT expired by end time.", module);
}
if (Debug.verboseOn()) {
Debug.logVerbose("Rule NOT expired by end time.", module);
}
// Test the recurrence limit.
if (getCount() != -1 && currentCount >= getCount())
if (getCount() != -1 && currentCount >= getCount()) {
return 0;
if (Debug.verboseOn()) Debug.logVerbose("Rule NOT expired by max count.", module);
}
if (Debug.verboseOn()) {
Debug.logVerbose("Rule NOT expired by max count.", module);
}
boolean isSeeking = true;
long nextRuntime = 0;
@@ -413,12 +441,14 @@ public class RecurrenceRule {
public boolean isValid(long startTime, long dateTime) {
long testTime = startTime;
if (testTime == dateTime)
if (testTime == dateTime) {
return true;
}
while (testTime < dateTime) {
testTime = next(startTime, testTime, 1);
if (testTime == dateTime)
if (testTime == dateTime) {
return true;
}
}
return false;
}
@@ -445,7 +475,6 @@ public class RecurrenceRule {
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());
@@ -492,16 +521,19 @@ public class RecurrenceRule {
// Test each byXXX rule.
if (UtilValidate.isNotEmpty(bySecondList)) {
if (!bySecondList.contains(String.valueOf(cal.get(Calendar.SECOND))))
if (!bySecondList.contains(String.valueOf(cal.get(Calendar.SECOND)))) {
return false;
}
}
if (UtilValidate.isNotEmpty(byMinuteList)) {
if (!byMinuteList.contains(String.valueOf(cal.get(Calendar.MINUTE))))
if (!byMinuteList.contains(String.valueOf(cal.get(Calendar.MINUTE)))) {
return false;
}
}
if (UtilValidate.isNotEmpty(byHourList)) {
if (!byHourList.contains(String.valueOf(cal.get(Calendar.HOUR_OF_DAY))))
if (!byHourList.contains(String.valueOf(cal.get(Calendar.HOUR_OF_DAY)))) {
return false;
}
}
if (UtilValidate.isNotEmpty(byDayList)) {
Iterator<String> iter = byDayList.iterator();
@@ -515,16 +547,18 @@ public class RecurrenceRule {
if ((hasNumber(dayRule)) && (getFrequency() == MONTHLY || getFrequency() == YEARLY)) {
int modifier = getDailyNumber(dayRule);
if (modifier == 0)
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)
if (dayPosCalc < 1) {
currentPos--;
}
if (modifier > 0) {
if (currentPos == modifier) {
foundDay = true;
@@ -615,13 +649,16 @@ public class RecurrenceRule {
int maxDay = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
int currentDay = cal.get(Calendar.DAY_OF_YEAR);
if (day > 0 && day == currentDay)
if (day > 0 && day == currentDay) {
foundDay = true;
if (day < 0 && day == ((currentDay - maxDay) - 1))
}
if (day < 0 && day == ((currentDay - maxDay) - 1)) {
foundDay = true;
}
}
if (!foundDay)
if (!foundDay) {
return false;
}
}
if (UtilValidate.isNotEmpty(byWeekNoList)) {
Iterator<String> iter = byWeekNoList.iterator();
@@ -639,13 +676,16 @@ public class RecurrenceRule {
int maxWeek = cal.getActualMaximum(Calendar.WEEK_OF_YEAR);
int currentWeek = cal.get(Calendar.WEEK_OF_YEAR);
if (week > 0 && week == currentWeek)
if (week > 0 && week == currentWeek) {
foundWeek = true;
if (week < 0 && week == ((currentWeek - maxWeek) - 1))
}
if (week < 0 && week == ((currentWeek - maxWeek) - 1)) {
foundWeek = true;
}
}
if (!foundWeek)
if (!foundWeek) {
return false;
}
}
if (UtilValidate.isNotEmpty(byMonthList)) {
Iterator<String> iter = byMonthList.iterator();
@@ -664,8 +704,9 @@ public class RecurrenceRule {
foundMonth = true;
}
}
if (!foundMonth)
if (!foundMonth) {
return false;
}
}
return true;
@@ -677,8 +718,9 @@ public class RecurrenceRule {
List<String> numberList = Arrays.asList(list);
String firstChar = str.substring(0, 1);
if (numberList.contains(firstChar))
if (numberList.contains(firstChar)) {
return true;
}
return false;
}
@@ -690,8 +732,9 @@ public class RecurrenceRule {
for (int i = 0; i < str.length(); i++) {
String thisChar = str.substring(i, i);
if (hasNumber(thisChar))
if (hasNumber(thisChar)) {
numberBuf.append(thisChar);
}
}
String numberStr = numberBuf.toString();
@@ -724,20 +767,27 @@ public class RecurrenceRule {
private int getCalendarDay(String day) {
if (day != null) {
day = day.trim();
if ("MO".equalsIgnoreCase(day))
if ("MO".equalsIgnoreCase(day)) {
return Calendar.MONDAY;
if ("TU".equalsIgnoreCase(day))
}
if ("TU".equalsIgnoreCase(day)) {
return Calendar.TUESDAY;
if ("WE".equalsIgnoreCase(day))
}
if ("WE".equalsIgnoreCase(day)) {
return Calendar.WEDNESDAY;
if ("TH".equalsIgnoreCase(day))
}
if ("TH".equalsIgnoreCase(day)) {
return Calendar.THURSDAY;
if ("FR".equalsIgnoreCase(day))
}
if ("FR".equalsIgnoreCase(day)) {
return Calendar.FRIDAY;
if ("SA".equalsIgnoreCase(day))
}
if ("SA".equalsIgnoreCase(day)) {
return Calendar.SATURDAY;
if ("SU".equalsIgnoreCase(day))
}
if ("SU".equalsIgnoreCase(day)) {
return Calendar.SUNDAY;
}
}
return 0;
}
@@ -760,10 +810,12 @@ public class RecurrenceRule {
throws RecurrenceRuleException {
String freq[] = {"", "SECONDLY", "MINUTELY", "HOURLY", "DAILY", "WEEKLY", "MONTHLY", "YEARLY"};
if (frequency < 1 || frequency > 7)
if (frequency < 1 || frequency > 7) {
throw new RecurrenceRuleException("Invalid frequency");
if (interval < 0)
}
if (interval < 0) {
throw new RecurrenceRuleException("Invalid interval");
}
String freqStr = freq[frequency];
@@ -37,12 +37,15 @@ public final class RecurrenceUtil {
public static Date parseDate(String dateStr) {
String formatString = "";
if (dateStr.length() == 16)
if (dateStr.length() == 16) {
dateStr = dateStr.substring(0, 14);
if (dateStr.length() == 15)
}
if (dateStr.length() == 15) {
formatString = "yyyyMMdd'T'hhmmss";
if (dateStr.length() == 8)
}
if (dateStr.length() == 8) {
formatString = "yyyyMMdd";
}
SimpleDateFormat formatter = new SimpleDateFormat(formatString);
ParsePosition pos = new ParsePosition(0);
@@ -52,12 +55,14 @@ public final class RecurrenceUtil {
/** Returns a List of parsed date strings. */
public static List<Date> parseDateList(List<String> dateList) {
List<Date> newList = new ArrayList<Date>();
List<Date> newList = new ArrayList<>();
if (dateList == null)
if (dateList == null) {
return newList;
for (String value: dateList)
}
for (String value: dateList) {
newList.add(parseDate(value));
}
return newList;
}
@@ -67,10 +72,11 @@ public final class RecurrenceUtil {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
if (cal.isSet(Calendar.MINUTE))
if (cal.isSet(Calendar.MINUTE)) {
formatString = "yyyyMMdd'T'hhmmss";
else
} else {
formatString = "yyyyMMdd";
}
SimpleDateFormat formatter = new SimpleDateFormat(formatString);
return formatter.format(date);
@@ -78,9 +84,10 @@ public final class RecurrenceUtil {
/** Returns a Llist of date strings from a List of Date objects */
public static List<String> formatDateList(List<Date> dateList) {
List<String> newList = new ArrayList<String>();
for (Date date: dateList)
List<String> newList = new ArrayList<>();
for (Date date: dateList) {
newList.add(formatDate(date));
}
return newList;
}
@@ -53,20 +53,26 @@ public abstract class TemporalExpression implements Serializable, Comparable<Tem
@Override
public boolean equals(Object obj) {
if (this == obj)
if (this == obj) {
return true;
if (obj == null)
}
if (obj == null) {
return false;
if (getClass() != obj.getClass())
}
if (getClass() != obj.getClass()) {
return false;
}
TemporalExpression other = (TemporalExpression) obj;
if (id == null) {
if (other.id != null)
if (other.id != null) {
return false;
} else if (!id.equals(other.id))
}
} else if (!id.equals(other.id)) {
return false;
if (sequence != other.sequence)
}
if (sequence != other.sequence) {
return false;
}
return true;
}
@@ -24,7 +24,7 @@ public class TemporalExpressionPrinter implements TemporalExpressionVisitor {
protected final StringBuilder sb = new StringBuilder();
protected int indentSize = 2;
protected int currentIndent = 0;
public TemporalExpressionPrinter(TemporalExpression expression) {
this.expression = expression;
}