001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.springframework.webflow.engine.support;
017:
018: import java.util.Arrays;
019: import java.util.Iterator;
020: import java.util.LinkedList;
021: import java.util.List;
022:
023: import org.springframework.core.style.ToStringCreator;
024: import org.springframework.webflow.engine.AnnotatedAction;
025: import org.springframework.webflow.engine.TransitionCriteria;
026: import org.springframework.webflow.engine.WildcardTransitionCriteria;
027: import org.springframework.webflow.execution.RequestContext;
028:
029: /**
030: * An ordered chain of <code>TransitionCriteria</code>. Iterates over each element
031: * in the chain, continues until one returns false or the list is exhausted. So
032: * in effect it will do a logical AND between the contained criteria.
033: *
034: * @author Keith Donald
035: */
036: public class TransitionCriteriaChain implements TransitionCriteria {
037:
038: /**
039: * The ordered chain of TransitionCriteria objects.
040: */
041: private List criteriaChain = new LinkedList();
042:
043: /**
044: * Creates an initially empty transition criteria chain.
045: * @see #add(TransitionCriteria)
046: */
047: public TransitionCriteriaChain() {
048: }
049:
050: /**
051: * Creates a transition criteria chain with the specified criteria.
052: * @param criteria the criteria
053: */
054: public TransitionCriteriaChain(TransitionCriteria[] criteria) {
055: criteriaChain.addAll(Arrays.asList(criteria));
056: }
057:
058: /**
059: * Add given criteria object to the end of the chain.
060: * @param criteria the criteria
061: * @return this object, so multiple criteria can be added in a single
062: * statement
063: */
064: public TransitionCriteriaChain add(TransitionCriteria criteria) {
065: this .criteriaChain.add(criteria);
066: return this ;
067: }
068:
069: public boolean test(RequestContext context) {
070: Iterator it = criteriaChain.iterator();
071: while (it.hasNext()) {
072: TransitionCriteria criteria = (TransitionCriteria) it
073: .next();
074: if (!criteria.test(context)) {
075: return false;
076: }
077: }
078: return true;
079: }
080:
081: public String toString() {
082: return new ToStringCreator(this ).append("criteriaChain",
083: criteriaChain).toString();
084: }
085:
086: // static helpers
087:
088: /**
089: * Create a transition criteria chain chaining given list of actions.
090: * @param actions the actions (and their execution properties) to chain together
091: */
092: public static TransitionCriteria criteriaChainFor(
093: AnnotatedAction[] actions) {
094: if (actions == null || actions.length == 0) {
095: return WildcardTransitionCriteria.INSTANCE;
096: }
097: TransitionCriteriaChain chain = new TransitionCriteriaChain();
098: for (int i = 0; i < actions.length; i++) {
099: chain.add(new ActionTransitionCriteria(actions[i]));
100: }
101: return chain;
102: }
103: }
|