1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.kuali.rice.kns.web.struts.form.pojo; |
19 | |
|
20 | |
import org.apache.commons.beanutils.PropertyUtils; |
21 | |
import org.apache.commons.lang.StringUtils; |
22 | |
import org.apache.commons.lang.time.StopWatch; |
23 | |
import org.apache.log4j.Logger; |
24 | |
import org.apache.struts.action.ActionForm; |
25 | |
import org.kuali.rice.core.framework.services.CoreFrameworkServiceLocator; |
26 | |
import org.kuali.rice.core.web.format.FormatException; |
27 | |
import org.kuali.rice.core.web.format.Formatter; |
28 | |
import org.kuali.rice.kns.util.WebUtils; |
29 | |
import org.kuali.rice.kns.web.EditablePropertiesHistoryHolder; |
30 | |
import org.kuali.rice.krad.exception.ValidationException; |
31 | |
import org.kuali.rice.krad.util.GlobalVariables; |
32 | |
import org.kuali.rice.krad.util.KRADConstants; |
33 | |
import org.kuali.rice.krad.util.ObjectUtils; |
34 | |
|
35 | |
import javax.servlet.http.HttpServletRequest; |
36 | |
import java.lang.reflect.InvocationTargetException; |
37 | |
import java.util.ArrayList; |
38 | |
import java.util.Collections; |
39 | |
import java.util.Enumeration; |
40 | |
import java.util.HashMap; |
41 | |
import java.util.HashSet; |
42 | |
import java.util.Iterator; |
43 | |
import java.util.List; |
44 | |
import java.util.Map; |
45 | |
import java.util.Set; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 0 | public class PojoFormBase extends ActionForm implements PojoForm { |
53 | |
private static final long serialVersionUID = 1L; |
54 | |
|
55 | |
|
56 | 0 | private static final Logger LOG = Logger.getLogger(PojoFormBase.class); |
57 | |
|
58 | |
private static final String PREVIOUS_REQUEST_EDITABLE_PROPERTIES_GUID = "editablePropertiesGuid"; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
static final String CREATE_ERR_MSG = "Can't create formatter for keypath "; |
64 | |
static final String CONVERT_ERR_MSG = "Can't convert value for keypath: "; |
65 | |
|
66 | 0 | static Map classCache = Collections.synchronizedMap(new HashMap()); |
67 | |
|
68 | 0 | private Map unconvertedValues = new HashMap(); |
69 | 0 | private List unknownKeys = new ArrayList(); |
70 | 0 | private Map formatterTypes = new HashMap(); |
71 | 0 | private List<String> maxUploadFileSizes = new ArrayList<String>(); |
72 | 0 | private Set<String> editableProperties = new HashSet<String>(); |
73 | 0 | protected Set<String> requiredNonEditableProperties = new HashSet<String>(); |
74 | |
private String strutsActionMappingScope; |
75 | 0 | private boolean isNewForm = true; |
76 | |
|
77 | |
private String populateEditablePropertiesGuid; |
78 | |
private String actionEditablePropertiesGuid; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
@Override |
108 | |
public void postprocessRequestParameters(Map requestParameters) { |
109 | |
|
110 | 0 | } |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
@Override |
121 | |
public void populate(HttpServletRequest request) { |
122 | 0 | String watchName = "PojoFormBase.populate"; |
123 | 0 | StopWatch watch = new StopWatch(); |
124 | 0 | watch.start(); |
125 | 0 | if (LOG.isDebugEnabled()) { |
126 | 0 | LOG.debug(watchName + ": started"); |
127 | |
} |
128 | 0 | unconvertedValues.clear(); |
129 | 0 | unknownKeys = new ArrayList(); |
130 | 0 | addRequiredNonEditableProperties(); |
131 | 0 | Map params = request.getParameterMap(); |
132 | |
|
133 | 0 | String contentType = request.getContentType(); |
134 | 0 | String method = request.getMethod(); |
135 | |
|
136 | 0 | if ("POST".equalsIgnoreCase(method) && contentType != null && contentType.startsWith("multipart/form-data")) { |
137 | 0 | Map fileElements = (HashMap)request.getAttribute(KRADConstants.UPLOADED_FILE_REQUEST_ATTRIBUTE_KEY); |
138 | 0 | Enumeration names = Collections.enumeration(fileElements.keySet()); |
139 | 0 | while (names.hasMoreElements()) { |
140 | 0 | String name = (String) names.nextElement(); |
141 | 0 | params.put(name, fileElements.get(name)); |
142 | 0 | } |
143 | |
} |
144 | |
|
145 | 0 | postprocessRequestParameters(params); |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | 0 | for (Iterator iter = params.keySet().iterator(); iter.hasNext();) { |
153 | 0 | String keypath = (String) iter.next(); |
154 | 0 | if (shouldPropertyBePopulatedInForm(keypath, request)) { |
155 | 0 | Object param = params.get(keypath); |
156 | |
|
157 | |
|
158 | 0 | populateForProperty(keypath, param, params); |
159 | |
} |
160 | 0 | } |
161 | 0 | this.registerIsNewForm(false); |
162 | 0 | watch.stop(); |
163 | 0 | if (LOG.isDebugEnabled()) { |
164 | 0 | LOG.debug(watchName + ": " + watch.toString()); |
165 | |
} |
166 | 0 | } |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
protected void populateForProperty(String paramPath, Object paramValue, |
177 | |
Map params) { |
178 | |
|
179 | 0 | Class type = null; |
180 | |
try { |
181 | |
|
182 | |
|
183 | 0 | type = getPropertyType(paramPath); |
184 | |
} |
185 | 0 | catch (Exception e) { |
186 | |
|
187 | 0 | } |
188 | |
|
189 | |
|
190 | 0 | if (type == null) { |
191 | 0 | unknownKeys.add(paramPath); |
192 | |
} |
193 | |
else { |
194 | 0 | Formatter formatter = null; |
195 | |
try { |
196 | 0 | formatter = buildFormatter(paramPath, type, params); |
197 | |
|
198 | 0 | ObjectUtils.setObjectProperty(formatter, this, paramPath, type, paramValue); |
199 | |
} |
200 | 0 | catch (FormatException e1) { |
201 | 0 | GlobalVariables.getMessageMap().putError(paramPath, e1.getErrorKey(), e1.getErrorArgs()); |
202 | 0 | cacheUnconvertedValue(paramPath, paramValue); |
203 | |
} |
204 | 0 | catch (InvocationTargetException e1) { |
205 | 0 | if (e1.getTargetException().getClass().equals(FormatException.class)) { |
206 | |
|
207 | 0 | FormatException formatException = (FormatException) e1.getTargetException(); |
208 | 0 | GlobalVariables.getMessageMap().putError(paramPath, formatException.getErrorKey(), formatException.getErrorArgs()); |
209 | 0 | cacheUnconvertedValue(paramPath, paramValue); |
210 | 0 | } |
211 | |
else { |
212 | 0 | LOG.error("Error occurred in populate " + e1.getMessage()); |
213 | 0 | throw new RuntimeException(e1.getMessage(), e1); |
214 | |
} |
215 | |
} |
216 | 0 | catch (Exception e1) { |
217 | 0 | LOG.error("Error occurred in populate " + e1.getMessage()); |
218 | 0 | LOG.error("FormClass: " + this.getClass().getName() ); |
219 | 0 | LOG.error("keypath: " + paramPath ); |
220 | 0 | LOG.error("Detected Type: " + type.getName() ); |
221 | 0 | LOG.error( "Value: " + paramValue ); |
222 | 0 | if ( paramValue != null ) { |
223 | 0 | LOG.error( "Value Class: " + paramValue.getClass().getName() ); |
224 | |
} |
225 | 0 | throw new RuntimeException(e1.getMessage(), e1); |
226 | 0 | } |
227 | |
} |
228 | 0 | } |
229 | |
|
230 | |
|
231 | |
private Formatter buildFormatter(String keypath, Class propertyType, Map requestParams) { |
232 | 0 | Formatter formatter = buildFormatterForKeypath(keypath, propertyType, requestParams); |
233 | 0 | if (formatter == null) { |
234 | 0 | formatter = buildFormatterForType(propertyType); |
235 | |
} |
236 | 0 | return formatter; |
237 | |
} |
238 | |
|
239 | |
|
240 | |
|
241 | |
private Formatter buildFormatterForKeypath(String keypath, Class propertyType, Map requestParams) { |
242 | 0 | Formatter formatter = null; |
243 | |
|
244 | 0 | Class formatterClass = formatterClassForKeypath(keypath); |
245 | |
|
246 | 0 | if (formatterClass != null) { |
247 | |
try { |
248 | 0 | formatter = (Formatter) formatterClass.newInstance(); |
249 | |
} |
250 | 0 | catch (InstantiationException e) { |
251 | 0 | throw new FormatException("unable to instantiate formatter class '" + formatterClass.getName() + "'", e); |
252 | |
} |
253 | 0 | catch (IllegalAccessException e) { |
254 | 0 | throw new FormatException("unable to access formatter class '" + formatterClass.getName() + "'", e); |
255 | 0 | } |
256 | 0 | formatter.setPropertyType(propertyType); |
257 | |
} |
258 | 0 | return formatter; |
259 | |
} |
260 | |
|
261 | |
|
262 | |
|
263 | |
private Formatter buildFormatterForType(Class propertyType) { |
264 | 0 | Formatter formatter = null; |
265 | |
|
266 | 0 | if (Formatter.findFormatter(propertyType) != null) { |
267 | 0 | formatter = Formatter.getFormatter(propertyType); |
268 | |
} |
269 | 0 | return formatter; |
270 | |
} |
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
protected Class getPropertyType(String keypath) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { |
282 | 0 | Map propertyTypes = (Map) classCache.get(getClass()); |
283 | 0 | if (propertyTypes == null) { |
284 | 0 | propertyTypes = new HashMap(); |
285 | 0 | classCache.put(getClass(), propertyTypes); |
286 | |
} |
287 | |
|
288 | |
|
289 | 0 | if (!propertyTypes.containsKey(keypath)) { |
290 | 0 | Class type = ObjectUtils.easyGetPropertyType(this, keypath); |
291 | 0 | propertyTypes.put(keypath, type); |
292 | |
} |
293 | |
|
294 | 0 | Class propertyType = (Class) propertyTypes.get(keypath); |
295 | 0 | return propertyType; |
296 | |
} |
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
protected Formatter getFormatter(String keypath, Class propertyType) { |
307 | |
|
308 | 0 | Class type = formatterClassForKeypath(keypath); |
309 | |
|
310 | |
Formatter formatter; |
311 | 0 | if (type == null) { |
312 | |
|
313 | 0 | formatter = Formatter.getFormatter(propertyType); |
314 | |
} |
315 | |
else { |
316 | |
try { |
317 | 0 | formatter = (Formatter) type.newInstance(); |
318 | 0 | formatter.setPropertyType(propertyType); |
319 | |
} |
320 | 0 | catch (Exception e) { |
321 | 0 | throw new ValidationException(CREATE_ERR_MSG, e); |
322 | 0 | } |
323 | |
} |
324 | 0 | return formatter; |
325 | |
} |
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
protected Class formatterClassForKeypath(String keypath) { |
336 | |
|
337 | 0 | String indexlessKey = keypath.replaceAll("(\\[[0-9]*+\\]|\\(.*?\\))", ""); |
338 | |
|
339 | 0 | return (Class)formatterTypes.get( indexlessKey ); |
340 | |
} |
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
@Override |
351 | |
public Object formatValue(Object value, String keypath, Class type) { |
352 | |
|
353 | 0 | Formatter formatter = getFormatter(keypath, type); |
354 | 0 | if ( LOG.isDebugEnabled() ) { |
355 | 0 | LOG.debug("formatValue (value,keypath,type) = (" + value + "," + keypath + "," + type.getName() + ")"); |
356 | |
} |
357 | |
|
358 | |
try { |
359 | 0 | return Formatter.isSupportedType(type) ? formatter.formatForPresentation(value) : value; |
360 | |
} |
361 | 0 | catch (FormatException e) { |
362 | 0 | GlobalVariables.getMessageMap().putError(keypath, e.getErrorKey(), e.getErrorArgs()); |
363 | 0 | return value.toString(); |
364 | |
} |
365 | |
} |
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
public void setFormatterType(String keypath, Class type) { |
372 | 0 | formatterTypes.put(keypath, type); |
373 | 0 | } |
374 | |
|
375 | |
@Override |
376 | |
public Map getUnconvertedValues() { |
377 | 0 | return unconvertedValues; |
378 | |
} |
379 | |
|
380 | |
public void setUnconvertedValues(Map unconvertedValues) { |
381 | 0 | this.unconvertedValues = unconvertedValues; |
382 | 0 | } |
383 | |
|
384 | |
protected List getUnknownKeys() { |
385 | 0 | return unknownKeys; |
386 | |
} |
387 | |
|
388 | |
protected void cacheUnconvertedValue(String key, Object value) { |
389 | 0 | Class type = value.getClass(); |
390 | 0 | if (type.isArray()) { |
391 | 0 | value = Formatter.isEmptyValue(value) ? null : ((Object[]) value)[0]; |
392 | |
} |
393 | |
|
394 | 0 | unconvertedValues.put(key, value); |
395 | 0 | } |
396 | |
|
397 | |
|
398 | |
@Override |
399 | |
public void processValidationFail() { |
400 | |
|
401 | 0 | } |
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
public Map getFormatterTypes() { |
412 | 0 | return formatterTypes; |
413 | |
} |
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
|
420 | |
|
421 | |
|
422 | |
public void setFormatterTypes(Map formatterTypes) { |
423 | 0 | this.formatterTypes = formatterTypes; |
424 | 0 | } |
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
|
431 | |
|
432 | |
|
433 | |
|
434 | |
protected final void addMaxUploadSize( String sizeString ) { |
435 | 0 | maxUploadFileSizes.add( sizeString ); |
436 | 0 | } |
437 | |
|
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
protected final void initMaxUploadSizes() { |
443 | 0 | if ( maxUploadFileSizes.isEmpty() ) { |
444 | 0 | customInitMaxUploadSizes(); |
445 | |
|
446 | 0 | if ( maxUploadFileSizes.isEmpty() ) { |
447 | 0 | addMaxUploadSize(CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString( |
448 | |
KRADConstants.KRAD_NAMESPACE, KRADConstants.DetailTypes.ALL_DETAIL_TYPE, KRADConstants.MAX_UPLOAD_SIZE_PARM_NM)); |
449 | |
} |
450 | |
} |
451 | 0 | } |
452 | |
|
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
protected void customInitMaxUploadSizes() { |
458 | |
|
459 | 0 | } |
460 | |
|
461 | |
public final List<String> getMaxUploadSizes() { |
462 | 0 | initMaxUploadSizes(); |
463 | |
|
464 | 0 | return maxUploadFileSizes; |
465 | |
} |
466 | |
|
467 | |
@Override |
468 | |
public void registerEditableProperty(String editablePropertyName){ |
469 | 0 | if ( LOG.isDebugEnabled() ) { |
470 | 0 | LOG.debug( "KualiSessionId: " + GlobalVariables.getUserSession().getKualiSessionId() + " -- Registering Property: " + editablePropertyName ); |
471 | |
} |
472 | 0 | editableProperties.add(editablePropertyName); |
473 | 0 | } |
474 | |
|
475 | |
public void registerRequiredNonEditableProperty(String requiredNonEditableProperty) { |
476 | 0 | requiredNonEditableProperties.add(requiredNonEditableProperty); |
477 | 0 | } |
478 | |
|
479 | |
@Override |
480 | |
public void clearEditablePropertyInformation(){ |
481 | 0 | if ( LOG.isDebugEnabled() ) { |
482 | 0 | LOG.debug( "KualiSessionId: " + GlobalVariables.getUserSession().getKualiSessionId() + " -- Clearing Editable Properties" ); |
483 | |
} |
484 | 0 | editableProperties = new HashSet<String>(); |
485 | 0 | } |
486 | |
|
487 | |
@Override |
488 | |
public Set<String> getEditableProperties(){ |
489 | 0 | return editableProperties; |
490 | |
} |
491 | |
|
492 | |
public boolean isPropertyEditable(String propertyName) { |
493 | 0 | final Set<String> populateEditableProperties = getPopulateEditableProperties(); |
494 | 0 | return WebUtils.isPropertyEditable(populateEditableProperties, propertyName); |
495 | |
} |
496 | |
|
497 | |
|
498 | |
|
499 | |
|
500 | |
@Override |
501 | |
public void addRequiredNonEditableProperties(){ |
502 | 0 | } |
503 | |
|
504 | |
public boolean isPropertyNonEditableButRequired(String propertyName) { |
505 | 0 | return WebUtils.isPropertyEditable(requiredNonEditableProperties, propertyName); |
506 | |
} |
507 | |
|
508 | |
protected String getParameter(HttpServletRequest request, String parameterName){ |
509 | 0 | return request.getParameter(parameterName); |
510 | |
} |
511 | |
|
512 | |
protected String[] getParameterValues(HttpServletRequest request, String parameterName){ |
513 | 0 | return request.getParameterValues(parameterName); |
514 | |
} |
515 | |
|
516 | |
@Override |
517 | |
public Set<String> getRequiredNonEditableProperties(){ |
518 | 0 | return requiredNonEditableProperties; |
519 | |
} |
520 | |
|
521 | |
|
522 | |
|
523 | |
|
524 | |
@Override |
525 | |
public void registerStrutsActionMappingScope(String strutsActionMappingScope) { |
526 | 0 | this.strutsActionMappingScope = strutsActionMappingScope; |
527 | 0 | } |
528 | |
|
529 | |
public String getStrutsActionMappingScope() { |
530 | 0 | return strutsActionMappingScope; |
531 | |
} |
532 | |
|
533 | |
|
534 | |
|
535 | |
|
536 | |
@Override |
537 | |
public void registerIsNewForm(boolean isNewForm) { |
538 | 0 | this.isNewForm = isNewForm; |
539 | 0 | } |
540 | |
|
541 | |
@Override |
542 | |
public boolean getIsNewForm() { |
543 | 0 | return this.isNewForm; |
544 | |
} |
545 | |
|
546 | |
|
547 | |
|
548 | |
|
549 | |
|
550 | |
@Override |
551 | |
public boolean shouldPropertyBePopulatedInForm(String requestParameterName, HttpServletRequest request) { |
552 | |
|
553 | 0 | if (requestParameterName.equals(PojoFormBase.PREVIOUS_REQUEST_EDITABLE_PROPERTIES_GUID)) { |
554 | 0 | return false; |
555 | |
} |
556 | 0 | else if (StringUtils.equalsIgnoreCase("session",getStrutsActionMappingScope()) && !getIsNewForm()) { |
557 | 0 | return isPropertyEditable(requestParameterName) || isPropertyNonEditableButRequired(requestParameterName); |
558 | |
} |
559 | 0 | return true; |
560 | |
|
561 | |
} |
562 | |
|
563 | |
|
564 | |
|
565 | |
|
566 | |
|
567 | |
|
568 | |
|
569 | |
@Override |
570 | |
public Set<String> getMethodToCallsToBypassSessionRetrievalForGETRequests() { |
571 | 0 | Set<String> defaultMethodToCalls = new HashSet<String>(); |
572 | 0 | defaultMethodToCalls.add(KRADConstants.START_METHOD); |
573 | 0 | return defaultMethodToCalls; |
574 | |
} |
575 | |
|
576 | |
|
577 | |
|
578 | |
|
579 | |
|
580 | |
|
581 | |
|
582 | |
@Override |
583 | |
public void setPopulateEditablePropertiesGuid(String guid) { |
584 | 0 | this.populateEditablePropertiesGuid = guid; |
585 | 0 | } |
586 | |
|
587 | |
|
588 | |
|
589 | |
|
590 | |
public String getPopulateEditablePropertiesGuid() { |
591 | 0 | return this.populateEditablePropertiesGuid; |
592 | |
} |
593 | |
|
594 | |
|
595 | |
|
596 | |
|
597 | |
|
598 | |
@Override |
599 | |
public void setActionEditablePropertiesGuid(String guid) { |
600 | 0 | this.actionEditablePropertiesGuid = guid; |
601 | 0 | } |
602 | |
|
603 | |
|
604 | |
|
605 | |
|
606 | |
public String getActionEditablePropertiesGuid() { |
607 | 0 | return actionEditablePropertiesGuid; |
608 | |
} |
609 | |
|
610 | |
|
611 | |
|
612 | |
|
613 | |
public Set<String> getPopulateEditableProperties() { |
614 | 0 | EditablePropertiesHistoryHolder holder = (EditablePropertiesHistoryHolder) GlobalVariables.getUserSession().getObjectMap().get( |
615 | |
KRADConstants.EDITABLE_PROPERTIES_HISTORY_HOLDER_ATTR_NAME); |
616 | 0 | if (holder == null) { |
617 | 0 | holder = new EditablePropertiesHistoryHolder(); |
618 | |
} |
619 | 0 | GlobalVariables.getUserSession().addObject(KRADConstants.EDITABLE_PROPERTIES_HISTORY_HOLDER_ATTR_NAME, holder); |
620 | |
|
621 | 0 | return holder.getEditableProperties(getPopulateEditablePropertiesGuid()); |
622 | |
} |
623 | |
|
624 | |
|
625 | |
|
626 | |
|
627 | |
public void copyPopulateEditablePropertiesToActionEditableProperties() { |
628 | 0 | Set<String> populateEditableProperties = getPopulateEditableProperties(); |
629 | 0 | for (String property : populateEditableProperties) { |
630 | 0 | registerEditableProperty(property); |
631 | |
} |
632 | 0 | } |
633 | |
|
634 | |
|
635 | |
} |