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