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     */
017    package org.apache.camel.model;
018    
019    import java.util.ArrayList;
020    import java.util.Collections;
021    import java.util.List;
022    
023    import javax.xml.bind.annotation.XmlAccessType;
024    import javax.xml.bind.annotation.XmlAccessorType;
025    import javax.xml.bind.annotation.XmlElement;
026    import javax.xml.bind.annotation.XmlElementRef;
027    import javax.xml.bind.annotation.XmlRootElement;
028    
029    import org.apache.camel.Predicate;
030    import org.apache.camel.Processor;
031    import org.apache.camel.builder.ExpressionClause;
032    import org.apache.camel.processor.ChoiceProcessor;
033    import org.apache.camel.processor.FilterProcessor;
034    import org.apache.camel.spi.RouteContext;
035    import org.apache.camel.util.CollectionStringBuffer;
036    
037    /**
038     * Represents an XML <choice/> element
039     *
040     * @version $Revision: 885197 $
041     */
042    @XmlRootElement(name = "choice")
043    @XmlAccessorType(XmlAccessType.FIELD)
044    public class ChoiceDefinition extends ProcessorDefinition<ChoiceDefinition> {
045    
046        @XmlElementRef
047        private List<WhenDefinition> whenClauses = new ArrayList<WhenDefinition>();
048        @XmlElement(required = false)
049        private OtherwiseDefinition otherwise;
050    
051        @Override
052        public String toString() {
053            if (getOtherwise() != null) {
054                return "Choice[" + getWhenClauses() + " " + getOtherwise() + "]";
055            } else {
056                return "Choice[" + getWhenClauses() + "]";
057    
058            }
059        }
060        @Override
061        public String getShortName() {
062            return "choice";
063        }
064               
065        @Override
066        public Processor createProcessor(RouteContext routeContext) throws Exception {
067            List<FilterProcessor> filters = new ArrayList<FilterProcessor>();
068            for (WhenDefinition whenClaus : whenClauses) {
069                filters.add(whenClaus.createProcessor(routeContext));
070            }
071            Processor otherwiseProcessor = null;
072            if (otherwise != null) {
073                otherwiseProcessor = otherwise.createProcessor(routeContext);
074            }
075            return new ChoiceProcessor(filters, otherwiseProcessor);
076        }
077    
078        // Fluent API
079        // -------------------------------------------------------------------------
080        /**
081         * Sets the predicate for the when node
082         *
083         * @param predicate  the predicate
084         * @return the builder
085         */
086        public ChoiceDefinition when(Predicate predicate) {
087            WhenDefinition when = new WhenDefinition(predicate);
088            when.setParent(this);
089            getWhenClauses().add(when);
090            return this;
091        }
092    
093        /**
094         * Creates an expression for the when node
095         *
096         * @return expression to be used as builder to configure the when node
097         */
098        public ExpressionClause<ChoiceDefinition> when() {
099            WhenDefinition when = new WhenDefinition();
100            when.setParent(this);
101            getWhenClauses().add(when);
102            ExpressionClause<ChoiceDefinition> clause = new ExpressionClause<ChoiceDefinition>(this);
103            when.setExpression(clause);
104            return clause;
105        }
106    
107        /**
108         * Sets the otherwise node
109         * 
110         * @return the builder
111         */
112        public ChoiceDefinition otherwise() {
113            OtherwiseDefinition answer = new OtherwiseDefinition();
114            answer.setParent(this);
115            setOtherwise(answer);
116            return this;
117        }
118    
119        // Properties
120        // -------------------------------------------------------------------------
121    
122        @Override
123        public String getLabel() {
124            CollectionStringBuffer buffer = new CollectionStringBuffer();
125            List<WhenDefinition> list = getWhenClauses();
126            for (WhenDefinition whenType : list) {
127                buffer.append(whenType.getLabel());
128            }
129            return buffer.toString();
130        }
131    
132        public List<WhenDefinition> getWhenClauses() {
133            return whenClauses;
134        }
135    
136        public void setWhenClauses(List<WhenDefinition> whenClauses) {
137            this.whenClauses = whenClauses;
138        }
139    
140        @SuppressWarnings("unchecked")
141        public List<ProcessorDefinition> getOutputs() {
142            if (otherwise != null) {
143                return otherwise.getOutputs();
144            } else if (whenClauses.isEmpty()) {
145                return Collections.EMPTY_LIST;
146            } else {
147                WhenDefinition when = whenClauses.get(whenClauses.size() - 1);
148                return when.getOutputs();
149            }
150        }
151    
152        public OtherwiseDefinition getOtherwise() {
153            return otherwise;
154        }
155    
156        public void setOtherwise(OtherwiseDefinition otherwise) {
157            this.otherwise = otherwise;
158        }
159    
160    }