List<DateRange> periodRanges = FastList.newInstance();
for (int i = 0; i < numPeriods; i++) {
Timestamp curPeriodStart = UtilDateTime.adjustTimestamp(startStamp, periodType, i, timeZone, locale);
Timestamp curPeriodEnd = UtilDateTime.adjustTimestamp(curPeriodStart, periodType, 1, timeZone, locale);
curPeriodEnd = new Timestamp(curPeriodEnd.getTime() - 1);
periodRanges.add(new DateRange(curPeriodStart, curPeriodEnd));
}
try {
// Process recurring work efforts
Set<GenericValue> exclusions = FastSet.newInstance();
Set<GenericValue> inclusions = FastSet.newInstance();
DateRange range = new DateRange(startStamp, endStamp);
Calendar cal = UtilDateTime.toCalendar(startStamp, timeZone, locale);
for (GenericValue workEffort : validWorkEfforts) {
if (UtilValidate.isNotEmpty(workEffort.getString("tempExprId"))) {
// check if either the workeffort is public or the requested party is a member
if (UtilValidate.isNotEmpty(partyIdsToUse) && !"WES_PUBLIC".equals(workEffort.getString("scopeEnumId")) && !partyIdsToUse.contains(workEffort.getString("partyId"))) {
continue;
}
TemporalExpression tempExpr = TemporalExpressionWorker.getTemporalExpression(delegator, workEffort.getString("tempExprId"));
Set<Date> occurrences = tempExpr.getRange(range, cal);
for (Date occurrence : occurrences) {
for (DateRange periodRange : periodRanges) {
if (periodRange.includesDate(occurrence)) {
GenericValue cloneWorkEffort = (GenericValue) workEffort.clone();
TimeDuration duration = TimeDuration.fromNumber(workEffort.getDouble("estimatedMilliSeconds"));
if (!duration.isZero()) {
Calendar endCal = UtilDateTime.toCalendar(occurrence, timeZone, locale);
Date endDate = duration.addToCalendar(endCal).getTime();
cloneWorkEffort.set("estimatedStartDate", new Timestamp(occurrence.getTime()));
cloneWorkEffort.set("estimatedCompletionDate", new Timestamp(endDate.getTime()));
} else {
cloneWorkEffort.set("estimatedStartDate", periodRange.startStamp());
cloneWorkEffort.set("estimatedCompletionDate", periodRange.endStamp());
}
inclusions.add(cloneWorkEffort);
}
}
}
exclusions.add(workEffort);
}
}
validWorkEfforts.removeAll(exclusions);
validWorkEfforts.addAll(inclusions);
} catch (GenericEntityException e) {
Debug.logWarning(e, module);
}
// For each period in the set we check all work efforts to see if they fall within range
boolean firstEntry = true;
for (DateRange periodRange : periodRanges) {
List<Map<String, Object>> curWorkEfforts = FastList.newInstance();
Map<String, Object> entry = FastMap.newInstance();
for (GenericValue workEffort : validWorkEfforts) {
Timestamp startDate = workEffort.getTimestamp("estimatedStartDate");
if (workEffort.getTimestamp("actualStartDate") != null) {
startDate = workEffort.getTimestamp("actualStartDate");
}
Timestamp endDate = workEffort.getTimestamp("estimatedCompletionDate");
if (workEffort.getTimestamp("actualCompletionDate") != null) {
endDate = workEffort.getTimestamp("actualCompletionDate");
}
if (endDate == null) endDate = startDate;
DateRange weRange = new DateRange(startDate, endDate);
if (periodRange.intersectsRange(weRange)) {
Map<String, Object> calEntry = FastMap.newInstance();
calEntry.put("workEffort", workEffort);
long length = ((weRange.end().after(endStamp) ? endStamp.getTime() : weRange.end().getTime()) - (weRange.start().before(startStamp) ? startStamp.getTime() : weRange.start().getTime()));
int periodSpan = (int) Math.ceil((double) length / periodLen);
calEntry.put("periodSpan", Integer.valueOf(periodSpan));
DateRange calEntryRange = new DateRange((weRange.start().before(startStamp) ? startStamp : weRange.start()), (weRange.end().after(endStamp) ? endStamp : weRange.end()));
calEntry.put("calEntryRange", calEntryRange);
if (firstEntry) {
// If this is the first period any valid entry is starting here
calEntry.put("startOfPeriod", Boolean.TRUE);
firstEntry = false;