Search in sources :

Example 1 with BwCategoryFilter

use of org.bedework.calfacade.filter.BwCategoryFilter 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 2 with BwCategoryFilter

use of org.bedework.calfacade.filter.BwCategoryFilter 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 3 with BwCategoryFilter

use of org.bedework.calfacade.filter.BwCategoryFilter 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 4 with BwCategoryFilter

use of org.bedework.calfacade.filter.BwCategoryFilter 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)

Aggregations

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