Search in sources :

Example 1 with AndFilter

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

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

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

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

the class Filters method makeWhere.

/* Generate a where clause for a query which selects the events for the
   * given filter.
   *
   * @param f         Filter element.
   */
private void makeWhere(final FilterBase f) throws CalFacadeException {
    if ((f instanceof AndFilter) || (f instanceof OrFilter)) {
        boolean itsAnd = (f instanceof AndFilter);
        qseg.append('(');
        boolean first = true;
        for (FilterBase flt : f.getChildren()) {
            if (!first) {
                if (itsAnd) {
                    qseg.append(" and ");
                } else {
                    qseg.append(" or ");
                }
            }
            makeWhere(flt);
            first = false;
        }
        qseg.append(")");
    }
    if (f instanceof BwHrefFilter) {
        // Special case this
        qseg.append('(');
        qseg.append(masterName);
        qseg.append(".");
        qseg.append("colPath");
        qseg.append("=:");
        parTerm();
        qseg.append(" and ");
        qseg.append(masterName);
        qseg.append(".");
        qseg.append("name");
        qseg.append("=:");
        parTerm();
        qseg.append(')');
        queryLimited = true;
    } else if (f instanceof PropertyFilter) {
        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 multi = pi.getMultiValued();
        boolean param = pi.getParam();
        if (param) {
            BwIcalPropertyInfoEntry parentPi = BwIcalPropertyInfo.getPinfo(pf.getParentPropertyIndex());
            fieldName = parentPi.getDbFieldName() + "." + fieldName;
        }
        if (multi) {
            if (f instanceof PresenceFilter) {
                PresenceFilter prf = (PresenceFilter) f;
                // qseg.append("(size(");
                qseg.append("((select count(*) from ");
                qseg.append(masterName);
                qseg.append(".");
                qseg.append(fieldName);
                if (pi.getPresenceField() != null) {
                    qseg.append(".");
                    qseg.append(pi.getPresenceField());
                }
                qseg.append(")");
                if (prf.getTestPresent()) {
                    qseg.append(">0)");
                } else {
                    qseg.append("=0)");
                }
            } else if (pf instanceof TimeRangeFilter) {
                String fld = "joined_" + pi.getDbFieldName();
                String subfld = "unknown";
                if (pi.getPindex() == PropertyInfoIndex.VALARM) {
                    subfld = "triggerTime";
                }
                doTimeRange((TimeRangeFilter) pf, false, fld, subfld);
            } else if (pf instanceof BwCategoryFilter) {
                BwCategory cat = ((BwCategoryFilter) pf).getEntity();
                if (cat.unsaved()) {
                    ((BwCategoryFilter) pf).setEntity(cb.getCategory(cat.getUid()));
                }
                qseg.append("(:");
                parTerm();
                if (f.getNot()) {
                    qseg.append(" not");
                }
                qseg.append(" in elements(");
                qseg.append(masterName);
                qseg.append(".");
                qseg.append(fieldName);
                qseg.append("))");
            } else if (pf instanceof BwObjectFilter) {
                // String fld = "joined_" + pi.getField();
                String subfld = "value";
                // if (pi.getPindex() == PropertyInfoIndex.CATEGORIES) {
                // subfld = "word.value";
                // }
                doObject(((BwObjectFilter) pf).getEntity(), masterName, fieldName, subfld, true);
            } else {
                qseg.append("(:");
                parTerm();
                if (f.getNot()) {
                    qseg.append(" not");
                }
                qseg.append(" in elements(");
                qseg.append(masterName);
                qseg.append(".");
                qseg.append(fieldName);
                qseg.append("))");
            }
        // not multi follow
        } else if (f instanceof PresenceFilter) {
            PresenceFilter prf = (PresenceFilter) f;
            qseg.append('(');
            qseg.append(masterName);
            qseg.append(".");
            qseg.append(fieldName);
            if (prf.getTestPresent()) {
                qseg.append(" is not null");
            } else {
                qseg.append(" is null");
            }
            qseg.append(")");
        } else if (pf instanceof EntityTimeRangeFilter) {
            doEntityTimeRange((EntityTimeRangeFilter) pf);
        } else if (pf instanceof TimeRangeFilter) {
            doTimeRange((TimeRangeFilter) pf, (pi.getFieldType().getName().equals(BwDateTime.class.getName())), masterName, fieldName);
        } else if (pf instanceof BwObjectFilter) {
            doObject(((BwObjectFilter) pf).getEntity(), masterName, fieldName, null, false);
        } 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) BwDateTime(org.bedework.calfacade.BwDateTime) 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) 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 5 with AndFilter

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

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