Coverage Report - org.kuali.rice.krms.impl.repository.RuleBo
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleBo
0%
0/98
0%
0/42
0
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.kuali.rice.krms.impl.repository;
 18  
 
 19  
 
 20  
 import java.util.Map.Entry
 21  
 import org.kuali.rice.core.api.util.tree.Node
 22  
 import org.kuali.rice.core.api.util.tree.Tree
 23  
 import org.kuali.rice.krad.bo.PersistableBusinessObjectBase
 24  
 import org.kuali.rice.krms.api.repository.LogicalOperator
 25  
 import org.kuali.rice.krms.api.repository.action.ActionDefinition
 26  
 import org.kuali.rice.krms.api.repository.proposition.PropositionType
 27  
 import org.kuali.rice.krms.api.repository.rule.RuleDefinition
 28  
 import org.kuali.rice.krms.api.repository.rule.RuleDefinitionContract
 29  
 import org.kuali.rice.krms.impl.ui.CompoundOpCodeNode
 30  
 import org.kuali.rice.krms.impl.ui.RuleTreeNode
 31  
 import org.kuali.rice.krms.impl.ui.SimplePropositionNode
 32  
 import org.kuali.rice.krms.impl.ui.SimplePropositionEditNode
 33  
 import org.kuali.rice.krms.impl.ui.CompoundPropositionEditNode
 34  
 
 35  
 public class RuleBo extends PersistableBusinessObjectBase implements RuleDefinitionContract {
 36  
    
 37  
    String id;
 38  
    String namespace;
 39  
    String description;
 40  
    String name;
 41  
    String typeId;
 42  
    String propId;
 43  
 
 44  
    PropositionBo proposition;
 45  
    List<ActionBo> actions;
 46  
    List<RuleAttributeBo> attributeBos;
 47  
    //def List<PropositionBo> allChildPropositions
 48  
    
 49  
    // for Rule editor display
 50  
    Tree<RuleTreeNode, String> propositionTree;
 51  
    
 52  
    // for rule editor display
 53  
    String propositionSummary;
 54  
    private StringBuffer propositionSummaryBuffer;
 55  
    String selectedPropositionId;
 56  
 
 57  
    public RuleBo() {
 58  0
        actions = new ArrayList<ActionBo>();
 59  0
        attributeBos = new ArrayList<RuleAttributeBo>();
 60  
    }
 61  
 
 62  
    public PropositionBo getProposition(){
 63  0
        return proposition;
 64  
    }
 65  
    public void setProposition(PropositionBo proposition){
 66  0
        if (proposition != null) {
 67  0
            propId = proposition.getId();
 68  
        } else {
 69  0
            propId = null;
 70  
        }
 71  0
        this.proposition = proposition;
 72  
    }
 73  
    
 74  
    public Map<String, String> getAttributes() {
 75  0
        HashMap<String, String> attributes = new HashMap<String, String>();
 76  0
        for (RuleAttributeBo attr : attributeBos) {
 77  0
            attributes.put( attr.getAttributeDefinition().getName(), attr.getValue() );
 78  
        }
 79  0
        return attributes;
 80  
    }
 81  
    
 82  
    public String getPropositionSummary(){
 83  0
        return propositionSummaryBuffer.toString();
 84  
    }
 85  
    
 86  
    /**
 87  
     * This method is used by the RuleEditor to display the proposition in tree form.
 88  
     *
 89  
     * @return Tree representing a rule proposition.
 90  
     */
 91  
     public Tree getPropositionTree() {
 92  0
         if (this.propositionTree == null){
 93  0
             this.propositionTree = refreshPropositionTree(false);
 94  
         }
 95  
 
 96  0
         return this.propositionTree;
 97  
     }
 98  
     public void setPropositionTree(Tree<RuleTreeNode, String> tree) {
 99  0
         this.propositionTree == tree;
 100  
        }
 101  
 
 102  
    public Tree refreshPropositionTree(Boolean editMode){
 103  0
        Tree myTree = new Tree<RuleTreeNode, String>();
 104  
 
 105  0
        Node<RuleTreeNode, String> rootNode = new Node<RuleTreeNode, String>();
 106  0
        myTree.setRootElement(rootNode);
 107  
 
 108  0
        propositionSummaryBuffer = new StringBuffer();
 109  0
        PropositionBo prop = this.getProposition();
 110  0
        buildPropTree( rootNode, prop, editMode );
 111  0
        this.propositionTree = myTree;
 112  0
        return myTree;
 113  
    }
 114  
 
 115  
     /**
 116  
      * This method builds a propositionTree recursively walking through the children of the proposition.
 117  
      * @param sprout - parent tree node
 118  
      * @param prop - PropositionBo for which to make the tree node
 119  
      * @param editMode - Boolean determines the node type used to represent the proposition
 120  
      *     false: create a view only node text control
 121  
      *     true: create an editable node with multiple controls
 122  
      *     null:  use the proposition.editMode property to determine the node type
 123  
      */
 124  
    private void buildPropTree( Node sprout, PropositionBo prop, Boolean editMode){
 125  
        // Depending on the type of proposition (simple/compound), and the editMode,
 126  
        // Create a treeNode of the appropriate type for the node and attach it to the
 127  
        // sprout parameter passed in.
 128  
        // If the prop is a compound proposition, calls itself for each of the compoundComponents
 129  0
        if (prop != null) {
 130  0
            if (PropositionType.SIMPLE.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())){
 131  
                // Simple Proposition
 132  
                // add a node for the description display with a child proposition node
 133  0
                Node<RuleTreeNode, String> child = new Node<RuleTreeNode, String>();
 134  0
                child.setNodeLabel(prop.getDescription());
 135  0
                if (prop.getEditMode()){
 136  0
                    child.setNodeLabel("");
 137  0
                    child.setNodeType(SimplePropositionEditNode.NODE_TYPE);
 138  0
                    SimplePropositionEditNode pNode = new SimplePropositionEditNode(prop);
 139  0
                    child.setData(pNode);
 140  
                 } else {
 141  0
                   child.setNodeType(SimplePropositionNode.NODE_TYPE);
 142  0
                    SimplePropositionNode pNode = new SimplePropositionNode(prop);
 143  0
                    child.setData(pNode);
 144  
                }
 145  0
                sprout.getChildren().add(child);
 146  0
                propositionSummaryBuffer.append(prop.getParameterDisplayString())
 147  
            }
 148  0
            else if (PropositionType.COMPOUND.getCode().equalsIgnoreCase(prop.getPropositionTypeCode())){
 149  
                // Compound Proposition
 150  0
                propositionSummaryBuffer.append(" ( ");
 151  0
                Node<RuleTreeNode, String> aNode = new Node<RuleTreeNode, String>();
 152  0
                aNode.setNodeLabel(prop.getDescription());
 153  
                // editMode has description as an editable field
 154  0
                if (prop.getEditMode()){
 155  0
                    aNode.setNodeLabel("");
 156  0
                    aNode.setNodeType("ruleTreeNode compoundNode editNode");
 157  0
                    CompoundPropositionEditNode pNode = new CompoundPropositionEditNode(prop);
 158  0
                    aNode.setData(pNode);
 159  
                } else {
 160  0
                    aNode.setNodeType("ruleTreeNode compoundNode");
 161  0
                    RuleTreeNode pNode = new RuleTreeNode(prop);
 162  0
                    aNode.setData(pNode);
 163  
                }
 164  0
                sprout.getChildren().add(aNode);
 165  
 
 166  0
                boolean first = true;
 167  0
                List <PropositionBo> allMyChildren = prop.getCompoundComponents();
 168  0
                for (PropositionBo child : allMyChildren){
 169  
                    // add an opcode node in between each of the children.
 170  0
                    if (!first){
 171  0
                        addOpCodeNode(aNode, prop);
 172  
                    }
 173  0
                    first = false;
 174  
                    // call to build the childs node
 175  0
                    buildPropTree(aNode, child, editMode);
 176  
                }
 177  0
                propositionSummaryBuffer.append(" ) ");
 178  
            }
 179  
        }
 180  
    }
 181  
 
 182  
    /**
 183  
     * 
 184  
     * This method adds an opCode Node to separate components in a compound proposition.
 185  
     * 
 186  
     * @param currentNode
 187  
     * @param prop
 188  
     * @return
 189  
     */
 190  
    private void addOpCodeNode(Node currentNode, PropositionBo prop){
 191  0
        String opCodeLabel = "";
 192  
        
 193  0
        if (LogicalOperator.AND.getCode().equalsIgnoreCase(prop.getCompoundOpCode())){
 194  0
            opCodeLabel = "AND";
 195  0
        } else if (LogicalOperator.OR.getCode().equalsIgnoreCase(prop.getCompoundOpCode())){
 196  0
            opCodeLabel = "OR";
 197  
        }
 198  0
        propositionSummaryBuffer.append(" "+opCodeLabel+" ");
 199  0
        Node<RuleTreeNode, String> aNode = new Node<RuleTreeNode, String>();
 200  0
        aNode.setNodeLabel("");
 201  0
        aNode.setNodeType("ruleTreeNode compoundOpCodeNode");
 202  0
        aNode.setData(new CompoundOpCodeNode(prop));
 203  0
        currentNode.getChildren().add(aNode);
 204  
    }
 205  
    
 206  
    
 207  
    /**
 208  
     * 
 209  
    * Converts a mutable bo to it's immutable counterpart
 210  
    * @param bo the mutable business object
 211  
    * @return the immutable object
 212  
    */
 213  
   static RuleDefinition to(RuleBo bo) {
 214  0
       if (bo == null) { return null; }
 215  0
       return org.kuali.rice.krms.api.repository.rule.RuleDefinition.Builder.create(bo).build();
 216  
   }
 217  
 
 218  
   /**
 219  
    * Converts a immutable object to it's mutable bo counterpart
 220  
    * @param im immutable object
 221  
    * @return the mutable bo
 222  
    */
 223  
   static RuleBo from(RuleDefinition im) {
 224  0
       if (im == null) { return null; }
 225  
 
 226  0
       RuleBo bo = new RuleBo();
 227  0
       bo.id = im.getId();
 228  0
       bo.namespace = im.getNamespace();
 229  0
       bo.name = im.getName();
 230  0
       bo.description = im.getDescription();
 231  0
       bo.typeId = im.getTypeId();
 232  0
       bo.propId = im.getPropId();
 233  0
       bo.proposition = PropositionBo.from(im.getProposition());
 234  0
       bo.versionNumber = im.getVersionNumber();
 235  
       
 236  0
       bo.actions = new ArrayList<ActionBo>();
 237  0
       for (ActionDefinition action : im.getActions()){
 238  0
           bo.actions.add( ActionBo.from(action) );
 239  
       }
 240  
 
 241  
       // build the set of agenda attribute BOs
 242  0
       List<RuleAttributeBo> attrs = new ArrayList<RuleAttributeBo>();
 243  
 
 244  
       // for each converted pair, build an RuleAttributeBo and add it to the set
 245  0
       RuleAttributeBo attributeBo;
 246  0
       for (Entry<String,String> entry  : im.getAttributes().entrySet()){
 247  0
           KrmsAttributeDefinitionBo attrDefBo = KrmsRepositoryServiceLocator
 248  
                   .getKrmsAttributeDefinitionService()
 249  0
                   .getKrmsAttributeBo(entry.getKey(), im.getNamespace());
 250  0
           attributeBo = new RuleAttributeBo();
 251  0
           attributeBo.setRuleId( im.getId() );
 252  0
           attributeBo.setAttributeDefinitionId( attrDefBo.getId() );
 253  0
           attributeBo.setValue( entry.getValue() );
 254  0
           attributeBo.setAttributeDefinition( attrDefBo );
 255  0
           attrs.add( attributeBo );
 256  
       }
 257  0
       bo.setAttributeBos(attrs);
 258  
 
 259  0
       return bo;
 260  
   }
 261  
 
 262  
 }