Search in sources :

Example 1 with ValueBuilder

use of org.apache.camel.builder.ValueBuilder in project camel by apache.

the class JpaRouteTest method testRouteJpa.

@Test
public void testRouteJpa() throws Exception {
    // should auto setup transaction manager and entity factory
    JpaComponent jpa = context.getComponent("jpa", JpaComponent.class);
    assertNotNull("Should have been auto assigned", jpa.getEntityManagerFactory());
    assertNotNull("Should have been auto assigned", jpa.getTransactionManager());
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    ValueBuilder header = mock.message(0).header(JpaConstants.ENTITYMANAGER);
    header.isNotNull();
    header.isInstanceOf(EntityManager.class);
    template.sendBody("direct:start", new SendEmail("someone@somewhere.org"));
    assertMockEndpointsSatisfied();
    assertEntityInDB(1);
}
Also used : ValueBuilder(org.apache.camel.builder.ValueBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) SendEmail(org.apache.camel.examples.SendEmail) JpaComponent(org.apache.camel.component.jpa.JpaComponent) Test(org.junit.Test)

Example 2 with ValueBuilder

use of org.apache.camel.builder.ValueBuilder in project camel by apache.

the class BinaryExpression method createInExpression.

private Expression createInExpression(final Expression leftExp, final Expression rightExp) {
    return new Expression() {

        @Override
        public <T> T evaluate(Exchange exchange, Class<T> type) {
            // okay the in operator is a bit more complex as we need to build a list of values
            // from the right hand side expression.
            // each element on the right hand side must be separated by comma (default for create iterator)
            Iterator<Object> it = ObjectHelper.createIterator(rightExp.evaluate(exchange, Object.class));
            List<Object> values = new ArrayList<Object>();
            while (it.hasNext()) {
                values.add(it.next());
            }
            // then reuse value builder to create the in predicate with the list of values
            ValueBuilder vb = new ValueBuilder(leftExp);
            Predicate predicate = vb.in(values.toArray());
            if (operator == BinaryOperatorType.NOT_IN) {
                predicate = PredicateBuilder.not(predicate);
            }
            boolean answer = predicate.matches(exchange);
            return exchange.getContext().getTypeConverter().convertTo(type, answer);
        }

        @Override
        public String toString() {
            return left + " " + token.getText() + " " + right;
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) ValueBuilder(org.apache.camel.builder.ValueBuilder) Expression(org.apache.camel.Expression) ArrayList(java.util.ArrayList) Predicate(org.apache.camel.Predicate)

Example 3 with ValueBuilder

use of org.apache.camel.builder.ValueBuilder in project camel by apache.

the class ExpressionNodeHelper method toExpressionDefinition.

/**
     * Determines which {@link ExpressionDefinition} describes the given expression best possible.
     * <p/>
     * This implementation will use types such as {@link SimpleExpression}, {@link XPathExpression} etc.
     * if the given expression is detect as such a type.
     *
     * @param expression the expression
     * @return a definition which describes the expression
     */
public static ExpressionDefinition toExpressionDefinition(Expression expression) {
    if (expression instanceof SimpleBuilder) {
        SimpleBuilder builder = (SimpleBuilder) expression;
        // we keep the original expression by using the constructor that accepts an expression
        SimpleExpression answer = new SimpleExpression(builder);
        answer.setExpression(builder.getText());
        answer.setResultType(builder.getResultType());
        return answer;
    } else if (expression instanceof XPathBuilder) {
        XPathBuilder builder = (XPathBuilder) expression;
        // we keep the original expression by using the constructor that accepts an expression
        XPathExpression answer = new XPathExpression(builder);
        answer.setExpression(builder.getText());
        answer.setResultType(builder.getResultType());
        return answer;
    } else if (expression instanceof ValueBuilder) {
        // ValueBuilder wraps the actual expression so unwrap
        ValueBuilder builder = (ValueBuilder) expression;
        expression = builder.getExpression();
    }
    if (expression instanceof ExpressionDefinition) {
        return (ExpressionDefinition) expression;
    }
    return new ExpressionDefinition(expression);
}
Also used : XPathExpression(org.apache.camel.model.language.XPathExpression) ValueBuilder(org.apache.camel.builder.ValueBuilder) SimpleBuilder(org.apache.camel.builder.SimpleBuilder) XPathBuilder(org.apache.camel.builder.xml.XPathBuilder) ExpressionDefinition(org.apache.camel.model.language.ExpressionDefinition) SimpleExpression(org.apache.camel.model.language.SimpleExpression)

Example 4 with ValueBuilder

use of org.apache.camel.builder.ValueBuilder in project camel by apache.

the class ExpressionNodeHelper method toExpressionDefinition.

/**
     * Determines which {@link ExpressionDefinition} describes the given predicate best possible.
     * <p/>
     * This implementation will use types such as {@link SimpleExpression}, {@link XPathExpression} etc.
     * if the given predicate is detect as such a type.
     *
     * @param predicate the predicate
     * @return a definition which describes the predicate
     */
public static ExpressionDefinition toExpressionDefinition(Predicate predicate) {
    if (predicate instanceof SimpleBuilder) {
        SimpleBuilder builder = (SimpleBuilder) predicate;
        // we keep the original expression by using the constructor that accepts an expression
        SimpleExpression answer = new SimpleExpression(builder);
        answer.setExpression(builder.getText());
        return answer;
    } else if (predicate instanceof XPathBuilder) {
        XPathBuilder builder = (XPathBuilder) predicate;
        // we keep the original expression by using the constructor that accepts an expression
        XPathExpression answer = new XPathExpression(builder);
        answer.setExpression(builder.getText());
        return answer;
    } else if (predicate instanceof ValueBuilder) {
        // ValueBuilder wraps the actual predicate so unwrap
        ValueBuilder builder = (ValueBuilder) predicate;
        Expression expression = builder.getExpression();
        if (expression instanceof Predicate) {
            predicate = (Predicate) expression;
        }
    }
    if (predicate instanceof ExpressionDefinition) {
        return (ExpressionDefinition) predicate;
    }
    return new ExpressionDefinition(predicate);
}
Also used : XPathExpression(org.apache.camel.model.language.XPathExpression) ValueBuilder(org.apache.camel.builder.ValueBuilder) SimpleBuilder(org.apache.camel.builder.SimpleBuilder) XPathExpression(org.apache.camel.model.language.XPathExpression) SimpleExpression(org.apache.camel.model.language.SimpleExpression) Expression(org.apache.camel.Expression) XPathBuilder(org.apache.camel.builder.xml.XPathBuilder) ExpressionDefinition(org.apache.camel.model.language.ExpressionDefinition) SimpleExpression(org.apache.camel.model.language.SimpleExpression) Predicate(org.apache.camel.Predicate)

Aggregations

ValueBuilder (org.apache.camel.builder.ValueBuilder)4 Expression (org.apache.camel.Expression)2 Predicate (org.apache.camel.Predicate)2 SimpleBuilder (org.apache.camel.builder.SimpleBuilder)2 XPathBuilder (org.apache.camel.builder.xml.XPathBuilder)2 ExpressionDefinition (org.apache.camel.model.language.ExpressionDefinition)2 SimpleExpression (org.apache.camel.model.language.SimpleExpression)2 XPathExpression (org.apache.camel.model.language.XPathExpression)2 ArrayList (java.util.ArrayList)1 Exchange (org.apache.camel.Exchange)1 JpaComponent (org.apache.camel.component.jpa.JpaComponent)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 SendEmail (org.apache.camel.examples.SendEmail)1 Test (org.junit.Test)1