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