KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.JdkVersion;
19 import org.springframework.core.enums.LabeledEnum;
20 import org.springframework.webflow.execution.Event;
21 import org.springframework.webflow.execution.RequestContext;
22 import org.springframework.webflow.execution.support.EventFactorySupport;
23
24 /**
25  * Result object-to-event adapter interface that tries to do a
26  * sensible conversion of the result object into a web flow event.
27  * It uses the following conversion table:
28  * <table border="1">
29  * <tr>
30  * <th>Result object type</th>
31  * <th>Event id</th>
32  * <th>Remarks</th>
33  * </tr>
34  * <tr>
35  * <td>null</td>
36  * <td>{@link org.springframework.webflow.execution.support.EventFactorySupport#getNullEventId()}</td>
37  * <td>&nbsp;</td>
38  * </tr>
39  * <tr>
40  * <td>{@link java.lang.Boolean} or boolean</td>
41  * <td>{@link org.springframework.webflow.execution.support.EventFactorySupport#getYesEventId()}/
42  * {@link org.springframework.webflow.execution.support.EventFactorySupport#getNoEventId()}</td>
43  * <td>&nbsp;</td>
44  * </tr>
45  * <tr>
46  * <td>{@link org.springframework.core.enums.LabeledEnum}</td>
47  * <td>{@link org.springframework.core.enums.LabeledEnum#getLabel()}</td>
48  * <td>The result object will included in the event as an attribute
49  * named "result".</td>
50  * </tr>
51  * <tr>
52  * <td>{@link java.lang.Enum}</td>
53  * <td>{@link java.lang.Enum#name()}</td>
54  * <td>The result object will included in the event as an attribute
55  * named "result".</td>
56  * </tr>
57  * <tr>
58  * <td>{@link java.lang.String}</td>
59  * <td>The string.</td>
60  * <td>&nbsp;</td>
61  * </tr>
62  * <tr>
63  * <td>{@link org.springframework.webflow.execution.Event}</td>
64  * <td>The resulting event object.</td>
65  * <td>&nbsp;</td>
66  * </tr>
67  * </table>
68  *
69  * @author Keith Donald
70  * @author Erwin Vervaet
71  */

72 public class ResultObjectBasedEventFactory extends EventFactorySupport implements ResultEventFactory {
73     
74     public Event createResultEvent(Object JavaDoc source, Object JavaDoc resultObject, RequestContext context) {
75         if (resultObject == null) {
76             // this handles the case where the declared result return type is mapped
77
// by this class but the value is null
78
return event(source, getNullEventId());
79         }
80         else if (isBoolean(resultObject.getClass())) {
81             return event(source, ((Boolean JavaDoc)resultObject).booleanValue());
82         }
83         else if (isLabeledEnum(resultObject.getClass())) {
84             String JavaDoc resultId = ((LabeledEnum)resultObject).getLabel();
85             return event(source, resultId, getResultAttributeName(), resultObject);
86         }
87         else if (isJdk5Enum(resultObject.getClass())) {
88             // java.lang.Enum.toString() returns the name!
89
return event(source, resultObject.toString(), getResultAttributeName(), resultObject);
90         }
91         else if (isString(resultObject.getClass())) {
92             return event(source, (String JavaDoc)resultObject);
93         }
94         else if (isEvent(resultObject.getClass())) {
95             return (Event)resultObject;
96         }
97         else {
98             throw new IllegalArgumentException JavaDoc("Cannot deal with result object '" + resultObject +
99                     "' of type '" + resultObject.getClass() + "'");
100         }
101     }
102
103     /**
104      * Check whether or not given type is mapped to a corresponding
105      * event using special mapping rules.
106      */

107     public boolean isMappedValueType(Class JavaDoc type) {
108         return isBoolean(type) || isLabeledEnum(type) || isJdk5Enum(type) || isString(type) || isEvent(type);
109     }
110     
111     // internal helpers to determine the 'type' of a class
112

113     private boolean isBoolean(Class JavaDoc type) {
114         return Boolean JavaDoc.class.equals(type) || boolean.class.equals(type);
115     }
116
117     private boolean isLabeledEnum(Class JavaDoc type) {
118         return LabeledEnum.class.isAssignableFrom(type);
119     }
120
121     private boolean isJdk5Enum(Class JavaDoc type) {
122         if (JdkVersion.getMajorJavaVersion() >= JdkVersion.JAVA_15) {
123             return type.isEnum();
124         }
125         else {
126             return false;
127         }
128     }
129
130     private boolean isString(Class JavaDoc type) {
131         return String JavaDoc.class.equals(type);
132     }
133     
134     private boolean isEvent(Class JavaDoc type) {
135         return Event.class.isAssignableFrom(type);
136     }
137 }
Popular Tags