Search in sources :

Example 1 with GetPeriodsPars

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);
    }
}
Also used : EventPeriod(org.bedework.calfacade.util.Granulator.EventPeriod) BwFreeBusyComponent(org.bedework.calfacade.BwFreeBusyComponent) GetPeriodsPars(org.bedework.calfacade.util.Granulator.GetPeriodsPars) BwDateTime(org.bedework.calfacade.BwDateTime) ArrayList(java.util.ArrayList) Period(net.fortuna.ical4j.model.Period) EventPeriod(org.bedework.calfacade.util.Granulator.EventPeriod) ParseException(java.text.ParseException) DateTime(net.fortuna.ical4j.model.DateTime) BwDateTime(org.bedework.calfacade.BwDateTime) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Aggregations

ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 DateTime (net.fortuna.ical4j.model.DateTime)1 Period (net.fortuna.ical4j.model.Period)1 BwDateTime (org.bedework.calfacade.BwDateTime)1 BwFreeBusyComponent (org.bedework.calfacade.BwFreeBusyComponent)1 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)1 EventPeriod (org.bedework.calfacade.util.Granulator.EventPeriod)1 GetPeriodsPars (org.bedework.calfacade.util.Granulator.GetPeriodsPars)1