001    package org.kuali.rice.krms.tree;
002    
003    import org.kuali.rice.core.api.util.tree.Node;
004    import org.kuali.rice.core.api.util.tree.Tree;
005    import org.kuali.rice.krms.api.repository.LogicalOperator;
006    import org.kuali.rice.krms.api.repository.proposition.PropositionType;
007    import org.kuali.rice.krms.dto.PropositionEditor;
008    import org.kuali.rice.krms.dto.RuleEditor;
009    import org.kuali.rice.krms.tree.node.TreeNode;
010    
011    import java.util.HashMap;
012    import java.util.List;
013    import java.util.Map;
014    
015    /**
016     * Created with IntelliJ IDEA.
017     * User: Peggy
018     * Date: 2/4/13
019     * Time: 3:26 PM
020     * To change this template use File | Settings | File Templates.
021     */
022    public class RuleViewTreeBuilder extends AbstractTreeBuilder {
023    
024        private static final long serialVersionUID = 1L;
025    
026        public Tree<TreeNode, String> buildTree(RuleEditor rule) {
027    
028            Tree myTree = new Tree<TreeNode, String>();
029    
030            Node<TreeNode, String> rootNode = new Node<TreeNode, String>();
031            rootNode.setNodeLabel("root");
032            rootNode.setNodeType("rootNode");
033            rootNode.setData(new TreeNode("Rule:"));
034            myTree.setRootElement(rootNode);
035    
036            if (rule == null) {
037                return myTree;
038            }
039    
040            PropositionEditor prop = (PropositionEditor) rule.getProposition();
041    
042            if (prop != null) {
043    
044                buildPreviewTree(rule, rootNode, prop);
045    
046                //Underline the first node in the preview.
047                if ((rootNode.getChildren() != null) && (rootNode.getChildren().size() > 0)) {
048                    Node<TreeNode, String> firstNode = rootNode.getChildren().get(0);
049                    if ((firstNode.getChildren() != null) && (firstNode.getChildren().size() > 0)) {
050                        firstNode.setNodeType("subruleHeader subruleElement");
051                        firstNode.setNodeLabel("<u>" + firstNode.getNodeLabel() + ":</u>");
052                    }
053                }
054            }
055    
056            return myTree;
057        }
058    
059        private void buildPreviewTree(RuleEditor rule, Node<TreeNode, String> currentNode, PropositionEditor prop) {
060            if (prop != null) {
061    
062                Node<TreeNode, String> newNode = new Node<TreeNode, String>();
063                newNode.setNodeLabel(this.buildNodeLabel(rule, prop));
064                newNode.setNodeType("subruleElement");
065    
066                TreeNode tNode = new TreeNode(newNode.getNodeLabel());
067                tNode.setListItems(this.getListItems(prop));
068                tNode.setKey(prop.getKey());
069                newNode.setData(tNode);
070                currentNode.getChildren().add(newNode);
071    
072                if (PropositionType.COMPOUND.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())) {
073    
074                    boolean first = true;
075                    for (PropositionEditor child : prop.getCompoundEditors()) {
076                        // add an opcode node in between each of the children.
077                        if (!first) {
078                            //addOpCodeNode(newNode, propositionEditor);
079                            Node<TreeNode, String> opNode = new Node<TreeNode, String>();
080                            if (LogicalOperator.AND.getCode().equalsIgnoreCase(prop.getCompoundOpCode())) {
081                                opNode.setNodeLabel("AND");
082                            } else if (LogicalOperator.OR.getCode().equalsIgnoreCase(prop.getCompoundOpCode())) {
083                                opNode.setNodeLabel("OR");
084                            }
085    
086                            opNode.setData(new TreeNode(prop.getCompoundOpCode()));
087                            newNode.getChildren().add(opNode);
088                        }
089                        first = false;
090                        // call to build the childs node
091                        buildPreviewTree(rule, newNode, child);
092                    }
093                }
094    
095            }
096        }
097    
098        public List<String> getListItems(PropositionEditor propositionEditor) {
099            return null;
100        }
101    }