001    /**
002     * Copyright 2005-2014 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.rule;
017    
018    import org.apache.commons.lang.ObjectUtils;
019    import org.apache.commons.lang.builder.HashCodeBuilder;
020    import org.hibernate.annotations.GenericGenerator;
021    import org.hibernate.annotations.Parameter;
022    import org.kuali.rice.core.api.exception.RiceRuntimeException;
023    import org.kuali.rice.core.api.reflect.ObjectDefinition;
024    import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
025    import org.kuali.rice.kew.actionrequest.ActionRequestValue;
026    import org.kuali.rice.kew.actionrequest.KimGroupRecipient;
027    import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
028    import org.kuali.rice.kew.actionrequest.Recipient;
029    import org.kuali.rice.kew.api.rule.RuleResponsibilityContract;
030    import org.kuali.rice.kew.service.KEWServiceLocator;
031    import org.kuali.rice.kew.api.KewApiConstants;
032    import org.kuali.rice.kew.user.RoleRecipient;
033    import org.kuali.rice.kim.api.group.Group;
034    import org.kuali.rice.kim.api.identity.principal.Principal;
035    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
036    import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
037    
038    import javax.persistence.Column;
039    import javax.persistence.Entity;
040    import javax.persistence.FetchType;
041    import javax.persistence.GeneratedValue;
042    import javax.persistence.Id;
043    import javax.persistence.JoinColumn;
044    import javax.persistence.ManyToOne;
045    import javax.persistence.Table;
046    import java.util.List;
047    
048    
049    /**
050     * A model bean representing the responsibility of a user, workgroup, or role
051     * to perform some action on a document.  Used by the rule system to
052     * identify the appropriate responsibile parties to generate
053     * {@link ActionRequestValue}s to.
054     *
055     * @author Kuali Rice Team (rice.collab@kuali.org)
056     */
057    @Entity
058    @Table(name="KREW_RULE_RSP_T")
059    //@Sequence(name="KREW_RSP_S", property="id")
060    public class RuleResponsibilityBo extends PersistableBusinessObjectBase implements RuleResponsibilityContract {
061    
062            private static final long serialVersionUID = -1565688857123316797L;
063            @Id
064            @GeneratedValue(generator="KREW_RSP_S")
065            @GenericGenerator(name="KREW_RSP_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
066                            @Parameter(name="sequence_name",value="KREW_RSP_S"),
067                            @Parameter(name="value_column",value="id")
068            })
069            @Column(name="RULE_RSP_ID")
070            private String id;
071        @Column(name="RSP_ID")
072            private String responsibilityId;
073        @Column(name="RULE_ID", insertable=false, updatable=false)
074        private String ruleBaseValuesId;
075        @Column(name="ACTN_RQST_CD")
076            private String actionRequestedCd;
077        @Column(name="NM")
078            private String ruleResponsibilityName;
079        @Column(name="TYP")
080            private String ruleResponsibilityType;
081        @Column(name="PRIO")
082            private Integer priority;
083        @Column(name="APPR_PLCY")
084            private String approvePolicy;
085    
086        @ManyToOne(fetch=FetchType.EAGER)
087            @JoinColumn(name="RULE_ID")
088            private RuleBaseValues ruleBaseValues;
089        //@OneToMany(fetch=FetchType.EAGER,cascade={CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE},
090        //        mappedBy="ruleResponsibility")
091        //private List<RuleDelegation> delegationRules = new ArrayList<RuleDelegation>();
092    
093        public Principal getPrincipal()
094        {
095            if (isUsingPrincipal()) {
096                    return KEWServiceLocator.getIdentityHelperService().getPrincipal(ruleResponsibilityName);
097            }
098            return null;
099        }
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    }