Object left = extractValueFromExpression(relation.getFirstChild(), query);
Object right = extractValueFromExpression(relation.getSecondChild(), query);
if (relation.getOperator().getSelector() == ExpressionOperator.Equal) {
row.put(left, right);
} else {
DatabaseRecord nested = new DatabaseRecord();
if (relation.getOperator().getSelector() == ExpressionOperator.GreaterThan) {
nested.put("$gt", right);
} else if (relation.getOperator().getSelector() == ExpressionOperator.LessThan) {
nested.put("$lt", right);
} else if (relation.getOperator().getSelector() == ExpressionOperator.LessThanEqual) {
nested.put("$lte", right);
} else if (relation.getOperator().getSelector() == ExpressionOperator.GreaterThanEqual) {
nested.put("$gte", right);
} else if (relation.getOperator().getSelector() == ExpressionOperator.NotEqual) {
nested.put("$ne", right);
} else if (relation.getOperator().getSelector() == ExpressionOperator.In) {
nested.put("$in", right);
row.put(left, nested);
} else if (relation.getOperator().getSelector() == ExpressionOperator.NotIn) {
nested.put("$nin", right);
row.put(left, nested);
} else {
throw new EISException("Query too complex for Mongo translation, relation [" + expression + "] not supported in query: " + query);
}
row.put(left, nested);
}
} else if (expression.isLogicalExpression()) {
LogicalExpression logic = (LogicalExpression)expression;
DatabaseRecord first = new DatabaseRecord();
DatabaseRecord second = new DatabaseRecord();
appendExpressionToQueryRow(logic.getFirstChild(), first, query);
appendExpressionToQueryRow(logic.getSecondChild(), second, query);
List nested = new Vector();
nested.add(first);
nested.add(second);
if (logic.getOperator().getSelector() == ExpressionOperator.And) {
row.put("$and", nested);
} else if (logic.getOperator().getSelector() == ExpressionOperator.Or) {
row.put("$or", nested);
} else {
throw new EISException("Query too complex for Mongo translation, logic [" + expression + "] not supported in query: " + query);
}
} else if (expression.isFunctionExpression()) {
FunctionExpression function = (FunctionExpression)expression;
if (function.getOperator().getSelector() == ExpressionOperator.Like) {
Object left = extractValueFromExpression((Expression)function.getChildren().get(0), query);
Object right = extractValueFromExpression((Expression)function.getChildren().get(1), query);
if (!(right instanceof String)) {
throw new EISException("Query too complex for Mongo translation, like with [" + right + "] not supported in query: " + query);
}
String pattern = (String)right;
DatabaseRecord nested = new DatabaseRecord();
if (!this.isLikeRegex) {
pattern = Helper.convertLikeToRegex(pattern);
}
nested.put("$regex", pattern);
row.put(left, nested);
} else if (function.getOperator().getSelector() == ExpressionOperator.Not) {
DatabaseRecord nested = new DatabaseRecord();
appendExpressionToQueryRow((Expression)function.getChildren().get(0), nested, query);
row.put("$not", nested);
} else {
throw new EISException("Query too complex for Mongo translation, function [" + expression + "] not supported in query: " + query);
}