Search in sources :

Example 1 with ObjectFilter

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

the class BwObjectFilter method match.

/* ====================================================================
   *                   matching methods
   * ==================================================================== */
@Override
public boolean match(final Object o, final String userHref) throws WebdavException {
    ObjectFilter of = getEntity();
    Object ent = of.getEntity();
    boolean not = of.getNot();
    String val = null;
    Integer ival = null;
    if (ent instanceof BwCalendar) {
        val = ((BwCalendar) ent).getPath();
    } else if (ent instanceof String) {
        val = (String) ent;
    } else if (ent instanceof Integer) {
        ival = (Integer) ent;
    } else {
        throw new WebdavException("Unmatchable filter");
    }
    BwEvent ev = null;
    if (o instanceof BwEvent) {
        ev = (BwEvent) o;
    }
    if (of instanceof EntityTypeFilter) {
        if (ev == null) {
            return false;
        }
        if (not) {
            return ev.getEntityType() != ival;
        }
        return ev.getEntityType() == ival;
    }
    PropertyInfoIndex pii = of.getPropertyIndex();
    BwIcalPropertyInfoEntry pi = BwIcalPropertyInfo.getPinfo(pii);
    if (pi.getParam()) {
        pii = of.getParentPropertyIndex();
    }
    switch(pii) {
        case CLASS:
            if (ev.getClassification() == null) {
                return false;
            }
            return stringMatch(ev.getClassification(), val);
        case CREATED:
            return stringMatch(ev.getCreated(), val);
        case DESCRIPTION:
            for (BwLongString ls : ev.getDescriptions()) {
                if (stringMatch(ls.getValue(), val)) {
                    return true;
                }
            }
            return false;
        case DTSTAMP:
            return stringMatch(ev.getDtstamp(), val);
        case DTEND:
        /* Event only */
        case DUE:
            /* Todo only */
            return matchDateTime(pi, ev.getDtend(), val);
        case DTSTART:
            return matchDateTime(pi, ev.getDtstart(), val);
        case DURATION:
            return stringMatch(ev.getDuration(), val);
        case GEO:
            if (ev.getGeo() == null) {
                return false;
            }
            return stringMatch(ev.getGeo().toString(), val);
        case LAST_MODIFIED:
            return stringMatch(ev.getLastmod(), val);
        case LOCATION:
            if (ev.getLocation() == null) {
                return false;
            }
            return stringMatch(ev.getLocation().getAddress().getValue(), val);
        case ORGANIZER:
            if (ev.getOrganizer() == null) {
                return false;
            }
            return stringMatch(ev.getOrganizer().getOrganizerUri(), val);
        case PRIORITY:
            if (ev.getPriority() == null) {
                return false;
            }
            return stringMatch(String.valueOf(ev.getPriority()), val);
        case RECURRENCE_ID:
            if (ev.getRecurrenceId() == null) {
                return false;
            }
            return stringMatch(ev.getRecurrenceId(), val);
        case SEQUENCE:
            return stringMatch(String.valueOf(ev.getSequence()), val);
        case STATUS:
            if (ev.getStatus() == null) {
                return false;
            }
            return stringMatch(ev.getStatus(), val);
        case SUMMARY:
            for (BwString s : ev.getSummaries()) {
                if (stringMatch(s.getValue(), val)) {
                    return true;
                }
            }
            return false;
        case UID:
            return stringMatch(ev.getUid(), val);
        case URL:
            if (ev.getLink() == null) {
                return false;
            }
            return stringMatch(ev.getLink(), val);
        case TRANSP:
            try {
                if (ev.getPeruserTransparency(userHref) == null) {
                    return false;
                }
                return stringMatch(ev.getPeruserTransparency(userHref), val);
            } catch (Throwable t) {
                throw new WebdavException(t);
            }
        case COMPLETED:
            if (ev.getCompleted() == null) {
                return false;
            }
            return stringMatch(ev.getCompleted(), val);
        case PERCENT_COMPLETE:
            if (ev.getPercentComplete() == null) {
                return false;
            }
            return stringMatch(String.valueOf(ev.getPercentComplete()), val);
        case ATTACH:
            break;
        case ATTENDEE:
            break;
        case CATEGORIES:
            for (BwCategory cat : ev.getCategories()) {
                if (stringMatch(cat.getWordVal(), val)) {
                    return true;
                }
            }
            return false;
        case COMMENT:
            for (BwString s : ev.getComments()) {
                if (stringMatch(s.getValue(), val)) {
                    return true;
                }
            }
            return false;
        case CONTACT:
            for (BwContact c : ev.getContacts()) {
                if (stringMatch(c.getCn().getValue(), val)) {
                    return true;
                }
            }
            return false;
        case EXDATE:
            for (BwDateTime dt : ev.getExdates()) {
                if (stringMatch(dt.getDtval(), val)) {
                    return true;
                }
            }
            return false;
        case EXRULE:
            for (String s : ev.getExrules()) {
                if (stringMatch(s, val)) {
                    return true;
                }
            }
            return false;
        case REQUEST_STATUS:
            for (BwRequestStatus rs : ev.getRequestStatuses()) {
                if (stringMatch(rs.getCode(), val)) {
                    return true;
                }
            }
            return false;
        case RELATED_TO:
            if (ev.getRelatedTo() == null) {
                return false;
            }
            return stringMatch(ev.getRelatedTo().getValue(), val);
        case RESOURCES:
            for (BwString s : ev.getResources()) {
                if (stringMatch(s.getValue(), val)) {
                    return true;
                }
            }
            return false;
        case RDATE:
            for (BwDateTime dt : ev.getRdates()) {
                if (stringMatch(dt.getDtval(), val)) {
                    return true;
                }
            }
            return false;
        case RRULE:
            for (String s : ev.getRrules()) {
                if (stringMatch(s, val)) {
                    return true;
                }
            }
            return false;
        case FREEBUSY:
            break;
        case TZID:
            break;
        case TZNAME:
            break;
        case TZOFFSETFROM:
            break;
        case TZOFFSETTO:
            break;
        case TZURL:
            break;
        case ACTION:
            break;
        case REPEAT:
            break;
        case TRIGGER:
            break;
        case COLLECTION:
            return stringMatch(ev.getColPath(), val);
        case CREATOR:
            return stringMatch(ev.getCreatorHref(), val);
        case OWNER:
            return stringMatch(ev.getOwnerHref(), val);
        case ENTITY_TYPE:
            break;
    }
    return false;
}
Also used : BwDateTime(org.bedework.calfacade.BwDateTime) BwRequestStatus(org.bedework.calfacade.BwRequestStatus) WebdavException(org.bedework.webdav.servlet.shared.WebdavException) BwCategory(org.bedework.calfacade.BwCategory) BwEvent(org.bedework.calfacade.BwEvent) ObjectFilter(org.bedework.caldav.util.filter.ObjectFilter) BwString(org.bedework.calfacade.BwString) BwLongString(org.bedework.calfacade.BwLongString) BwCalendar(org.bedework.calfacade.BwCalendar) BwString(org.bedework.calfacade.BwString) BwContact(org.bedework.calfacade.BwContact) EntityTypeFilter(org.bedework.caldav.util.filter.EntityTypeFilter) BwLongString(org.bedework.calfacade.BwLongString) PropertyInfoIndex(org.bedework.util.calendar.PropertyIndex.PropertyInfoIndex) BwIcalPropertyInfoEntry(org.bedework.calfacade.ical.BwIcalPropertyInfo.BwIcalPropertyInfoEntry)

Example 2 with ObjectFilter

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

the class FilterBuilder method dump.

private void dump(final FilterBase f, final String curLine) {
    if (f instanceof OrFilter) {
        debug(curLine + "  OR ");
        Iterator<FilterBase> it = f.getChildren().iterator();
        dumpChildren(it, curLine);
        return;
    }
    if (f instanceof AndFilter) {
        debug(curLine + "  AND ");
        Iterator<FilterBase> it = f.getChildren().iterator();
        dumpChildren(it, curLine);
        return;
    }
    if (f instanceof ObjectFilter) {
        ObjectFilter of = (ObjectFilter) f;
        if (of.getEntity() instanceof BwCalendar) {
            final StringBuilder sb = new StringBuilder(curLine);
            sb.append(curLine);
            sb.append("  cal=");
            sb.append(((BwCalendar) of.getEntity()).getPath());
            debug(sb.toString());
        } else {
            debug(curLine + f.toString());
        }
    } else {
        debug(curLine + f.toString());
    }
}
Also used : AndFilter(org.bedework.caldav.util.filter.AndFilter) OrFilter(org.bedework.caldav.util.filter.OrFilter) ObjectFilter(org.bedework.caldav.util.filter.ObjectFilter) BwCalendar(org.bedework.calfacade.BwCalendar) FilterBase(org.bedework.caldav.util.filter.FilterBase)

Example 3 with ObjectFilter

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

the class ESQueryFilter method locationKeyFilter.

/**
 * Build a filter for a single location identified by the key with
 * the given name and value.
 *
 * @param name - of key field
 * @param val - expected full value
 * @return a filter builder
 */
public FilterBuilder locationKeyFilter(final String name, final String val) throws CalFacadeException {
    final ObjectFilter<String> kf = new ObjectFilter<>(null, PropertyInfoIndex.LOC_KEYS_FLD, null, name);
    kf.setEntity(val);
    kf.setExact(true);
    kf.setCaseless(false);
    final ObjectFilter<String> ef = new ObjectFilter<>(null, PropertyInfoIndex.ENTITY_TYPE, null, null);
    ef.setEntity(docTypeLocation);
    ef.setExact(true);
    ef.setCaseless(false);
    final AndFilter and = new AndFilter();
    and.addChild(ef);
    and.addChild(kf);
    return buildFilter(and);
}
Also used : AndFilter(org.bedework.caldav.util.filter.AndFilter) ObjectFilter(org.bedework.caldav.util.filter.ObjectFilter)

Example 4 with ObjectFilter

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

the class ESQueryFilter method makeFilter.

private FilterBuilder makeFilter(final FilterBase f) throws CalFacadeException {
    if (f == null) {
        return null;
    }
    if (f instanceof BooleanFilter) {
        final BooleanFilter bf = (BooleanFilter) f;
        if (!bf.getValue()) {
            return new MatchNone();
        } else {
            return new MatchAllFilterBuilder();
        }
    }
    if (f instanceof AndFilter) {
        final List<FilterBuilder> fbs = makeFilters(f.getChildren(), true);
        if (fbs.size() == 1) {
            return fbs.get(0);
        }
        final AndFilterBuilder afb = new AndFilterBuilder();
        afb.filterName(f.getName());
        for (final FilterBuilder fb : fbs) {
            if (fb instanceof TermOrTerms) {
                afb.add(makeFilter((TermOrTerms) fb));
            } else {
                afb.add(fb);
            }
        }
        return afb;
    }
    if (f instanceof OrFilter) {
        final List<FilterBuilder> fbs = makeFilters(f.getChildren(), false);
        if (fbs.size() == 1) {
            return fbs.get(0);
        }
        final OrFilterBuilder ofb = new OrFilterBuilder();
        ofb.filterName(f.getName());
        for (final FilterBuilder fb : fbs) {
            if (fb instanceof TermOrTerms) {
                ofb.add(((TermOrTerms) fb).makeFb());
            } else {
                ofb.add(fb);
            }
        }
        return ofb;
    }
    if (f instanceof BwViewFilter) {
        return doView((BwViewFilter) f);
    }
    if (f instanceof BwHrefFilter) {
        queryLimited = true;
        final TermFilterBuilder tfb = FilterBuilders.termFilter(hrefJname, ((BwHrefFilter) f).getHref());
        tfb.filterName(f.getName());
        return tfb;
    }
    if (!(f instanceof PropertyFilter)) {
        return null;
    }
    final PropertyFilter pf = (PropertyFilter) f;
    /*
    if (pf.getPropertyIndex() == PropertyInfoIndex.CATEGORY_PATH) {
      // Special case this one.
      return new TermOrTerms(PropertyInfoIndex.CATEGORY_PATH.getJname(),
                             ((ObjectFilter)pf).getEntity());
    }*/
    final BwIcalPropertyInfo.BwIcalPropertyInfoEntry pi = BwIcalPropertyInfo.getPinfo(pf.getPropertyIndex());
    if (pi == null) {
        throw new CalFacadeException("org.bedework.filters.unknownproperty", String.valueOf(pf.getPropertyIndex()));
    }
    final String fieldName;
    final PropertyInfoIndex pii = pf.getPropertyIndex();
    if (pf.getParentPropertyIndex() != null) {
        fieldName = makePropertyRef(pf.getParentPropertyIndex(), pii);
    } else {
        fieldName = getJname(pii);
    }
    if (f instanceof PresenceFilter) {
        final PresenceFilter prf = (PresenceFilter) f;
        if (prf.getTestPresent()) {
            return makeFilter(pf.getPropertyIndexes(), null, pf.getIntKey(), pf.getStrKey(), OperationType.presence, pf.getNot());
        }
        return makeFilter(pf.getPropertyIndexes(), null, pf.getIntKey(), pf.getStrKey(), OperationType.absence, pf.getNot());
    }
    if (pf instanceof TimeRangeFilter) {
        return makeFilter(pf.getPropertyIndexes(), ((TimeRangeFilter) pf).getEntity(), pf.getIntKey(), pf.getStrKey(), OperationType.timeRange, pf.getNot());
    }
    if (pf instanceof BwCollectionFilter) {
        if (!pf.getNot()) {
            queryLimited = true;
        }
        final BwCalendar col = ((BwCollectionFilter) pf).getEntity();
        return new TermOrTerms(colpathJname, col.getPath(), pf.getNot(), f.getName());
    }
    if (pf instanceof EntityTypeFilter) {
        final EntityTypeFilter etf = (EntityTypeFilter) pf;
        return new TermOrTerms(entityTypeJname, IcalDefs.entityTypeNames[etf.getEntity()], pf.getNot(), f.getName());
    }
    if (pf instanceof EntityTimeRangeFilter) {
        final EntityTimeRangeFilter etrf = (EntityTimeRangeFilter) pf;
        final TimeRange tr = etrf.getEntity();
        String start = null;
        String end = null;
        if (tr.getStart() != null) {
            start = tr.getStart().toString();
        }
        if (tr.getEnd() != null) {
            end = tr.getEnd().toString();
        }
        return addDateRangeFilter(null, etrf.getEntityType(), start, end);
    }
    if (pii == PropertyInfoIndex.COLLECTION) {
        if (!pf.getNot()) {
            queryLimited = true;
        }
    }
    if (pf instanceof ObjectFilter) {
        final OperationType op;
        if (((ObjectFilter) pf).getPrefixMatch()) {
            op = OperationType.prefix;
        } else {
            op = OperationType.compare;
        }
        return makeFilter(pf.getPropertyIndexes(), getValue((ObjectFilter) pf), pf.getIntKey(), pf.getStrKey(), op, pf.getNot());
    }
    return null;
/*
    if (f instanceof TimeRangeFilter) {
      addThisJoin(pi);
      return;
    }

    if (f instanceof BwObjectFilter) {
      addThisJoin(pi);
      return;
    }*/
}
Also used : BooleanFilter(org.bedework.caldav.util.filter.BooleanFilter) PresenceFilter(org.bedework.caldav.util.filter.PresenceFilter) EntityTimeRangeFilter(org.bedework.caldav.util.filter.EntityTimeRangeFilter) TimeRangeFilter(org.bedework.caldav.util.filter.TimeRangeFilter) BwIcalPropertyInfo(org.bedework.calfacade.ical.BwIcalPropertyInfo) EntityTypeFilter(org.bedework.caldav.util.filter.EntityTypeFilter) BwCollectionFilter(org.bedework.calfacade.filter.BwCollectionFilter) AndFilterBuilder(org.elasticsearch.index.query.AndFilterBuilder) TermFilterBuilder(org.elasticsearch.index.query.TermFilterBuilder) FilterBuilder(org.elasticsearch.index.query.FilterBuilder) OrFilterBuilder(org.elasticsearch.index.query.OrFilterBuilder) RangeFilterBuilder(org.elasticsearch.index.query.RangeFilterBuilder) BoolFilterBuilder(org.elasticsearch.index.query.BoolFilterBuilder) QueryFilterBuilder(org.elasticsearch.index.query.QueryFilterBuilder) NotFilterBuilder(org.elasticsearch.index.query.NotFilterBuilder) NestedFilterBuilder(org.elasticsearch.index.query.NestedFilterBuilder) MatchAllFilterBuilder(org.elasticsearch.index.query.MatchAllFilterBuilder) TermsFilterBuilder(org.elasticsearch.index.query.TermsFilterBuilder) AndFilterBuilder(org.elasticsearch.index.query.AndFilterBuilder) BwHrefFilter(org.bedework.calfacade.filter.BwHrefFilter) OrFilter(org.bedework.caldav.util.filter.OrFilter) BwCalendar(org.bedework.calfacade.BwCalendar) ObjectFilter(org.bedework.caldav.util.filter.ObjectFilter) EntityTimeRangeFilter(org.bedework.caldav.util.filter.EntityTimeRangeFilter) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) OrFilterBuilder(org.elasticsearch.index.query.OrFilterBuilder) AndFilter(org.bedework.caldav.util.filter.AndFilter) BwViewFilter(org.bedework.calfacade.filter.BwViewFilter) TimeRange(org.bedework.caldav.util.TimeRange) MatchAllFilterBuilder(org.elasticsearch.index.query.MatchAllFilterBuilder) PropertyInfoIndex(org.bedework.util.calendar.PropertyIndex.PropertyInfoIndex) TermFilterBuilder(org.elasticsearch.index.query.TermFilterBuilder) PropertyFilter(org.bedework.caldav.util.filter.PropertyFilter) BwIcalPropertyInfoEntry(org.bedework.calfacade.ical.BwIcalPropertyInfo.BwIcalPropertyInfoEntry)

Example 5 with ObjectFilter

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

Aggregations

ObjectFilter (org.bedework.caldav.util.filter.ObjectFilter)8 AndFilter (org.bedework.caldav.util.filter.AndFilter)5 FilterBase (org.bedework.caldav.util.filter.FilterBase)4 OrFilter (org.bedework.caldav.util.filter.OrFilter)4 BwCalendar (org.bedework.calfacade.BwCalendar)4 ArrayList (java.util.ArrayList)3 EntityTimeRangeFilter (org.bedework.caldav.util.filter.EntityTimeRangeFilter)3 EntityTypeFilter (org.bedework.caldav.util.filter.EntityTypeFilter)3 PresenceFilter (org.bedework.caldav.util.filter.PresenceFilter)3 TimeRangeFilter (org.bedework.caldav.util.filter.TimeRangeFilter)3 BwCategory (org.bedework.calfacade.BwCategory)3 PropertyInfoIndex (org.bedework.util.calendar.PropertyIndex.PropertyInfoIndex)3 Collection (java.util.Collection)2 TimeRange (org.bedework.caldav.util.TimeRange)2 PropertyFilter (org.bedework.caldav.util.filter.PropertyFilter)2 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)2 BwCategoryFilter (org.bedework.calfacade.filter.BwCategoryFilter)2 BwHrefFilter (org.bedework.calfacade.filter.BwHrefFilter)2 BwObjectFilter (org.bedework.calfacade.filter.BwObjectFilter)2 BwIcalPropertyInfo (org.bedework.calfacade.ical.BwIcalPropertyInfo)2