KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.springframework.core.style.ToStringCreator;
22 import org.springframework.util.Assert;
23 import org.springframework.webflow.core.collection.LocalAttributeMap;
24 import org.springframework.webflow.core.collection.MutableAttributeMap;
25 import org.springframework.webflow.execution.Action;
26 import org.springframework.webflow.execution.Event;
27 import org.springframework.webflow.execution.RequestContext;
28
29 /**
30  * An action that will execute an ordered chain of other actions when executed.
31  * <p>
32  * The event id of the last not-null result returned by the executed actions
33  * will be used as the result event id of the composite action. Lacking that,
34  * the action will return the "success" event.
35  * <p>
36  * The resulting event will have an "actionResults" event attribute
37  * with a list of all events returned by the executed actions, including the null
38  * events. This allows you to relate an executed action and its result event by
39  * their index in the list.
40  * <p>
41  * This is the classic GoF composite design pattern.
42  *
43  * @author Keith Donald
44  */

45 public class CompositeAction extends AbstractAction {
46
47     /**
48      * The resulting event whill have an attribute of this name which holds a
49      * list of all events returned by the executed actions. ("actionResults")
50      */

51     public static final String JavaDoc ACTION_RESULTS_ATTRIBUTE_NAME = "actionResults";
52
53     /**
54      * The actions to execute.
55      */

56     private Action[] actions;
57
58     /**
59      * Should execution stop if one action returns an error event?
60      */

61     private boolean stopOnError;
62
63     /**
64      * Create a composite action composed of given actions.
65      * @param actions the actions
66      */

67     public CompositeAction(Action[] actions) {
68         Assert.notEmpty(actions, "At least one action is required");
69         this.actions = actions;
70     }
71
72     /**
73      * Returns the actions contained by this composite action.
74      * @return the actions
75      */

76     protected Action[] getActions() {
77         return actions;
78     }
79
80     /**
81      * Returns the stop on error flag.
82      */

83     public boolean isStopOnError() {
84         return stopOnError;
85     }
86
87     /**
88      * Sets the stop on error flag. This determines whether or not execution
89      * should stop with the first action that returns an error event. In the
90      * error case, the composite action will also return the "error" event.
91      */

92     public void setStopOnError(boolean stopOnError) {
93         this.stopOnError = stopOnError;
94     }
95
96     public Event doExecute(RequestContext context) throws Exception JavaDoc {
97         Action[] actions = getActions();
98         String JavaDoc eventId = getEventFactorySupport().getSuccessEventId();
99         MutableAttributeMap eventAttributes = new LocalAttributeMap();
100         List JavaDoc actionResults = new ArrayList JavaDoc(actions.length);
101         for (int i = 0; i < actions.length; i++) {
102             Event result = actions[i].execute(context);
103             actionResults.add(result);
104             if (result != null) {
105                 eventId = result.getId();
106                 if (isStopOnError() && result.getId().equals(getEventFactorySupport().getErrorEventId())) {
107                     break;
108                 }
109             }
110         }
111         eventAttributes.put(ACTION_RESULTS_ATTRIBUTE_NAME, actionResults);
112         return new Event(this, eventId, eventAttributes);
113     }
114
115     public String JavaDoc toString() {
116         return new ToStringCreator(this).append("actions", getActions()).append("stopOnError", isStopOnError())
117                 .toString();
118     }
119 }
Popular Tags