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