001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.model;
018
019import java.util.Collections;
020import java.util.List;
021
022import javax.xml.bind.annotation.XmlAccessType;
023import javax.xml.bind.annotation.XmlAccessorType;
024import javax.xml.bind.annotation.XmlElementRef;
025import javax.xml.bind.annotation.XmlTransient;
026
027import org.apache.camel.Expression;
028import org.apache.camel.ExpressionFactory;
029import org.apache.camel.Predicate;
030import org.apache.camel.builder.ExpressionClause;
031import org.apache.camel.model.language.ExpressionDefinition;
032
033/**
034 * A base {@link ExpressionNode} which does <b>not</b> support any outputs.
035 * <p/>
036 * This node is to be extended by definitions which need to support an
037 * expression but the definition should not contain any outputs, such as
038 * {@link org.apache.camel.model.TransformDefinition}.
039 */
040@XmlAccessorType(XmlAccessType.FIELD)
041@XmlTransient
042public abstract class ExpressionNode extends ProcessorDefinition<ExpressionNode> {
043
044    @XmlElementRef
045    private ExpressionDefinition expression;
046
047    public ExpressionNode() {
048    }
049
050    public ExpressionNode(ExpressionDefinition expression) {
051        setExpression(expression);
052    }
053
054    public ExpressionNode(Expression expression) {
055        setExpression(expression);
056    }
057
058    public ExpressionNode(Predicate predicate) {
059        setPredicate(predicate);
060    }
061
062    public ExpressionDefinition getExpression() {
063        return expression;
064    }
065
066    public void setExpression(Expression expression) {
067        if (expression != null) {
068            setExpression(ExpressionNodeHelper.toExpressionDefinition(expression));
069        }
070    }
071
072    private void setPredicate(Predicate predicate) {
073        if (predicate != null) {
074            setExpression(ExpressionNodeHelper.toExpressionDefinition(predicate));
075        }
076    }
077
078    public void setExpression(ExpressionDefinition expression) {
079        // favour using the helper to set the expression as it can unwrap some
080        // unwanted builders when using Java DSL
081        this.expression = expression;
082    }
083
084    @Override
085    public String getLabel() {
086        if (getExpression() == null) {
087            return "";
088        }
089        return getExpression().getLabel();
090    }
091
092    @Override
093    public void configureChild(ProcessorDefinition<?> output) {
094        // reuse the logic from pre create processor
095        preCreateProcessor();
096    }
097
098    @Override
099    public void preCreateProcessor() {
100        Expression exp = getExpression();
101        if (getExpression() != null && getExpression().getExpressionValue() != null) {
102            exp = getExpression().getExpressionValue();
103        }
104
105        if (exp instanceof ExpressionClause) {
106            ExpressionClause<?> clause = (ExpressionClause<?>)exp;
107            if (clause.getExpressionType() != null) {
108                // if using the Java DSL then the expression may have been set
109                // using the
110                // ExpressionClause which is a fancy builder to define
111                // expressions and predicates
112                // using fluent builders in the DSL. However we need afterwards
113                // a callback to
114                // reset the expression to the expression type the
115                // ExpressionClause did build for us
116                ExpressionFactory model = clause.getExpressionType();
117                if (model instanceof ExpressionDefinition) {
118                    setExpression((ExpressionDefinition)model);
119                }
120            }
121        }
122
123        if (getExpression() != null && getExpression().getExpression() == null) {
124            // use toString from predicate or expression so we have some
125            // information to show in the route model
126            if (getExpression().getPredicate() != null) {
127                getExpression().setExpression(getExpression().getPredicate().toString());
128            } else if (getExpression().getExpressionValue() != null) {
129                getExpression().setExpression(getExpression().getExpressionValue().toString());
130            }
131        }
132    }
133
134    @Override
135    public List<ProcessorDefinition<?>> getOutputs() {
136        return Collections.emptyList();
137    }
138
139    @Override
140    public ExpressionNode id(String id) {
141        if (!(this instanceof OutputNode)) {
142            // let parent handle assigning the id, as we do not support outputs
143            getParent().id(id);
144            return this;
145        } else {
146            return super.id(id);
147        }
148    }
149
150}