private Predicate createPredicate(PathBuilder<?> builder, PartTree tree, BeanWrapper bean){ //BeanWrapper paramWrapper = PropertyAccessorFactory.forBeanPropertyAccess(param); BooleanExpression base = null; for (OrPart node : tree) { //logger.debug("OrPart : "+node.getClass()); BooleanExpression criteria = null; for (Part part : node) { //logger.debug("Part "+part.getClass()); if(part.getProperty() != null){ BooleanExpression newCriteria = create(builder, part, bean); if(newCriteria != null){ //logger.debug("ok...."); criteria = criteria == null ? newCriteria : and(criteria, newCriteria, bean); } } } base = base == null ? criteria : or(base, criteria, bean); } //logger.debug("base "+base); return base; }
protected Object doExecute(Object[] parameters) { Class<?> domainClass = queryMethod.getEntityInformation().getJavaType(); int paramIndex = 0; PartTree tree = new PartTree(getQueryMethod().getName(), getQueryMethod().getEntityInformation().getJavaType()); System.out.println(tree); List<Filter> orFilters = new ArrayList<Filter>(); for (Iterator<OrPart> orPartIter = tree.iterator(); orPartIter.hasNext();) { OrPart orPart = orPartIter.next(); List<Filter> andFilters = new ArrayList<Filter>(); for (Iterator<Part> partIter = orPart.iterator(); partIter.hasNext();) { Part part = partIter.next(); PropertyPath propertyPath = part.getProperty(); String propName = propertyPath.getSegment(); Object propValue = parameters[paramIndex++]; FilterOperator operator = getFilterOperation(part); Filter filter = new Query.FilterPredicate(propName, operator, propValue); andFilters.add(filter); } if (andFilters.size() == 1) { orFilters.add(andFilters.get(0)); } else if (andFilters.size() >= 2){ orFilters.add(CompositeFilterOperator.and(andFilters)); } } com.googlecode.objectify.cmd.Query q = ofy().load().type(domainClass); if (orFilters.size() == 1) { q = q.filter(orFilters.get(0)); } else if (orFilters.size() >= 2){ q = q.filter(CompositeFilterOperator.or(orFilters)); } return q.list(); }
/** * Convert a {@link PartTree} into a where query alike to the one present in the * {@link Query}'s where property. */ public static String toIndexedQuery(final PartTree tree) { final StringBuilder result = new StringBuilder(); final Iterator<OrPart> orIt = tree.iterator(); while(orIt.hasNext()) { final OrPart orPart = orIt.next(); final Iterator<Part> partIt = orPart.iterator(); while(partIt.hasNext()) { final Part part = partIt.next(); result.append(" " + part.getProperty().getSegment() + " "); result.append(convertOperator(part.getType())); if(partIt.hasNext()) { result.append(" AND "); } } if(orIt.hasNext()) { result.append(" OR "); } } return StringUtil.removeExtraSpaces(result.toString()); }