use of org.bedework.calfacade.util.Granulator.GetPeriodsPars in project bw-calendar-engine by Bedework.
the class FreeAndBusyHandler method granulateFreeBusy.
/* ====================================================================
* Private methods
* ==================================================================== */
/*
private void addFbcal(Collection<BwCalendar> cals,
BwCalendar cal) throws CalFacadeException {
if (cal.getCalType() == BwCalendar.calTypeCollection) {
// Leaf
cals.add(cal);
return;
}
Collection<BwCalendar> chs = getSvc().getCalendarsHandler().getChildren(cal);
for (BwCalendar ch: chs) {
addFbcal(cals, ch);
}
}
*/
private void granulateFreeBusy(final FbGranulatedResponse fbresp, final BwEvent fb, final BwDateTime start, final BwDateTime end, final BwDuration granularity) throws CalFacadeException {
DateTime startDt;
DateTime endDt;
try {
startDt = new DateTime(start.getDate());
endDt = new DateTime(end.getDate());
} catch (ParseException pe) {
throw new CalFacadeException(pe);
}
if (fb.getDtstart().after(start)) {
// XXX Should warn - or fill in with tentative?
// warn("Response start after requested start");
}
if (fb.getDtend().before(end)) {
// XXX Should warn - or fill in with tentative?
// warn("Response end before requested end");
}
fbresp.setStart(start);
fbresp.setEnd(end);
Collection<EventPeriod> periods = new ArrayList<EventPeriod>();
if (fb.getFreeBusyPeriods() != null) {
for (BwFreeBusyComponent fbcomp : fb.getFreeBusyPeriods()) {
for (Period p : fbcomp.getPeriods()) {
DateTime pstart = p.getStart();
DateTime pend = p.getEnd();
if (!pend.isUtc()) {
pend.setUtc(true);
}
/* Adjust for servers sending times outside requested range */
if (pend.after(endDt)) {
pend = endDt;
}
if (pstart.before(startDt)) {
pstart = startDt;
}
if (!pend.after(pstart)) {
continue;
}
periods.add(new EventPeriod(pstart, pend, fbcomp.getType()));
}
}
}
GetPeriodsPars gpp = new GetPeriodsPars();
gpp.periods = periods;
gpp.startDt = start;
gpp.dur = granularity;
BwDateTime bwend = end;
Collection<EventPeriod> respeps = new ArrayList<EventPeriod>();
fbresp.eps = respeps;
// XXX do this better
int limit = 10000;
/* endDt is null first time through, then represents end of last
* segment.
*/
while ((gpp.endDt == null) || (gpp.endDt.before(bwend))) {
// }
if (limit < 0) {
throw new CalFacadeException("org.bedework.svci.limit.exceeded");
}
limit--;
Collection<?> periodEvents = Granulator.getPeriodsEvents(gpp);
/* Some events fall in the period. Add an entry.
* We eliminated cancelled events earler. Now we should set the
* free/busy type based on the events status.
*/
DateTime psdt;
DateTime pedt;
try {
psdt = new DateTime(gpp.startDt.getDtval());
pedt = new DateTime(gpp.endDt.getDtval());
} catch (ParseException pe) {
throw new CalFacadeException(pe);
}
psdt.setUtc(true);
pedt.setUtc(true);
EventPeriod ep = new EventPeriod(psdt, pedt, 0);
setFreeBusyType(ep, periodEvents);
respeps.add(ep);
}
}
Aggregations