001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.springframework.webflow.execution.support;
017:
018: import org.springframework.webflow.core.collection.AttributeMap;
019: import org.springframework.webflow.core.collection.CollectionUtils;
020: import org.springframework.webflow.execution.Event;
021:
022: /**
023: * A convenience support class assisting in the creation of {@link Event} objects.
024: * <p>
025: * This class can be used as a simple utility class when you need to create
026: * common event objects. Alternatively you could extend it as a base support class
027: * when creating custom event factories.
028: *
029: * @author Keith Donald
030: * @author Erwin Vervaet
031: */
032: public class EventFactorySupport {
033:
034: /**
035: * The default 'success' result event identifier ("success").
036: */
037: private static final String SUCCESS_EVENT_ID = "success";
038:
039: /**
040: * The default 'error' result event identifier ("error").
041: */
042: private static final String ERROR_EVENT_ID = "error";
043:
044: /**
045: * The default 'yes' result event identifier ("yes").
046: */
047: private static final String YES_EVENT_ID = "yes";
048:
049: /**
050: * The default 'no' result event identifier ("no").
051: */
052: private static final String NO_EVENT_ID = "no";
053:
054: /**
055: * The default 'null' result event identifier ("null").
056: */
057: private static final String NULL_EVENT_ID = "null";
058:
059: /**
060: * The default 'exception' event attribute name ("exception").
061: */
062: private static final String EXCEPTION_ATTRIBUTE_NAME = "exception";
063:
064: /**
065: * The default 'result' event attribute name ("result").
066: */
067: private static final String RESULT_ATTRIBUTE_NAME = "result";
068:
069: /**
070: * The success event identifier.
071: */
072: private String successEventId = SUCCESS_EVENT_ID;
073:
074: /**
075: * The error event identifier.
076: */
077: private String errorEventId = ERROR_EVENT_ID;
078:
079: /**
080: * The yes event identifier.
081: */
082: private String yesEventId = YES_EVENT_ID;
083:
084: /**
085: * The no event identifier.
086: */
087: private String noEventId = NO_EVENT_ID;
088:
089: /**
090: * The null event identifier.
091: */
092: private String nullEventId = NULL_EVENT_ID;
093:
094: /**
095: * The exception event attribute name.
096: */
097: private String exceptionAttributeName = EXCEPTION_ATTRIBUTE_NAME;
098:
099: /**
100: * The result event attribute name.
101: */
102: private String resultAttributeName = RESULT_ATTRIBUTE_NAME;
103:
104: public String getSuccessEventId() {
105: return successEventId;
106: }
107:
108: public void setSuccessEventId(String successEventId) {
109: this .successEventId = successEventId;
110: }
111:
112: public String getErrorEventId() {
113: return errorEventId;
114: }
115:
116: public void setErrorEventId(String errorEventId) {
117: this .errorEventId = errorEventId;
118: }
119:
120: public String getYesEventId() {
121: return yesEventId;
122: }
123:
124: public void setYesEventId(String yesEventId) {
125: this .yesEventId = yesEventId;
126: }
127:
128: public String getNoEventId() {
129: return noEventId;
130: }
131:
132: public void setNoEventId(String noEventId) {
133: this .noEventId = noEventId;
134: }
135:
136: public String getNullEventId() {
137: return nullEventId;
138: }
139:
140: public void setNullEventId(String nullEventId) {
141: this .nullEventId = nullEventId;
142: }
143:
144: public String getExceptionAttributeName() {
145: return exceptionAttributeName;
146: }
147:
148: public void setExceptionAttributeName(String exceptionAttributeName) {
149: this .exceptionAttributeName = exceptionAttributeName;
150: }
151:
152: public String getResultAttributeName() {
153: return resultAttributeName;
154: }
155:
156: public void setResultAttributeName(String 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 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 source, Object result) {
176: return event(source, getSuccessEventId(),
177: getResultAttributeName(), result);
178: }
179:
180: /**
181: * Returns an "error" event.
182: * @param source the source of the event
183: */
184: public Event error(Object source) {
185: return event(source, getErrorEventId());
186: }
187:
188: /**
189: * Returns an "error" event caused by the provided exception.
190: * @param source the source of the event
191: * @param e the exception that caused the error event, to be put as an
192: * event attribute under the name {@link #getExceptionAttributeName()}
193: */
194: public Event error(Object source, Exception e) {
195: return event(source, getErrorEventId(),
196: getExceptionAttributeName(), e);
197: }
198:
199: /**
200: * Returns a "yes" event.
201: * @param source the source of the event
202: */
203: public Event yes(Object source) {
204: return event(source, getYesEventId());
205: }
206:
207: /**
208: * Returns a "no" result event.
209: * @param source the source of the event
210: */
211: public Event no(Object source) {
212: return event(source, getNoEventId());
213: }
214:
215: /**
216: * Returns an event to communicate an occurrence of a boolean expression.
217: * @param source the source of the event
218: * @param booleanResult the boolean
219: * @return yes or no
220: */
221: public Event event(Object source, boolean booleanResult) {
222: if (booleanResult) {
223: return yes(source);
224: } else {
225: return no(source);
226: }
227: }
228:
229: /**
230: * Returns a event with the specified identifier.
231: * @param source the source of the event
232: * @param eventId the result event identifier
233: * @return the event
234: */
235: public Event event(Object source, String eventId) {
236: return new Event(source, eventId, null);
237: }
238:
239: /**
240: * Returns a event with the specified identifier and the specified set of
241: * attributes.
242: * @param source the source of the event
243: * @param eventId the result event identifier
244: * @param attributes the event payload attributes
245: * @return the event
246: */
247: public Event event(Object source, String eventId,
248: AttributeMap attributes) {
249: return new Event(source, eventId, attributes);
250: }
251:
252: /**
253: * Returns a result event with the specified identifier and
254: * a single attribute.
255: * @param source the source of the event
256: * @param eventId the result id
257: * @param attributeName the attribute name
258: * @param attributeValue the attribute value
259: * @return the event
260: */
261: public Event event(Object source, String eventId,
262: String attributeName, Object attributeValue) {
263: return new Event(source, eventId, CollectionUtils
264: .singleEntryMap(attributeName, attributeValue));
265: }
266: }
|