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.apache.commons.lang.StringEscapeUtils;
019import org.kuali.rice.core.api.util.tree.Node;
020import org.kuali.rice.core.api.util.tree.Tree;
021import org.kuali.rice.krms.api.repository.LogicalOperator;
022import org.kuali.rice.krms.api.repository.proposition.PropositionDefinitionContract;
023import org.kuali.rice.krms.api.repository.proposition.PropositionType;
024import org.kuali.rice.krms.api.repository.rule.RuleDefinitionContract;
025import org.kuali.rice.krms.dto.PropositionEditor;
026import org.kuali.rice.krms.dto.RuleEditor;
027import org.kuali.rice.krms.tree.node.TreeNode;
028import org.kuali.rice.krms.util.KRMSConstants;
029import org.kuali.rice.krms.util.PropositionTreeUtil;
030
031import java.util.List;
032
033/**
034 * This is a helper class to build the preview tree to be displayed on the manage requisites page on the ui to display
035 * a readonly tree of the rule on the edit with logic tab.
036 *
037 * @author Kuali Student Team
038 */
039public class RulePreviewTreeBuilder extends AbstractTreeBuilder{
040
041    public Tree<TreeNode, String> buildTree(RuleEditor rule) {
042
043        Tree myTree = new Tree<TreeNode, String>();
044
045        Node<TreeNode, String> rootNode = new Node<TreeNode, String>();
046        rootNode.setNodeLabel("root");
047        rootNode.setNodeType("rootNode");
048        rootNode.setData(new TreeNode("Rule:"));
049        myTree.setRootElement(rootNode);
050
051        if (rule == null) {
052            return myTree;
053        }
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                TreeNode treeNode = firstNode.getData();
064                treeNode.setData("<u>" + treeNode.getData() + ":</u>");
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.setNodeLabel(null);
076            newNode.setNodeType(KRMSConstants.NODE_TYPE_SUBRULEELEMENT);
077            addNodeType(newNode, KRMSConstants.NODE_TYPE_VIEWELEMENT);
078
079            TreeNode tNode = null;
080            if (PropositionType.SIMPLE.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
081                tNode = new TreeNode(this.buildNodeLabel(prop));
082            } else if (PropositionType.COMPOUND.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
083                tNode = new TreeNode(this.getDescription(prop));
084                tNode.setCompound(true);
085                boolean first = true;
086                for (PropositionEditor child : prop.getCompoundEditors()) {
087                    // add an opcode node in between each of the children.
088                    if (!first) {
089                        //addOpCodeNode(newNode, propositionEditor);
090                        Node<TreeNode, String> opNode = new Node<TreeNode, String>();
091                        opNode.setNodeLabel(PropositionTreeUtil.getLabelForOperator(prop.getCompoundOpCode()));
092                        opNode.setData(new TreeNode(null));
093                        newNode.getChildren().add(opNode);
094                    }
095                    first = false;
096                    // call to build the child nodes
097                    buildPreviewTree(rule, newNode, child);
098                }
099            }
100
101            tNode.setListItems(this.getListItems(prop));
102            newNode.setData(tNode);
103            tNode.setKey(prop.getKey());
104            currentNode.getChildren().add(newNode);
105
106        }
107    }
108
109    @Override
110    protected String buildNodeLabel(PropositionEditor prop){
111        //Build the node label.
112        String prefix = this.getPropositionPrefix(prop);
113        return prefix + this.getDescription(prop);
114    }
115
116    @Override
117    public String getNaturalLanguageUsageKey() {
118        return null;
119    }
120
121    public List<Object> getListItems(PropositionEditor propositionEditor) {
122        return null;
123    }
124}