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