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 javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlRootElement;
023
024import org.apache.camel.Expression;
025import org.apache.camel.Predicate;
026import org.apache.camel.model.language.ExpressionDefinition;
027import org.apache.camel.spi.Metadata;
028
029/**
030 * Processes a message multiple times
031 */
032@Metadata(label = "eip,routing")
033@XmlRootElement(name = "loop")
034@XmlAccessorType(XmlAccessType.FIELD)
035public class LoopDefinition extends OutputExpressionNode {
036
037    @XmlAttribute
038    @Metadata(javaType = "java.lang.Boolean")
039    private String copy;
040    @XmlAttribute
041    @Metadata(javaType = "java.lang.Boolean")
042    private String doWhile;
043
044    public LoopDefinition() {
045    }
046
047    public LoopDefinition(Expression expression) {
048        super(expression);
049    }
050
051    public LoopDefinition(Predicate predicate) {
052        super(predicate);
053        setDoWhile(Boolean.toString(true));
054    }
055
056    public LoopDefinition(ExpressionDefinition expression) {
057        super(expression);
058    }
059
060    /**
061     * Enables copy mode so a copy of the input Exchange is used for each
062     * iteration.
063     * 
064     * @return the builder
065     */
066    public LoopDefinition copy() {
067        setCopy(Boolean.toString(true));
068        return this;
069    }
070
071    public String getCopy() {
072        return copy;
073    }
074
075    public String getDoWhile() {
076        return doWhile;
077    }
078
079    /**
080     * Enables the while loop that loops until the predicate evaluates to false
081     * or null.
082     */
083    public void setDoWhile(String doWhile) {
084        this.doWhile = doWhile;
085    }
086
087    /**
088     * If the copy attribute is true, a copy of the input Exchange is used for
089     * each iteration. That means each iteration will start from a copy of the
090     * same message.
091     * <p/>
092     * By default loop will loop the same exchange all over, so each iteration
093     * may have different message content.
094     */
095    public void setCopy(String copy) {
096        this.copy = copy;
097    }
098
099    @Override
100    public String toString() {
101        return "Loop[" + getExpression() + " -> " + getOutputs() + "]";
102    }
103
104    @Override
105    public String getShortName() {
106        return "loop";
107    }
108
109    @Override
110    public String getLabel() {
111        return "loop[" + getExpression() + "]";
112    }
113
114    /**
115     * Expression to define how many times we should loop. Notice the expression
116     * is only evaluated once, and should return a number as how many times to
117     * loop. A value of zero or negative means no looping. The loop is like a
118     * for-loop fashion, if you want a while loop, then the dynamic router may
119     * be a better choice.
120     */
121    @Override
122    public void setExpression(ExpressionDefinition expression) {
123        // override to include javadoc what the expression is used for
124        super.setExpression(expression);
125    }
126}