use of org.mongodb.morphia.query.Criteria in project copper-cms by PogeyanOSS.
the class MongoExpressionVisitor method visitBinary.
@Override
public Object visitBinary(BinaryExpression binaryExpression, BinaryOperator operator, Object leftSide, Object rightSide) {
if (leftSide instanceof BinaryExpression) {
// needs brackets to show the higher priority
if (BinaryOperator.AND.equals(((BinaryExpression) binaryExpression).getOperator()) || BinaryOperator.OR.equals(((BinaryExpression) binaryExpression).getOperator())) {
}
} else if (leftSide instanceof PropertyExpression) {
PropertyExpression leftOp = (PropertyExpression) leftSide;
String rightSideValue = null;
if (rightSide instanceof PropertyExpression) {
PropertyExpression rightOp = (PropertyExpression) rightSide;
rightSideValue = rightOp.getUriLiteral();
} else if (rightSide instanceof LiteralExpression) {
LiteralExpression rightOp = (LiteralExpression) rightSide;
rightSideValue = rightOp.getUriLiteral();
} else if (rightSide instanceof String) {
rightSideValue = rightSide.toString();
}
switch(operator) {
case EQ:
return this.query.criteria(getQueryName(leftOp.getUriLiteral())).equal(getStringObjectValue(rightSideValue));
case NE:
return this.query.criteria(getQueryName(leftOp.getUriLiteral())).notEqual(getStringObjectValue(rightSideValue));
case GE:
return this.query.criteria(getQueryName(leftOp.getUriLiteral())).greaterThanOrEq(getNumberObjectValue(rightSideValue));
case GT:
return this.query.criteria(getQueryName(leftOp.getUriLiteral())).greaterThan(getNumberObjectValue(rightSideValue));
case LE:
return this.query.criteria(getQueryName(leftOp.getUriLiteral())).lessThanOrEq(getNumberObjectValue(rightSideValue));
case LT:
return this.query.criteria(getQueryName(leftOp.getUriLiteral())).lessThan(getNumberObjectValue(rightSideValue));
default:
// Other operators are not supported for SQL Statements
throw new UnsupportedOperationException("Unsupported operator: " + operator.toUriLiteral());
}
} else {
ArrayList<Criteria> operands = new ArrayList<>();
if (leftSide.getClass() == CriteriaContainerImpl.class) {
operands.add((Criteria) leftSide);
}
if (rightSide.getClass() == CriteriaContainerImpl.class) {
operands.add((Criteria) rightSide);
}
switch(operator) {
case OR:
this.query.or(operands.toArray(new Criteria[operands.size()]));
break;
case AND:
this.query.and(operands.toArray(new Criteria[operands.size()]));
break;
default:
// Other operators are not supported for SQL Statements
throw new UnsupportedOperationException("Unsupported operator: " + operator.toUriLiteral());
}
}
// return the binary statement
return this.query;
}
Aggregations