1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.kuali.rice.krad.uif.service.impl; |
12 | |
|
13 | |
import org.apache.commons.lang.StringUtils; |
14 | |
import org.kuali.rice.kim.bo.Person; |
15 | |
import org.kuali.rice.krad.datadictionary.AttributeDefinition; |
16 | |
import org.kuali.rice.krad.inquiry.Inquirable; |
17 | |
import org.kuali.rice.krad.service.DataDictionaryService; |
18 | |
import org.kuali.rice.krad.service.KRADServiceLocator; |
19 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
20 | |
import org.kuali.rice.krad.uif.UifConstants; |
21 | |
import org.kuali.rice.krad.uif.authorization.Authorizer; |
22 | |
import org.kuali.rice.krad.uif.authorization.PresentationController; |
23 | |
import org.kuali.rice.krad.uif.container.CollectionGroup; |
24 | |
import org.kuali.rice.krad.uif.container.Container; |
25 | |
import org.kuali.rice.krad.uif.container.View; |
26 | |
import org.kuali.rice.krad.uif.core.BindingInfo; |
27 | |
import org.kuali.rice.krad.uif.core.Component; |
28 | |
import org.kuali.rice.krad.uif.core.DataBinding; |
29 | |
import org.kuali.rice.krad.uif.core.PropertyReplacer; |
30 | |
import org.kuali.rice.krad.uif.core.RequestParameter; |
31 | |
import org.kuali.rice.krad.uif.field.AttributeField; |
32 | |
import org.kuali.rice.krad.uif.layout.LayoutManager; |
33 | |
import org.kuali.rice.krad.uif.modifier.ComponentModifier; |
34 | |
import org.kuali.rice.krad.uif.service.ExpressionEvaluatorService; |
35 | |
import org.kuali.rice.krad.uif.service.ViewDictionaryService; |
36 | |
import org.kuali.rice.krad.uif.service.ViewHelperService; |
37 | |
import org.kuali.rice.krad.uif.util.BooleanMap; |
38 | |
import org.kuali.rice.krad.uif.util.CloneUtils; |
39 | |
import org.kuali.rice.krad.uif.util.ComponentFactory; |
40 | |
import org.kuali.rice.krad.uif.util.ComponentUtils; |
41 | |
import org.kuali.rice.krad.uif.util.ObjectPropertyUtils; |
42 | |
import org.kuali.rice.krad.uif.util.ViewModelUtils; |
43 | |
import org.kuali.rice.krad.uif.widget.Inquiry; |
44 | |
import org.kuali.rice.krad.util.GlobalVariables; |
45 | |
import org.kuali.rice.krad.util.KRADConstants; |
46 | |
import org.kuali.rice.krad.util.ObjectUtils; |
47 | |
import org.kuali.rice.krad.valuefinder.ValueFinder; |
48 | |
import org.kuali.rice.krad.web.form.UifFormBase; |
49 | |
import org.springframework.util.MethodInvoker; |
50 | |
|
51 | |
import java.lang.reflect.Field; |
52 | |
import java.util.ArrayList; |
53 | |
import java.util.Collection; |
54 | |
import java.util.HashMap; |
55 | |
import java.util.HashSet; |
56 | |
import java.util.List; |
57 | |
import java.util.Map; |
58 | |
import java.util.Map.Entry; |
59 | |
import java.util.Set; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | 0 | public class ViewHelperServiceImpl implements ViewHelperService { |
67 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ViewHelperServiceImpl.class); |
68 | |
|
69 | |
private transient DataDictionaryService dataDictionaryService; |
70 | |
private transient ExpressionEvaluatorService expressionEvaluatorService; |
71 | |
private transient ViewDictionaryService viewDictionaryService; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
@Override |
87 | |
public void populateViewFromRequestParameters(View view, Map<String, String> parameters) { |
88 | |
|
89 | 0 | Set<String> fieldNamesToPopulate = new HashSet<String>(); |
90 | |
|
91 | 0 | Field[] fields = CloneUtils.getFields(view.getClass(), true); |
92 | 0 | for (int i = 0; i < fields.length; i++) { |
93 | 0 | Field field = fields[i]; |
94 | |
|
95 | 0 | RequestParameter requestParameter = field.getAnnotation(RequestParameter.class); |
96 | 0 | if (requestParameter != null) { |
97 | |
|
98 | 0 | if (StringUtils.isNotBlank(requestParameter.parameterName())) { |
99 | 0 | fieldNamesToPopulate.add(requestParameter.parameterName()); |
100 | |
} else { |
101 | 0 | fieldNamesToPopulate.add(field.getName()); |
102 | |
} |
103 | |
} |
104 | |
} |
105 | |
|
106 | |
|
107 | 0 | Map<String, Set<PropertyReplacer>> viewPropertyReplacers = new HashMap<String, Set<PropertyReplacer>>(); |
108 | 0 | for (PropertyReplacer replacer : view.getPropertyReplacers()) { |
109 | 0 | Set<PropertyReplacer> propertyReplacers = new HashSet<PropertyReplacer>(); |
110 | 0 | if (viewPropertyReplacers.containsKey(replacer.getPropertyName())) { |
111 | 0 | propertyReplacers = viewPropertyReplacers.get(replacer.getPropertyName()); |
112 | |
} |
113 | 0 | propertyReplacers.add(replacer); |
114 | |
|
115 | 0 | viewPropertyReplacers.put(replacer.getPropertyName(), propertyReplacers); |
116 | 0 | } |
117 | |
|
118 | |
|
119 | 0 | Map<String, String> viewRequestParameters = new HashMap<String, String>(); |
120 | 0 | for (String fieldToPopulate : fieldNamesToPopulate) { |
121 | 0 | if (parameters.containsKey(fieldToPopulate)) { |
122 | 0 | String fieldValue = parameters.get(fieldToPopulate); |
123 | |
|
124 | 0 | if (StringUtils.isNotBlank(fieldValue)) { |
125 | 0 | viewRequestParameters.put(fieldToPopulate, fieldValue); |
126 | 0 | ObjectPropertyUtils.setPropertyValue(view, fieldToPopulate, fieldValue); |
127 | |
|
128 | |
|
129 | |
|
130 | 0 | String conditionalProperty = StringUtils.substring(fieldToPopulate, 0, 1).toLowerCase() |
131 | |
+ StringUtils.substring(fieldToPopulate, 1, fieldToPopulate.length()); |
132 | 0 | conditionalProperty = UifConstants.EL_CONDITIONAL_PROPERTY_PREFIX + conditionalProperty; |
133 | 0 | ObjectPropertyUtils.setPropertyValue(view, conditionalProperty, fieldValue, true); |
134 | |
|
135 | 0 | if (viewPropertyReplacers.containsKey(fieldToPopulate)) { |
136 | 0 | Set<PropertyReplacer> propertyReplacers = viewPropertyReplacers.get(fieldToPopulate); |
137 | 0 | for (PropertyReplacer replacer : propertyReplacers) { |
138 | 0 | view.getPropertyReplacers().remove(replacer); |
139 | |
} |
140 | |
} |
141 | |
} |
142 | 0 | } |
143 | |
} |
144 | |
|
145 | 0 | view.setViewRequestParameters(viewRequestParameters); |
146 | 0 | } |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
@Override |
153 | |
public void performInitialization(View view) { |
154 | |
|
155 | 0 | ComponentUtils.processIds(view, new HashMap<String, Integer>()); |
156 | |
|
157 | 0 | performComponentInitialization(view, view); |
158 | 0 | } |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
public void performComponentLifecycle(UifFormBase form, Component component, String origId){ |
171 | 0 | Component origComponent = form.getView().getViewIndex().getComponentById(origId); |
172 | |
|
173 | 0 | Component parent = (Component) origComponent.getContext().get(UifConstants.ContextVariableNames.PARENT); |
174 | 0 | component.pushAllToContext(origComponent.getContext()); |
175 | |
|
176 | 0 | performComponentInitialization(form.getView(), component); |
177 | 0 | performComponentApplyModel(form.getView(), component, form); |
178 | 0 | performComponentFinalize(form.getView(), component, form, parent); |
179 | |
|
180 | 0 | component.setId(origId); |
181 | 0 | } |
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
public void performComponentInitialization(View view, Component component) { |
209 | 0 | if (component == null) { |
210 | 0 | return; |
211 | |
} |
212 | |
|
213 | |
|
214 | 0 | if (StringUtils.isBlank(component.getId())) { |
215 | 0 | component.setId(view.getNextId()); |
216 | |
} |
217 | |
|
218 | 0 | LOG.debug("Initializing component: " + component.getId() + " with type: " + component.getClass()); |
219 | |
|
220 | |
|
221 | 0 | component.performInitialization(view); |
222 | |
|
223 | |
|
224 | 0 | if (component instanceof AttributeField) { |
225 | 0 | initializeAttributeFieldFromDataDictionary(view, (AttributeField) component); |
226 | |
} |
227 | |
|
228 | |
|
229 | 0 | if (component instanceof CollectionGroup) { |
230 | |
|
231 | |
} |
232 | |
|
233 | |
|
234 | 0 | runComponentModifiers(view, component, null, UifConstants.ViewPhases.INITIALIZE); |
235 | |
|
236 | |
|
237 | 0 | for (Component nestedComponent : component.getNestedComponents()) { |
238 | 0 | performComponentInitialization(view, nestedComponent); |
239 | |
} |
240 | |
|
241 | |
|
242 | 0 | for (PropertyReplacer replacer : component.getPropertyReplacers()) { |
243 | 0 | if (Component.class.isAssignableFrom(replacer.getReplacement().getClass())) { |
244 | 0 | performComponentInitialization(view, (Component) replacer.getReplacement()); |
245 | |
} |
246 | |
} |
247 | |
|
248 | |
|
249 | 0 | performCustomInitialization(view, component); |
250 | 0 | } |
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
protected void initializeAttributeFieldFromDataDictionary(View view, AttributeField field) { |
262 | 0 | AttributeDefinition attributeDefinition = null; |
263 | |
|
264 | 0 | String dictionaryAttributeName = field.getDictionaryAttributeName(); |
265 | 0 | String dictionaryObjectEntry = field.getDictionaryObjectEntry(); |
266 | |
|
267 | |
|
268 | |
|
269 | 0 | if (StringUtils.isNotBlank(dictionaryObjectEntry) && StringUtils.isBlank(dictionaryAttributeName)) { |
270 | 0 | dictionaryAttributeName = field.getPropertyName(); |
271 | |
} |
272 | |
|
273 | |
|
274 | 0 | if (StringUtils.isNotBlank(dictionaryAttributeName) && StringUtils.isNotBlank(dictionaryObjectEntry)) { |
275 | 0 | attributeDefinition = getDataDictionaryService().getAttributeDefinition(dictionaryObjectEntry, |
276 | |
dictionaryAttributeName); |
277 | |
} |
278 | |
|
279 | |
|
280 | 0 | if (attributeDefinition == null) { |
281 | 0 | String propertyPath = field.getBindingInfo().getBindingPath(); |
282 | 0 | if (StringUtils.isNotBlank(field.getBindingInfo().getCollectionPath())) { |
283 | 0 | propertyPath = field.getBindingInfo().getCollectionPath(); |
284 | 0 | if (StringUtils.isNotBlank(field.getBindingInfo().getBindByNamePrefix())) { |
285 | 0 | propertyPath += "." + field.getBindingInfo().getBindByNamePrefix(); |
286 | |
} |
287 | 0 | propertyPath += "." + field.getBindingInfo().getBindingName(); |
288 | |
} |
289 | |
|
290 | 0 | attributeDefinition = findNestedDictionaryAttribute(view, field, null, propertyPath); |
291 | |
} |
292 | |
|
293 | |
|
294 | 0 | if (attributeDefinition != null) { |
295 | 0 | field.copyFromAttributeDefinition(attributeDefinition); |
296 | |
} |
297 | |
|
298 | 0 | if (field.getControl() == null) { |
299 | 0 | field.setControl(ComponentFactory.getTextControl()); |
300 | |
} |
301 | 0 | } |
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
protected AttributeDefinition findNestedDictionaryAttribute(View view, AttributeField field, String parentPath, |
333 | |
String propertyPath) { |
334 | 0 | AttributeDefinition attributeDefinition = null; |
335 | |
|
336 | |
|
337 | 0 | String dictionaryAttributeName = propertyPath; |
338 | 0 | String dictionaryObjectEntry = null; |
339 | |
|
340 | 0 | if (field.getBindingInfo().isBindToMap()) { |
341 | 0 | parentPath = ""; |
342 | 0 | if (!field.getBindingInfo().isBindToForm() && StringUtils.isNotBlank(field.getBindingInfo().getBindingObjectPath())) { |
343 | 0 | parentPath = field.getBindingInfo().getBindingObjectPath(); |
344 | |
} |
345 | 0 | if (StringUtils.isNotBlank(field.getBindingInfo().getBindByNamePrefix())) { |
346 | 0 | if (StringUtils.isNotBlank(parentPath)) { |
347 | 0 | parentPath += "." + field.getBindingInfo().getBindByNamePrefix(); |
348 | |
} |
349 | |
else { |
350 | 0 | parentPath = field.getBindingInfo().getBindByNamePrefix(); |
351 | |
} |
352 | |
} |
353 | |
|
354 | 0 | dictionaryAttributeName = field.getBindingInfo().getBindingName(); |
355 | |
} |
356 | |
|
357 | 0 | if (StringUtils.isNotBlank(parentPath)) { |
358 | 0 | Class<?> dictionaryModelClass = ViewModelUtils.getPropertyTypeByClassAndView(view, parentPath); |
359 | 0 | if (dictionaryModelClass != null) { |
360 | 0 | dictionaryObjectEntry = dictionaryModelClass.getName(); |
361 | |
|
362 | 0 | attributeDefinition = getDataDictionaryService().getAttributeDefinition(dictionaryObjectEntry, |
363 | |
dictionaryAttributeName); |
364 | |
} |
365 | |
} |
366 | |
|
367 | |
|
368 | |
|
369 | 0 | if ((attributeDefinition == null) && StringUtils.contains(propertyPath, ".")) { |
370 | 0 | String nextParentPath = StringUtils.substringBefore(propertyPath, "."); |
371 | 0 | if (StringUtils.isNotBlank(parentPath)) { |
372 | 0 | nextParentPath = parentPath + "." + nextParentPath; |
373 | |
} |
374 | 0 | String nextPropertyPath = StringUtils.substringAfter(propertyPath, "."); |
375 | |
|
376 | 0 | return findNestedDictionaryAttribute(view, field, nextParentPath, nextPropertyPath); |
377 | |
} |
378 | |
|
379 | |
|
380 | 0 | if (attributeDefinition != null) { |
381 | 0 | field.setDictionaryAttributeName(dictionaryAttributeName); |
382 | 0 | field.setDictionaryObjectEntry(dictionaryObjectEntry); |
383 | |
} |
384 | |
|
385 | 0 | return attributeDefinition; |
386 | |
} |
387 | |
|
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
protected Class<?> getDictionaryModelClass(View view, AttributeField field) { |
399 | 0 | return ViewModelUtils.getParentObjectClassForMetadata(view, field); |
400 | |
} |
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
@Override |
407 | |
public void performApplyModel(View view, Object model) { |
408 | |
|
409 | |
|
410 | 0 | invokeAuthorizerPresentationController(view, (UifFormBase) model); |
411 | |
|
412 | |
|
413 | 0 | setViewContext(view, model); |
414 | |
|
415 | 0 | performComponentApplyModel(view, view, model); |
416 | 0 | } |
417 | |
|
418 | |
|
419 | |
|
420 | |
|
421 | |
|
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
protected void invokeAuthorizerPresentationController(View view, UifFormBase model) { |
430 | 0 | PresentationController presentationController = ObjectUtils.newInstance(view.getPresentationControllerClass()); |
431 | 0 | Authorizer authorizer = ObjectUtils.newInstance(view.getAuthorizerClass()); |
432 | |
|
433 | 0 | Person user = GlobalVariables.getUserSession().getPerson(); |
434 | |
|
435 | 0 | Set<String> actionFlags = presentationController.getActionFlags(model); |
436 | 0 | actionFlags = authorizer.getActionFlags(model, user, actionFlags); |
437 | |
|
438 | 0 | view.setActionFlags(new BooleanMap(actionFlags)); |
439 | |
|
440 | 0 | Set<String> editModes = presentationController.getEditModes(model); |
441 | 0 | editModes = authorizer.getEditModes(model, user, editModes); |
442 | |
|
443 | 0 | view.setEditModes(new BooleanMap(editModes)); |
444 | 0 | } |
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
protected void setViewContext(View view, Object model) { |
456 | 0 | view.pushAllToContext(getPreModelContext(view)); |
457 | |
|
458 | |
|
459 | 0 | for (Entry<String, String> variableExpression : view.getExpressionVariables().entrySet()) { |
460 | 0 | String variableName = variableExpression.getKey(); |
461 | 0 | Object value = getExpressionEvaluatorService().evaluateExpression(model, view.getContext(), |
462 | |
variableExpression.getValue()); |
463 | 0 | view.pushObjectToContext(variableName, value); |
464 | 0 | } |
465 | 0 | } |
466 | |
|
467 | |
|
468 | |
|
469 | |
|
470 | |
|
471 | |
|
472 | |
|
473 | |
|
474 | |
|
475 | |
protected Map<String, Object> getPreModelContext(View view) { |
476 | 0 | Map<String, Object> context = new HashMap<String, Object>(); |
477 | |
|
478 | 0 | context.put(UifConstants.ContextVariableNames.VIEW, view); |
479 | 0 | context.put(UifConstants.ContextVariableNames.VIEW_HELPER, this); |
480 | |
|
481 | 0 | Map<String, String> properties = KRADServiceLocator.getKualiConfigurationService().getAllProperties(); |
482 | 0 | context.put(UifConstants.ContextVariableNames.CONFIG_PROPERTIES, properties); |
483 | 0 | context.put(UifConstants.ContextVariableNames.CONSTANTS, KRADConstants.class); |
484 | |
|
485 | 0 | return context; |
486 | |
} |
487 | |
|
488 | |
|
489 | |
|
490 | |
|
491 | |
|
492 | |
|
493 | |
|
494 | |
|
495 | |
|
496 | |
|
497 | |
|
498 | |
|
499 | |
|
500 | |
|
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
|
506 | |
protected void performComponentApplyModel(View view, Component component, Object model) { |
507 | 0 | if (component == null) { |
508 | 0 | return; |
509 | |
} |
510 | |
|
511 | |
|
512 | 0 | component.pushAllToContext(getCommonContext(view, component)); |
513 | 0 | getExpressionEvaluatorService().evaluateObjectProperties(component, model, component.getContext()); |
514 | |
|
515 | 0 | if (component instanceof Container) { |
516 | 0 | LayoutManager layoutManager = ((Container) component).getLayoutManager(); |
517 | |
|
518 | 0 | if (layoutManager != null) { |
519 | 0 | layoutManager.getContext().putAll(getCommonContext(view, component)); |
520 | 0 | layoutManager.pushObjectToContext(UifConstants.ContextVariableNames.PARENT, component); |
521 | 0 | layoutManager.pushObjectToContext(UifConstants.ContextVariableNames.MANAGER, layoutManager); |
522 | 0 | getExpressionEvaluatorService().evaluateObjectProperties(layoutManager, model, |
523 | |
layoutManager.getContext()); |
524 | |
} |
525 | |
} |
526 | |
|
527 | 0 | if (component instanceof DataBinding) { |
528 | 0 | BindingInfo bindingInfo = ((DataBinding) component).getBindingInfo(); |
529 | 0 | getExpressionEvaluatorService().evaluateObjectProperties(bindingInfo, model, component.getContext()); |
530 | |
} |
531 | |
|
532 | |
|
533 | 0 | Component parent = (Component) component.getContext().get(UifConstants.ContextVariableNames.PARENT); |
534 | 0 | component.performApplyModel(view, model, parent); |
535 | |
|
536 | |
|
537 | 0 | performCustomApplyModel(view, component, model); |
538 | |
|
539 | |
|
540 | 0 | runComponentModifiers(view, component, model, UifConstants.ViewPhases.APPLY_MODEL); |
541 | |
|
542 | |
|
543 | 0 | for (Component nestedComponent : component.getNestedComponents()) { |
544 | 0 | if (nestedComponent != null) { |
545 | 0 | nestedComponent.pushObjectToContext(UifConstants.ContextVariableNames.PARENT, component); |
546 | |
} |
547 | |
|
548 | 0 | performComponentApplyModel(view, nestedComponent, model); |
549 | |
} |
550 | 0 | } |
551 | |
|
552 | |
|
553 | |
|
554 | |
|
555 | |
|
556 | |
|
557 | |
|
558 | |
|
559 | |
|
560 | |
|
561 | |
|
562 | |
|
563 | |
|
564 | |
|
565 | |
|
566 | |
protected void runComponentModifiers(View view, Component component, Object model, String runPhase) { |
567 | 0 | for (ComponentModifier modifier : component.getComponentModifiers()) { |
568 | |
|
569 | 0 | if (StringUtils.equals(modifier.getRunPhase(), runPhase)) { |
570 | |
|
571 | 0 | boolean runModifier = true; |
572 | 0 | if (StringUtils.isNotBlank(modifier.getRunCondition())) { |
573 | 0 | Map<String, Object> context = new HashMap<String, Object>(); |
574 | 0 | context.put(UifConstants.ContextVariableNames.COMPONENT, component); |
575 | 0 | context.put(UifConstants.ContextVariableNames.VIEW, view); |
576 | |
|
577 | 0 | String conditionEvaluation = getExpressionEvaluatorService().evaluateExpressionTemplate(model, |
578 | |
context, modifier.getRunCondition()); |
579 | 0 | runModifier = Boolean.parseBoolean(conditionEvaluation); |
580 | |
} |
581 | |
|
582 | 0 | if (runModifier) { |
583 | 0 | if (StringUtils.equals(runPhase, UifConstants.ViewPhases.APPLY_MODEL) |
584 | |
|| StringUtils.equals(runPhase, UifConstants.ViewPhases.FINALIZE)) { |
585 | 0 | modifier.performModification(view, model, component); |
586 | |
} else { |
587 | 0 | modifier.performModification(view, component); |
588 | |
} |
589 | |
} |
590 | 0 | } |
591 | |
} |
592 | 0 | } |
593 | |
|
594 | |
|
595 | |
|
596 | |
|
597 | |
|
598 | |
|
599 | |
|
600 | |
|
601 | |
|
602 | |
|
603 | |
protected Map<String, Object> getCommonContext(View view, Component component) { |
604 | 0 | Map<String, Object> context = new HashMap<String, Object>(); |
605 | |
|
606 | 0 | context.putAll(view.getContext()); |
607 | 0 | context.put(UifConstants.ContextVariableNames.COMPONENT, component); |
608 | |
|
609 | 0 | return context; |
610 | |
} |
611 | |
|
612 | |
|
613 | |
|
614 | |
|
615 | |
|
616 | |
@Override |
617 | |
public void performFinalize(View view, Object model) { |
618 | 0 | performComponentFinalize(view, view, model, null); |
619 | 0 | } |
620 | |
|
621 | |
|
622 | |
|
623 | |
|
624 | |
|
625 | |
|
626 | |
|
627 | |
|
628 | |
|
629 | |
|
630 | |
|
631 | |
|
632 | |
|
633 | |
|
634 | |
protected void performComponentFinalize(View view, Component component, Object model, Component parent) { |
635 | 0 | if (component == null) { |
636 | 0 | return; |
637 | |
} |
638 | |
|
639 | |
|
640 | 0 | invokeMethodFinalizer(view, component, model); |
641 | |
|
642 | |
|
643 | 0 | component.performFinalize(view, model, parent); |
644 | |
|
645 | |
|
646 | 0 | performCustomFinalize(view, component, model, parent); |
647 | |
|
648 | |
|
649 | 0 | runComponentModifiers(view, component, model, UifConstants.ViewPhases.FINALIZE); |
650 | |
|
651 | |
|
652 | 0 | for (Component nestedComponent : component.getNestedComponents()) { |
653 | 0 | performComponentFinalize(view, nestedComponent, model, component); |
654 | |
} |
655 | 0 | } |
656 | |
|
657 | |
|
658 | |
|
659 | |
|
660 | |
|
661 | |
|
662 | |
|
663 | |
|
664 | |
|
665 | |
|
666 | |
|
667 | |
|
668 | |
|
669 | |
|
670 | |
protected void invokeMethodFinalizer(View view, Component component, Object model) { |
671 | 0 | String finalizeMethodToCall = component.getFinalizeMethodToCall(); |
672 | 0 | MethodInvoker finalizeMethodInvoker = component.getFinalizeMethodInvoker(); |
673 | |
|
674 | 0 | if (StringUtils.isBlank(finalizeMethodToCall) && (finalizeMethodInvoker == null)) { |
675 | 0 | return; |
676 | |
} |
677 | |
|
678 | 0 | if (finalizeMethodInvoker == null) { |
679 | 0 | finalizeMethodInvoker = new MethodInvoker(); |
680 | |
} |
681 | |
|
682 | |
|
683 | |
|
684 | 0 | if (StringUtils.isBlank(finalizeMethodInvoker.getTargetMethod())) { |
685 | 0 | finalizeMethodInvoker.setTargetMethod(finalizeMethodToCall); |
686 | |
} |
687 | |
|
688 | |
|
689 | 0 | if ((finalizeMethodInvoker.getTargetClass() == null) && (finalizeMethodInvoker.getTargetObject() == null)) { |
690 | 0 | finalizeMethodInvoker.setTargetObject(view.getViewHelperService()); |
691 | |
} |
692 | |
|
693 | |
|
694 | 0 | List<Object> additionalArguments = component.getFinalizeMethodAdditionalArguments(); |
695 | 0 | if (additionalArguments == null) { |
696 | 0 | additionalArguments = new ArrayList<Object>(); |
697 | |
} |
698 | |
|
699 | 0 | Object[] arguments = new Object[2 + additionalArguments.size()]; |
700 | 0 | arguments[0] = component; |
701 | 0 | arguments[1] = model; |
702 | |
|
703 | 0 | int argumentIndex = 1; |
704 | 0 | for (Object argument : additionalArguments) { |
705 | 0 | argumentIndex++; |
706 | 0 | arguments[argumentIndex] = argument; |
707 | |
} |
708 | 0 | finalizeMethodInvoker.setArguments(arguments); |
709 | |
|
710 | |
|
711 | |
try { |
712 | 0 | LOG.debug("Invoking render method: " + finalizeMethodInvoker.getTargetMethod() + " for component: " |
713 | |
+ component.getId()); |
714 | 0 | finalizeMethodInvoker.prepare(); |
715 | |
|
716 | 0 | Class<?> methodReturnType = finalizeMethodInvoker.getPreparedMethod().getReturnType(); |
717 | 0 | if (StringUtils.equals("void", methodReturnType.getName())) { |
718 | 0 | finalizeMethodInvoker.invoke(); |
719 | |
} else { |
720 | 0 | String renderOutput = (String) finalizeMethodInvoker.invoke(); |
721 | |
|
722 | 0 | component.setSelfRendered(true); |
723 | 0 | component.setRenderOutput(renderOutput); |
724 | |
} |
725 | 0 | } catch (Exception e) { |
726 | 0 | LOG.error("Error invoking rendering method for component: " + component.getId(), e); |
727 | 0 | throw new RuntimeException("Error invoking rendering method for component: " + component.getId(), e); |
728 | 0 | } |
729 | 0 | } |
730 | |
|
731 | |
|
732 | |
|
733 | |
|
734 | |
|
735 | |
@Override |
736 | |
public void processCollectionAddLine(View view, Object model, String collectionPath) { |
737 | |
|
738 | 0 | CollectionGroup collectionGroup = view.getViewIndex().getCollectionGroupByPath(collectionPath); |
739 | 0 | if (collectionGroup == null) { |
740 | 0 | logAndThrowRuntime("Unable to get collection group component for path: " + collectionPath); |
741 | |
} |
742 | |
|
743 | |
|
744 | 0 | Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(model, collectionPath); |
745 | 0 | if (collection == null) { |
746 | 0 | logAndThrowRuntime("Unable to get collection property from model for path: " + collectionPath); |
747 | |
} |
748 | |
|
749 | |
|
750 | 0 | String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath(); |
751 | 0 | Object addLine = ObjectPropertyUtils.getPropertyValue(model, addLinePath); |
752 | 0 | if (addLine == null) { |
753 | 0 | logAndThrowRuntime("Add line instance not found for path: " + addLinePath); |
754 | |
} |
755 | |
|
756 | 0 | processBeforeAddLine(view, collectionGroup, model, addLine); |
757 | |
|
758 | |
|
759 | 0 | boolean isValidLine = performAddLineValidation(view, collectionGroup, model, addLine); |
760 | 0 | if (isValidLine) { |
761 | |
|
762 | |
|
763 | |
|
764 | 0 | collection.add(addLine); |
765 | |
|
766 | |
|
767 | 0 | collectionGroup.initializeNewCollectionLine(view, model, collectionGroup, true); |
768 | |
} |
769 | |
|
770 | 0 | processAfterAddLine(view, collectionGroup, model, addLine); |
771 | 0 | } |
772 | |
|
773 | |
|
774 | |
|
775 | |
|
776 | |
|
777 | |
|
778 | |
|
779 | |
|
780 | |
|
781 | |
|
782 | |
|
783 | |
|
784 | |
|
785 | |
|
786 | |
|
787 | |
|
788 | |
|
789 | |
protected boolean performAddLineValidation(View view, CollectionGroup collectionGroup, Object model, Object addLine) { |
790 | 0 | boolean isValid = true; |
791 | |
|
792 | |
|
793 | |
|
794 | |
|
795 | 0 | return isValid; |
796 | |
} |
797 | |
|
798 | |
|
799 | |
|
800 | |
|
801 | |
|
802 | |
public void processCollectionDeleteLine(View view, Object model, String collectionPath, int lineIndex) { |
803 | |
|
804 | 0 | CollectionGroup collectionGroup = view.getViewIndex().getCollectionGroupByPath(collectionPath); |
805 | 0 | if (collectionGroup == null) { |
806 | 0 | logAndThrowRuntime("Unable to get collection group component for path: " + collectionPath); |
807 | |
} |
808 | |
|
809 | |
|
810 | 0 | Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(model, collectionPath); |
811 | 0 | if (collection == null) { |
812 | 0 | logAndThrowRuntime("Unable to get collection property from model for path: " + collectionPath); |
813 | |
} |
814 | |
|
815 | |
|
816 | |
|
817 | 0 | if (collection instanceof List) { |
818 | 0 | Object deleteLine = ((List<Object>) collection).get(lineIndex); |
819 | |
|
820 | |
|
821 | 0 | boolean isValid = performDeleteLineValidation(view, collectionGroup, deleteLine); |
822 | 0 | if (isValid) { |
823 | 0 | ((List<Object>) collection).remove(lineIndex); |
824 | 0 | processAfterDeleteLine(view, collectionGroup, model, lineIndex); |
825 | |
} |
826 | 0 | } else { |
827 | 0 | logAndThrowRuntime("Only List collection implementations are supported for the delete by index method"); |
828 | |
} |
829 | 0 | } |
830 | |
|
831 | |
|
832 | |
|
833 | |
|
834 | |
|
835 | |
|
836 | |
|
837 | |
|
838 | |
|
839 | |
|
840 | |
|
841 | |
|
842 | |
|
843 | |
|
844 | |
protected boolean performDeleteLineValidation(View view, CollectionGroup collectionGroup, Object deleteLine) { |
845 | 0 | boolean isValid = true; |
846 | |
|
847 | |
|
848 | |
|
849 | |
|
850 | 0 | return isValid; |
851 | |
} |
852 | |
|
853 | |
|
854 | |
|
855 | |
|
856 | |
|
857 | |
|
858 | |
|
859 | |
|
860 | |
public void buildInquiryLink(Object dataObject, String propertyName, Inquiry inquiry) { |
861 | 0 | Inquirable inquirable = getViewDictionaryService().getInquirable(dataObject.getClass(), inquiry.getViewName()); |
862 | 0 | if (inquirable != null) { |
863 | 0 | inquirable.buildInquirableLink(dataObject, propertyName, inquiry); |
864 | |
} else { |
865 | |
|
866 | 0 | inquiry.setRender(false); |
867 | |
} |
868 | 0 | } |
869 | |
|
870 | |
|
871 | |
|
872 | |
|
873 | |
|
874 | |
public void applyDefaultValues(View view, UifFormBase model) { |
875 | |
|
876 | |
|
877 | 0 | Map<String, AttributeField> attributeFields = view.getViewIndex().getAttributeFieldIndex(); |
878 | 0 | for (Entry<String, AttributeField> attributeFieldEntry : attributeFields.entrySet()) { |
879 | 0 | String bindingPath = attributeFieldEntry.getKey(); |
880 | 0 | AttributeField attributeField = attributeFieldEntry.getValue(); |
881 | |
|
882 | 0 | populateDefaultValueForField(view, model, attributeField, bindingPath); |
883 | 0 | } |
884 | |
|
885 | |
|
886 | 0 | model.setDefaultsApplied(true); |
887 | 0 | } |
888 | |
|
889 | |
|
890 | |
|
891 | |
|
892 | |
|
893 | |
|
894 | |
public void applyDefaultValuesForCollectionLine(View view, Object model, CollectionGroup collectionGroup, |
895 | |
Object line) { |
896 | |
|
897 | 0 | List<AttributeField> attributeFields = ComponentUtils.getComponentsOfTypeDeep(collectionGroup.getAddLineFields(), |
898 | |
AttributeField.class); |
899 | 0 | for (AttributeField attributeField : attributeFields) { |
900 | 0 | String bindingPath = ""; |
901 | 0 | if (StringUtils.isNotBlank(attributeField.getBindingInfo().getBindByNamePrefix())) { |
902 | 0 | bindingPath = attributeField.getBindingInfo().getBindByNamePrefix() + "."; |
903 | |
} |
904 | 0 | bindingPath += attributeField.getBindingInfo().getBindingName(); |
905 | |
|
906 | 0 | populateDefaultValueForField(view, line, attributeField, bindingPath); |
907 | 0 | } |
908 | 0 | } |
909 | |
|
910 | |
|
911 | |
|
912 | |
|
913 | |
|
914 | |
|
915 | |
|
916 | |
|
917 | |
|
918 | |
|
919 | |
|
920 | |
|
921 | |
|
922 | |
|
923 | |
|
924 | |
|
925 | |
|
926 | |
|
927 | |
|
928 | |
|
929 | |
|
930 | |
protected void populateDefaultValueForField(View view, Object object, AttributeField attributeField, |
931 | |
String bindingPath) { |
932 | |
|
933 | 0 | String defaultValue = attributeField.getDefaultValue(); |
934 | 0 | if (StringUtils.isBlank(defaultValue) && (attributeField.getDefaultValueFinderClass() != null)) { |
935 | 0 | ValueFinder defaultValueFinder = ObjectUtils.newInstance(attributeField.getDefaultValueFinderClass()); |
936 | 0 | defaultValue = defaultValueFinder.getValue(); |
937 | |
} |
938 | |
|
939 | |
|
940 | 0 | if (StringUtils.isNotBlank(defaultValue) && ObjectPropertyUtils.isWritableProperty(object, bindingPath)) { |
941 | 0 | if (getExpressionEvaluatorService().containsElPlaceholder(defaultValue)) { |
942 | 0 | Map<String, Object> context = getPreModelContext(view); |
943 | 0 | defaultValue = getExpressionEvaluatorService().evaluateExpressionTemplate(null, context, defaultValue); |
944 | |
} |
945 | |
|
946 | |
|
947 | 0 | ObjectPropertyUtils.setPropertyValue(object, bindingPath, defaultValue); |
948 | |
} |
949 | 0 | } |
950 | |
|
951 | |
|
952 | |
|
953 | |
|
954 | |
|
955 | |
|
956 | |
|
957 | |
|
958 | |
|
959 | |
|
960 | |
protected void performCustomInitialization(View view, Component component) { |
961 | |
|
962 | 0 | } |
963 | |
|
964 | |
|
965 | |
|
966 | |
|
967 | |
|
968 | |
|
969 | |
|
970 | |
|
971 | |
|
972 | |
|
973 | |
|
974 | |
|
975 | |
|
976 | |
protected void performCustomApplyModel(View view, Component component, Object model) { |
977 | |
|
978 | 0 | } |
979 | |
|
980 | |
|
981 | |
|
982 | |
|
983 | |
|
984 | |
|
985 | |
|
986 | |
|
987 | |
|
988 | |
|
989 | |
|
990 | |
|
991 | |
|
992 | |
protected void performCustomFinalize(View view, Component component, Object model, Component parent) { |
993 | |
|
994 | 0 | } |
995 | |
|
996 | |
|
997 | |
|
998 | |
|
999 | |
|
1000 | |
|
1001 | |
|
1002 | |
|
1003 | |
|
1004 | |
|
1005 | |
|
1006 | |
|
1007 | |
|
1008 | |
|
1009 | |
|
1010 | |
|
1011 | |
protected void processBeforeAddLine(View view, CollectionGroup collectionGroup, Object model, Object addLine) { |
1012 | |
|
1013 | 0 | } |
1014 | |
|
1015 | |
|
1016 | |
|
1017 | |
|
1018 | |
|
1019 | |
|
1020 | |
|
1021 | |
|
1022 | |
|
1023 | |
|
1024 | |
|
1025 | |
|
1026 | |
|
1027 | |
|
1028 | |
|
1029 | |
|
1030 | |
protected void processAfterAddLine(View view, CollectionGroup collectionGroup, Object model, Object addLine) { |
1031 | |
|
1032 | 0 | } |
1033 | |
|
1034 | |
|
1035 | |
|
1036 | |
|
1037 | |
|
1038 | |
|
1039 | |
|
1040 | |
|
1041 | |
|
1042 | |
|
1043 | |
|
1044 | |
|
1045 | |
|
1046 | |
|
1047 | |
|
1048 | |
|
1049 | |
protected void processAfterDeleteLine(View view, CollectionGroup collectionGroup, Object model, int lineIndex) { |
1050 | |
|
1051 | 0 | } |
1052 | |
|
1053 | |
protected void logAndThrowRuntime(String message) { |
1054 | 0 | LOG.error(message); |
1055 | 0 | throw new RuntimeException(message); |
1056 | |
} |
1057 | |
|
1058 | |
protected DataDictionaryService getDataDictionaryService() { |
1059 | 0 | if (this.dataDictionaryService == null) { |
1060 | 0 | this.dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService(); |
1061 | |
} |
1062 | |
|
1063 | 0 | return this.dataDictionaryService; |
1064 | |
} |
1065 | |
|
1066 | |
public void setDataDictionaryService(DataDictionaryService dataDictionaryService) { |
1067 | 0 | this.dataDictionaryService = dataDictionaryService; |
1068 | 0 | } |
1069 | |
|
1070 | |
protected ExpressionEvaluatorService getExpressionEvaluatorService() { |
1071 | 0 | if (this.expressionEvaluatorService == null) { |
1072 | 0 | this.expressionEvaluatorService = KRADServiceLocatorWeb.getExpressionEvaluatorService(); |
1073 | |
} |
1074 | |
|
1075 | 0 | return this.expressionEvaluatorService; |
1076 | |
} |
1077 | |
|
1078 | |
public void setExpressionEvaluatorService(ExpressionEvaluatorService expressionEvaluatorService) { |
1079 | 0 | this.expressionEvaluatorService = expressionEvaluatorService; |
1080 | 0 | } |
1081 | |
|
1082 | |
public ViewDictionaryService getViewDictionaryService() { |
1083 | 0 | if (this.viewDictionaryService == null) { |
1084 | 0 | this.viewDictionaryService = KRADServiceLocatorWeb.getViewDictionaryService(); |
1085 | |
} |
1086 | 0 | return this.viewDictionaryService; |
1087 | |
} |
1088 | |
|
1089 | |
public void setViewDictionaryService(ViewDictionaryService viewDictionaryService) { |
1090 | 0 | this.viewDictionaryService = viewDictionaryService; |
1091 | 0 | } |
1092 | |
} |