///////////////////////////////////////////////////////////////////////////////////////// // JPQL ///////////////////////////////////////////////////////////////////////////////////////// protected String _jpqlQueryFrom(final BooleanQueryClause qryClause) { if (qryClause == null || CollectionUtils.isNullOrEmpty(qryClause.getClauses())) { log.warn("A filter with NO filter parameters was received... al records will be returned"); return null; } String dbFieldId = qryClause.getFieldId().asString(); StringBuilder outJPQL = new StringBuilder(); outJPQL.append("("); Set<QualifiedQueryClause<? extends QueryClause>> clauses = qryClause.getClauses(); QueryClauseOccur prevClauseOccur = null; for (Iterator<QualifiedQueryClause<? extends QueryClause>> clauseIt = clauses.iterator(); clauseIt.hasNext(); ) { QualifiedQueryClause<? extends QueryClause> clause = clauseIt.next(); String jpqlQuery = _jpqlQueryFrom(clause.getClause()); String jpqlJoin = _jpqlJoinFor(clause.getOccur()); if (jpqlQuery != null) { if (prevClauseOccur != null) outJPQL.append(jpqlJoin); if (clauseIt.hasNext()) prevClauseOccur = clause.getOccur(); outJPQL.append("("); outJPQL.append(jpqlQuery); // The clause outJPQL.append(")"); } else { log.error("A null lucene query was returned for field {}", dbFieldId); } } outJPQL.append(")"); return outJPQL.toString(); }
///////////////////////////////////////////////////////////////////////////////////////// // QUERY ///////////////////////////////////////////////////////////////////////////////////////// protected void _setJPAQueryParams(final Query qry, final BooleanQueryClause qryClause) { Set<QualifiedQueryClause<? extends QueryClause>> clauses = qryClause.getClauses(); for (Iterator<QualifiedQueryClause<? extends QueryClause>> clauseIt = clauses.iterator(); clauseIt.hasNext(); ) { QueryClause clause = clauseIt.next().getClause(); String dbFieldId = clause.getFieldId().asString(); if (clause instanceof BooleanQueryClause) { BooleanQueryClause boolQry = (BooleanQueryClause) clause; _setJPAQueryParams(qry, boolQry); // recurse! } else if (clause instanceof EqualsQueryClause<?>) { EqualsQueryClause<?> eqQry = (EqualsQueryClause<?>) clause; qry.setParameter(dbFieldId, eqQry.getValue().toString()); } else if (clause instanceof ContainsTextQueryClause) { ContainsTextQueryClause containsTxtClause = (ContainsTextQueryClause) clause; qry.setParameter(dbFieldId, containsTxtClause.getText()); } else if (clause instanceof RangeQueryClause<?>) { RangeQueryClause<?> rangeQry = (RangeQueryClause<?>) clause; if (rangeQry.getRange().hasLowerBound() && rangeQry.getRange().hasUpperBound()) { qry.setParameter(dbFieldId + "Start", rangeQry.getRange().lowerEndpoint()); qry.setParameter(dbFieldId + "End", rangeQry.getRange().lowerEndpoint()); } else if (rangeQry.getRange().hasLowerBound()) { qry.setParameter(dbFieldId, rangeQry.getRange().lowerEndpoint()); } else if (rangeQry.getRange().hasUpperBound()) { qry.setParameter(dbFieldId, rangeQry.getRange().upperEndpoint()); } } else if (clause instanceof ContainedInQueryClause<?>) { ContainedInQueryClause<?> containedInQry = (ContainedInQueryClause<?>) clause; Collection<?> spectrum = Lists.newArrayList(containedInQry.getSpectrum()); qry.setParameter(dbFieldId, spectrum); } } }