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.action;
17:
18: import org.springframework.binding.expression.EvaluationContext;
19: import org.springframework.binding.expression.Expression;
20: import org.springframework.binding.expression.SettableExpression;
21: import org.springframework.util.Assert;
22: import org.springframework.webflow.core.collection.MutableAttributeMap;
23: import org.springframework.webflow.execution.Event;
24: import org.springframework.webflow.execution.RequestContext;
25: import org.springframework.webflow.execution.ScopeType;
26:
27: /**
28: * An action that sets an attribute in a {@link ScopeType scope} when executed.
29: * Always returns the "success" event.
30: *
31: * @author Keith Donald
32: */
33: public class SetAction extends AbstractAction {
34:
35: /**
36: * The expression for setting the scoped attribute value.
37: */
38: private SettableExpression attributeExpression;
39:
40: /**
41: * The target scope.
42: */
43: private ScopeType scope;
44:
45: /**
46: * The expression for resolving the scoped attribute value.
47: */
48: private Expression valueExpression;
49:
50: /**
51: * Creates a new set attribute action.
52: * @param attributeExpression the writeable attribute expression
53: * @param scope the target scope of the attribute
54: * @param valueExpression the evaluatable attribute value expression
55: */
56: public SetAction(SettableExpression attributeExpression,
57: ScopeType scope, Expression valueExpression) {
58: Assert.notNull(attributeExpression,
59: "The attribute expression is required");
60: Assert.notNull(scope, "The scope type is required");
61: Assert.notNull(valueExpression,
62: "The value expression is required");
63: this .attributeExpression = attributeExpression;
64: this .scope = scope;
65: this .valueExpression = valueExpression;
66: }
67:
68: protected Event doExecute(RequestContext context) throws Exception {
69: EvaluationContext evaluationContext = getEvaluationContext(context);
70: Object value = valueExpression.evaluate(context,
71: evaluationContext);
72: MutableAttributeMap scopeMap = scope.getScope(context);
73: attributeExpression.evaluateToSet(scopeMap, value,
74: evaluationContext);
75: return success();
76: }
77:
78: /**
79: * Template method subclasses may override to customize the expression
80: * evaluation context. This implementation returns null.
81: * @param context the request context
82: * @return the evaluation context
83: */
84: protected EvaluationContext getEvaluationContext(
85: RequestContext context) {
86: return null;
87: }
88: }
|