001/**
002 * Copyright 2005-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krms.util;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.util.tree.Node;
020import org.kuali.rice.core.api.util.tree.Tree;
021import org.kuali.rice.krad.uif.UifConstants;
022import org.kuali.rice.krad.uif.container.CollectionGroup;
023import org.kuali.rice.krad.uif.container.Group;
024import org.kuali.rice.krad.uif.container.NodePrototype;
025import org.kuali.rice.krad.uif.container.TreeGroup;
026import org.kuali.rice.krad.uif.element.Message;
027import org.kuali.rice.krad.uif.util.ComponentUtils;
028import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
029import org.kuali.rice.krad.uif.view.View;
030
031import java.util.ArrayList;
032import java.util.List;
033
034/**
035 * @author Kuali Student Team
036 */
037public class ViewTreeGroup extends TreeGroup {
038
039
040    /**
041     * This method is overridden to add a null check.
042     *
043     * @param view
044     * @param model
045     */
046    protected void buildTreeGroups(View view, Object model) {
047        // get Tree data property
048        Tree<Object, String> treeData = ObjectPropertyUtils.getPropertyValue(model, getBindingInfo().getBindingPath());
049
050        if (treeData == null) {
051            return;
052        }
053
054        // build component tree that corresponds with tree data
055        Tree<Group, Message> treeGroups = new Tree<Group, Message>();
056
057        String bindingPrefix = getBindingInfo().getBindingPrefixForNested();
058        Node<Group, Message> rootNode = buildTreeNode(treeData.getRootElement(),
059                bindingPrefix + ".rootElement", "_root");
060        treeGroups.setRootElement(rootNode);
061
062        setTreeGroups(treeGroups);
063    }
064
065    /**
066     * This method is overridden to set suffix on subcollections.
067     *
068     * @param nodeData
069     * @param bindingPrefix
070     * @param parentNode
071     * @return
072     */
073    @Override
074    protected Node<Group, Message> buildTreeNode(Node<Object, String> nodeData, String bindingPrefix, String parentNode) {
075
076        if (nodeData == null) {
077            return null;
078        }
079
080        Node<Group, Message> node = new Node<Group, Message>();
081        node.setNodeType(nodeData.getNodeType());
082
083        //Note: view tree does noet use the prototype map.
084        NodePrototype prototype = this.getDefaultNodePrototype();
085
086        Message message = ComponentUtils.copy(prototype.getLabelPrototype(), parentNode);
087        ComponentUtils.pushObjectToContext(message, UifConstants.ContextVariableNames.NODE, nodeData);
088        message.setMessageText(nodeData.getNodeLabel());
089        node.setNodeLabel(message);
090
091        Group nodeGroup = ComponentUtils.copyComponent(prototype.getDataGroupPrototype(), bindingPrefix + ".data",
092                parentNode);
093        ComponentUtils.pushObjectToContext(nodeGroup, UifConstants.ContextVariableNames.NODE, nodeData);
094
095        String nodePath = bindingPrefix + ".data";
096        if (StringUtils.isNotBlank(getBindingInfo().getBindingObjectPath())) {
097            nodePath = getBindingInfo().getBindingObjectPath() + "." + nodePath;
098        }
099        ComponentUtils.pushObjectToContext(nodeGroup, UifConstants.ContextVariableNames.NODE_PATH, nodePath);
100
101        /*Overridden section*/
102        List<CollectionGroup> components = ComponentUtils.getComponentsOfTypeShallow(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}