View Javadoc

1   /**
2    * Copyright 2005-2012 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;
17  
18  import org.apache.commons.lang.ObjectUtils;
19  import org.apache.commons.lang.builder.HashCodeBuilder;
20  import org.hibernate.annotations.GenericGenerator;
21  import org.hibernate.annotations.Parameter;
22  import org.kuali.rice.core.api.exception.RiceRuntimeException;
23  import org.kuali.rice.core.api.reflect.ObjectDefinition;
24  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
25  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
26  import org.kuali.rice.kew.actionrequest.KimGroupRecipient;
27  import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
28  import org.kuali.rice.kew.actionrequest.Recipient;
29  import org.kuali.rice.kew.api.rule.RuleResponsibilityContract;
30  import org.kuali.rice.kew.service.KEWServiceLocator;
31  import org.kuali.rice.kew.api.KewApiConstants;
32  import org.kuali.rice.kew.user.RoleRecipient;
33  import org.kuali.rice.kim.api.group.Group;
34  import org.kuali.rice.kim.api.identity.principal.Principal;
35  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
36  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
37  
38  import javax.persistence.Column;
39  import javax.persistence.Entity;
40  import javax.persistence.FetchType;
41  import javax.persistence.GeneratedValue;
42  import javax.persistence.Id;
43  import javax.persistence.JoinColumn;
44  import javax.persistence.ManyToOne;
45  import javax.persistence.Table;
46  import java.util.List;
47  
48  
49  /**
50   * A model bean representing the responsibility of a user, workgroup, or role
51   * to perform some action on a document.  Used by the rule system to
52   * identify the appropriate responsibile parties to generate
53   * {@link ActionRequestValue}s to.
54   *
55   * @author Kuali Rice Team (rice.collab@kuali.org)
56   */
57  @Entity
58  @Table(name="KREW_RULE_RSP_T")
59  //@Sequence(name="KREW_RSP_S", property="id")
60  public class RuleResponsibilityBo extends PersistableBusinessObjectBase implements RuleResponsibilityContract {
61  
62  	private static final long serialVersionUID = -1565688857123316797L;
63  	@Id
64  	@GeneratedValue(generator="KREW_RSP_S")
65  	@GenericGenerator(name="KREW_RSP_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
66  			@Parameter(name="sequence_name",value="KREW_RSP_S"),
67  			@Parameter(name="value_column",value="id")
68  	})
69  	@Column(name="RULE_RSP_ID")
70  	private String id;
71      @Column(name="RSP_ID")
72  	private String responsibilityId;
73      @Column(name="RULE_ID", insertable=false, updatable=false)
74      private String ruleBaseValuesId;
75      @Column(name="ACTN_RQST_CD")
76  	private String actionRequestedCd;
77      @Column(name="NM")
78  	private String ruleResponsibilityName;
79      @Column(name="TYP")
80  	private String ruleResponsibilityType;
81      @Column(name="PRIO")
82  	private Integer priority;
83      @Column(name="APPR_PLCY")
84  	private String approvePolicy;
85  
86      @ManyToOne(fetch=FetchType.EAGER)
87  	@JoinColumn(name="RULE_ID")
88  	private RuleBaseValues ruleBaseValues;
89      //@OneToMany(fetch=FetchType.EAGER,cascade={CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE},
90      //        mappedBy="ruleResponsibility")
91      //private List<RuleDelegation> delegationRules = new ArrayList<RuleDelegation>();
92  
93      public Principal getPrincipal()
94      {
95      	if (isUsingPrincipal()) {
96      		return KEWServiceLocator.getIdentityHelperService().getPrincipal(ruleResponsibilityName);
97      	}
98      	return null;
99      }
100 
101     public Group getGroup() {
102         if (isUsingGroup()) {
103         	return KimApiServiceLocator.getGroupService().getGroup(ruleResponsibilityName);
104         }
105         return null;
106     }
107 
108     public String getRole() {
109         if (isUsingRole()) {
110             return ruleResponsibilityName;
111         }
112         return null;
113     }
114 
115     public String getResolvedRoleName() {
116         if (isUsingRole()) {
117             return getRole().substring(getRole().indexOf("!") + 1, getRole().length());
118         }
119         return null;
120     }
121 
122     public String getRoleAttributeName() {
123 	    return getRole().substring(0, getRole().indexOf("!"));
124     }
125     
126     public RoleAttribute resolveRoleAttribute() {
127         if (isUsingRole()) {
128             String attributeName = getRoleAttributeName();
129             return (RoleAttribute) GlobalResourceLoader.getResourceLoader().getObject(new ObjectDefinition(attributeName));
130         }
131         return null;
132     }
133 
134     @Override
135     public boolean isUsingRole() {
136     	return (ruleResponsibilityName != null && ruleResponsibilityType != null && ruleResponsibilityType.equals(KewApiConstants.RULE_RESPONSIBILITY_ROLE_ID));
137     }
138 
139     @Override
140     public boolean isUsingPrincipal() {
141     	return (ruleResponsibilityName != null && !ruleResponsibilityName.trim().equals("") && ruleResponsibilityType != null && ruleResponsibilityType.equals(KewApiConstants.RULE_RESPONSIBILITY_WORKFLOW_ID));
142     }
143 
144     @Override
145     public boolean isUsingGroup() {
146     	return (ruleResponsibilityName != null && !ruleResponsibilityName.trim().equals("") && ruleResponsibilityType != null && ruleResponsibilityType.equals(KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID));
147     }
148 
149     public String getRuleBaseValuesId() {
150         return ruleBaseValuesId;
151     }
152 
153     public void setRuleBaseValuesId(String ruleBaseValuesId) {
154         this.ruleBaseValuesId = ruleBaseValuesId;
155     }
156 
157     public RuleBaseValues getRuleBaseValues() {
158         return ruleBaseValues;
159     }
160 
161     public void setRuleBaseValues(RuleBaseValues ruleBaseValues) {
162         this.ruleBaseValues = ruleBaseValues;
163     }
164 
165     public String getActionRequestedCd() {
166         return actionRequestedCd;
167     }
168 
169     public void setActionRequestedCd(String actionRequestedCd) {
170         this.actionRequestedCd = actionRequestedCd;
171     }
172 
173     public String getId() {
174         return id;
175     }
176 
177     public void setId(String ruleResponsibilityId) {
178         this.id = ruleResponsibilityId;
179     }
180     public Integer getPriority() {
181         return priority;
182     }
183 
184     public void setPriority(Integer priority) {
185         this.priority = priority;
186     }
187 
188     public String getApprovePolicy() {
189         return approvePolicy;
190     }
191 
192     public void setApprovePolicy(String approvePolicy) {
193         this.approvePolicy = approvePolicy;
194     }
195 
196     public Object copy(boolean preserveKeys) {
197         RuleResponsibilityBo ruleResponsibilityClone = new RuleResponsibilityBo();
198         ruleResponsibilityClone.setApprovePolicy(getApprovePolicy());
199         if (actionRequestedCd != null) {
200             ruleResponsibilityClone.setActionRequestedCd(actionRequestedCd);
201         }
202         if (id != null && preserveKeys) {
203             ruleResponsibilityClone.setId(id);
204         }
205 
206         if (responsibilityId != null) {
207             ruleResponsibilityClone.setResponsibilityId(responsibilityId);
208         }
209 
210         if (ruleResponsibilityName != null) {
211             ruleResponsibilityClone.setRuleResponsibilityName(ruleResponsibilityName);
212         }
213         if (ruleResponsibilityType != null) {
214             ruleResponsibilityClone.setRuleResponsibilityType(ruleResponsibilityType);
215         }
216         if (priority != null) {
217             ruleResponsibilityClone.setPriority(priority);
218         }
219 //        if (delegationRules != null) {
220 //            for (Iterator iter = delegationRules.iterator(); iter.hasNext();) {
221 //                RuleDelegation delegation = (RuleDelegation) iter.next();
222 //                RuleDelegation delegationClone = (RuleDelegation)delegation.copy(preserveKeys);
223 //                delegationClone.setRuleResponsibility(ruleResponsibilityClone);
224 //                ruleResponsibilityClone.getDelegationRules().add(delegationClone);
225 //
226 //            }
227 //        }
228         return ruleResponsibilityClone;
229     }
230 
231     public String getRuleResponsibilityName() {
232         return ruleResponsibilityName;
233     }
234 
235     public void setRuleResponsibilityName(String ruleResponsibilityName) {
236         this.ruleResponsibilityName = ruleResponsibilityName;
237     }
238 
239     public String getRuleResponsibilityType() {
240         return ruleResponsibilityType;
241     }
242 
243     public void setRuleResponsibilityType(String ruleResponsibilityType) {
244         this.ruleResponsibilityType = ruleResponsibilityType;
245     }
246 
247     public String getResponsibilityId() {
248         return responsibilityId;
249     }
250     public void setResponsibilityId(String responsibilityId) {
251         this.responsibilityId = responsibilityId;
252     }
253     
254     public List<RuleDelegationBo> getDelegationRules() {
255     	return KEWServiceLocator.getRuleDelegationService().findByResponsibilityId(getResponsibilityId());
256     }
257     
258     public RuleDelegationBo getDelegationRule(int index) {
259     	return getDelegationRules().get(index);
260     }
261     
262 //    public boolean isDelegating() {
263 //        return !getDelegationRules().isEmpty();
264 //    }
265 //
266 //    public List getDelegationRules() {
267 //        return delegationRules;
268 //    }
269 //    public void setDelegationRules(List delegationRules) {
270 //        this.delegationRules = delegationRules;
271 //    }
272 //
273 //    public RuleDelegation getDelegationRule(int index) {
274 //        while (getDelegationRules().size() <= index) {
275 //            RuleDelegation ruleDelegation = new RuleDelegation();
276 //            ruleDelegation.setRuleResponsibility(this);
277 //            ruleDelegation.setDelegationRuleBaseValues(new RuleBaseValues());
278 //            getDelegationRules().add(ruleDelegation);
279 //        }
280 //        return (RuleDelegation) getDelegationRules().get(index);
281 //    }
282     
283     // convenience methods for the web-tier
284     
285     public String getActionRequestedDisplayValue() {
286     	return KewApiConstants.ACTION_REQUEST_CODES.get(getActionRequestedCd());
287     }
288     
289     public String getRuleResponsibilityTypeDisplayValue() {
290     	return KewApiConstants.RULE_RESPONSIBILITY_TYPES.get(getRuleResponsibilityType());
291     }
292     
293     public boolean equals(Object o) {
294         if (o == null) return false;
295         if (!(o instanceof RuleResponsibilityBo)) return false;
296         RuleResponsibilityBo pred = (RuleResponsibilityBo) o;
297         return ObjectUtils.equals(ruleResponsibilityName, pred.getRuleResponsibilityName()) &&
298                ObjectUtils.equals(actionRequestedCd, pred.getActionRequestedCd()) &&
299                ObjectUtils.equals(priority, pred.getPriority()) &&
300                ObjectUtils.equals(approvePolicy, pred.getApprovePolicy());
301     }
302     
303     /**
304 	 * @see java.lang.Object#hashCode()
305 	 */
306 	@Override
307 	public int hashCode() {
308 		return new HashCodeBuilder()
309 		.append(this.actionRequestedCd)
310 		.append(this.approvePolicy)
311 		.append(this.priority)
312 		.append(this.ruleResponsibilityName).toHashCode();
313 	}
314 
315     @Override
316     public String getGroupId() {
317         if (!isUsingGroup()) {
318             return null;
319         }
320         return getGroup().getId();
321     }
322 
323     @Override
324     public String getPrincipalId() {
325         if (getPrincipal() == null) {
326             return null;
327         }
328         return getPrincipal().getPrincipalId();
329     }
330 
331     @Override
332     public String getRoleName() {
333         return getRole();
334     }
335 
336     /**
337      * Convenience method to return the Recipient for this RuleResponsibility
338      * @return the Recipient for this RuleResponsibility
339      */
340     public Recipient getRecipient() {
341         if (isUsingPrincipal()) {
342             return new KimPrincipalRecipient(getPrincipal());
343         } else if (isUsingGroup()) {
344             return new KimGroupRecipient(getGroup());
345         } else if (isUsingRole()) {
346             return new RoleRecipient(getRole());
347         } else {
348             return null;
349         }
350     }
351 
352     public static org.kuali.rice.kew.api.rule.RuleResponsibility to(RuleResponsibilityBo bo) {
353         if (bo == null) {
354             return null;
355         }
356         return org.kuali.rice.kew.api.rule.RuleResponsibility.Builder.create(bo).build();
357         /*org.kuali.rice.kew.api.rule.RuleResponsibility.Builder builder = org.kuali.rice.kew.api.rule.RuleResponsibility.Builder.create();
358         builder.setPriority(bo.getPriority());
359         builder.setResponsibilityId(bo.getResponsibilityId());
360         builder.setActionRequestedCd(bo.getActionRequestedCd());
361         builder.setApprovePolicy(bo.getApprovePolicy());
362         builder.setPrincipalId(bo.getPrincipal() == null ? null : bo.getPrincipal().getPrincipalId());
363         builder.setGroupId(bo.getGroup() == null ? null : bo.getGroup().getId());
364         builder.setRoleName(bo.getResolvedRoleName());
365         if (CollectionUtils.isNotEmpty(bo.getDelegationRules())) {
366             List<org.kuali.rice.kew.api.rule.RuleDelegation.Builder> delegationRuleBuilders =
367                     new ArrayList<org.kuali.rice.kew.api.rule.RuleDelegation.Builder>();
368             for (RuleDelegation delegationRule : bo.getDelegationRules()) {
369                 delegationRuleBuilders.add(
370                         org.kuali.rice.kew.api.rule.RuleDelegation.Builder.create(RuleDelegation.to(delegationRule)));
371             }
372             builder.setDelegationRules(delegationRuleBuilders);
373         } else {
374             builder.setDelegationRules(Collections.<org.kuali.rice.kew.api.rule.RuleDelegation.Builder>emptyList());
375         }
376         return builder.build();*/
377     }
378 }