View Javadoc
1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krms.util;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.util.tree.Node;
20  import org.kuali.rice.core.api.util.tree.Tree;
21  import org.kuali.rice.krad.uif.UifConstants;
22  import org.kuali.rice.krad.uif.container.CollectionGroup;
23  import org.kuali.rice.krad.uif.container.Group;
24  import org.kuali.rice.krad.uif.container.NodePrototype;
25  import org.kuali.rice.krad.uif.container.TreeGroup;
26  import org.kuali.rice.krad.uif.element.Message;
27  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleUtils;
28  import org.kuali.rice.krad.uif.util.ComponentUtils;
29  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
30  import org.kuali.rice.krad.uif.view.View;
31  
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  /**
36   * @author Kuali Student Team
37   */
38  public class ViewTreeGroup extends TreeGroup {
39  
40  
41      /**
42       * This method is overridden to add a null check.
43       *
44       * @param model
45       */
46      protected void buildTreeGroups(Object model) {
47          // get Tree data property
48          Tree<Object, String> treeData = ObjectPropertyUtils.getPropertyValue(model, getBindingInfo().getBindingPath());
49  
50          if (treeData == null) {
51              return;
52          }
53  
54          // build component tree that corresponds with tree data
55          Tree<Group, Message> treeGroups = new Tree<Group, Message>();
56  
57          String bindingPrefix = getBindingInfo().getBindingPrefixForNested();
58          Node<Group, Message> rootNode = buildTreeNode(treeData.getRootElement(),
59                  bindingPrefix + ".rootElement", "_root");
60          treeGroups.setRootElement(rootNode);
61  
62          setTreeGroups(treeGroups);
63      }
64  
65      /**
66       * This method is overridden to set suffix on subcollections.
67       *
68       * @param nodeData
69       * @param bindingPrefix
70       * @param parentNode
71       * @return
72       */
73      @Override
74      protected Node<Group, Message> buildTreeNode(Node<Object, String> nodeData, String bindingPrefix, String parentNode) {
75  
76          if (nodeData == null) {
77              return null;
78          }
79  
80          Node<Group, Message> node = new Node<Group, Message>();
81          node.setNodeType(nodeData.getNodeType());
82  
83          //Note: view tree does noet use the prototype map.
84          NodePrototype prototype = this.getDefaultNodePrototype();
85  
86          Message message = ComponentUtils.copy(prototype.getLabelPrototype(), parentNode);
87          ComponentUtils.pushObjectToContext(message, UifConstants.ContextVariableNames.NODE, nodeData);
88          message.setMessageText(nodeData.getNodeLabel());
89          node.setNodeLabel(message);
90  
91          Group nodeGroup = ComponentUtils.copyComponent(prototype.getDataGroupPrototype(), bindingPrefix + ".data",
92                  parentNode);
93          ComponentUtils.pushObjectToContext(nodeGroup, UifConstants.ContextVariableNames.NODE, nodeData);
94  
95          String nodePath = bindingPrefix + ".data";
96          if (StringUtils.isNotBlank(getBindingInfo().getBindingObjectPath())) {
97              nodePath = getBindingInfo().getBindingObjectPath() + "." + nodePath;
98          }
99          ComponentUtils.pushObjectToContext(nodeGroup, UifConstants.ContextVariableNames.NODE_PATH, nodePath);
100 
101         /*Overridden section*/
102         List<CollectionGroup> components = ViewLifecycleUtils.getElementsOfTypeShallow(nodeGroup, CollectionGroup.class);
103         for (CollectionGroup fieldCollectionGroup : components) {
104             fieldCollectionGroup.setSubCollectionSuffix(parentNode);
105         }
106         node.setData(nodeGroup);
107 
108         List<Node<Group, Message>> nodeChildren = new ArrayList<Node<Group, Message>>();
109 
110         int childIndex = 0;
111         for (Node<Object, String> childDataNode : nodeData.getChildren()) {
112             String nextBindingPrefix = bindingPrefix + ".children[" + childIndex + "]";
113             Node<Group, Message> childNode = buildTreeNode(childDataNode, nextBindingPrefix,
114                     "_node" + childIndex + parentNode); //Removed unnecessary suffix elements.
115 
116             nodeChildren.add(childNode);
117 
118             // Don't forget about me:
119             ++childIndex;
120         }
121         node.setChildren(nodeChildren);
122 
123         return node;
124     }
125 }