Search in sources :

Example 11 with FilterBase

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

the class Filters method reconstruct.

/* Return null if there are no terms to test for - otherwise return the
   * modified filter.
   *
   * Mostly we replace ObjectFilter with its wrapped form.
   */
private FilterBase reconstruct(final FilterBase f, final boolean forOverrides) throws CalFacadeException {
    if (f == null) {
        return null;
    }
    if ((f instanceof AndFilter) || (f instanceof OrFilter)) {
        boolean itsAnd = (f instanceof AndFilter);
        List<FilterBase> fs = new ArrayList<FilterBase>();
        for (FilterBase flt : f.getChildren()) {
            FilterBase chf = reconstruct(flt, forOverrides);
            if (chf != null) {
                fs.add(chf);
            }
        }
        if (fs.size() == 0) {
            return null;
        }
        if (fs.size() == 1) {
            return fs.get(0);
        }
        FilterBase res;
        if (itsAnd) {
            res = new AndFilter();
        } else {
            res = new OrFilter();
        }
        for (FilterBase flt : fs) {
            res.addChild(flt);
        }
        return res;
    }
    if (!(f instanceof PropertyFilter)) {
        throw new CalFacadeException("org.bedework.filters.unknownfilter", String.valueOf(f));
    }
    PropertyFilter pf = (PropertyFilter) f;
    if (pf.getPropertyIndex() == PropertyInfoIndex.HREF) {
        // Special case this
        return f;
    }
    if (BwIcalPropertyInfo.getPinfo(pf.getPropertyIndex()) == null) {
        throw new CalFacadeException("org.bedework.filters.unknownproperty", String.valueOf(pf.getPropertyIndex()));
    }
    if (f instanceof PresenceFilter) {
        if (!forOverrides) {
            return f;
        }
        if ((pf.getPropertyIndex() == PropertyInfoIndex.COLLECTION) || (pf.getPropertyIndex() == PropertyInfoIndex.DTEND) || (pf.getPropertyIndex() == PropertyInfoIndex.DTSTART) || (pf.getPropertyIndex() == PropertyInfoIndex.DURATION)) {
            return f;
        }
        termsDropped = true;
        return null;
    } else if (pf instanceof EntityTimeRangeFilter) {
        return f;
    } else if (pf instanceof TimeRangeFilter) {
        if (!forOverrides) {
            return f;
        }
        termsDropped = true;
        return null;
    } else if (pf instanceof BwCategoryFilter) {
        if (!forOverrides) {
            return f;
        }
        termsDropped = true;
        return null;
    } else if (pf instanceof ObjectFilter) {
        if (!forOverrides) {
            if (pf instanceof EntityTypeFilter) {
                EntityTypeFilter etf = (EntityTypeFilter) pf;
                if ((etf.getEntity() == IcalDefs.entityTypeVavailability) || (etf.getEntity() == IcalDefs.entityTypeVavailability)) {
                    // Ensure we get or exclude both
                    boolean not = etf.getNot();
                    FilterBase both;
                    if (not) {
                        both = new AndFilter();
                    } else {
                        both = new OrFilter();
                    }
                    try {
                        both.addChild(EntityTypeFilter.makeEntityTypeFilter(null, "vavailability", false));
                        both.addChild(EntityTypeFilter.makeEntityTypeFilter(null, "available", false));
                    } catch (Throwable t) {
                        throw new CalFacadeException(t);
                    }
                    return both;
                }
            }
            return new BwObjectFilter(null, (ObjectFilter) pf);
        }
        if (pf.getPropertyIndex() != PropertyInfoIndex.COLLECTION) {
            termsDropped = true;
            return null;
        }
        return new BwObjectFilter(null, (ObjectFilter) pf);
    } else {
        /* We assume we can't handle this one as a query.
       */
        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) ArrayList(java.util.ArrayList) BwCategoryFilter(org.bedework.calfacade.filter.BwCategoryFilter) OrFilter(org.bedework.caldav.util.filter.OrFilter) BwObjectFilter(org.bedework.calfacade.filter.BwObjectFilter) ObjectFilter(org.bedework.caldav.util.filter.ObjectFilter) EntityTimeRangeFilter(org.bedework.caldav.util.filter.EntityTimeRangeFilter) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) EntityTypeFilter(org.bedework.caldav.util.filter.EntityTypeFilter) AndFilter(org.bedework.caldav.util.filter.AndFilter) PropertyFilter(org.bedework.caldav.util.filter.PropertyFilter) FilterBase(org.bedework.caldav.util.filter.FilterBase) BwObjectFilter(org.bedework.calfacade.filter.BwObjectFilter)

Example 12 with FilterBase

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

the class Filters method addJoins.

/*
  private String makeUtcformat(String dt) {
    int len = dt.length();

    if (len == 16) {
      return dt;
    }

    if (len == 15) {
      return dt + "Z";
    }

    if (len == 8) {
      return dt + "T000000Z";
    }

    throw new RuntimeException("Bad date " + dt);
  }*/
private void addJoins(final FilterBase f) throws CalFacadeException {
    if ((f instanceof AndFilter) || (f instanceof OrFilter)) {
        for (FilterBase flt : f.getChildren()) {
            addJoins(flt);
        }
        return;
    }
    if (!(f instanceof PropertyFilter)) {
        return;
    }
    if (f instanceof PresenceFilter) {
        return;
    }
    if (f instanceof BwHrefFilter) {
        return;
    }
    PropertyFilter pf = (PropertyFilter) f;
    BwIcalPropertyInfoEntry pi = BwIcalPropertyInfo.getPinfo(pf.getPropertyIndex());
    if (pi == null) {
        throw new CalFacadeException("org.bedework.filters.unknownproperty", String.valueOf(pf.getPropertyIndex()));
    }
    if (!pi.getMultiValued()) {
        return;
    }
    if (f instanceof TimeRangeFilter) {
        addThisJoin(pi);
        return;
    }
    if (f instanceof BwObjectFilter) {
        addThisJoin(pi);
        return;
    }
}
Also used : PresenceFilter(org.bedework.caldav.util.filter.PresenceFilter) AndFilter(org.bedework.caldav.util.filter.AndFilter) BwIcalPropertyInfoEntry(org.bedework.calfacade.ical.BwIcalPropertyInfo.BwIcalPropertyInfoEntry) BwHrefFilter(org.bedework.calfacade.filter.BwHrefFilter) EntityTimeRangeFilter(org.bedework.caldav.util.filter.EntityTimeRangeFilter) TimeRangeFilter(org.bedework.caldav.util.filter.TimeRangeFilter) PropertyFilter(org.bedework.caldav.util.filter.PropertyFilter) OrFilter(org.bedework.caldav.util.filter.OrFilter) FilterBase(org.bedework.caldav.util.filter.FilterBase) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) BwObjectFilter(org.bedework.calfacade.filter.BwObjectFilter)

Example 13 with FilterBase

use of org.bedework.caldav.util.filter.FilterBase 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 14 with FilterBase

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

the class ProcessRealias method realiasEvents.

private boolean realiasEvents() throws Throwable {
    boolean add = false;
    boolean remove = false;
    BwXproperty xp = null;
    /* Expect from (possibly quoted)
     *        to  (possibly quoted)
     */
    try {
        open();
        final String fromHref = getAliasPath();
        if (fromHref == null) {
            return false;
        }
        final String wd = word();
        if (wd == null) {
            return false;
        }
        if ("remove".equals(wd)) {
            remove = true;
        } else {
            if ("add".equals(wd)) {
                add = true;
            } else if ("replace".equals(wd)) {
                remove = true;
            } else {
                error("Expect add | remove | replace");
                return false;
            }
            final BwCalendar col = getCal();
            if (col == null) {
                return false;
            }
            /* At the moment an alias is represented by an x-property with 
           the form:
           X-BEDEWORK-ALIAS; \
             X-BEDEWORK-PARAM-DISPLAYNAME=Jobs;\
             X-BEDEWORK-PARAM-PATH=/public/aliases/Browse By Topic/Jobs;\
             X-BEDEWORK-PARAM-ALIASPATH=/public/cals/MainCal:\
             /user/agrp_calsuite-MainCampus/Browse By Topic/Jobs"
        
           That is - it appears the displayname comes from the top level
           the path is what it points to
           the aliaspath is the path of the final target 
           the value is the path of the alias itself.
         */
            final BwCalendar aliasTarget = getAliasTarget(col);
            xp = new BwXproperty();
            xp.setName("X-BEDEWORK-ALIAS");
            xp.setPars("X-BEDEWORK-PARAM-DISPLAYNAME=" + col.getName() + ";X-BEDEWORK-PARAM-PATH=" + col.getAliasUri().substring(BwCalendar.internalAliasUriPrefix.length()) + ";X-BEDEWORK-PARAM-ALIASPATH=" + aliasTarget.getPath());
            xp.setValue(col.getPath());
        }
        final FilterBase fltr = parseQuery("topical_area=\"\t" + fromHref + "\"");
        if (fltr == null) {
            return false;
        }
        close();
        /* Now we need to process the stuff in batches */
        open();
        final BwIndexer idx = getIndexer();
        final SearchResult sr = idx.search(null, false, fltr, null, null, null, null, batchSize, includeDeleted, RecurringRetrievalMode.entityOnly);
        if (sr.getFound() == 0) {
            warn("No events found");
            return false;
        }
        for (; ; ) {
            final List<SearchResultEntry> sres = idx.getSearchResult(sr, BwIndexer.Position.next, PrivilegeDefs.privAny);
            if (Util.isEmpty(sres)) {
                break;
            }
            int updated = 0;
            for (final SearchResultEntry sre : sres) {
                final Object o = sre.getEntity();
                if (!(o instanceof EventInfo)) {
                    warn("Unhandled entity " + o.getClass());
                    continue;
                }
                EventInfo ei = (EventInfo) o;
                /* Fetch the persistent version
           */
                final String colPath = ei.getEvent().getColPath();
                final String name = ei.getEvent().getName();
                ei = getEvents().get(colPath, name);
                if (ei == null) {
                    warn("Unable to retrieve persistent copy of " + colPath + " " + name);
                    continue;
                }
                updated += doRealias(ei, fromHref, xp, add, remove);
                if ((updated % 10) == 0) {
                    info("done " + updated);
                }
            }
            info("Total updated: " + updated);
        }
        return true;
    } finally {
        close();
    }
}
Also used : EventInfo(org.bedework.calfacade.svc.EventInfo) BwXproperty(org.bedework.calfacade.BwXproperty) BwIndexer(org.bedework.calfacade.indexing.BwIndexer) SearchResult(org.bedework.calfacade.indexing.SearchResult) BwCalendar(org.bedework.calfacade.BwCalendar) FilterBase(org.bedework.caldav.util.filter.FilterBase) SearchResultEntry(org.bedework.calfacade.indexing.SearchResultEntry)

Example 15 with FilterBase

use of org.bedework.caldav.util.filter.FilterBase 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)

Aggregations

FilterBase (org.bedework.caldav.util.filter.FilterBase)25 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)9 ArrayList (java.util.ArrayList)8 OrFilter (org.bedework.caldav.util.filter.OrFilter)8 BwCalendar (org.bedework.calfacade.BwCalendar)8 AndFilter (org.bedework.caldav.util.filter.AndFilter)7 EntityTimeRangeFilter (org.bedework.caldav.util.filter.EntityTimeRangeFilter)5 PresenceFilter (org.bedework.caldav.util.filter.PresenceFilter)5 TimeRangeFilter (org.bedework.caldav.util.filter.TimeRangeFilter)5 BwObjectFilter (org.bedework.calfacade.filter.BwObjectFilter)5 ObjectFilter (org.bedework.caldav.util.filter.ObjectFilter)4 PropertyFilter (org.bedework.caldav.util.filter.PropertyFilter)4 BwCategoryFilter (org.bedework.calfacade.filter.BwCategoryFilter)4 EventInfo (org.bedework.calfacade.svc.EventInfo)4 TreeSet (java.util.TreeSet)3 BwCategory (org.bedework.calfacade.BwCategory)3 BwEvent (org.bedework.calfacade.BwEvent)3 BwHrefFilter (org.bedework.calfacade.filter.BwHrefFilter)3 BwIcalPropertyInfoEntry (org.bedework.calfacade.ical.BwIcalPropertyInfo.BwIcalPropertyInfoEntry)3 ToString (org.bedework.util.misc.ToString)3