Refine search
private Query toTwoHandSidedQuery(Operation<?> operation, Occur occur, QueryMetadata metadata) { Query lhs = toQuery(operation.getArg(0), metadata); Query rhs = toQuery(operation.getArg(1), metadata); BooleanQuery bq = new BooleanQuery(); bq.add(createBooleanClause(lhs, occur)); bq.add(createBooleanClause(rhs, occur)); return bq; }
private Path<?> getPath(Expression<?> leftHandSide) { if (leftHandSide instanceof Path<?>) { return (Path<?>) leftHandSide; } else if (leftHandSide instanceof Operation<?>) { Operation<?> operation = (Operation<?>) leftHandSide; if (operation.getOperator() == Ops.LOWER || operation.getOperator() == Ops.UPPER) { return (Path<?>) operation.getArg(0); } } throw new IllegalArgumentException("Unable to transform " + leftHandSide + " to path"); }
@Override public Void visit(Operation<?> expr, Void context) { visitOperation(expr.getType(), expr.getOperator(), expr.getArgs()); return null; }
@Override public Set<Expression<?>> visit(Operation<?> expr, Set<Expression<?>> known) { if (expr.getOperator() == Ops.ALIAS) { known = add(known, expr.getArg(1)); } for (Expression<?> arg : expr.getArgs()) { known = arg.accept(this, known); } return known; }
private Query toQuery(Operation<?> operation, QueryMetadata metadata) { Operator op = operation.getOperator(); if (op == Ops.OR) { return toTwoHandSidedQuery(operation, Occur.SHOULD, metadata); return toTwoHandSidedQuery(operation, Occur.MUST, metadata); } else if (op == Ops.NOT) { BooleanQuery bq = new BooleanQuery(); bq.add(new BooleanClause(toQuery(operation.getArg(0), metadata), Occur.MUST_NOT)); bq.add(new BooleanClause(new MatchAllDocsQuery(), Occur.MUST)); return bq; } else if (op == Ops.LIKE) { } else if (op == LuceneOps.LUCENE_QUERY) { @SuppressWarnings("unchecked") //This is the expected type Query rv = ((Constant<Query>) operation.getArg(0)).getConstant(); return rv;
protected Object asDBValue(Operation<?> expr, int index) { return expr.getArg(index).accept(this, null); }
@SuppressWarnings("rawtypes") @Override public Expression<?> visit(Operation<?> expr, Context context) { Expression<?>[] args = new Expression<?>[expr.getArgs().size()]; for (int i = 0; i < args.length; i++) { Context c = new Context(); args[i] = expr.getArg(i).accept(this, c); context.add(c); } if (context.replace) { if (expr.getType().equals(Boolean.class)) { Predicate predicate = ExpressionUtils.predicate(expr.getOperator(), args); return !context.paths.isEmpty() ? exists(context, predicate) : predicate; } else { return ExpressionUtils.operation(expr.getType(), expr.getOperator(), args); } } else { return expr; } }
@Override public Integer visit(Operation<?> expr, Void context) { int result = expr.getOperator().name().hashCode(); return 31 * result + expr.getArgs().hashCode(); }
@SuppressWarnings("unchecked") @Test public void various() { Expression[] args = new Expression[]{new StringPath("x"), new StringPath("y")}; List<Operation<?>> operations = new ArrayList<Operation<?>>(); // paths.add(new ArrayOperation(String[].class, "p")); operations.add(new BooleanOperation(Ops.EQ, args)); operations.add(new ComparableOperation(String.class, Ops.SUBSTR_1ARG, args)); operations.add(new DateOperation(Date.class, Ops.DateTimeOps.CURRENT_DATE, args)); operations.add(new DateTimeOperation(Date.class,Ops.DateTimeOps.CURRENT_TIMESTAMP, args)); operations.add(new EnumOperation(ExampleEnum.class,Ops.ALIAS, args)); operations.add(new NumberOperation(Integer.class,Ops.ADD, args)); operations.add(new SimpleOperation(String.class,Ops.TRIM, args)); operations.add(new StringOperation(Ops.CONCAT, args)); operations.add(new TimeOperation(Time.class,Ops.DateTimeOps.CURRENT_TIME, args)); for (Operation<?> operation : operations) { Operation<?> other = ExpressionUtils.operation(operation.getType(), operation.getOperator(), ImmutableList.copyOf(operation.getArgs())); assertEquals(operation.toString(), operation.accept(ToStringVisitor.DEFAULT, Templates.DEFAULT)); assertEquals(operation.hashCode(), other.hashCode()); assertEquals(operation, other); assertNotNull(operation.getOperator()); assertNotNull(operation.getArgs()); assertNotNull(operation.getType()); } }
} else if (je.getTarget() instanceof Operation) { Operation<?> op = (Operation) je.getTarget(); if (op.getOperator() == Ops.ALIAS) { boolean treat = false; if (Collection.class.isAssignableFrom(op.getArg(0).getType())) { if (op.getArg(0) instanceof CollectionExpression) { Class<?> par = ((CollectionExpression) op.getArg(0)).getParameter(0); treat = !par.equals(op.getArg(1).getType()); } else if (Map.class.isAssignableFrom(op.getArg(0).getType())) { if (op.getArg(0) instanceof MapExpression) { Class<?> par = ((MapExpression) op.getArg(0)).getParameter(1); treat = !par.equals(op.getArg(1).getType()); treat = !op.getArg(0).getType().equals(op.getArg(1).getType()); Expression<?> entityName = ConstantImpl.create(getEntityName(op.getArg(1).getType())); Expression<?> t = ExpressionUtils.operation(op.getType(), JPQLOps.TREAT, op.getArg(0), entityName); op = ExpressionUtils.operation(op.getType(), Ops.ALIAS, t, op.getArg(1));
private void verifyArguments(Operation<?> operation) { List<Expression<?>> arguments = operation.getArgs(); for (int i = 1; i < arguments.size(); ++i) { if (!(arguments.get(i) instanceof Constant<?>) && !(arguments.get(i) instanceof ParamExpression<?>) && !(arguments.get(i) instanceof PhraseElement) && !(arguments.get(i) instanceof TermElement)) { throw new IllegalArgumentException("operand was of unsupported type " + arguments.get(i).getClass().getName()); } } }
final Expression<?> expr = (Expression<?>) rv; if (precedence > -1 && expr instanceof Operation) { Operator op = ((Operation<?>) expr).getOperator(); int opPrecedence = templates.getPrecedence(op); if (precedence < opPrecedence) {
class SimpleHandler implements OperationHandler { private OperationHandler next; public void handle(Operation op) { if (op.getType() == OperationType.SOMEYOURTYPE) { //do some stuff } else { next.handle(op); } } }
public class ProcessOperationService implements IOperationVisitor { public void processOperation(Operation operation) { operation.accept(this); } public void visit (OperationActivation visited) { // Operation Activation specific implementation } public void visit (OperationPayment visited) { // OperationPayment specific implementation } public void visit ((OperationSendEmail visited) { // (Operation SendEmail specific implementation } }
private Query toQuery(Operation<?> operation, QueryMetadata metadata) { Operator op = operation.getOperator(); if (op == Ops.OR) { return toTwoHandSidedQuery(operation, Occur.SHOULD, metadata); return toTwoHandSidedQuery(operation, Occur.MUST, metadata); } else if (op == Ops.NOT) { BooleanQuery bq = new BooleanQuery(); bq.add(new BooleanClause(toQuery(operation.getArg(0), metadata), Occur.MUST_NOT)); bq.add(new BooleanClause(new MatchAllDocsQuery(), Occur.MUST)); return bq; } else if (op == Ops.LIKE) { } else if (op == LuceneOps.LUCENE_QUERY) { @SuppressWarnings("unchecked") //this is the expected type Constant<Query> expectedConstant = (Constant<Query>) operation.getArg(0); return expectedConstant.getConstant();
private String regexValue(Operation<?> expr, int index) { return Pattern.quote(expr.getArg(index).accept(this, null).toString()); }
if (e instanceof Operation) { Operation<?> op = (Operation<?>) e; if (op.getArg(0) instanceof Operation) { usedOperators.add(((Operation<?>) op.getArg(0)).getOperator()); } else if (op.getArgs().size() > 1 && op.getArg(1) instanceof Operation) { usedOperators.add(((Operation<?>) op.getArg(1)).getOperator()); usedOperators.add(op.getOperator());
@SuppressWarnings("rawtypes") @Override public Expression<?> visit(Operation<?> expr, Context context) { Expression<?>[] args = new Expression<?>[expr.getArgs().size()]; for (int i = 0; i < args.length; i++) { Context c = new Context(); args[i] = expr.getArg(i).accept(this, c); context.add(c); } if (context.replace) { if (expr.getType().equals(Boolean.class)) { Predicate predicate = ExpressionUtils.predicate(expr.getOperator(), args); return !context.paths.isEmpty() ? exists(context, predicate) : predicate; } else { return ExpressionUtils.operation(expr.getType(), expr.getOperator(), args); } } else { return expr; } }
@Override public String visit(Operation<?> o, Templates templates) { final Template template = templates.getTemplate(o.getOperator()); if (template != null) { final int precedence = templates.getPrecedence(o.getOperator()); final StringBuilder builder = new StringBuilder(); for (Template.Element element : template.getElements()) { final Object rv = element.convert(o.getArgs()); if (rv instanceof Expression) { if (precedence > -1 && rv instanceof Operation) { if (precedence < templates.getPrecedence(((Operation<?>) rv).getOperator())) { builder.append("("); builder.append(((Expression<?>) rv).accept(this, templates)); builder.append(")"); continue; } } builder.append(((Expression<?>) rv).accept(this, templates)); } else { builder.append(rv.toString()); } } return builder.toString(); } else { return "unknown operation with operator " + o.getOperator().name() + " and args " + o.getArgs(); } }
private void verifyArguments(Operation<?> operation) { List<Expression<?>> arguments = operation.getArgs(); for (int i = 1; i < arguments.size(); ++i) { if (!(arguments.get(i) instanceof Constant<?>) && !(arguments.get(i) instanceof ParamExpression<?>) && !(arguments.get(i) instanceof PhraseElement) && !(arguments.get(i) instanceof TermElement)) { throw new IllegalArgumentException("operand was of unsupported type " + arguments.get(i).getClass().getName()); } } }