Coverage Report - org.kuali.rice.kew.rule.bo.RuleTemplateBo
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleTemplateBo
0%
0/112
0%
0/36
1.537
 
 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.kew.rule.bo;
 17  
 
 18  
  import org.apache.commons.lang.ArrayUtils;
 19  
  import org.hibernate.annotations.Fetch;
 20  
  import org.hibernate.annotations.FetchMode;
 21  
  import org.hibernate.annotations.GenericGenerator;
 22  
  import org.hibernate.annotations.Parameter;
 23  
  import org.kuali.rice.kew.api.rule.RoleName;
 24  
  import org.kuali.rice.kew.api.rule.RuleTemplate;
 25  
  import org.kuali.rice.kew.api.rule.RuleTemplateContract;
 26  
  import org.kuali.rice.kew.rule.RoleAttribute;
 27  
  import org.kuali.rice.kew.rule.RuleTemplateOptionBo;
 28  
  import org.kuali.rice.kew.rule.WorkflowRuleAttribute;
 29  
  import org.kuali.rice.kew.api.KewApiConstants;
 30  
  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
 31  
 
 32  
  import javax.persistence.CascadeType;
 33  
  import javax.persistence.Column;
 34  
  import javax.persistence.Entity;
 35  
  import javax.persistence.FetchType;
 36  
  import javax.persistence.GeneratedValue;
 37  
  import javax.persistence.Id;
 38  
  import javax.persistence.JoinColumn;
 39  
  import javax.persistence.NamedQueries;
 40  
  import javax.persistence.NamedQuery;
 41  
  import javax.persistence.OneToMany;
 42  
  import javax.persistence.OneToOne;
 43  
  import javax.persistence.Table;
 44  
  import javax.persistence.Transient;
 45  
  import java.net.URLEncoder;
 46  
  import java.util.ArrayList;
 47  
  import java.util.Collections;
 48  
  import java.util.Iterator;
 49  
  import java.util.List;
 50  
 
 51  
 
 52  
 /**
 53  
  * A model bean which represents a template upon which a rule is created.
 54  
  * The RuleTemplate is essentially a collection of {@link RuleAttribute}s
 55  
  * (associated vai the {@link RuleTemplateAttributeBo} bean).
 56  
  *
 57  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 58  
  */
 59  0
 @Entity
 60  
 @Table(name="KREW_RULE_TMPL_T")
 61  
 //@Sequence(name="KREW_RTE_TMPL_S", property="id")
 62  
 @NamedQueries({@NamedQuery(name="findAllOrderedByName", query="SELECT rt FROM RuleTemplate rt ORDER BY rt.name ASC")})
 63  
 public class RuleTemplateBo extends PersistableBusinessObjectBase implements RuleTemplateContract {
 64  
 
 65  
     private static final long serialVersionUID = -3387940485523951302L;
 66  
 
 67  
     /**
 68  
      * A list of default rule template option keys.
 69  
      */
 70  0
     public static final String[] DEFAULT_OPTION_KEYS = {
 71  
         //KewApiConstants.RULE_INSTRUCTIONS_CD,
 72  
         KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ,
 73  
         KewApiConstants.ACTION_REQUEST_APPROVE_REQ,
 74  
         KewApiConstants.ACTION_REQUEST_COMPLETE_REQ,
 75  
         KewApiConstants.ACTION_REQUEST_FYI_REQ,
 76  
         KewApiConstants.ACTION_REQUEST_DEFAULT_CD
 77  
     };
 78  
     
 79  
     @Id
 80  
     @GeneratedValue(generator="KREW_RTE_TMPL_S")
 81  
         @GenericGenerator(name="KREW_RTE_TMPL_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
 82  
                         @Parameter(name="sequence_name",value="KREW_RTE_TMPL_S"),
 83  
                         @Parameter(name="value_column",value="id")
 84  
         })
 85  
         @Column(name="RULE_TMPL_ID")
 86  
         private String id;
 87  
     @Column(name="NM")
 88  
         private String name;
 89  
     @Column(name="RULE_TMPL_DESC")
 90  
         private String description;
 91  
 
 92  
     @Column(name="DLGN_RULE_TMPL_ID", insertable=false, updatable=false)
 93  
         private String delegationTemplateId;
 94  
     @OneToOne(fetch=FetchType.EAGER)
 95  
         @JoinColumn(name="DLGN_RULE_TMPL_ID")
 96  
         private RuleTemplateBo delegationTemplate;
 97  
     @Fetch(value = FetchMode.SELECT)
 98  
     @OneToMany(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE},
 99  
            mappedBy="ruleTemplate")
 100  
         private List<RuleTemplateAttributeBo> ruleTemplateAttributes;
 101  
     @Fetch(value = FetchMode.SELECT)
 102  
     @OneToMany(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE},
 103  
            mappedBy="ruleTemplate", orphanRemoval=true)
 104  
         private List<RuleTemplateOptionBo> ruleTemplateOptions;
 105  
 
 106  
     // required to be lookupable
 107  
     @Transient
 108  
     private String returnUrl;
 109  
 
 110  0
     public RuleTemplateBo() {
 111  0
         ruleTemplateAttributes = new ArrayList<RuleTemplateAttributeBo>();
 112  0
         ruleTemplateOptions = new ArrayList<RuleTemplateOptionBo>();
 113  0
     }
 114  
     
 115  
  
 116  
     /**
 117  
      * Removes any non-default rule template options on the template
 118  
      */
 119  
     public void removeNonDefaultOptions() {
 120  0
         Iterator<RuleTemplateOptionBo> it = ruleTemplateOptions.iterator();
 121  0
         while (it.hasNext()) {
 122  0
             RuleTemplateOptionBo option = it.next();
 123  
             // if it's not one of the default options, remove it
 124  0
             if (!ArrayUtils.contains(DEFAULT_OPTION_KEYS, option.getCode())) {
 125  0
                 it.remove();
 126  
             }
 127  0
         }
 128  0
     }
 129  
 
 130  
     public String getDelegateTemplateName() {
 131  0
         if (delegationTemplate != null) {
 132  0
             return delegationTemplate.getName();
 133  
         }        
 134  0
         return "";
 135  
     }
 136  
 
 137  
     public String getRuleTemplateActionsUrl() {
 138  0
         return "<a href=\"RuleTemplate.do?methodToCall=report&currentRuleTemplateId=" + id + "\" >report</a>" /*+ "&nbsp;&nbsp;|&nbsp;&nbsp;<a href=\"RuleTemplate.do?methodToCall=edit&ruleTemplate.id=" + id + "\" >edit</a>"*/;
 139  
 //                        "&nbsp;&nbsp;|&nbsp;&nbsp;<a onclick=\"if (confirm('Delete this record?')){ return true; } else {return false;} \" href=\"RuleTemplate.do?methodToCall=delete&ruleTemplate.id=" + id + "&redirectUrl=Lookup.do?methodToCall=search&lookupableImplServiceName=RuleTemplateLookupableImplService\" >delete</a>";
 140  
     }
 141  
 
 142  
 //    public void addRuleTemplateAttribute(RuleTemplateAttribute ruleTemplateAttribute, Integer counter) {
 143  
 //        boolean alreadyAdded = false;
 144  
 //        int location = 0;
 145  
 //        if (counter != null) {
 146  
 //            for (Iterator templateAttributeIter = getRuleTemplateAttributes().iterator(); templateAttributeIter.hasNext();) {
 147  
 //                RuleTemplateAttribute ruleTemplateAtt = (RuleTemplateAttribute) templateAttributeIter.next();
 148  
 //                //                if (ruleTemplateAtt.getId().longValue() == ruleTemplateAttribute.getId().longValue()) {
 149  
 //                if (counter.intValue() == location) {
 150  
 //                    ruleTemplateAtt.setDefaultValue(ruleTemplateAttribute.getDefaultValue());
 151  
 //                    ruleTemplateAtt.setDisplayOrder(ruleTemplateAttribute.getDisplayOrder());
 152  
 //                    ruleTemplateAtt.setLockVerNbr(ruleTemplateAttribute.getLockVerNbr());
 153  
 //                    ruleTemplateAtt.setRequired(ruleTemplateAttribute.getRequired());
 154  
 //                    ruleTemplateAtt.setId(ruleTemplateAttribute.getId());
 155  
 //                    ruleTemplateAtt.setId(ruleTemplateAttribute.getId());
 156  
 //                    alreadyAdded = true;
 157  
 //                }
 158  
 //                location++;
 159  
 //            }
 160  
 //        }
 161  
 //        if (!alreadyAdded) {
 162  
 //            ruleTemplateAttribute.setDisplayOrder(new Integer(getRuleTemplateAttributes().size() + 1));
 163  
 //            getRuleTemplateAttributes().add(ruleTemplateAttribute);
 164  
 //        }
 165  
 //    }
 166  
     
 167  
     /**
 168  
      * Returns the rule template attribute on this instance whose name matches the name of the rule template attribute
 169  
      * passed as a parameter, qualified by it's active state, or null if a match was not found.
 170  
      */
 171  
     private RuleTemplateAttributeBo getRuleTemplateAttribute(RuleTemplateAttributeBo ruleTemplateAttribute, Boolean active) {
 172  0
         for (RuleTemplateAttributeBo currentRuleTemplateAttribute: getRuleTemplateAttributes()) {
 173  0
             if (currentRuleTemplateAttribute.getRuleAttribute().getName().equals(ruleTemplateAttribute.getRuleAttribute().getName())) {
 174  0
                 if (active == null) {
 175  0
                     return currentRuleTemplateAttribute;
 176  
                 }
 177  0
                 else if (active.compareTo(currentRuleTemplateAttribute.getActive()) == 0) {
 178  0
                     return currentRuleTemplateAttribute;
 179  
                 }
 180  
             }
 181  
         }
 182  0
         return null;
 183  
     }
 184  
     
 185  
     public RuleTemplateAttributeBo getRuleTemplateAttribute(RuleTemplateAttributeBo ruleTemplateAttribute) {
 186  0
         return getRuleTemplateAttribute(ruleTemplateAttribute, null);
 187  
     }
 188  
     
 189  
     public boolean containsActiveRuleTemplateAttribute(RuleTemplateAttributeBo templateAttribute) {
 190  0
         return (getRuleTemplateAttribute(templateAttribute, Boolean.TRUE) != null);
 191  
     }
 192  
 
 193  
     public boolean containsRuleTemplateAttribute(RuleTemplateAttributeBo templateAttribute) {
 194  0
         return (getRuleTemplateAttribute(templateAttribute, null) != null);
 195  
     }
 196  
 
 197  
     public RuleTemplateAttributeBo getRuleTemplateAttribute(int index) {
 198  0
         while (getRuleTemplateAttributes().size() <= index) {
 199  0
             getRuleTemplateAttributes().add(new RuleTemplateAttributeBo());
 200  
         }
 201  0
         return (RuleTemplateAttributeBo) getRuleTemplateAttributes().get(index);
 202  
     }
 203  
 
 204  
     public List<RuleTemplateAttributeBo> getRuleTemplateAttributes() {
 205  0
             Collections.sort(ruleTemplateAttributes);
 206  0
         return ruleTemplateAttributes;
 207  
     }
 208  
 
 209  
     /**
 210  
      * Returns a List of only the active RuleTemplateAttributes on the RuleTemplate
 211  
      * sorted according to display order (ascending).
 212  
      * @return
 213  
      */
 214  
     public List<RuleTemplateAttributeBo> getActiveRuleTemplateAttributes() {
 215  0
         List<RuleTemplateAttributeBo> activeAttributes = new ArrayList<RuleTemplateAttributeBo>();
 216  0
         for (RuleTemplateAttributeBo templateAttribute : getRuleTemplateAttributes())
 217  
         {
 218  0
             if (templateAttribute.isActive())
 219  
             {
 220  0
                 activeAttributes.add(templateAttribute);
 221  
             }
 222  
         }
 223  0
         Collections.sort(activeAttributes);
 224  0
         return activeAttributes;
 225  
     }
 226  
     
 227  
     /**
 228  
      * This is implemented to allow us to use this collection on the inquiry for RuleTemplate.  In the
 229  
      * KNS code it does an explicit check that the property is writable.
 230  
      */
 231  
     public void setActiveRuleTemplateAttributes(List<RuleTemplateAttributeBo> ruleTemplateAttributes) {
 232  0
             throw new UnsupportedOperationException("setActiveRuleTemplateAttributes is not implemented");
 233  
     }
 234  
 
 235  
     public void setRuleTemplateAttributes(List<RuleTemplateAttributeBo> ruleTemplateAttributes) {
 236  0
         this.ruleTemplateAttributes = ruleTemplateAttributes;
 237  0
     }
 238  
 
 239  
     public String getDescription() {
 240  0
         return description;
 241  
     }
 242  
 
 243  
     public void setDescription(String description) {
 244  0
         this.description = description;
 245  0
     }
 246  
 
 247  
     public String getName() {
 248  0
         return name;
 249  
     }
 250  
 
 251  
     public void setName(String name) {
 252  0
         this.name = name;
 253  0
     }
 254  
 
 255  
     public String getId() {
 256  0
         return id;
 257  
     }
 258  
 
 259  
     public void setId(String id) {
 260  0
         this.id = id;
 261  0
     }
 262  
 
 263  
     public String getDelegationTemplateId() {
 264  0
         return delegationTemplateId;
 265  
     }
 266  
 
 267  
     public void setDelegationTemplateId(String delegationTemplateId) {
 268  0
         this.delegationTemplateId = delegationTemplateId;
 269  0
     }
 270  
 
 271  
     public RuleTemplateBo getDelegationTemplate() {
 272  0
         return delegationTemplate;
 273  
     }
 274  
 
 275  
     public void setDelegationTemplate(RuleTemplateBo delegationTemplate) {
 276  0
         this.delegationTemplate = delegationTemplate;
 277  0
     }
 278  
 
 279  
     public String getReturnUrl() {
 280  0
         return returnUrl;
 281  
     }
 282  
 
 283  
     public void setReturnUrl(String returnUrl) {
 284  0
         this.returnUrl = returnUrl;
 285  0
     }
 286  
 
 287  
     /**
 288  
      * Used from the rule quicklinks when doing the focus channel.
 289  
      */
 290  
     public String getEncodedName() {
 291  0
         return URLEncoder.encode(getName());
 292  
     }
 293  
 
 294  
     public List<RuleTemplateOptionBo> getRuleTemplateOptions() {
 295  0
         return ruleTemplateOptions;
 296  
     }
 297  
 
 298  
     public void setRuleTemplateOptions(List<RuleTemplateOptionBo> ruleTemplateOptions) {
 299  0
         this.ruleTemplateOptions = ruleTemplateOptions;
 300  0
     }
 301  
 
 302  
     public RuleTemplateOptionBo getRuleTemplateOption(String key) {
 303  0
         for (RuleTemplateOptionBo option: ruleTemplateOptions) {
 304  0
             if (option.getCode().equals(key)) {
 305  0
                 return option;
 306  
             }
 307  
         }
 308  0
         return null;
 309  
     }
 310  
 /*
 311  
     public void setInstructions(RuleTemplateOption instructions) {
 312  
         RuleTemplateOption option = getRuleTemplateOption(KewApiConstants.RULE_INSTRUCTIONS_CD);
 313  
         option.setValue(instructions.getValue());
 314  
         option.setId(instructions.getId());
 315  
         option.setLockVerNbr(instructions.getLockVerNbr());
 316  
     }
 317  
 */
 318  
     public void setAcknowledge(RuleTemplateOptionBo acknowledge) {
 319  0
         RuleTemplateOptionBo option = getRuleTemplateOption(KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ);
 320  0
         option.setValue(acknowledge.getValue());
 321  0
         option.setId(acknowledge.getId());
 322  0
         option.setVersionNumber(acknowledge.getVersionNumber());
 323  0
     }
 324  
 
 325  
     public void setComplete(RuleTemplateOptionBo complete) {
 326  0
         RuleTemplateOptionBo option = getRuleTemplateOption(KewApiConstants.ACTION_REQUEST_COMPLETE_REQ);
 327  0
         option.setValue(complete.getValue());
 328  0
         option.setId(complete.getId());
 329  0
         option.setVersionNumber(complete.getVersionNumber());
 330  0
     }
 331  
 
 332  
     public void setApprove(RuleTemplateOptionBo approve) {
 333  0
         RuleTemplateOptionBo option = getRuleTemplateOption(KewApiConstants.ACTION_REQUEST_APPROVE_REQ);
 334  0
         option.setValue(approve.getValue());
 335  0
         option.setId(approve.getId());
 336  0
         option.setVersionNumber(approve.getVersionNumber());
 337  0
     }
 338  
 
 339  
     public void setFyi(RuleTemplateOptionBo fyi) {
 340  0
         RuleTemplateOptionBo option = getRuleTemplateOption(KewApiConstants.ACTION_REQUEST_FYI_REQ);
 341  0
         option.setValue(fyi.getValue());
 342  0
         option.setId(fyi.getId());
 343  0
         option.setVersionNumber(fyi.getVersionNumber());
 344  0
     }
 345  
 
 346  
     public void setDefaultActionRequestValue(RuleTemplateOptionBo defaultActionRequestValue) {
 347  0
         RuleTemplateOptionBo option = getRuleTemplateOption(KewApiConstants.ACTION_REQUEST_DEFAULT_CD);
 348  0
         option.setValue(defaultActionRequestValue.getValue());
 349  0
         option.setId(defaultActionRequestValue.getId());
 350  0
         option.setVersionNumber(defaultActionRequestValue.getVersionNumber());
 351  0
     }
 352  
 /*
 353  
     public RuleTemplateOption getInstructions() {
 354  
         return getRuleTemplateOption(KewApiConstants.RULE_INSTRUCTIONS_CD);
 355  
     }
 356  
 */
 357  
     public RuleTemplateOptionBo getAcknowledge() {
 358  0
         return getRuleTemplateOption(KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ);
 359  
     }
 360  
 
 361  
     public RuleTemplateOptionBo getComplete() {
 362  0
         return getRuleTemplateOption(KewApiConstants.ACTION_REQUEST_COMPLETE_REQ);
 363  
     }
 364  
 
 365  
     public RuleTemplateOptionBo getApprove() {
 366  0
         return getRuleTemplateOption(KewApiConstants.ACTION_REQUEST_APPROVE_REQ);
 367  
     }
 368  
 
 369  
     public RuleTemplateOptionBo getFyi() {
 370  0
         return getRuleTemplateOption(KewApiConstants.ACTION_REQUEST_FYI_REQ);
 371  
     }
 372  
 
 373  
     public RuleTemplateOptionBo getDefaultActionRequestValue() {
 374  0
         return getRuleTemplateOption(KewApiConstants.ACTION_REQUEST_DEFAULT_CD);
 375  
     }
 376  
     
 377  
     /**
 378  
      * Returns a List of Roles from all RoleAttributes attached to this template.
 379  
      * @return list of roles
 380  
      */
 381  
     public List<RoleName> getRoles() {
 382  0
             List<RoleName> roles = new ArrayList<RoleName>();
 383  0
             List<RuleTemplateAttributeBo> ruleTemplateAttributes = getActiveRuleTemplateAttributes();
 384  0
                 Collections.sort(ruleTemplateAttributes);
 385  0
         for (RuleTemplateAttributeBo ruleTemplateAttribute : ruleTemplateAttributes)
 386  
         {
 387  0
             if (!ruleTemplateAttribute.isWorkflowAttribute())
 388  
             {
 389  0
                 continue;
 390  
             }
 391  0
             WorkflowRuleAttribute workflowAttribute = ruleTemplateAttribute.getWorkflowAttribute();
 392  0
             if (workflowAttribute instanceof RoleAttribute)
 393  
             {
 394  0
                 RoleAttribute roleAttribute = (RoleAttribute) workflowAttribute;
 395  0
                 roles.addAll(roleAttribute.getRoleNames());
 396  
             }
 397  0
         }
 398  0
                 return roles;
 399  
     }
 400  
 
 401  
     public static RuleTemplate to(RuleTemplateBo bo) {
 402  0
         if (bo == null) {
 403  0
             return null;
 404  
         }
 405  0
         return RuleTemplate.Builder.create(bo).build();
 406  
     }
 407  
 }