1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.rice.krad.uif.layout;
17  
18  import com.google.common.collect.Lists;
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
21  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
22  import org.kuali.rice.krad.datadictionary.parse.BeanTags;
23  import org.kuali.rice.krad.uif.UifPropertyPaths;
24  import org.kuali.rice.krad.uif.component.Component;
25  import org.kuali.rice.krad.uif.component.DataBinding;
26  import org.kuali.rice.krad.uif.component.KeepExpression;
27  import org.kuali.rice.krad.uif.container.CollectionGroup;
28  import org.kuali.rice.krad.uif.container.Container;
29  import org.kuali.rice.krad.uif.container.Group;
30  import org.kuali.rice.krad.uif.element.Action;
31  import org.kuali.rice.krad.uif.field.Field;
32  import org.kuali.rice.krad.uif.field.FieldGroup;
33  import org.kuali.rice.krad.uif.util.ComponentUtils;
34  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
35  import org.kuali.rice.krad.uif.view.View;
36  import org.kuali.rice.krad.uif.widget.Pager;
37  import org.kuali.rice.krad.web.form.UifFormBase;
38  
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  @BeanTags({@BeanTag(name = "stackedCollectionLayout-bean", parent = "Uif-StackedCollectionLayoutBase"),
62          @BeanTag(name = "stackedCollectionLayout-withGridItems-bean",
63                  parent = "Uif-StackedCollectionLayout-WithGridItems"),
64          @BeanTag(name = "stackedCollectionLayout-withBoxItems-bean",
65                  parent = "Uif-StackedCollectionLayout-WithBoxItems"),
66          @BeanTag(name = "stackedCollectionLayout-list-bean", parent = "Uif-StackedCollectionLayout-List")})
67  public class StackedLayoutManager extends LayoutManagerBase implements CollectionLayoutManager {
68      private static final long serialVersionUID = 4602368505430238846L;
69  
70      @KeepExpression
71      private String summaryTitle;
72      private List<String> summaryFields;
73  
74      private Group addLineGroup;
75      private Group lineGroupPrototype;
76      private FieldGroup subCollectionFieldGroupPrototype;
77      private Field selectFieldPrototype;
78      private Group wrapperGroup;
79      private Pager pagerWidget;
80  
81      private List<Group> stackedGroups;
82  
83      private boolean actionsInLineGroup;
84  
85      public StackedLayoutManager() {
86          super();
87  
88          summaryFields = new ArrayList<String>();
89          stackedGroups = new ArrayList<Group>();
90      }
91  
92      
93  
94  
95  
96  
97  
98  
99  
100 
101 
102     @Override
103     public void performInitialization(View view, Object model, Container container) {
104         super.performInitialization(view, model, container);
105 
106         stackedGroups = new ArrayList<Group>();
107     }
108 
109     
110 
111 
112 
113 
114 
115 
116 
117 
118 
119     @Override
120     public void performApplyModel(View view, Object model, Container container) {
121         super.performApplyModel(view, model, container);
122 
123         if (wrapperGroup != null) {
124             wrapperGroup.setItems(stackedGroups);
125         }
126     }
127 
128     
129 
130 
131 
132 
133 
134     @Override
135     public void performFinalize(View view, Object model, Container container) {
136         super.performFinalize(view, model, container);
137 
138         
139         if (container instanceof CollectionGroup
140                 && ((CollectionGroup) container).isUseServerPaging()
141                 && this.getPagerWidget() != null) {
142             CollectionGroup collectionGroup = (CollectionGroup) container;
143 
144             
145             CollectionLayoutUtils.setupPagerWidget(pagerWidget, collectionGroup, model);
146         }
147     }
148 
149     
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162     public void buildLine(View view, Object model, CollectionGroup collectionGroup, List<Field> lineFields,
163             List<FieldGroup> subCollectionFields, String bindingPath, List<Action> actions, String idSuffix,
164             Object currentLine, int lineIndex) {
165         boolean isAddLine = lineIndex == -1;
166 
167         
168         Group lineGroup = null;
169         if (isAddLine) {
170             stackedGroups = new ArrayList<Group>();
171 
172             if (addLineGroup == null) {
173                 lineGroup = ComponentUtils.copy(lineGroupPrototype, idSuffix);
174             } else {
175                 lineGroup = ComponentUtils.copy(getAddLineGroup(), idSuffix);
176                 lineGroup.addStyleClass(collectionGroup.getAddItemCssClass());
177             }
178 
179             if (collectionGroup.isAddViaLightBox()) {
180                 String actionScript = "showLightboxComponent('" + lineGroup.getId() + "');";
181                 if (StringUtils.isNotBlank(collectionGroup.getAddViaLightBoxAction().getActionScript())) {
182                     actionScript = collectionGroup.getAddViaLightBoxAction().getActionScript() + actionScript;
183                 }
184                 collectionGroup.getAddViaLightBoxAction().setActionScript(actionScript);
185                 lineGroup.setStyle("display: none");
186             }
187         } else {
188             lineGroup = ComponentUtils.copy(lineGroupPrototype, idSuffix);
189         }
190 
191         if (((UifFormBase) model).isAddedCollectionItem(currentLine)) {
192             lineGroup.addStyleClass(collectionGroup.getNewItemsCssClass());
193         }
194 
195         ComponentUtils.updateContextForLine(lineGroup, currentLine, lineIndex, idSuffix);
196 
197         
198         if (isAddLine) {
199             if (lineGroup.getHeader() != null) {
200                 lineGroup.getHeader().setRichHeaderMessage(collectionGroup.getAddLineLabel());
201             }
202         } else {
203             
204             List<Object> modelCollection = ObjectPropertyUtils.getPropertyValue(model,
205                     ((DataBinding) collectionGroup).getBindingInfo().getBindingPath());
206 
207             String headerText = buildLineHeaderText(view, modelCollection.get(lineIndex), lineGroup);
208 
209             
210             if (StringUtils.isNotBlank(headerText) && lineGroup.getHeader() != null) {
211                 lineGroup.getHeader().setHeaderText(headerText);
212             }
213         }
214 
215         
216         List<Component> groupFields = new ArrayList<Component>();
217         groupFields.addAll(lineFields);
218         groupFields.addAll(subCollectionFields);
219 
220         
221         if (collectionGroup.isRenderLineActions() && !collectionGroup.isReadOnly() && (lineGroup.getFooter() != null)) {
222             
223             if (isActionsInLineGroup()) {
224                 groupFields.addAll(actions);
225                 lineGroup.setRenderFooter(false);
226             } else {
227                 lineGroup.getFooter().setItems(actions);
228             }
229         }
230 
231         lineGroup.setItems(groupFields);
232 
233         stackedGroups.add(lineGroup);
234     }
235 
236     
237 
238 
239 
240 
241 
242 
243 
244 
245 
246 
247 
248 
249 
250 
251 
252 
253 
254 
255 
256     protected String buildLineHeaderText(View view, Object line, Group lineGroup) {
257         
258         if (view.getViewHelperService().getExpressionEvaluator().containsElPlaceholder(summaryTitle)) {
259             lineGroup.getPropertyExpressions().put(UifPropertyPaths.HEADER_TEXT, summaryTitle);
260             return null;
261         }
262 
263         
264         String summaryFieldString = "";
265         for (String summaryField : summaryFields) {
266             Object summaryFieldValue = ObjectPropertyUtils.getPropertyValue(line, summaryField);
267             if (StringUtils.isNotBlank(summaryFieldString)) {
268                 summaryFieldString += " - ";
269             }
270 
271             if (summaryFieldValue != null) {
272                 summaryFieldString += summaryFieldValue;
273             } else {
274                 summaryFieldString += "Null";
275             }
276         }
277 
278         String headerText = summaryTitle;
279         if (StringUtils.isNotBlank(summaryFieldString)) {
280             headerText += " ( " + summaryFieldString + " )";
281         }
282 
283         return headerText;
284     }
285 
286     
287 
288 
289     @Override
290     public Class<? extends Container> getSupportedContainer() {
291         return CollectionGroup.class;
292     }
293 
294     
295 
296 
297     @Override
298     public List<Component> getComponentsForLifecycle() {
299         List<Component> components = super.getComponentsForLifecycle();
300 
301         if (wrapperGroup != null) {
302             components.add(wrapperGroup);
303         } else {
304             components.addAll(stackedGroups);
305         }
306 
307         if (pagerWidget != null) {
308             components.add(pagerWidget);
309         }
310 
311         return components;
312     }
313 
314     
315 
316 
317     @Override
318     public List<Component> getComponentPrototypes() {
319         List<Component> components = super.getComponentPrototypes();
320 
321         components.add(addLineGroup);
322         components.add(lineGroupPrototype);
323         components.add(subCollectionFieldGroupPrototype);
324         components.add(selectFieldPrototype);
325 
326         return components;
327     }
328 
329     
330 
331 
332 
333 
334 
335 
336     @BeanTagAttribute(name = "summaryTitle")
337     public String getSummaryTitle() {
338         return this.summaryTitle;
339     }
340 
341     
342 
343 
344 
345 
346     public void setSummaryTitle(String summaryTitle) {
347         this.summaryTitle = summaryTitle;
348     }
349 
350     
351 
352 
353 
354 
355 
356 
357 
358 
359     @BeanTagAttribute(name = "summaryFields", type = BeanTagAttribute.AttributeType.LISTVALUE)
360     public List<String> getSummaryFields() {
361         return this.summaryFields;
362     }
363 
364     
365 
366 
367 
368 
369     public void setSummaryFields(List<String> summaryFields) {
370         this.summaryFields = summaryFields;
371     }
372 
373     
374 
375 
376 
377 
378 
379 
380 
381 
382 
383 
384 
385 
386     @BeanTagAttribute(name = "addLineGroup", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
387     public Group getAddLineGroup() {
388         return this.addLineGroup;
389     }
390 
391     
392 
393 
394 
395 
396     public void setAddLineGroup(Group addLineGroup) {
397         this.addLineGroup = addLineGroup;
398     }
399 
400     
401 
402 
403 
404 
405 
406 
407     @BeanTagAttribute(name = "lineGroupPrototype", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
408     public Group getLineGroupPrototype() {
409         return this.lineGroupPrototype;
410     }
411 
412     
413 
414 
415 
416 
417     public void setLineGroupPrototype(Group lineGroupPrototype) {
418         this.lineGroupPrototype = lineGroupPrototype;
419     }
420 
421     
422 
423 
424     @BeanTagAttribute(name = "subCollectionFieldGroupPrototype", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
425     public FieldGroup getSubCollectionFieldGroupPrototype() {
426         return this.subCollectionFieldGroupPrototype;
427     }
428 
429     
430 
431 
432 
433 
434     public void setSubCollectionFieldGroupPrototype(FieldGroup subCollectionFieldGroupPrototype) {
435         this.subCollectionFieldGroupPrototype = subCollectionFieldGroupPrototype;
436     }
437 
438     
439 
440 
441 
442 
443 
444 
445 
446 
447 
448 
449 
450 
451 
452     @BeanTagAttribute(name = "selectFieldPrototype", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
453     public Field getSelectFieldPrototype() {
454         return selectFieldPrototype;
455     }
456 
457     
458 
459 
460 
461 
462     public void setSelectFieldPrototype(Field selectFieldPrototype) {
463         this.selectFieldPrototype = selectFieldPrototype;
464     }
465 
466     
467 
468 
469 
470 
471 
472 
473 
474 
475 
476 
477 
478 
479 
480 
481     @BeanTagAttribute(name = "wrapperGroup", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
482     public Group getWrapperGroup() {
483         return wrapperGroup;
484     }
485 
486     
487 
488 
489 
490 
491     public void setWrapperGroup(Group wrapperGroup) {
492         this.wrapperGroup = wrapperGroup;
493     }
494 
495     
496 
497 
498 
499 
500     public Pager getPagerWidget() {
501         return pagerWidget;
502     }
503 
504     
505 
506 
507 
508 
509     public void setPagerWidget(Pager pagerWidget) {
510         this.pagerWidget = pagerWidget;
511     }
512 
513     
514 
515 
516 
517 
518     @BeanTagAttribute(name = "stackedGroups", type = BeanTagAttribute.AttributeType.LISTBEAN)
519     public List<Group> getStackedGroups() {
520         return this.stackedGroups;
521     }
522 
523     
524 
525 
526 
527 
528     public void setStackedGroups(List<Group> stackedGroups) {
529         this.stackedGroups = stackedGroups;
530     }
531 
532     
533 
534 
535 
536 
537 
538     public boolean isActionsInLineGroup() {
539         return actionsInLineGroup;
540     }
541 
542     
543 
544 
545 
546 
547     public void setActionsInLineGroup(boolean actionsInLineGroup) {
548         this.actionsInLineGroup = actionsInLineGroup;
549     }
550 
551     
552 
553 
554     @Override
555     protected <T> void copyProperties(T layoutManager) {
556         super.copyProperties(layoutManager);
557 
558         StackedLayoutManager stackedLayoutManagerCopy = (StackedLayoutManager) layoutManager;
559 
560         stackedLayoutManagerCopy.setSummaryTitle(this.getSummaryTitle());
561 
562         if (summaryFields != null) {
563             stackedLayoutManagerCopy.setSummaryFields(new ArrayList<String>(summaryFields));
564         }
565 
566         if (this.addLineGroup != null) {
567             stackedLayoutManagerCopy.setAddLineGroup((Group) this.getAddLineGroup().copy());
568         }
569 
570         if (this.lineGroupPrototype != null) {
571             stackedLayoutManagerCopy.setLineGroupPrototype((Group) this.getLineGroupPrototype().copy());
572         }
573 
574         if (this.wrapperGroup != null) {
575             stackedLayoutManagerCopy.setWrapperGroup((Group) this.getWrapperGroup().copy());
576         }
577 
578         if (this.subCollectionFieldGroupPrototype != null) {
579             stackedLayoutManagerCopy.setSubCollectionFieldGroupPrototype(
580                     (FieldGroup) this.getSubCollectionFieldGroupPrototype().copy());
581         }
582 
583         if (this.selectFieldPrototype != null) {
584             stackedLayoutManagerCopy.setSelectFieldPrototype((Field) this.getSelectFieldPrototype().copy());
585         }
586 
587         if (this.stackedGroups != null) {
588             List<Group> stackedGroupsCopy = Lists.newArrayListWithExpectedSize(stackedGroups.size());
589             for (Group stackedGroup : stackedGroups) {
590                 stackedGroupsCopy.add((Group) stackedGroup.copy());
591             }
592             stackedLayoutManagerCopy.setStackedGroups(stackedGroupsCopy);
593         }
594 
595         stackedLayoutManagerCopy.setPagerWidget((Pager) this.getPagerWidget().copy());
596 
597         stackedLayoutManagerCopy.setActionsInLineGroup(this.isActionsInLineGroup());
598     }
599 }