1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.seasar.cubby.internal.controller.impl; |
18 | |
|
19 | |
import static org.seasar.cubby.action.RequestParameterBindingType.NONE; |
20 | |
import static org.seasar.cubby.internal.util.LogMessages.format; |
21 | |
|
22 | |
import java.lang.reflect.InvocationTargetException; |
23 | |
import java.lang.reflect.Method; |
24 | |
import java.util.Map; |
25 | |
|
26 | |
import javax.servlet.http.HttpServletRequest; |
27 | |
|
28 | |
import org.seasar.cubby.action.Action; |
29 | |
import org.seasar.cubby.action.ActionContext; |
30 | |
import org.seasar.cubby.action.ActionErrors; |
31 | |
import org.seasar.cubby.action.ActionException; |
32 | |
import org.seasar.cubby.action.Form; |
33 | |
import org.seasar.cubby.action.InitializeMethod; |
34 | |
import org.seasar.cubby.action.PostRenderMethod; |
35 | |
import org.seasar.cubby.action.PreRenderMethod; |
36 | |
import org.seasar.cubby.action.RequestParameterBindingType; |
37 | |
import org.seasar.cubby.spi.beans.Attribute; |
38 | |
import org.seasar.cubby.spi.beans.BeanDesc; |
39 | |
import org.seasar.cubby.spi.beans.BeanDescFactory; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
class ActionContextImpl implements ActionContext { |
47 | |
|
48 | |
|
49 | |
private final Object action; |
50 | |
|
51 | |
|
52 | |
private final Class<?> actionClass; |
53 | |
|
54 | |
|
55 | |
private final Method actionMethod; |
56 | |
|
57 | |
|
58 | |
private final ActionErrors actionErrors; |
59 | |
|
60 | |
|
61 | |
private final Map<String, Object> flashMap; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public ActionContextImpl(final HttpServletRequest request, |
80 | |
final Object action, final Class<?> actionClass, |
81 | |
final Method actionMethod, final ActionErrors actionErrors, |
82 | 15 | final Map<String, Object> flashMap) { |
83 | 15 | this.action = action; |
84 | 15 | this.actionClass = actionClass; |
85 | 15 | this.actionMethod = actionMethod; |
86 | |
|
87 | 15 | this.actionErrors = actionErrors; |
88 | 15 | this.flashMap = flashMap; |
89 | |
|
90 | 15 | if (action instanceof Action) { |
91 | 3 | initializeAction((Action) action, actionErrors, flashMap); |
92 | |
} |
93 | 15 | } |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
private void initializeAction(final Action action, |
106 | |
final ActionErrors actionErrors, final Map<String, Object> flashMap) { |
107 | 3 | action.setErrors(actionErrors); |
108 | 3 | action.setFlash(flashMap); |
109 | 3 | } |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
public Object getAction() { |
115 | 4 | return action; |
116 | |
} |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
public Class<?> getActionClass() { |
122 | 2 | return actionClass; |
123 | |
} |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public Method getActionMethod() { |
129 | 4 | return actionMethod; |
130 | |
} |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public Object getFormBean() { |
136 | 8 | final Form form = getForm(); |
137 | 8 | if (form == null) { |
138 | 1 | return action; |
139 | |
} |
140 | 7 | if (form.bindingType() == NONE) { |
141 | 1 | return null; |
142 | |
} |
143 | 6 | if (Form.THIS.equals(form.value())) { |
144 | 2 | return action; |
145 | |
} |
146 | |
|
147 | 4 | final String attributeName = form.value(); |
148 | |
final Object formBean; |
149 | 4 | final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(actionClass); |
150 | 4 | if (beanDesc.hasPropertyAttribute(attributeName)) { |
151 | 3 | final Attribute attribute = beanDesc |
152 | |
.getPropertyAttribute(attributeName); |
153 | 3 | formBean = attribute.getValue(action); |
154 | 3 | if (formBean == null) { |
155 | 1 | throw new ActionException(format("ECUB0102", actionClass, |
156 | |
attributeName)); |
157 | |
} |
158 | 2 | } else if (beanDesc.hasFieldAttribute(attributeName)) { |
159 | 0 | final Attribute attribute = beanDesc |
160 | |
.getFieldAttribute(attributeName); |
161 | 0 | formBean = attribute.getValue(action); |
162 | 0 | if (formBean == null) { |
163 | 0 | throw new ActionException(format("ECUB0111", actionClass, |
164 | |
attributeName)); |
165 | |
} |
166 | 0 | } else { |
167 | 1 | throw new ActionException(format("ECUB0112", actionClass, |
168 | |
attributeName)); |
169 | |
} |
170 | 2 | return formBean; |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
private Form getForm() { |
180 | |
final Form form; |
181 | 14 | if (actionMethod.isAnnotationPresent(Form.class)) { |
182 | 12 | form = actionMethod.getAnnotation(Form.class); |
183 | |
} else { |
184 | 2 | form = actionClass.getAnnotation(Form.class); |
185 | |
} |
186 | 14 | return form; |
187 | |
} |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
public boolean isBindRequestParameterToAllProperties() { |
193 | 6 | final Form form = this.getForm(); |
194 | 6 | if (form == null) { |
195 | 1 | return false; |
196 | |
} |
197 | |
|
198 | 5 | final RequestParameterBindingType type = form.bindingType(); |
199 | 1 | switch (type) { |
200 | |
case ALL_PROPERTIES: |
201 | 3 | return true; |
202 | |
case ONLY_SPECIFIED_PROPERTIES: |
203 | 1 | return false; |
204 | |
default: |
205 | 1 | throw new IllegalStateException(type.toString()); |
206 | |
} |
207 | |
} |
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
public void invokeInitializeMethod() { |
213 | 4 | if (action instanceof Action) { |
214 | 1 | ((Action) action).invokeInitializeMethod(actionMethod); |
215 | 3 | } else if (actionMethod.isAnnotationPresent(InitializeMethod.class)) { |
216 | 1 | final InitializeMethod initializeMethod = actionMethod |
217 | |
.getAnnotation(InitializeMethod.class); |
218 | 1 | final String methodName = initializeMethod.value(); |
219 | 1 | this.invoke(action, methodName); |
220 | |
} |
221 | 4 | } |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
public void invokePreRenderMethod() { |
227 | 2 | if (action instanceof Action) { |
228 | 1 | ((Action) action).invokePreRenderMethod(actionMethod); |
229 | 1 | } else if (actionMethod.isAnnotationPresent(PreRenderMethod.class)) { |
230 | 1 | final PreRenderMethod preRenderMethod = actionMethod |
231 | |
.getAnnotation(PreRenderMethod.class); |
232 | 1 | final String methodName = preRenderMethod.value(); |
233 | 1 | this.invoke(action, methodName); |
234 | |
} |
235 | 2 | } |
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
public void invokePostRenderMethod() { |
241 | 2 | if (action instanceof Action) { |
242 | 1 | ((Action) action).invokePostRenderMethod(actionMethod); |
243 | 1 | } else if (actionMethod.isAnnotationPresent(PostRenderMethod.class)) { |
244 | 1 | final PostRenderMethod postRenderMethod = actionMethod |
245 | |
.getAnnotation(PostRenderMethod.class); |
246 | 1 | final String methodName = postRenderMethod.value(); |
247 | 1 | this.invoke(action, methodName); |
248 | |
} |
249 | 2 | } |
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
public ActionErrors getActionErrors() { |
255 | 1 | return actionErrors; |
256 | |
} |
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
public Map<String, Object> getFlashMap() { |
262 | 1 | return flashMap; |
263 | |
} |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
public void clearFlash() { |
269 | 1 | if (flashMap != null) { |
270 | 1 | flashMap.clear(); |
271 | |
} |
272 | 1 | } |
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
private void invoke(final Object action, final String methodName) { |
281 | |
try { |
282 | 3 | final Method method = action.getClass().getMethod(methodName); |
283 | 3 | method.invoke(action); |
284 | 0 | } catch (final NoSuchMethodException e) { |
285 | 0 | throw new ActionException(e); |
286 | 0 | } catch (final IllegalAccessException e) { |
287 | 0 | throw new ActionException(e); |
288 | 0 | } catch (final InvocationTargetException e) { |
289 | 0 | throw new ActionException(e); |
290 | 3 | } |
291 | 3 | } |
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
@Override |
297 | |
public String toString() { |
298 | 2 | final StringBuilder builder = new StringBuilder(); |
299 | 2 | builder.append("ActionContext["); |
300 | 2 | builder.append("action=").append(action); |
301 | 2 | builder.append(",actionClass=").append(actionClass); |
302 | 2 | builder.append(",actionMethod=").append(actionMethod); |
303 | 2 | builder.append("]"); |
304 | 2 | return builder.toString(); |
305 | |
} |
306 | |
|
307 | |
} |