1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.container;
17
18 import org.apache.commons.collections.ListUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.kuali.rice.core.api.mo.common.active.Inactivatable;
23 import org.kuali.rice.kim.api.identity.Person;
24 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
25 import org.kuali.rice.krad.uif.UifConstants;
26 import org.kuali.rice.krad.uif.UifParameters;
27 import org.kuali.rice.krad.uif.UifPropertyPaths;
28 import org.kuali.rice.krad.uif.component.Component;
29 import org.kuali.rice.krad.uif.component.ComponentSecurity;
30 import org.kuali.rice.krad.uif.component.DataBinding;
31 import org.kuali.rice.krad.uif.control.Control;
32 import org.kuali.rice.krad.uif.control.ControlBase;
33 import org.kuali.rice.krad.uif.element.Action;
34 import org.kuali.rice.krad.uif.field.Field;
35 import org.kuali.rice.krad.uif.field.FieldGroup;
36 import org.kuali.rice.krad.uif.field.InputField;
37 import org.kuali.rice.krad.uif.field.RemoteFieldsHolder;
38 import org.kuali.rice.krad.uif.layout.CollectionLayoutManager;
39 import org.kuali.rice.krad.uif.layout.TableLayoutManager;
40 import org.kuali.rice.krad.uif.service.ExpressionEvaluatorService;
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.ScriptUtils;
44 import org.kuali.rice.krad.uif.view.View;
45 import org.kuali.rice.krad.uif.view.ViewAuthorizer;
46 import org.kuali.rice.krad.uif.view.ViewModel;
47 import org.kuali.rice.krad.uif.view.ViewPresentationController;
48 import org.kuali.rice.krad.util.GlobalVariables;
49 import org.kuali.rice.krad.util.KRADUtils;
50 import org.kuali.rice.krad.util.ObjectUtils;
51 import org.kuali.rice.krad.web.form.UifFormBase;
52
53 import java.io.Serializable;
54 import java.util.ArrayList;
55 import java.util.Collection;
56 import java.util.HashMap;
57 import java.util.List;
58 import java.util.Map;
59
60
61
62
63
64
65
66
67
68 public class CollectionGroupBuilder implements Serializable {
69 private static final long serialVersionUID = -4762031957079895244L;
70 private static Log LOG = LogFactory.getLog(CollectionGroupBuilder.class);
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 public void build(View view, Object model, CollectionGroup collectionGroup) {
90
91 if (collectionGroup.isRenderAddLine() && !collectionGroup.isReadOnly() &&
92 !collectionGroup.isRenderAddBlankLineButton()) {
93 buildAddLine(view, model, collectionGroup);
94 }
95
96
97 if (collectionGroup.isRenderAddBlankLineButton() && (collectionGroup.getAddBlankLineAction() != null)) {
98 collectionGroup.getAddBlankLineAction().setRefreshId(collectionGroup.getId());
99 }
100
101
102 List<Object> modelCollection = ObjectPropertyUtils.getPropertyValue(model,
103 ((DataBinding) collectionGroup).getBindingInfo().getBindingPath());
104
105 if (modelCollection != null) {
106
107 List<Integer> showIndexes = performCollectionFiltering(view, model, collectionGroup, modelCollection);
108
109 if (collectionGroup.getDisplayCollectionSize() != -1 && showIndexes.size() > collectionGroup
110 .getDisplayCollectionSize()) {
111
112 List<Integer> newShowIndexes = new ArrayList<Integer>();
113 Integer counter = 0;
114
115 for (int index = 0; index < showIndexes.size(); index++) {
116 newShowIndexes.add(showIndexes.get(index));
117
118 counter++;
119
120 if (counter == collectionGroup.getDisplayCollectionSize()) {
121 break;
122 }
123 }
124
125 showIndexes = newShowIndexes;
126 }
127
128
129 for (int index = 0; index < modelCollection.size(); index++) {
130
131 if (showIndexes.contains(index)) {
132 String bindingPathPrefix = collectionGroup.getBindingInfo().getBindingName() + "[" + index + "]";
133 if (StringUtils.isNotBlank(collectionGroup.getBindingInfo().getBindByNamePrefix())) {
134 bindingPathPrefix =
135 collectionGroup.getBindingInfo().getBindByNamePrefix() + "." + bindingPathPrefix;
136 }
137
138 Object currentLine = modelCollection.get(index);
139
140 List<Action> lineActions = initializeLineActions(collectionGroup.getLineActions(), view, model,
141 collectionGroup, currentLine, index);
142
143 buildLine(view, model, collectionGroup, bindingPathPrefix, lineActions, false, currentLine, index);
144 }
145 }
146 }
147 }
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 protected List<Integer> performCollectionFiltering(View view, Object model, CollectionGroup collectionGroup,
165 Collection<?> collection) {
166 List<Integer> filteredIndexes = new ArrayList<Integer>();
167 for (int i = 0; i < collection.size(); i++) {
168 filteredIndexes.add(Integer.valueOf(i));
169 }
170
171 if (Inactivatable.class.isAssignableFrom(collectionGroup.getCollectionObjectClass()) && !collectionGroup
172 .isShowInactiveLines()) {
173 List<Integer> activeIndexes = collectionGroup.getActiveCollectionFilter().filter(view, model,
174 collectionGroup);
175 filteredIndexes = ListUtils.intersection(filteredIndexes, activeIndexes);
176 }
177
178 for (CollectionFilter collectionFilter : collectionGroup.getFilters()) {
179 List<Integer> indexes = collectionFilter.filter(view, model, collectionGroup);
180 filteredIndexes = ListUtils.intersection(filteredIndexes, indexes);
181 if (filteredIndexes.isEmpty()) {
182 break;
183 }
184 }
185
186 return filteredIndexes;
187 }
188
189
190
191
192
193
194
195
196
197
198 protected void buildAddLine(View view, Object model, CollectionGroup collectionGroup) {
199 boolean addLineBindsToForm = false;
200
201
202 initializeNewCollectionLine(view, model, collectionGroup, false);
203
204
205
206 if (StringUtils.isBlank(collectionGroup.getAddLinePropertyName())) {
207 addLineBindsToForm = true;
208 }
209
210 String addLineBindingPath = collectionGroup.getAddLineBindingInfo().getBindingPath();
211 List<Action> actions = getAddLineActions(view, model, collectionGroup);
212
213 Object addLine = ObjectPropertyUtils.getPropertyValue(model, addLineBindingPath);
214 buildLine(view, model, collectionGroup, addLineBindingPath, actions, addLineBindsToForm, addLine, -1);
215 }
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235 @SuppressWarnings("unchecked")
236 protected void buildLine(View view, Object model, CollectionGroup collectionGroup, String bindingPath,
237 List<Action> actions, boolean bindToForm, Object currentLine, int lineIndex) {
238 CollectionLayoutManager layoutManager = (CollectionLayoutManager) collectionGroup.getLayoutManager();
239
240
241 List<? extends Component> lineItems = null;
242 String lineSuffix = null;
243 if (lineIndex == -1) {
244 lineItems = ComponentUtils.copyComponentList(collectionGroup.getAddLineItems(), null);
245 lineSuffix = UifConstants.IdSuffixes.ADD_LINE;
246 } else {
247 lineItems = ComponentUtils.copyComponentList(collectionGroup.getItems(), null);
248 lineSuffix = UifConstants.IdSuffixes.LINE + Integer.toString(lineIndex);
249 }
250
251 if (StringUtils.isNotBlank(collectionGroup.getSubCollectionSuffix())) {
252 lineSuffix = collectionGroup.getSubCollectionSuffix() + lineSuffix;
253 }
254
255
256 List<Field> lineFields = processAnyRemoteFieldsHolder(view, model, collectionGroup, lineItems);
257
258
259 lineFields = (List<Field>) ComponentUtils.copyFieldList(lineFields, bindingPath, lineSuffix);
260
261
262
263 for (Field field : lineFields) {
264 List<CollectionGroup> components = ComponentUtils.getComponentsOfTypeDeep(field, CollectionGroup.class);
265 for (CollectionGroup fieldCollectionGroup : components) {
266 ComponentUtils.prefixBindingPath(fieldCollectionGroup, bindingPath);
267 fieldCollectionGroup.setSubCollectionSuffix(lineSuffix);
268 }
269 }
270
271 boolean readOnlyLine = collectionGroup.isReadOnly();
272
273
274 ComponentUtils.updateContextsForLine(lineFields, currentLine, lineIndex, lineSuffix);
275
276 for (Action action : actions) {
277 if(action !=null && StringUtils.isNotBlank(action.getFocusOnIdAfterSubmit()) &&
278 action.getFocusOnIdAfterSubmit().equalsIgnoreCase(UifConstants.Order.LINE_FIRST.toString())
279 && (lineFields.size() > 0)){
280 action.setFocusOnIdAfterSubmit(lineFields.get(0).getId() + UifConstants.IdSuffixes.CONTROL);
281 }
282 }
283
284
285 if (lineIndex == -1) {
286
287 } else {
288
289 boolean canViewLine = checkViewLineAuthorizationAndPresentationLogic(view, (ViewModel) model,
290 collectionGroup, currentLine);
291
292
293 if (!canViewLine) {
294 return;
295 }
296
297
298 if (!collectionGroup.isReadOnly()) {
299 readOnlyLine = !checkEditLineAuthorizationAndPresentationLogic(view, (ViewModel) model, collectionGroup,
300 currentLine);
301
302
303 if (!readOnlyLine && !((UifFormBase) model).isAddedCollectionItem(currentLine) &&
304 collectionGroup.isRenderSaveLineActions()) {
305 for (Field f : lineFields) {
306 if (f instanceof InputField && f.isRender()) {
307 ControlBase control = (ControlBase) ((InputField) f).getControl();
308 control.setOnChangeScript(control.getOnChangeScript() == null ?
309 ";collectionLineChanged(this, 'uif-newCollectionItem');" :
310 control.getOnChangeScript() +
311 ";collectionLineChanged(this, 'uif-newCollectionItem');");
312 }
313 }
314 }
315 }
316
317 ComponentUtils.pushObjectToContext(lineFields, UifConstants.ContextVariableNames.READONLY_LINE,
318 readOnlyLine);
319 ComponentUtils.pushObjectToContext(actions, UifConstants.ContextVariableNames.READONLY_LINE, readOnlyLine);
320 }
321
322
323 applyLineFieldAuthorizationAndPresentationLogic(view, (ViewModel) model, collectionGroup, currentLine,
324 readOnlyLine, lineFields, actions);
325
326 if (bindToForm) {
327 ComponentUtils.setComponentsPropertyDeep(lineFields, UifPropertyPaths.BIND_TO_FORM, Boolean.valueOf(true));
328 }
329
330
331 lineFields = removeNonRenderLineFields(view, model, collectionGroup, lineFields, currentLine, lineIndex);
332
333
334 List<FieldGroup> subCollectionFields = new ArrayList<FieldGroup>();
335 if ((lineIndex != -1) && (collectionGroup.getSubCollections() != null)) {
336 for (int subLineIndex = 0; subLineIndex < collectionGroup.getSubCollections().size(); subLineIndex++) {
337 CollectionGroup subCollectionPrototype = collectionGroup.getSubCollections().get(subLineIndex);
338 CollectionGroup subCollectionGroup = ComponentUtils.copy(subCollectionPrototype, lineSuffix);
339
340
341 boolean renderSubCollection = checkSubCollectionRender(view, model, collectionGroup,
342 subCollectionGroup);
343 if (!renderSubCollection) {
344 continue;
345 }
346
347 subCollectionGroup.getBindingInfo().setBindByNamePrefix(bindingPath);
348 if (subCollectionGroup.isRenderAddLine()) {
349 subCollectionGroup.getAddLineBindingInfo().setBindByNamePrefix(bindingPath);
350 }
351
352
353 String subCollectionSuffix = lineSuffix;
354 if (StringUtils.isNotBlank(subCollectionGroup.getSubCollectionSuffix())) {
355 subCollectionSuffix = subCollectionGroup.getSubCollectionSuffix() + lineSuffix;
356 }
357 subCollectionGroup.setSubCollectionSuffix(subCollectionSuffix);
358
359 FieldGroup fieldGroupPrototype = layoutManager.getSubCollectionFieldGroupPrototype();
360
361 FieldGroup subCollectionFieldGroup = ComponentUtils.copy(fieldGroupPrototype,
362 lineSuffix + UifConstants.IdSuffixes.SUB + subLineIndex);
363 subCollectionFieldGroup.setGroup(subCollectionGroup);
364
365 ComponentUtils.updateContextForLine(subCollectionFieldGroup, currentLine, lineIndex,
366 lineSuffix + UifConstants.IdSuffixes.SUB + subLineIndex);
367
368 subCollectionFields.add(subCollectionFieldGroup);
369 }
370 ComponentUtils.pushObjectToContext(subCollectionFields, UifConstants.ContextVariableNames.PARENT_LINE,
371 currentLine);
372 }
373
374
375 layoutManager.buildLine(view, model, collectionGroup, lineFields, subCollectionFields, bindingPath, actions,
376 lineSuffix, currentLine, lineIndex);
377
378
379 String selector = "";
380 if (lineIndex == -1) {
381 List<String> addIds = new ArrayList<String>();
382 for (Field f : lineFields) {
383 if (f instanceof InputField) {
384
385
386 Control control = ((InputField) f).getControl();
387 if (control != null) {
388 control.addStyleClass("ignoreValid");
389 selector = selector + ",#" + f.getId() + UifConstants.IdSuffixes.CONTROL;
390 }
391 } else if (f instanceof FieldGroup) {
392 List<InputField> fields = ComponentUtils.getComponentsOfTypeDeep(((FieldGroup) f).getGroup(),
393 InputField.class);
394 for (InputField nestedField : fields) {
395 Control control = nestedField.getControl();
396 if (control != null) {
397 control.addStyleClass("ignoreValid");
398 selector = selector + ",#" + nestedField.getId() + UifConstants.IdSuffixes.CONTROL;
399 }
400 }
401 }
402 }
403 collectionGroup.addDataAttribute("addControls", selector.replaceFirst(",", ""));
404 }
405 }
406
407
408
409
410
411
412
413
414
415
416
417 protected List<Field> processAnyRemoteFieldsHolder(View view, Object model, CollectionGroup group,
418 List<? extends Component> items) {
419 List<Field> processedItems = new ArrayList<Field>();
420
421
422
423 for (Component item : items) {
424 if (item instanceof RemoteFieldsHolder) {
425 List<InputField> translatedFields = ((RemoteFieldsHolder) item).fetchAndTranslateRemoteFields(view,
426 model, group);
427 processedItems.addAll(translatedFields);
428 } else {
429 processedItems.add((Field) item);
430 }
431 }
432
433 return processedItems;
434 }
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452 protected List<Field> removeNonRenderLineFields(View view, Object model, CollectionGroup collectionGroup,
453 List<Field> lineFields, Object currentLine, int lineIndex) {
454 List<Field> fields = new ArrayList<Field>();
455
456 for (Field lineField : lineFields) {
457 String conditionalRender = lineField.getPropertyExpression("render");
458
459
460 if (StringUtils.isNotBlank(conditionalRender)) {
461 Map<String, Object> context = getContextForField(view, collectionGroup, lineField);
462
463
464
465 conditionalRender = KRADServiceLocatorWeb.getExpressionEvaluatorService().replaceBindingPrefixes(view,
466 lineField, conditionalRender);
467
468 Boolean render = (Boolean) getExpressionEvaluatorService().evaluateExpression(model, context,
469 conditionalRender);
470 lineField.setRender(render);
471 }
472
473
474 if (lineField.isRender() || StringUtils.isNotBlank(lineField.getProgressiveRender())) {
475 fields.add(lineField);
476 }
477 }
478
479 return fields;
480 }
481
482
483
484
485
486
487
488
489
490
491
492
493 protected boolean checkViewLineAuthorizationAndPresentationLogic(View view, ViewModel model,
494 CollectionGroup collectionGroup, Object line) {
495 ViewPresentationController presentationController = view.getPresentationController();
496 ViewAuthorizer authorizer = view.getAuthorizer();
497
498 Person user = GlobalVariables.getUserSession().getPerson();
499
500
501 boolean canViewLine = authorizer.canViewLine(view, model, collectionGroup, collectionGroup.getPropertyName(),
502 line, user);
503 if (canViewLine) {
504 canViewLine = presentationController.canViewLine(view, model, collectionGroup,
505 collectionGroup.getPropertyName(), line);
506 }
507
508 return canViewLine;
509 }
510
511
512
513
514
515
516
517
518
519
520
521
522 protected boolean checkEditLineAuthorizationAndPresentationLogic(View view, ViewModel model,
523 CollectionGroup collectionGroup, Object line) {
524 ViewPresentationController presentationController = view.getPresentationController();
525 ViewAuthorizer authorizer = view.getAuthorizer();
526
527 Person user = GlobalVariables.getUserSession().getPerson();
528
529
530 boolean canEditLine = authorizer.canEditLine(view, model, collectionGroup, collectionGroup.getPropertyName(),
531 line, user);
532 if (canEditLine) {
533 canEditLine = presentationController.canEditLine(view, model, collectionGroup,
534 collectionGroup.getPropertyName(), line);
535 }
536
537 return canEditLine;
538 }
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555 protected void applyLineFieldAuthorizationAndPresentationLogic(View view, ViewModel model,
556 CollectionGroup collectionGroup, Object line, boolean readOnlyLine, List<Field> lineFields,
557 List<Action> actions) {
558 ViewPresentationController presentationController = view.getPresentationController();
559 ViewAuthorizer authorizer = view.getAuthorizer();
560
561 Person user = GlobalVariables.getUserSession().getPerson();
562
563 for (Field lineField : lineFields) {
564 String propertyName = null;
565 if (lineField instanceof DataBinding) {
566 propertyName = ((DataBinding) lineField).getPropertyName();
567 }
568
569
570
571 ComponentSecurity componentSecurity = lineField.getComponentSecurity();
572
573 Map<String, Object> context = getContextForField(view, collectionGroup, lineField);
574 getExpressionEvaluatorService().evaluateExpressionsOnConfigurable(view, componentSecurity, model, context);
575
576
577 if (lineField.isRender() && !lineField.isHidden()) {
578 boolean canViewField = authorizer.canViewLineField(view, model, collectionGroup,
579 collectionGroup.getPropertyName(), line, lineField, propertyName, user);
580 if (canViewField) {
581 canViewField = presentationController.canViewLineField(view, model, collectionGroup,
582 collectionGroup.getPropertyName(), line, lineField, propertyName);
583 }
584
585 if (!canViewField) {
586
587
588 lineField.setHidden(true);
589
590 if (lineField.getPropertyExpressions().containsKey("hidden")) {
591 lineField.getPropertyExpressions().remove("hidden");
592 }
593
594 continue;
595 }
596
597
598 boolean canEditField = !readOnlyLine;
599 if (!readOnlyLine) {
600 canEditField = authorizer.canEditLineField(view, model, collectionGroup,
601 collectionGroup.getPropertyName(), line, lineField, propertyName, user);
602 if (canEditField) {
603 canEditField = presentationController.canEditLineField(view, model, collectionGroup,
604 collectionGroup.getPropertyName(), line, lineField, propertyName);
605 }
606 }
607
608 if (readOnlyLine || !canEditField) {
609 lineField.setReadOnly(true);
610
611 if (lineField.getPropertyExpressions().containsKey("readOnly")) {
612 lineField.getPropertyExpressions().remove("readOnly");
613 }
614 } else if(lineField instanceof InputField){
615 lineField.setReadOnly(false);
616 }
617 }
618 }
619
620
621 for (Action action : actions) {
622 if (action.isRender()) {
623 boolean canPerformAction = authorizer.canPerformLineAction(view, model, collectionGroup,
624 collectionGroup.getPropertyName(), line, action, action.getActionEvent(), action.getId(), user);
625 if (canPerformAction) {
626 canPerformAction = presentationController.canPerformLineAction(view, model, collectionGroup,
627 collectionGroup.getPropertyName(), line, action, action.getActionEvent(), action.getId());
628 }
629
630 if (!canPerformAction) {
631 action.setRender(false);
632
633 if (action.getPropertyExpressions().containsKey("render")) {
634 action.getPropertyExpressions().remove("render");
635 }
636 }
637 }
638 }
639 }
640
641
642
643
644
645
646
647
648
649
650
651
652 protected boolean checkSubCollectionRender(View view, Object model, CollectionGroup collectionGroup,
653 CollectionGroup subCollectionGroup) {
654 String conditionalRender = subCollectionGroup.getPropertyExpression("render");
655
656
657
658
659 if (StringUtils.isNotBlank(conditionalRender)) {
660 Map<String, Object> context = new HashMap<String, Object>();
661 context.putAll(view.getContext());
662 context.put(UifConstants.ContextVariableNames.PARENT, collectionGroup);
663 context.put(UifConstants.ContextVariableNames.COMPONENT, subCollectionGroup);
664
665 Boolean render = (Boolean) getExpressionEvaluatorService().evaluateExpression(model, context,
666 conditionalRender);
667 subCollectionGroup.setRender(render);
668 }
669
670 return subCollectionGroup.isRender();
671 }
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688 protected List<Action> initializeLineActions(List<Action> lineActions, View view, Object model,
689 CollectionGroup collectionGroup, Object collectionLine, int lineIndex) {
690 String lineSuffix = UifConstants.IdSuffixes.LINE + Integer.toString(lineIndex);
691 if (StringUtils.isNotBlank(collectionGroup.getSubCollectionSuffix())) {
692 lineSuffix = collectionGroup.getSubCollectionSuffix() + lineSuffix;
693 }
694 List<Action> actions = ComponentUtils.copyComponentList(lineActions, lineSuffix);
695
696 for (Action action : actions) {
697 if (ComponentUtils.containsPropertyExpression(action, UifPropertyPaths.ACTION_PARAMETERS, true)) {
698
699 action.getPropertyExpressions().put(
700 UifPropertyPaths.ACTION_PARAMETERS + "['" + UifParameters.SELLECTED_COLLECTION_PATH + "']",
701 UifConstants.EL_PLACEHOLDER_PREFIX + "'" + collectionGroup.getBindingInfo().getBindingPath() +
702 "'" + UifConstants.EL_PLACEHOLDER_SUFFIX);
703 action.getPropertyExpressions().put(
704 UifPropertyPaths.ACTION_PARAMETERS + "['" + UifParameters.SELECTED_LINE_INDEX + "']",
705 UifConstants.EL_PLACEHOLDER_PREFIX + "'" + Integer.toString(lineIndex) +
706 "'" + UifConstants.EL_PLACEHOLDER_SUFFIX);
707 } else {
708 action.addActionParameter(UifParameters.SELLECTED_COLLECTION_PATH,
709 collectionGroup.getBindingInfo().getBindingPath());
710 action.addActionParameter(UifParameters.SELECTED_LINE_INDEX, Integer.toString(lineIndex));
711 }
712
713 action.setJumpToIdAfterSubmit(collectionGroup.getId());
714 action.setRefreshId(collectionGroup.getId());
715
716
717
718 if (action.isPerformClientSideValidation()) {
719 String preSubmitScript = "valid=valid && validateLine('" +
720 collectionGroup.getBindingInfo().getBindingPath() + "'," + Integer.toString(lineIndex) +
721 ");return valid;";
722
723
724 if (StringUtils.isNotBlank(action.getPreSubmitCall())) {
725 preSubmitScript = ScriptUtils.appendScript("var valid=true;valid=" + action.getPreSubmitCall(),
726 preSubmitScript);
727 } else {
728 preSubmitScript = "var valid=true;" + preSubmitScript;
729 }
730
731 action.setPreSubmitCall(preSubmitScript);
732 action.setPerformClientSideValidation(false);
733 }
734 }
735
736 ComponentUtils.updateContextsForLine(actions, collectionLine, lineIndex, lineSuffix);
737
738 return actions;
739 }
740
741
742
743
744
745
746
747
748
749
750
751
752
753 protected List<Action> getAddLineActions(View view, Object model, CollectionGroup collectionGroup) {
754 String lineSuffix = UifConstants.IdSuffixes.ADD_LINE;
755 if (StringUtils.isNotBlank(collectionGroup.getSubCollectionSuffix())) {
756 lineSuffix = collectionGroup.getSubCollectionSuffix() + lineSuffix;
757 }
758 List<Action> lineActions = ComponentUtils.copyComponentList(collectionGroup.getAddLineActions(), lineSuffix);
759
760 for (Action action : lineActions) {
761 action.addActionParameter(UifParameters.SELLECTED_COLLECTION_PATH,
762 collectionGroup.getBindingInfo().getBindingPath());
763 action.setJumpToIdAfterSubmit(collectionGroup.getId());
764 action.addActionParameter(UifParameters.ACTION_TYPE, UifParameters.ADD_LINE);
765 action.setRefreshId(collectionGroup.getId());
766
767 String baseId = collectionGroup.getBaseId();
768 if (StringUtils.isNotBlank(collectionGroup.getSubCollectionSuffix())) {
769 baseId += collectionGroup.getSubCollectionSuffix();
770 }
771
772 String preSubmitScript = "valid=valid && ";
773 if (collectionGroup.isAddViaLightBox()) {
774 preSubmitScript += "validateAddLine('" + collectionGroup.getId() + "', true); if (valid) {closeLightbox();}";
775 } else {
776 preSubmitScript += "validateAddLine('" + collectionGroup.getId() + "');";
777 }
778 preSubmitScript += "return valid;";
779
780
781 if (StringUtils.isNotBlank(action.getPreSubmitCall())) {
782 preSubmitScript = ScriptUtils.appendScript("var valid=true;valid=" + action.getPreSubmitCall(),
783 preSubmitScript);
784 } else {
785 preSubmitScript = "var valid=true;" + preSubmitScript;
786 }
787
788 action.setPreSubmitCall(preSubmitScript);
789 }
790
791
792 String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath();
793 Object addLine = ObjectPropertyUtils.getPropertyValue(model, addLinePath);
794
795 ComponentUtils.updateContextsForLine(lineActions, addLine, -1, lineSuffix);
796
797 return lineActions;
798 }
799
800
801
802
803
804
805
806
807
808
809 protected Map<String, Object> getContextForField(View view, CollectionGroup collectionGroup, Field field) {
810 Map<String, Object> context = new HashMap<String, Object>();
811
812 context.putAll(view.getContext());
813 context.putAll(field.getContext());
814 context.put(UifConstants.ContextVariableNames.PARENT, collectionGroup);
815 context.put(UifConstants.ContextVariableNames.COMPONENT, field);
816
817 return context;
818 }
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839 public void initializeNewCollectionLine(View view, Object model, CollectionGroup collectionGroup,
840 boolean clearExistingLine) {
841 Object newLine = null;
842
843
844 if (StringUtils.isBlank(collectionGroup.getAddLinePropertyName())) {
845
846 if (!(model instanceof UifFormBase)) {
847 throw new RuntimeException("Cannot create new collection line for group: "
848 + collectionGroup.getPropertyName()
849 + ". Model does not extend "
850 + UifFormBase.class.getName());
851 }
852
853
854 Map<String, Object> newCollectionLines = ObjectPropertyUtils.getPropertyValue(model,
855 UifPropertyPaths.NEW_COLLECTION_LINES);
856 if (newCollectionLines == null) {
857 newCollectionLines = new HashMap<String, Object>();
858 ObjectPropertyUtils.setPropertyValue(model, UifPropertyPaths.NEW_COLLECTION_LINES, newCollectionLines);
859 }
860
861
862 String newCollectionLineKey = KRADUtils.translateToMapSafeKey(
863 collectionGroup.getBindingInfo().getBindingPath());
864 String addLineBindingPath = UifPropertyPaths.NEW_COLLECTION_LINES + "['" + newCollectionLineKey + "']";
865 collectionGroup.getAddLineBindingInfo().setBindingPath(addLineBindingPath);
866
867
868 if (!newCollectionLines.containsKey(newCollectionLineKey) || (newCollectionLines.get(newCollectionLineKey)
869 == null) || clearExistingLine) {
870
871 newLine = ObjectUtils.newInstance(collectionGroup.getCollectionObjectClass());
872 newCollectionLines.put(newCollectionLineKey, newLine);
873 }
874 } else {
875
876 Object addLine = ObjectPropertyUtils.getPropertyValue(model,
877 collectionGroup.getAddLineBindingInfo().getBindingPath());
878 if ((addLine == null) || clearExistingLine) {
879 newLine = ObjectUtils.newInstance(collectionGroup.getCollectionObjectClass());
880 ObjectPropertyUtils.setPropertyValue(model, collectionGroup.getAddLineBindingInfo().getBindingPath(),
881 newLine);
882 }
883 }
884
885
886 if (newLine != null) {
887 view.getViewHelperService().applyDefaultValuesForCollectionLine(view, model, collectionGroup, newLine);
888 }
889 }
890
891 protected ExpressionEvaluatorService getExpressionEvaluatorService() {
892 return KRADServiceLocatorWeb.getExpressionEvaluatorService();
893 }
894
895 }