Coverage Report - org.kuali.rice.krms.impl.repository.PropositionBo
 
Classes in this File Line Coverage Branch Coverage Complexity
PropositionBo
0%
0/108
0%
0/56
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  
 package org.kuali.rice.krms.impl.repository
 17  
 
 18  
 
 19  
 import org.kuali.rice.krad.bo.PersistableBusinessObjectBase
 20  
 
 21  
 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
 22  
 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinitionContract;
 23  
 import org.kuali.rice.krad.service.BusinessObjectService;
 24  
 import org.kuali.rice.krad.service.KRADServiceLocator
 25  
 import org.kuali.rice.krms.api.repository.proposition.PropositionType
 26  
 import org.kuali.rice.krms.api.repository.proposition.PropositionParameterType
 27  
 import org.kuali.rice.krad.service.SequenceAccessorService
 28  
 import org.kuali.rice.krms.api.repository.LogicalOperator;
 29  
 
 30  
 
 31  
 public class PropositionBo extends PersistableBusinessObjectBase implements PropositionDefinitionContract {
 32  
 
 33  
         def String id
 34  
         def String description
 35  
     def String ruleId
 36  
         def String typeId
 37  
         def String propositionTypeCode
 38  
         
 39  
         def List<PropositionParameterBo> parameters
 40  
         
 41  
         // Compound parameter related properties
 42  
         def String compoundOpCode
 43  
         def List<PropositionBo> compoundComponents
 44  
 
 45  
     // parameter display string (for tree display)
 46  
     def String parameterDisplayString
 47  
     def boolean editMode = false
 48  
     def String categoryId;
 49  
 
 50  
     private SequenceAccessorService sequenceAccessorService;  //todo move to wrapper object
 51  
 
 52  
     private void setupParameterDisplayString(){
 53  0
         if (PropositionType.SIMPLE.getCode().equalsIgnoreCase(getPropositionTypeCode())){
 54  
             // Simple Propositions should have 3 parameters ordered in reverse polish notation.
 55  
             // TODO: enhance to get term names for term type parameters.
 56  0
             List<PropositionParameterBo> parameters = getParameters();
 57  0
             if (parameters != null && parameters.size() == 3){
 58  0
                 setParameterDisplayString(getParamValue(parameters.get(0))
 59  0
                         + " " + getParamValue(parameters.get(2))
 60  0
                         + " " + getParamValue(parameters.get(1)));
 61  
             } else {
 62  
                 // should not happen
 63  
             }
 64  
         }
 65  
     }
 66  
 
 67  
     private String getParamValue(PropositionParameterBo prop){
 68  0
         if (PropositionParameterType.TERM.getCode().equalsIgnoreCase(prop.getParameterType())){
 69  0
             String termName = "";
 70  0
             String termId = prop.getValue();
 71  0
             if (termId != null && termId.length()>0){
 72  
                 //TODO: use termBoService
 73  0
                 TermBo term = getBoService().findBySinglePrimaryKey(TermBo.class,termId);
 74  0
                 termName = term.getSpecification().getName();
 75  
             }
 76  0
             return termName;
 77  
         } else {
 78  0
             return prop.getValue();
 79  
         }
 80  
     }
 81  
     /**
 82  
      * @return the parameterDisplayString
 83  
      */
 84  
     public String getParameterDisplayString() {
 85  0
         if (parameterDisplayString == null){
 86  0
             setupParameterDisplayString()
 87  
         }
 88  0
         return this.parameterDisplayString;
 89  
     }
 90  
 
 91  
     /**
 92  
      * @param parameterDisplayString the parameterDisplayString to set
 93  
      */
 94  
     public void setParameterDisplayString(String parameterDisplayString) {
 95  0
         this.parameterDisplayString = parameterDisplayString;
 96  
     }
 97  
 
 98  
     public boolean getEditMode(){
 99  0
         return this.editMode;
 100  
     }
 101  
 
 102  
     public void setEditMode(boolean editMode){
 103  0
         this.editMode = editMode;
 104  
     }
 105  
 
 106  
     public String getCategoryId(){
 107  0
         return this.categoryId;
 108  
     }
 109  
 
 110  
     public void setCategoryId(String categoryId){
 111  0
         this.categoryId = categoryId;
 112  
     }
 113  
 
 114  
     public BusinessObjectService getBoService() {
 115  0
         return KRADServiceLocator.getBusinessObjectService();
 116  
     }
 117  
 
 118  
 
 119  
         /**
 120  
         * Converts a mutable bo to it's immutable counterpart
 121  
         * @param bo the mutable business object
 122  
         * @return the immutable object
 123  
         */
 124  
    static PropositionDefinition to(PropositionBo bo) {
 125  0
            if (bo == null) { return null }
 126  0
            return org.kuali.rice.krms.api.repository.proposition.PropositionDefinition.Builder.create(bo).build()
 127  
    }
 128  
 
 129  
    /**
 130  
         * Converts a immutable object to it's mutable bo counterpart
 131  
         * @param im immutable object
 132  
         * @return the mutable bo
 133  
         */
 134  
    static PropositionBo from(PropositionDefinition im) {
 135  0
            if (im == null) { return null }
 136  
 
 137  0
            PropositionBo bo = new PropositionBo()
 138  0
            bo.id = im.id
 139  0
            bo.description = im.description
 140  
 
 141  
        //bo.ruleId = im.ruleId
 142  0
        setRuleIdRecursive(im.ruleId, bo)
 143  
        
 144  0
            bo.typeId = im.typeId
 145  0
            bo.propositionTypeCode = im.propositionTypeCode
 146  0
            bo.parameters = new ArrayList<PropositionParameterBo>()
 147  0
            for ( parm in im.parameters){
 148  0
                    bo.parameters.add (PropositionParameterBo.from(parm))
 149  
            }
 150  0
            bo.compoundOpCode = im.compoundOpCode
 151  0
            bo.compoundComponents = new ArrayList<PropositionBo>()
 152  0
            for (prop in im.compoundComponents){
 153  0
                    bo.compoundComponents.add (PropositionBo.from(prop))
 154  
            }
 155  0
            bo.versionNumber = im.versionNumber
 156  0
            return bo
 157  
    }
 158  
    
 159  
    private static void setRuleIdRecursive(String ruleId, PropositionBo prop) {
 160  0
        prop.ruleId = ruleId;
 161  0
        if (prop.compoundComponents != null) for (PropositionBo child : prop.compoundComponents) if (child != null) {
 162  0
            setRuleIdRecursive(ruleId, child);
 163  
        }
 164  
    }
 165  
  
 166  
     /**
 167  
        * This method creates a partially populated Simple PropositionBo with
 168  
        * three parameters:  a term type paramter (value not assigned)
 169  
        *                    a operation parameter
 170  
        *                    a constant parameter (value set to empty string)
 171  
        * The returned PropositionBo has an generatedId. The type code, ruleId and TypeId properties are assigned the
 172  
        * same value as the sibling param passed in.
 173  
        * Each PropositionParameter has the id generated, and type, sequenceNumber,
 174  
        * propId default values set. The value is set to "".
 175  
        * @param sibling -
 176  
        * @param pType
 177  
        * @return  a PropositionBo partially populated.
 178  
        */
 179  
   public static PropositionBo createSimplePropositionBoStub(PropositionBo sibling, String pType){
 180  
       // create a simple proposition Bo
 181  0
       PropositionBo prop = null;
 182  0
       if (PropositionType.SIMPLE.getCode().equalsIgnoreCase(pType)){
 183  0
           prop = new PropositionBo();
 184  0
           prop.setId(getNewPropId());
 185  0
           prop.setPropositionTypeCode(pType);
 186  0
           prop.setRuleId(sibling.getRuleId());
 187  0
           prop.setTypeId(sibling.getTypeId());
 188  0
           prop.setEditMode(true);
 189  
 
 190  
           // create blank proposition parameters
 191  0
           PropositionParameterBo pTerm = new PropositionParameterBo();
 192  0
           pTerm.setId(getNewPropParameterId());
 193  0
           pTerm.setParameterType("T");
 194  0
           pTerm.setPropId(prop.getId());
 195  0
           pTerm.setSequenceNumber(new Integer("0"));
 196  0
           pTerm.setVersionNumber(new Long(1));
 197  0
           pTerm.setValue("");
 198  
 
 199  
           // create blank proposition parameters
 200  0
           PropositionParameterBo pOp = new PropositionParameterBo();
 201  0
           pOp.setId(getNewPropParameterId());
 202  0
           pOp.setParameterType("F");
 203  0
           pOp.setPropId(prop.getId());
 204  0
           pOp.setSequenceNumber(new Integer("2"));
 205  0
           pOp.setVersionNumber(new Long(1));
 206  
 
 207  
           // create blank proposition parameters
 208  0
           PropositionParameterBo pConst = new PropositionParameterBo();
 209  0
           pConst.setId(getNewPropParameterId());
 210  0
           pConst.setParameterType("C");
 211  0
           pConst.setPropId(prop.getId());
 212  0
           pConst.setSequenceNumber(new Integer("1"));
 213  0
           pConst.setVersionNumber(new Long(1));
 214  0
           pConst.setValue("");
 215  
 
 216  0
           List<PropositionParameterBo> paramList = Arrays.asList(pTerm, pConst, pOp);
 217  0
           prop.setParameters(paramList);
 218  
       }
 219  0
       return prop;
 220  
   }
 221  
 
 222  
     public static PropositionBo createCompoundPropositionBoStub(PropositionBo existing){
 223  
         // create a simple proposition Bo
 224  0
         PropositionBo prop = new PropositionBo();
 225  0
         prop.setId(getNewPropId());
 226  0
         prop.setPropositionTypeCode(PropositionType.COMPOUND.code);
 227  0
         prop.setCompoundOpCode(LogicalOperator.AND.code);  // default to and
 228  0
         prop.setDescription("");
 229  0
         prop.setEditMode(true);
 230  0
         if (existing != null){
 231  0
             prop.setRuleId(existing.getRuleId());
 232  0
             prop.setTypeId(existing.getTypeId());
 233  
         }
 234  
 
 235  0
         PropositionBo newProp = createSimplePropositionBoStub(existing, PropositionType.SIMPLE.code)
 236  0
         List <PropositionBo> components = new ArrayList<PropositionBo>(2);
 237  0
         components.add(existing);
 238  0
         components.add(newProp);
 239  0
         prop.setCompoundComponents(components);
 240  0
         return prop;
 241  
     }
 242  
 
 243  
     public static PropositionBo createCompoundPropositionBoStub2(PropositionBo existing){
 244  
         // create a simple proposition Bo
 245  0
         PropositionBo prop = new PropositionBo();
 246  0
         prop.setId(getNewPropId());
 247  0
         prop.setPropositionTypeCode(PropositionType.COMPOUND.code);
 248  0
         prop.setRuleId(existing.getRuleId());
 249  0
         prop.setTypeId(existing.getTypeId());
 250  0
         prop.setCompoundOpCode(LogicalOperator.AND.code);  // default to and
 251  0
         prop.setDescription("");
 252  0
         prop.setEditMode(true);
 253  
 
 254  0
         List <PropositionBo> components = new ArrayList<PropositionBo>();
 255  0
         components.add(existing);
 256  0
         prop.setCompoundComponents(components);
 257  0
         return prop;
 258  
     }
 259  
 
 260  
     private static String getNewPropId(){
 261  0
         SequenceAccessorService sas = KRADServiceLocator.getSequenceAccessorService();
 262  0
         Long id = sas.getNextAvailableSequenceNumber("KRMS_PROP_S",
 263  
                 PropositionBo.class);
 264  0
         return id.toString();
 265  
     }
 266  
     private static String getNewPropParameterId(){
 267  0
         SequenceAccessorService sas = KRADServiceLocator.getSequenceAccessorService();
 268  0
         Long id = sas.getNextAvailableSequenceNumber("KRMS_PROP_PARM_S",
 269  
                 PropositionParameterBo.class);
 270  0
         return id.toString();
 271  
     }
 272  
 }