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