KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > engine > builder > FlowArtifactFactory


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.builder;
17
18 import org.springframework.binding.mapping.AttributeMapper;
19 import org.springframework.webflow.core.collection.AttributeMap;
20 import org.springframework.webflow.engine.ActionState;
21 import org.springframework.webflow.engine.DecisionState;
22 import org.springframework.webflow.engine.EndState;
23 import org.springframework.webflow.engine.Flow;
24 import org.springframework.webflow.engine.FlowAttributeMapper;
25 import org.springframework.webflow.engine.FlowExecutionExceptionHandler;
26 import org.springframework.webflow.engine.State;
27 import org.springframework.webflow.engine.SubflowState;
28 import org.springframework.webflow.engine.TargetStateResolver;
29 import org.springframework.webflow.engine.Transition;
30 import org.springframework.webflow.engine.TransitionCriteria;
31 import org.springframework.webflow.engine.TransitionableState;
32 import org.springframework.webflow.engine.ViewSelector;
33 import org.springframework.webflow.engine.ViewState;
34 import org.springframework.webflow.execution.Action;
35
36 /**
37  * A factory for core web flow elements such as {@link Flow flows},
38  * {@link State states}, and {@link Transition transitions}.
39  * <p>
40  * This factory encapsulates the construction of each Flow implementation as
41  * well as each core artifact type. Subclasses may customize how the core elements
42  * are created, useful for plugging in custom implementations.
43  *
44  * @author Keith Donald
45  * @author Erwin Vervaet
46  */

47 public class FlowArtifactFactory {
48
49     /**
50      * Factory method that creates a new {@link Flow} definition object.
51      * <p>
52      * Note this method does not return a fully configured Flow instance, it
53      * only encapsulates the selection of implementation. A
54      * {@link FlowAssembler} delegating to a calling {@link FlowBuilder} is
55      * expected to assemble the Flow fully before returning it to external
56      * clients.
57      * @param id the flow identifier, should be unique to all flows in an
58      * application (required)
59      * @param attributes attributes to assign to the Flow, which may also be
60      * used to affect flow construction; may be null
61      * @return the initial flow instance, ready for assembly by a FlowBuilder
62      * @throws FlowArtifactLookupException an exception occured creating the
63      * Flow instance
64      */

65     public Flow createFlow(String JavaDoc id, AttributeMap attributes) throws FlowArtifactLookupException {
66         Flow flow = new Flow(id);
67         flow.getAttributeMap().putAll(attributes);
68         return flow;
69     }
70
71     /**
72      * Factory method that creates a new view state, a state where a user is
73      * allowed to participate in the flow. This method is an atomic operation
74      * that returns a fully initialized state. It encapsulates the selection of
75      * the view state implementation as well as the state assembly.
76      * @param id the identifier to assign to the state, must be unique to its
77      * owning flow (required)
78      * @param flow the flow that will own (contain) this state (required)
79      * @param entryActions any state entry actions; may be null
80      * @param viewSelector the state view selector strategy; may be null
81      * @param renderActions any 'render actions' to execute on entry and refresh;
82      * may be null
83      * @param transitions any transitions (paths) out of this state; may be null
84      * @param exceptionHandlers any exception handlers; may be null
85      * @param exitActions any state exit actions; may be null
86      * @param attributes attributes to assign to the State, which may also be
87      * used to affect state construction; may be null
88      * @return the fully initialized view state instance
89      * @throws FlowArtifactLookupException an exception occured creating the
90      * state
91      */

92     public State createViewState(String JavaDoc id, Flow flow, Action[] entryActions, ViewSelector viewSelector,
93             Action[] renderActions, Transition[] transitions, FlowExecutionExceptionHandler[] exceptionHandlers,
94             Action[] exitActions, AttributeMap attributes) throws FlowArtifactLookupException {
95         ViewState viewState = new ViewState(flow, id);
96         if (viewSelector != null) {
97             viewState.setViewSelector(viewSelector);
98         }
99         viewState.getRenderActionList().addAll(renderActions);
100         configureCommonProperties(viewState, entryActions, transitions, exceptionHandlers, exitActions, attributes);
101         return viewState;
102     }
103
104     /**
105      * Factory method that creates a new action state, a state where a system
106      * action is executed. This method is an atomic operation that returns a
107      * fully initialized state. It encapsulates the selection of the action
108      * state implementation as well as the state assembly.
109      * @param id the identifier to assign to the state, must be unique to its
110      * owning flow (required)
111      * @param flow the flow that will own (contain) this state (required)
112      * @param entryActions any state entry actions; may be null
113      * @param actions the actions to execute when the state is entered
114      * (required)
115      * @param transitions any transitions (paths) out of this state; may be null
116      * @param exceptionHandlers any exception handlers; may be null
117      * @param exitActions any state exit actions; may be null
118      * @param attributes attributes to assign to the State, which may also be
119      * used to affect state construction; may be null
120      * @return the fully initialized action state instance
121      * @throws FlowArtifactLookupException an exception occured creating the
122      * state
123      */

124     public State createActionState(String JavaDoc id, Flow flow, Action[] entryActions, Action[] actions,
125             Transition[] transitions, FlowExecutionExceptionHandler[] exceptionHandlers, Action[] exitActions,
126             AttributeMap attributes) throws FlowArtifactLookupException {
127         ActionState actionState = new ActionState(flow, id);
128         actionState.getActionList().addAll(actions);
129         configureCommonProperties(actionState, entryActions, transitions, exceptionHandlers, exitActions, attributes);
130         return actionState;
131     }
132
133     /**
134      * Factory method that creates a new decision state, a state where a flow
135      * routing decision is made. This method is an atomic operation that returns
136      * a fully initialized state. It encapsulates the selection of the decision
137      * state implementation as well as the state assembly.
138      * @param id the identifier to assign to the state, must be unique to its
139      * owning flow (required)
140      * @param flow the flow that will own (contain) this state (required)
141      * @param entryActions any state entry actions; may be null
142      * @param transitions any transitions (paths) out of this state
143      * @param exceptionHandlers any exception handlers; may be null
144      * @param exitActions any state exit actions; may be null
145      * @param attributes attributes to assign to the State, which may also be
146      * used to affect state construction; may be null
147      * @return the fully initialized decision state instance
148      * @throws FlowArtifactLookupException an exception occured creating the
149      * state
150      */

151     public State createDecisionState(String JavaDoc id, Flow flow, Action[] entryActions, Transition[] transitions,
152             FlowExecutionExceptionHandler[] exceptionHandlers, Action[] exitActions, AttributeMap attributes)
153             throws FlowArtifactLookupException {
154         DecisionState decisionState = new DecisionState(flow, id);
155         configureCommonProperties(decisionState, entryActions, transitions, exceptionHandlers, exitActions, attributes);
156         return decisionState;
157     }
158
159     /**
160      * Factory method that creates a new subflow state, a state where a parent
161      * flow spawns another flow as a subflow. This method is an atomic operation
162      * that returns a fully initialized state. It encapsulates the selection of
163      * the subflow state implementation as well as the state assembly.
164      * @param id the identifier to assign to the state, must be unique to its
165      * owning flow (required)
166      * @param flow the flow that will own (contain) this state (required)
167      * @param entryActions any state entry actions; may be null
168      * @param subflow the subflow definition (required)
169      * @param attributeMapper the subflow input and output attribute mapper; may
170      * be null
171      * @param transitions any transitions (paths) out of this state
172      * @param exceptionHandlers any exception handlers; may be null
173      * @param exitActions any state exit actions; may be null
174      * @param attributes attributes to assign to the State, which may also be
175      * used to affect state construction; may be null
176      * @return the fully initialized subflow state instance
177      * @throws FlowArtifactLookupException an exception occured creating the
178      * state
179      */

180     public State createSubflowState(String JavaDoc id, Flow flow, Action[] entryActions, Flow subflow,
181             FlowAttributeMapper attributeMapper, Transition[] transitions,
182             FlowExecutionExceptionHandler[] exceptionHandlers, Action[] exitActions, AttributeMap attributes)
183             throws FlowArtifactLookupException {
184         SubflowState subflowState = new SubflowState(flow, id, subflow);
185         if (attributeMapper != null) {
186             subflowState.setAttributeMapper(attributeMapper);
187         }
188         configureCommonProperties(subflowState, entryActions, transitions, exceptionHandlers, exitActions, attributes);
189         return subflowState;
190     }
191
192     /**
193      * Factory method that creates a new end state, a state where an executing
194      * flow session terminates. This method is an atomic operation that returns
195      * a fully initialized state. It encapsulates the selection of the end state
196      * implementation as well as the state assembly.
197      * @param id the identifier to assign to the state, must be unique to its
198      * owning flow (required)
199      * @param flow the flow that will own (contain) this state (required)
200      * @param entryActions any state entry actions; may be null
201      * @param viewSelector the state confirmation view selector strategy; may be
202      * null
203      * @param outputMapper the state output mapper; may be null
204      * @param exceptionHandlers any exception handlers; may be null
205      * @param attributes attributes to assign to the State, which may also be
206      * used to affect state construction; may be null
207      * @return the fully initialized subflow state instance
208      * @throws FlowArtifactLookupException an exception occured creating the
209      * state
210      */

211     public State createEndState(String JavaDoc id, Flow flow, Action[] entryActions, ViewSelector viewSelector,
212             AttributeMapper outputMapper, FlowExecutionExceptionHandler[] exceptionHandlers, AttributeMap attributes)
213             throws FlowArtifactLookupException {
214         EndState endState = new EndState(flow, id);
215         if (viewSelector != null) {
216             endState.setViewSelector(viewSelector);
217         }
218         if (outputMapper != null) {
219             endState.setOutputMapper(outputMapper);
220         }
221         configureCommonProperties(endState, entryActions, exceptionHandlers, attributes);
222         return endState;
223     }
224
225     /**
226      * Factory method that creates a new transition, a path from one step in a
227      * flow to another. This method is an atomic operation that returns a fully
228      * initialized transition. It encapsulates the selection of the transition
229      * implementation as well as the transition assembly.
230      * @param targetStateResolver the resolver of the target state of the transition (required)
231      * @param matchingCriteria the criteria that matches the transition; may be
232      * null
233      * @param executionCriteria the criteria that governs execution of the
234      * transition after match; may be null
235      * @param attributes attributes to assign to the transition, which may also
236      * be used to affect transition construction; may be null
237      * @return the fully initialized transition instance
238      * @throws FlowArtifactLookupException an exception occured creating the
239      * transition
240      */

241     public Transition createTransition(TargetStateResolver targetStateResolver, TransitionCriteria matchingCriteria,
242             TransitionCriteria executionCriteria, AttributeMap attributes) throws FlowArtifactLookupException {
243         Transition transition = new Transition(targetStateResolver);
244         if (matchingCriteria != null) {
245             transition.setMatchingCriteria(matchingCriteria);
246         }
247         if (executionCriteria != null) {
248             transition.setExecutionCriteria(executionCriteria);
249         }
250         transition.getAttributeMap().putAll(attributes);
251         return transition;
252     }
253
254     // internal helpers
255

256     /**
257      * Configure common properties for a transitionable state.
258      */

259     private void configureCommonProperties(TransitionableState state, Action[] entryActions, Transition[] transitions,
260             FlowExecutionExceptionHandler[] exceptionHandlers, Action[] exitActions, AttributeMap attributes) {
261         configureCommonProperties(state, entryActions, exceptionHandlers, attributes);
262         state.getTransitionSet().addAll(transitions);
263         state.getExitActionList().addAll(exitActions);
264     }
265
266     /**
267      * Configure common properties for a state.
268      */

269     private void configureCommonProperties(State state, Action[] entryActions,
270             FlowExecutionExceptionHandler[] exceptionHandlers, AttributeMap attributes) {
271         state.getEntryActionList().addAll(entryActions);
272         state.getExceptionHandlerSet().addAll(exceptionHandlers);
273         state.getAttributeMap().putAll(attributes);
274     }
275 }
Popular Tags