KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > engine > support > BooleanExpressionTransitionCriteria


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.webflow.engine.support;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.springframework.binding.expression.EvaluationContext;
22 import org.springframework.binding.expression.Expression;
23 import org.springframework.util.Assert;
24 import org.springframework.webflow.engine.TransitionCriteria;
25 import org.springframework.webflow.execution.RequestContext;
26
27 /**
28  * Transition criteria that tests the value of an expression. The
29  * expression is used to express a condition that guards transition
30  * execution in a web flow. Expressions will be evaluated agains the request
31  * context and should return a boolean result.
32  *
33  * @author Keith Donald
34  * @author Erwin Vervaet
35  */

36 public class BooleanExpressionTransitionCriteria implements TransitionCriteria {
37
38     /**
39      * Constant alias that points to the id of the last event that occured
40      * in a web flow execution.
41      */

42     private static final String JavaDoc RESULT_ALIAS = "result";
43     
44     /**
45      * The expression evaluator to use.
46      */

47     private Expression booleanExpression;
48
49     /**
50      * Create a new expression based transition criteria object.
51      * @param booleanExpression the expression evaluator testing the criteria,
52      * this expression should be a condition that returns a Boolean value
53      */

54     public BooleanExpressionTransitionCriteria(Expression booleanExpression) {
55         Assert.notNull(booleanExpression, "The expression to test is required");
56         this.booleanExpression = booleanExpression;
57     }
58
59     public boolean test(RequestContext context) {
60         Object JavaDoc result = booleanExpression.evaluate(context, getEvaluationContext(context));
61         Assert.isInstanceOf(Boolean JavaDoc.class, result, "Impossible to determine result of boolean expression: ");
62         return ((Boolean JavaDoc)result).booleanValue();
63     }
64
65     /**
66      * Setup a context with a few aliased values to make writing expression based
67      * transition conditions a bit easier.
68      */

69     protected EvaluationContext getEvaluationContext(RequestContext context) {
70         final Map JavaDoc attributes = new HashMap JavaDoc(1, 1);
71         // ${#result == lastEvent.id}
72
if (context.getLastEvent() != null) {
73             attributes.put(RESULT_ALIAS, context.getLastEvent().getId());
74         }
75         return new EvaluationContext() {
76             public Map JavaDoc getAttributes() {
77                 return attributes;
78             }
79         };
80     }
81
82     public String JavaDoc toString() {
83         return booleanExpression.toString();
84     }
85 }
Popular Tags