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