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            String data = null;
080            boolean compound = false;
081            if (PropositionType.SIMPLE.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
082                data = this.buildNodeLabel(prop);
083            } else if (PropositionType.COMPOUND.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
084                data = this.getDescription(prop);
085                compound = true;
086                boolean first = true;
087                for (PropositionEditor child : prop.getCompoundEditors()) {
088                    // add an opcode node in between each of the children.
089                    if (!first) {
090                        //addOpCodeNode(newNode, propositionEditor);
091                        Node<TreeNode, String> opNode = new Node<TreeNode, String>();
092                        opNode.setNodeLabel(PropositionTreeUtil.getLabelForOperator(prop.getCompoundOpCode()));
093                        opNode.setData(new TreeNode(null));
094                        newNode.getChildren().add(opNode);
095                    }
096                    first = false;
097                    // call to build the child nodes
098                    buildPreviewTree(rule, newNode, child);
099                }
100            }
101
102            newNode.setData(this.createTreeNode(rule, data, prop, compound));
103            currentNode.getChildren().add(newNode);
104
105        }
106    }
107
108    public TreeNode createTreeNode(RuleEditor rule, String data, PropositionEditor prop, boolean compound){
109        TreeNode tNode = new TreeNode(data);
110        tNode.setKey(prop.getKey());
111        return tNode;
112    }
113
114    @Override
115    protected String buildNodeLabel(PropositionEditor prop){
116        //Build the node label.
117        String prefix = this.getPropositionPrefix(prop);
118        return prefix + this.getDescription(prop);
119    }
120
121    @Override
122    public String getNaturalLanguageUsageKey() {
123        return null;
124    }
125
126}