01: /*
02: * Copyright 2004-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
19: import java.util.Map;
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
37: TransitionCriteria {
38:
39: /**
40: * Constant alias that points to the id of the last event that occured
41: * in a web flow execution.
42: */
43: private static final String RESULT_ALIAS = "result";
44:
45: /**
46: * The expression evaluator to use.
47: */
48: private Expression booleanExpression;
49:
50: /**
51: * Create a new expression based transition criteria object.
52: * @param booleanExpression the expression evaluator testing the criteria,
53: * this expression should be a condition that returns a Boolean value
54: */
55: public BooleanExpressionTransitionCriteria(
56: Expression booleanExpression) {
57: Assert.notNull(booleanExpression,
58: "The expression to test is required");
59: this .booleanExpression = booleanExpression;
60: }
61:
62: public boolean test(RequestContext context) {
63: Object result = booleanExpression.evaluate(context,
64: getEvaluationContext(context));
65: Assert
66: .isInstanceOf(Boolean.class, result,
67: "Impossible to determine result of boolean expression: ");
68: return ((Boolean) result).booleanValue();
69: }
70:
71: /**
72: * Setup a context with a few aliased values to make writing expression based
73: * transition conditions a bit easier.
74: */
75: protected EvaluationContext getEvaluationContext(
76: RequestContext context) {
77: final Map attributes = new HashMap(1, 1);
78: // ${#result == lastEvent.id}
79: if (context.getLastEvent() != null) {
80: attributes
81: .put(RESULT_ALIAS, context.getLastEvent().getId());
82: }
83: return new EvaluationContext() {
84: public Map getAttributes() {
85: return attributes;
86: }
87: };
88: }
89:
90: public String toString() {
91: return booleanExpression.toString();
92: }
93: }
|