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.tree;
017
018import org.kuali.rice.core.api.util.tree.Node;
019import org.kuali.rice.core.api.util.tree.Tree;
020import org.kuali.rice.krms.api.repository.proposition.PropositionType;
021import org.kuali.rice.krms.dto.PropositionEditor;
022import org.kuali.rice.krms.dto.RuleEditor;
023import org.kuali.rice.krms.tree.node.TreeNode;
024import org.kuali.rice.krms.util.KRMSConstants;
025import org.kuali.rice.krms.util.PropositionTreeUtil;
026
027import java.util.List;
028
029/**
030 * This is a helper class to build the view tree to be displayed on the manage requisites page on the ui to display
031 * a readonly tree of the rule.
032 *
033 * @author Kuali Student Team
034 */
035public class RuleViewTreeBuilder extends AbstractTreeBuilder {
036
037    private static final long serialVersionUID = 1L;
038
039    public Tree<TreeNode, String> buildTree(RuleEditor rule) {
040
041        Tree myTree = new Tree<TreeNode, String>();
042
043        Node<TreeNode, String> rootNode = new Node<TreeNode, String>();
044        rootNode.setNodeLabel("root");
045        rootNode.setNodeType("rootNode");
046        rootNode.setData(new TreeNode("Rule:"));
047        myTree.setRootElement(rootNode);
048
049        if (rule == null) {
050            return myTree;
051        }
052
053        if (rule.getPropositionEditor() != null) {
054
055            buildPreviewTree(rule, rootNode, rule.getPropositionEditor());
056
057            //Underline the first node in the preview.
058            if ((rootNode.getChildren() != null) && (rootNode.getChildren().size() > 0)) {
059                Node<TreeNode, String> firstNode = rootNode.getChildren().get(0);
060                if ((firstNode.getChildren() != null) && (firstNode.getChildren().size() > 0)) {
061                    firstNode.setNodeType(KRMSConstants.NODE_TYPE_SUBRULEHEADER);
062                    addNodeType(firstNode, KRMSConstants.NODE_TYPE_SUBRULEELEMENT);
063                    firstNode.setNodeLabel("<u>" + firstNode.getNodeLabel() + ":</u>");
064                }
065            }
066        }
067
068        return myTree;
069    }
070
071    private void buildPreviewTree(RuleEditor rule, Node<TreeNode, String> currentNode, PropositionEditor prop) {
072        if (prop != null) {
073
074            Node<TreeNode, String> newNode = new Node<TreeNode, String>();
075            newNode.setNodeType(KRMSConstants.NODE_TYPE_SUBRULEELEMENT);
076            addNodeType(newNode, KRMSConstants.NODE_TYPE_VIEWELEMENT);
077
078            if (PropositionType.SIMPLE.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
079                newNode.setNodeLabel(this.buildNodeLabel(prop));
080            } else if (PropositionType.COMPOUND.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
081                newNode.setNodeLabel(this.buildNodeLabel(prop));
082                boolean first = true;
083                for (PropositionEditor child : prop.getCompoundEditors()) {
084                    // add an opcode node in between each of the children.
085                    if (!first) {
086                        //addOpCodeNode(newNode, propositionEditor);
087                        Node<TreeNode, String> opNode = new Node<TreeNode, String>();
088                        opNode.setNodeLabel(PropositionTreeUtil.getLabelForOperator(prop.getCompoundOpCode()));
089                        opNode.setData(new TreeNode(prop.getCompoundOpCode()));
090                        newNode.getChildren().add(opNode);
091                    }
092                    first = false;
093                    // call to build the child nodes
094                    buildPreviewTree(rule, newNode, child);
095                }
096            }
097
098            TreeNode tNode = new TreeNode(newNode.getNodeLabel());
099            tNode.setListItems(this.getListItems(prop));
100            tNode.setKey(prop.getKey());
101            newNode.setData(tNode);
102            currentNode.getChildren().add(newNode);
103
104        }
105    }
106
107    public List<Object> getListItems(PropositionEditor propositionEditor) {
108        return null;
109    }
110
111    @Override
112    public String getNaturalLanguageUsageKey() {
113        return null;
114    }
115}