View Javadoc

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