use of org.bedework.calfacade.filter.BwHrefFilter 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;
}*/
}
use of org.bedework.calfacade.filter.BwHrefFilter 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));
}
}
}
use of org.bedework.calfacade.filter.BwHrefFilter 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;
}
}
use of org.bedework.calfacade.filter.BwHrefFilter 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;
}
}
Aggregations