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 *
035 * @author Kuali Student Team
036 */
037public class RulePreviewTreeBuilder extends AbstractTreeBuilder{
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        buildPreviewTree(rule, rootNode, rule.getPropositionEditor());
054
055        //Underline the first node in the preview.
056        if ((rootNode.getChildren() != null) && (rootNode.getChildren().size() > 0)) {
057            Node<TreeNode, String> firstNode = rootNode.getChildren().get(0);
058            if ((firstNode.getChildren() != null) && (firstNode.getChildren().size() > 0)) {
059                firstNode.setNodeType(KRMSConstants.NODE_TYPE_SUBRULEHEADER);
060                addNodeType(firstNode, KRMSConstants.NODE_TYPE_SUBRULEELEMENT);
061                TreeNode treeNode = firstNode.getData();
062                treeNode.setData("<u>" + treeNode.getData() + ":</u>");
063            }
064        }
065
066        return myTree;
067    }
068
069    private void buildPreviewTree(RuleEditor rule, Node<TreeNode, String> currentNode, PropositionEditor prop) {
070        if (prop != null) {
071
072            Node<TreeNode, String> newNode = new Node<TreeNode, String>();
073            newNode.setNodeLabel(null);
074            newNode.setNodeType(KRMSConstants.NODE_TYPE_SUBRULEELEMENT);
075            addNodeType(newNode, KRMSConstants.NODE_TYPE_VIEWELEMENT);
076
077            TreeNode tNode = null;
078            if (PropositionType.SIMPLE.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
079                tNode = new TreeNode(this.buildNodeLabel(prop));
080            } else if (PropositionType.COMPOUND.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
081                tNode = new TreeNode(StringEscapeUtils.escapeHtml(this.getDescription(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(null));
090                        newNode.getChildren().add(opNode);
091
092                    }
093                    first = false;
094                    // call to build the childs node
095                    buildPreviewTree(rule, newNode, child);
096                }
097            }
098
099            tNode.setListItems(this.getListItems(prop));
100            newNode.setData(tNode);
101            tNode.setKey(prop.getKey());
102            currentNode.getChildren().add(newNode);
103
104        }
105    }
106
107    @Override
108    protected String buildNodeLabel(PropositionEditor prop){
109        //Build the node label.
110        String prefix = this.getPropositionPrefix(prop);
111        return prefix + StringEscapeUtils.escapeHtml(this.getDescription(prop));
112    }
113
114    @Override
115    public String getNaturalLanguageUsageKey() {
116        return null;
117    }
118
119    public List<Object> getListItems(PropositionEditor propositionEditor) {
120        return null;
121    }
122}