Search in sources :

Example 6 with OrFilter

use of org.bedework.caldav.util.filter.OrFilter in project bw-calendar-engine by Bedework.

the class FreeAndBusyHandler method getFreeBusy.

/* (non-Javadoc)
   * @see org.bedework.calsvci.SchedulingI#getFreeBusy(java.util.Collection, org.bedework.calfacade.BwPrincipal, org.bedework.calfacade.BwDateTime, org.bedework.calfacade.BwDateTime, org.bedework.calfacade.BwOrganizer, java.lang.String)
   */
@Override
public BwEvent getFreeBusy(final Collection<BwCalendar> fbset, final BwPrincipal who, final BwDateTime start, final BwDateTime end, final BwOrganizer org, final String uid, final String exceptUid) throws CalFacadeException {
    CalendarsI colHandler = getSvc().getCalendarsHandler();
    Collection<BwCalendar> cals = null;
    if (fbset != null) {
        /* Don't check - we do so at the fetch of events
      getCal().checkAccess(cal, PrivilegeDefs.privReadFreeBusy, false);
      */
        cals = addToFreeBusySet(cals, fbset);
    } else if (getPrincipal().equals(who)) {
        cals = getFreebusySet();
    } else {
        /* CalDAV uses Inbox to determine scheduling acccess */
        try {
            getSpecialCalendar(who, BwCalendar.calTypeInbox, true, PrivilegeDefs.privReadFreeBusy);
        } catch (CalFacadeAccessException cae) {
            getSpecialCalendar(who, BwCalendar.calTypeInbox, true, PrivilegeDefs.privScheduleFreeBusy);
        }
        cals = addToFreeBusySet(cals, colHandler.getChildren(colHandler.getHome(who, true)));
    }
    if (cals == null) {
        throw new CalFacadeAccessException();
    }
    BwEvent fb = new BwEventObj();
    fb.setEntityType(IcalDefs.entityTypeFreeAndBusy);
    fb.setOwnerHref(who.getPrincipalRef());
    fb.setDtstart(start);
    fb.setDtend(end);
    fb.setEndType(StartEndComponent.endTypeDate);
    if (uid == null) {
        assignGuid(fb);
    } else {
        fb.setUid(uid);
    }
    fb.setDtstamps(getCurrentTimestamp());
    String uri = getSvc().getDirectories().principalToCaladdr(who);
    BwAttendee att = new BwAttendee();
    att.setAttendeeUri(uri);
    fb.addAttendee(att);
    fb.setOrganizer((BwOrganizer) org.clone());
    Collection<EventInfo> events = new TreeSet<EventInfo>();
    /* Only events and freebusy for freebusy reports. */
    FilterBase filter = new OrFilter();
    try {
        filter.addChild(EntityTypeFilter.makeEntityTypeFilter(null, "event", false));
        filter.addChild(EntityTypeFilter.makeEntityTypeFilter(null, "freeAndBusy", false));
    } catch (Throwable t) {
        throw new CalFacadeException(t);
    }
    String userHref = who.getPrincipalRef();
    for (BwCalendar c : cals) {
        if (!c.getAffectsFreeBusy()) {
            continue;
        }
        // XXX If it's an external subscription we probably just get free busy and
        // merge it in.
        RecurringRetrievalMode rrm = new RecurringRetrievalMode(Rmode.expanded, start, end);
        Collection<BwCalendar> cs = new ArrayList<BwCalendar>();
        cs.add(c);
        Collection<EventInfo> evs = getEvents(cs, filter, start, end, // retrieveList
        null, rrm, true);
        // Filter out transparent events
        for (EventInfo ei : evs) {
            BwEvent ev = ei.getEvent();
            if ((exceptUid != null) && exceptUid.equals(ev.getUid())) {
                continue;
            }
            if (!c.getIgnoreTransparency() && IcalDefs.transparencyTransparent.equals(ev.getPeruserTransparency(userHref))) {
                // Ignore this one.
                continue;
            }
            if (BwEvent.statusCancelled.equals(ev.getStatus())) {
                // Ignore this one.
                continue;
            }
            if (ev.getAttendeeSchedulingObject()) {
                att = ev.findAttendee(uri);
                if (att != null) {
                    int pstat = IcalDefs.checkPartstat(att.getPartstat());
                    if (pstat == IcalDefs.partstatDeclined) {
                        continue;
                    }
                    if (pstat == IcalDefs.partstatNeedsAction) {
                        continue;
                    }
                }
            }
            events.add(ei);
        }
    }
    try {
        EventPeriods eventPeriods = new EventPeriods(start, end);
        for (EventInfo ei : events) {
            BwEvent ev = ei.getEvent();
            int type;
            if (ev.getEntityType() == IcalDefs.entityTypeEvent) {
                if (BwEvent.statusCancelled.equals(ev.getStatus())) {
                    // Ignore this one.
                    continue;
                }
                type = BwFreeBusyComponent.typeBusy;
                if (ev.getAttendeeSchedulingObject()) {
                    att = ev.findAttendee(uri);
                    if (att != null) {
                        if (IcalDefs.checkPartstat(att.getPartstat()) == IcalDefs.partstatTentative) {
                            type = BwFreeBusyComponent.typeBusyTentative;
                        }
                    }
                }
                if (BwEvent.statusTentative.equals(ev.getStatus())) {
                    type = BwFreeBusyComponent.typeBusyTentative;
                } else if (BwEvent.statusUnavailable.equals(ev.getStatus())) {
                    type = BwFreeBusyComponent.typeBusyUnavailable;
                }
                eventPeriods.addPeriod(ev.getDtstart(), ev.getDtend(), type);
            } else if (ev.getEntityType() == IcalDefs.entityTypeFreeAndBusy) {
                Collection<BwFreeBusyComponent> fbcs = ev.getFreeBusyPeriods();
                for (BwFreeBusyComponent fbc : fbcs) {
                    type = fbc.getType();
                    for (Period p : fbc.getPeriods()) {
                        eventPeriods.addPeriod(p.getStart(), p.getEnd(), type);
                    }
                }
            }
        }
        /* iterate through the sorted periods combining them where they are
       adjacent or overlap */
        BwFreeBusyComponent fbc = eventPeriods.makeFreeBusyComponent(BwFreeBusyComponent.typeBusy);
        if (fbc != null) {
            fb.addFreeBusyPeriod(fbc);
        }
        fbc = eventPeriods.makeFreeBusyComponent(BwFreeBusyComponent.typeBusyUnavailable);
        if (fbc != null) {
            fb.addFreeBusyPeriod(fbc);
        }
        fbc = eventPeriods.makeFreeBusyComponent(BwFreeBusyComponent.typeBusyTentative);
        if (fbc != null) {
            fb.addFreeBusyPeriod(fbc);
        }
    } catch (Throwable t) {
        if (debug) {
            error(t);
        }
        throw new CalFacadeException(t);
    }
    return fb;
}
Also used : EventPeriods(org.bedework.calfacade.util.EventPeriods) BwFreeBusyComponent(org.bedework.calfacade.BwFreeBusyComponent) EventInfo(org.bedework.calfacade.svc.EventInfo) RecurringRetrievalMode(org.bedework.calfacade.RecurringRetrievalMode) ArrayList(java.util.ArrayList) Period(net.fortuna.ical4j.model.Period) EventPeriod(org.bedework.calfacade.util.Granulator.EventPeriod) BwEvent(org.bedework.calfacade.BwEvent) CalendarsI(org.bedework.calsvci.CalendarsI) BwCalendar(org.bedework.calfacade.BwCalendar) OrFilter(org.bedework.caldav.util.filter.OrFilter) CalFacadeAccessException(org.bedework.calfacade.exc.CalFacadeAccessException) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) TreeSet(java.util.TreeSet) Collection(java.util.Collection) BwAttendee(org.bedework.calfacade.BwAttendee) BwEventObj(org.bedework.calfacade.BwEventObj) FilterBase(org.bedework.caldav.util.filter.FilterBase)

Example 7 with OrFilter

use of org.bedework.caldav.util.filter.OrFilter in project bw-calendar-engine by Bedework.

the class CalintfImpl method getFreeBusyEntities.

private Collection<CoreEventInfo> getFreeBusyEntities(final Collection<BwCalendar> cals, final BwDateTime start, final BwDateTime end, final boolean ignoreTransparency) throws CalFacadeException {
    /* Only events and freebusy for freebusy reports. */
    final FilterBase filter = new OrFilter();
    try {
        filter.addChild(EntityTypeFilter.makeEntityTypeFilter(null, "event", false));
        filter.addChild(EntityTypeFilter.makeEntityTypeFilter(null, "freeAndBusy", false));
    } catch (final Throwable t) {
        throw new CalFacadeException(t);
    }
    final RecurringRetrievalMode rrm = new RecurringRetrievalMode(Rmode.expanded, start, end);
    final Collection<CoreEventInfo> evs = getEvents(cals, filter, start, end, null, DeletedState.noDeleted, rrm, true);
    // Filter out transparent and cancelled events
    final Collection<CoreEventInfo> events = new TreeSet<>();
    for (final CoreEventInfo cei : evs) {
        final BwEvent ev = cei.getEvent();
        if (!ignoreTransparency && IcalDefs.transparencyTransparent.equals(ev.getPeruserTransparency(this.getPrincipal().getPrincipalRef()))) {
            // Ignore this one.
            continue;
        }
        if (ev.getSuppressed() || BwEvent.statusCancelled.equals(ev.getStatus())) {
            // Ignore this one.
            continue;
        }
        events.add(cei);
    }
    return events;
}
Also used : RecurringRetrievalMode(org.bedework.calfacade.RecurringRetrievalMode) CoreEventInfo(org.bedework.calcorei.CoreEventInfo) TreeSet(java.util.TreeSet) BwEvent(org.bedework.calfacade.BwEvent) OrFilter(org.bedework.caldav.util.filter.OrFilter) FilterBase(org.bedework.caldav.util.filter.FilterBase) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 8 with OrFilter

use of org.bedework.caldav.util.filter.OrFilter in project bw-calendar-engine by Bedework.

the class Filters method match.

private boolean match(final FilterBase f, final BwEvent ev, final String userHref) throws CalFacadeException {
    if (f == null) {
        return true;
    }
    if (debug) {
        debug("match " + f);
    }
    if ((f instanceof AndFilter) || (f instanceof OrFilter)) {
        boolean itsAnd = (f instanceof AndFilter);
        for (FilterBase flt : f.getChildren()) {
            if (match(flt, ev, userHref)) {
                if (!itsAnd) {
                    // Success for OR
                    if (debug) {
                        debug("match true");
                    }
                    return true;
                }
            } else if (itsAnd) {
                debug("match true");
                return false;
            }
        }
        // For AND all matched, for OR nothing matched
        debug("match " + itsAnd);
        return itsAnd;
    }
    if (f instanceof EntityTimeRangeFilter) {
        // Matched in db query
        return true;
    }
    if (!(f instanceof PropertyFilter)) {
        /* We assume we can't handle this one as a query.
       */
        throw new CalFacadeException("org.bedework.filters.unknownfilter", String.valueOf(f));
    }
    PropertyFilter pf = (PropertyFilter) f;
    BwIcalPropertyInfoEntry pi = BwIcalPropertyInfo.getPinfo(pf.getPropertyIndex());
    if (pi == null) {
        throw new CalFacadeException("org.bedework.filters.unknownproperty", String.valueOf(pf.getPropertyIndex()));
    }
    String fieldName = pi.getDbFieldName();
    boolean param = pi.getParam();
    if (param) {
        BwIcalPropertyInfoEntry parentPi = BwIcalPropertyInfo.getPinfo(pf.getParentPropertyIndex());
        fieldName = parentPi.getDbFieldName() + "." + fieldName;
    }
    try {
        if (pf instanceof BwCategoryFilter) {
            return traceMatch(pf.match(ev, userHref));
        }
        if (f instanceof PresenceFilter) {
            return traceMatch(matchPresence(pf.getPropertyIndex(), ev, userHref));
        }
        if (pf instanceof TimeRangeFilter) {
            return traceMatch(match((TimeRangeFilter) pf, ev));
        }
        if (pf instanceof BwObjectFilter) {
            return traceMatch(pf.match(ev, userHref));
        }
    } catch (WebdavException wde) {
        throw new CalFacadeException(wde);
    }
    throw new CalFacadeException("org.bedework.filters.unknownfilter", String.valueOf(f));
}
Also used : PresenceFilter(org.bedework.caldav.util.filter.PresenceFilter) EntityTimeRangeFilter(org.bedework.caldav.util.filter.EntityTimeRangeFilter) TimeRangeFilter(org.bedework.caldav.util.filter.TimeRangeFilter) WebdavException(org.bedework.webdav.servlet.shared.WebdavException) BwCategoryFilter(org.bedework.calfacade.filter.BwCategoryFilter) OrFilter(org.bedework.caldav.util.filter.OrFilter) EntityTimeRangeFilter(org.bedework.caldav.util.filter.EntityTimeRangeFilter) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) AndFilter(org.bedework.caldav.util.filter.AndFilter) BwIcalPropertyInfoEntry(org.bedework.calfacade.ical.BwIcalPropertyInfo.BwIcalPropertyInfoEntry) PropertyFilter(org.bedework.caldav.util.filter.PropertyFilter) FilterBase(org.bedework.caldav.util.filter.FilterBase) BwObjectFilter(org.bedework.calfacade.filter.BwObjectFilter)

Example 9 with OrFilter

use of org.bedework.caldav.util.filter.OrFilter in project bw-calendar-engine by Bedework.

the class Filters method parReplace.

/* Fill in the parameters after we generated the query.
   */
private void parReplace(final FilterBase f) throws CalFacadeException {
    if (f instanceof AndFilter) {
        AndFilter fb = (AndFilter) f;
        for (FilterBase flt : fb.getChildren()) {
            parReplace(flt);
        }
        return;
    }
    if (f instanceof BwHrefFilter) {
        BwHrefFilter hf = (BwHrefFilter) f;
        // Special case this
        sess.setString(parPrefix + qi, hf.getPathPart());
        qi++;
        sess.setString(parPrefix + qi, hf.getNamePart());
        qi++;
    }
    if (f instanceof BwCategoryFilter) {
        BwCategoryFilter cf = (BwCategoryFilter) f;
        BwCategory cat = cf.getEntity();
        /* XXX - this is what we want to be able to do
      sess.setString(parPrefix + qi, cat.getUid());
      */
        sess.setEntity(parPrefix + qi, cat);
        qi++;
        return;
    }
    if (f instanceof EntityTimeRangeFilter) {
        doEntityTimeRangeReplace((EntityTimeRangeFilter) f);
        return;
    }
    if (f instanceof TimeRangeFilter) {
        TimeRangeFilter trf = (TimeRangeFilter) f;
        TimeRange tr = trf.getEntity();
        if (tr.getStart() != null) {
            sess.setParameter(parPrefix + qi, tr.getStart().toString());
            qi++;
        }
        if (tr.getEnd() != null) {
            sess.setParameter(parPrefix + qi, tr.getEnd().toString());
            qi++;
        }
        return;
    }
    if (f instanceof BwObjectFilter) {
        ObjectFilter of = ((BwObjectFilter) f).getEntity();
        Object o = of.getEntity();
        Collection c = null;
        boolean isString = o instanceof String;
        if (!isString) {
            if (o instanceof Collection) {
                c = (Collection) o;
                if (c.size() > 0) {
                    o = c.iterator().next();
                    isString = o instanceof String;
                }
            }
        }
        boolean doCaseless = isString && of.getCaseless();
        if (c != null) {
            // TODO - Assuming String collection
            for (Object co : c) {
                String s = (String) co;
                if (doCaseless) {
                    s = s.toLowerCase();
                }
                sess.setParameter(parPrefix + qi, s);
                qi++;
            }
            return;
        }
        if (o instanceof BwCalendar) {
            BwCalendar cal = unwrap((BwCalendar) o);
            sess.setString(parPrefix + qi, cal.getPath());
        } else if (o instanceof BwPrincipal) {
            sess.setString(parPrefix + qi, ((BwPrincipal) o).getPrincipalRef());
        } else if (o instanceof BwDbentity) {
            sess.setEntity(parPrefix + qi, o);
        } else if (of.getExact()) {
            if (doCaseless) {
                o = ((String) o).toLowerCase();
            }
            sess.setParameter(parPrefix + qi, o);
        } else if (of.getEntity() instanceof String) {
            String s = o.toString();
            if (doCaseless) {
                s = s.toLowerCase();
            }
            sess.setString(parPrefix + qi, "%" + s + "%");
        } else {
            sess.setString(parPrefix + qi, "%" + o + "%");
        }
        qi++;
        return;
    }
    if (f instanceof OrFilter) {
        OrFilter fb = (OrFilter) f;
        for (FilterBase flt : fb.getChildren()) {
            parReplace(flt);
        }
        return;
    }
}
Also used : BwHrefFilter(org.bedework.calfacade.filter.BwHrefFilter) EntityTimeRangeFilter(org.bedework.caldav.util.filter.EntityTimeRangeFilter) TimeRangeFilter(org.bedework.caldav.util.filter.TimeRangeFilter) BwCategory(org.bedework.calfacade.BwCategory) BwCategoryFilter(org.bedework.calfacade.filter.BwCategoryFilter) BwObjectFilter(org.bedework.calfacade.filter.BwObjectFilter) ObjectFilter(org.bedework.caldav.util.filter.ObjectFilter) BwCalendar(org.bedework.calfacade.BwCalendar) OrFilter(org.bedework.caldav.util.filter.OrFilter) EntityTimeRangeFilter(org.bedework.caldav.util.filter.EntityTimeRangeFilter) AndFilter(org.bedework.caldav.util.filter.AndFilter) TimeRange(org.bedework.caldav.util.TimeRange) BwPrincipal(org.bedework.calfacade.BwPrincipal) Collection(java.util.Collection) BwDbentity(org.bedework.calfacade.base.BwDbentity) FilterBase(org.bedework.caldav.util.filter.FilterBase) BwObjectFilter(org.bedework.calfacade.filter.BwObjectFilter)

Example 10 with OrFilter

use of org.bedework.caldav.util.filter.OrFilter in project bw-calendar-engine by Bedework.

the class SimpleFilterParser method or.

private FilterBase or(final FilterBase of, final FilterBase f) {
    if (of == null) {
        return f;
    }
    if (of instanceof OrFilter) {
        of.addChild(f);
        return of;
    }
    final OrFilter nof = new OrFilter();
    nof.addChild(of);
    nof.addChild(f);
    return nof;
}
Also used : OrFilter(org.bedework.caldav.util.filter.OrFilter)

Aggregations

OrFilter (org.bedework.caldav.util.filter.OrFilter)10 FilterBase (org.bedework.caldav.util.filter.FilterBase)8 AndFilter (org.bedework.caldav.util.filter.AndFilter)7 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)7 EntityTimeRangeFilter (org.bedework.caldav.util.filter.EntityTimeRangeFilter)6 TimeRangeFilter (org.bedework.caldav.util.filter.TimeRangeFilter)6 PresenceFilter (org.bedework.caldav.util.filter.PresenceFilter)5 PropertyFilter (org.bedework.caldav.util.filter.PropertyFilter)5 BwObjectFilter (org.bedework.calfacade.filter.BwObjectFilter)5 ObjectFilter (org.bedework.caldav.util.filter.ObjectFilter)4 BwCalendar (org.bedework.calfacade.BwCalendar)4 BwCategoryFilter (org.bedework.calfacade.filter.BwCategoryFilter)4 BwHrefFilter (org.bedework.calfacade.filter.BwHrefFilter)4 BwIcalPropertyInfoEntry (org.bedework.calfacade.ical.BwIcalPropertyInfo.BwIcalPropertyInfoEntry)4 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 TreeSet (java.util.TreeSet)2 TimeRange (org.bedework.caldav.util.TimeRange)2 EntityTypeFilter (org.bedework.caldav.util.filter.EntityTypeFilter)2 BwCategory (org.bedework.calfacade.BwCategory)2