@Override public List<UserFilter> findAvailableFilters(String projectName, String[] ids, String userName) { //where ID from provided array AND it's shared on project Query q = Query.query(where(ID).in(Arrays.asList(ids)) .andOperator(new Criteria() .orOperator( where(OWNER).is(userName), where(PROJECT).is(projectName), where(ENTRIES).elemMatch(where("projectId").is(projectName))))); return mongoTemplate.find(q, UserFilter.class); } }
@Override protected Criteria visit(ComparisonNode node) { ComparisonOperator operator = node.getOperator(); Collection<?> values = node.getValues().stream().map(normalizer).collect(Collectors.toList()); String field = node.getField().asKey(); if(ComparisonOperator.EQ.equals(operator)) { return where(field).is(single(values)); } else if(ComparisonOperator.NE.equals(operator)) { return where(field).ne(single(values)); } else if (ComparisonOperator.EX.equals(operator)) { return where(field).exists((Boolean)single(values)); } else if (ComparisonOperator.GT.equals(operator)) { return where(field).gt(single(values)); } else if (ComparisonOperator.LT.equals(operator)) { return where(field).lt(single(values)); } else if (ComparisonOperator.GTE.equals(operator)) { return where(field).gte(single(values)); } else if (ComparisonOperator.LTE.equals(operator)) { return where(field).lte(single(values)); } else if (ComparisonOperator.IN.equals(operator)) { return where(field).in(values); } else if (ComparisonOperator.NIN.equals(operator)) { return where(field).nin(values); } else if (ComparisonOperator.RE.equals(operator)) { return where(field).regex((String)single(values)); } else if (ComparisonOperator.SUB_CONDITION_ANY.equals(operator)) { return where(field).elemMatch(condition(node)); } throw new UnsupportedOperationException("This visitor does not support the operator " + operator + "."); }
private Criteria criteria(QueryOperator operator, String field, QueryLiteral<?> value) { Criteria criteria = new Criteria(field); switch (operator) { case $ALL: return criteria.all(((ListQueryLiteral) value).getLiterals()); case $EQ: return criteria.is(value.getLiteral()); case $GT: return criteria.gt(value.getLiteral()); case $GTE: return criteria.gte(value.getLiteral()); case $IN: return criteria.in(((ListQueryLiteral) value).getLiterals()); case $NIN: return criteria.nin(((ListQueryLiteral) value).getLiterals()); case $LT: return criteria.lt(value.getLiteral()); case $LTE: return criteria.lte(value.getLiteral()); case $NE: return criteria.ne(value.getLiteral()); case $LIKE: return criteria.regex((String) value.getLiteral(), "i"); // i means case insensitive case $ELEM_MATCH: return criteria.elemMatch(getCriteriaFromResourceQuery((ResourceQuery) value.getLiteral())); case $EXISTS: return criteria.exists((Boolean) value.getLiteral()); } return criteria; }