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