KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > execution > support > EventFactorySupport


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.execution.support;
17
18 import org.springframework.webflow.core.collection.AttributeMap;
19 import org.springframework.webflow.core.collection.CollectionUtils;
20 import org.springframework.webflow.execution.Event;
21
22 /**
23  * A convenience support class assisting in the creation of {@link Event} objects.
24  * <p>
25  * This class can be used as a simple utility class when you need to create
26  * common event objects. Alternatively you could extend it as a base support class
27  * when creating custom event factories.
28  *
29  * @author Keith Donald
30  * @author Erwin Vervaet
31  */

32 public class EventFactorySupport {
33
34     /**
35      * The default 'success' result event identifier ("success").
36      */

37     private static final String JavaDoc SUCCESS_EVENT_ID = "success";
38
39     /**
40      * The default 'error' result event identifier ("error").
41      */

42     private static final String JavaDoc ERROR_EVENT_ID = "error";
43
44     /**
45      * The default 'yes' result event identifier ("yes").
46      */

47     private static final String JavaDoc YES_EVENT_ID = "yes";
48
49     /**
50      * The default 'no' result event identifier ("no").
51      */

52     private static final String JavaDoc NO_EVENT_ID = "no";
53
54     /**
55      * The default 'null' result event identifier ("null").
56      */

57     private static final String JavaDoc NULL_EVENT_ID = "null";
58
59     /**
60      * The default 'exception' event attribute name ("exception").
61      */

62     private static final String JavaDoc EXCEPTION_ATTRIBUTE_NAME = "exception";
63
64     /**
65      * The default 'result' event attribute name ("result").
66      */

67     private static final String JavaDoc RESULT_ATTRIBUTE_NAME = "result";
68     
69     /**
70      * The success event identifier.
71      */

72     private String JavaDoc successEventId = SUCCESS_EVENT_ID;
73
74     /**
75      * The error event identifier.
76      */

77     private String JavaDoc errorEventId = ERROR_EVENT_ID;
78
79     /**
80      * The yes event identifier.
81      */

82     private String JavaDoc yesEventId = YES_EVENT_ID;
83
84     /**
85      * The no event identifier.
86      */

87     private String JavaDoc noEventId = NO_EVENT_ID;
88
89     /**
90      * The null event identifier.
91      */

92     private String JavaDoc nullEventId = NULL_EVENT_ID;
93
94     /**
95      * The exception event attribute name.
96      */

97     private String JavaDoc exceptionAttributeName = EXCEPTION_ATTRIBUTE_NAME;
98
99     /**
100      * The result event attribute name.
101      */

102     private String JavaDoc resultAttributeName = RESULT_ATTRIBUTE_NAME;
103
104     public String JavaDoc getSuccessEventId() {
105         return successEventId;
106     }
107
108     public void setSuccessEventId(String JavaDoc successEventId) {
109         this.successEventId = successEventId;
110     }
111
112     public String JavaDoc getErrorEventId() {
113         return errorEventId;
114     }
115
116     public void setErrorEventId(String JavaDoc errorEventId) {
117         this.errorEventId = errorEventId;
118     }
119
120     public String JavaDoc getYesEventId() {
121         return yesEventId;
122     }
123
124     public void setYesEventId(String JavaDoc yesEventId) {
125         this.yesEventId = yesEventId;
126     }
127
128     public String JavaDoc getNoEventId() {
129         return noEventId;
130     }
131
132     public void setNoEventId(String JavaDoc noEventId) {
133         this.noEventId = noEventId;
134     }
135
136     public String JavaDoc getNullEventId() {
137         return nullEventId;
138     }
139
140     public void setNullEventId(String JavaDoc nullEventId) {
141         this.nullEventId = nullEventId;
142     }
143
144     public String JavaDoc getExceptionAttributeName() {
145         return exceptionAttributeName;
146     }
147
148     public void setExceptionAttributeName(String JavaDoc exceptionAttributeName) {
149         this.exceptionAttributeName = exceptionAttributeName;
150     }
151
152     public String JavaDoc getResultAttributeName() {
153         return resultAttributeName;
154     }
155
156     public void setResultAttributeName(String JavaDoc resultAttributeName) {
157         this.resultAttributeName = resultAttributeName;
158     }
159
160     /**
161      * Returns a "success" event.
162      * @param source the source of the event
163      */

164     public Event success(Object JavaDoc source) {
165         return event(source, getSuccessEventId());
166     }
167
168     /**
169      * Returns a "success" event with the provided result object as an
170      * attribute. The result object is identified by the attribute name
171      * {@link #getResultAttributeName()}.
172      * @param source the source of the event
173      * @param result the action success result
174      */

175     public Event success(Object JavaDoc source, Object JavaDoc result) {
176         return event(source, getSuccessEventId(), getResultAttributeName(), result);
177     }
178
179     /**
180      * Returns an "error" event.
181      * @param source the source of the event
182      */

183     public Event error(Object JavaDoc source) {
184         return event(source, getErrorEventId());
185     }
186
187     /**
188      * Returns an "error" event caused by the provided exception.
189      * @param source the source of the event
190      * @param e the exception that caused the error event, to be put as an
191      * event attribute under the name {@link #getExceptionAttributeName()}
192      */

193     public Event error(Object JavaDoc source, Exception JavaDoc e) {
194         return event(source, getErrorEventId(), getExceptionAttributeName(), e);
195     }
196
197     /**
198      * Returns a "yes" event.
199      * @param source the source of the event
200      */

201     public Event yes(Object JavaDoc source) {
202         return event(source, getYesEventId());
203     }
204
205     /**
206      * Returns a "no" result event.
207      * @param source the source of the event
208      */

209     public Event no(Object JavaDoc source) {
210         return event(source, getNoEventId());
211     }
212
213     /**
214      * Returns an event to communicate an occurrence of a boolean expression.
215      * @param source the source of the event
216      * @param booleanResult the boolean
217      * @return yes or no
218      */

219     public Event event(Object JavaDoc source, boolean booleanResult) {
220         if (booleanResult) {
221             return yes(source);
222         }
223         else {
224             return no(source);
225         }
226     }
227
228     /**
229      * Returns a event with the specified identifier.
230      * @param source the source of the event
231      * @param eventId the result event identifier
232      * @return the event
233      */

234     public Event event(Object JavaDoc source, String JavaDoc eventId) {
235         return new Event(source, eventId, null);
236     }
237
238     /**
239      * Returns a event with the specified identifier and the specified set of
240      * attributes.
241      * @param source the source of the event
242      * @param eventId the result event identifier
243      * @param attributes the event payload attributes
244      * @return the event
245      */

246     public Event event(Object JavaDoc source, String JavaDoc eventId, AttributeMap attributes) {
247         return new Event(source, eventId, attributes);
248     }
249
250     /**
251      * Returns a result event with the specified identifier and
252      * a single attribute.
253      * @param source the source of the event
254      * @param eventId the result id
255      * @param attributeName the attribute name
256      * @param attributeValue the attribute value
257      * @return the event
258      */

259     public Event event(Object JavaDoc source, String JavaDoc eventId, String JavaDoc attributeName, Object JavaDoc attributeValue) {
260         return new Event(source, eventId, CollectionUtils.singleEntryMap(attributeName, attributeValue));
261     }
262 }
Popular Tags