KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > action > SetAction


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.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, ScopeType scope, Expression valueExpression) {
57         Assert.notNull(attributeExpression, "The attribute expression is required");
58         Assert.notNull(scope, "The scope type is required");
59         Assert.notNull(valueExpression, "The value expression is required");
60         this.attributeExpression = attributeExpression;
61         this.scope = scope;
62         this.valueExpression = valueExpression;
63     }
64
65     protected Event doExecute(RequestContext context) throws Exception JavaDoc {
66         EvaluationContext evaluationContext = getEvaluationContext(context);
67         Object JavaDoc value = valueExpression.evaluate(context, evaluationContext);
68         MutableAttributeMap scopeMap = scope.getScope(context);
69         attributeExpression.evaluateToSet(scopeMap, value, evaluationContext);
70         return success();
71     }
72
73     /**
74      * Template method subclasses may override to customize the expression
75      * evaluation context. This implementation returns null.
76      * @param context the request context
77      * @return the evaluation context
78      */

79     protected EvaluationContext getEvaluationContext(RequestContext context) {
80         return null;
81     }
82 }
Popular Tags