001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.kew.rule;
017
018import org.apache.commons.lang.StringUtils;
019import org.hibernate.annotations.GenericGenerator;
020import org.hibernate.annotations.Parameter;
021import org.kuali.rice.core.api.delegation.DelegationType;
022import org.kuali.rice.kew.api.rule.RuleDelegationContract;
023import org.kuali.rice.kew.doctype.bo.DocumentType;
024import org.kuali.rice.kew.service.KEWServiceLocator;
025import org.kuali.rice.kim.api.services.KimApiServiceLocator;
026import org.kuali.rice.kim.impl.group.GroupBo;
027import org.kuali.rice.kim.impl.identity.PersonImpl;
028import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
029
030import javax.persistence.CascadeType;
031import javax.persistence.Column;
032import javax.persistence.Entity;
033import javax.persistence.FetchType;
034import javax.persistence.GeneratedValue;
035import javax.persistence.Id;
036import javax.persistence.JoinColumn;
037import javax.persistence.OneToOne;
038import javax.persistence.Table;
039import javax.persistence.Transient;
040
041
042/**
043 * A model bean representing the delegation of a rule from a responsibility to
044 * another rule.  Specifies the delegation type which can be either
045 * {@link {@link DelegationType#PRIMARY} or {@link DelegationType#SECONDARY}.
046 *
047 * @author Kuali Rice Team (rice.collab@kuali.org)
048 */
049@Entity
050@Table(name="KREW_DLGN_RSP_T")
051//@Sequence(name="KREW_RTE_TMPL_S", property="ruleDelegationId")
052public class RuleDelegationBo extends PersistableBusinessObjectBase implements RuleDelegationContract {
053
054        private static final long serialVersionUID = 7989203310473741293L;
055        @Id
056        @GeneratedValue(generator="KREW_RTE_TMPL_S")
057        @GenericGenerator(name="KREW_RTE_TMPL_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
058                        @Parameter(name="sequence_name",value="KREW_RTE_TMPL_S"),
059                        @Parameter(name="value_column",value="id")
060        })
061        @Column(name="DLGN_RULE_ID")
062        private String ruleDelegationId;
063    @Column(name="RSP_ID")
064        private String responsibilityId;
065    @Column(name="DLGN_RULE_BASE_VAL_ID", insertable=false, updatable=false)
066        private String delegateRuleId;
067    @Column(name="DLGN_TYP")
068    private String delegationTypeCode = DelegationType.PRIMARY.getCode();
069
070    @OneToOne(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST})
071        @JoinColumn(name="DLGN_RULE_BASE_VAL_ID")
072        private RuleBaseValues delegationRule;
073//    @ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST})
074//      @JoinColumn(name="RULE_RSP_ID")
075//      private RuleResponsibility ruleResponsibility;
076
077    @Transient
078    private String groupReviewerName;
079    @Transient
080    private String groupReviewerNamespace;
081    @Transient
082    private String personReviewer;
083    @Transient
084    private String personReviewerType;
085
086    public RuleDelegationBo() {
087    }
088
089    public Object copy(boolean preserveKeys) {
090        RuleDelegationBo clone = new RuleDelegationBo();
091        if (ruleDelegationId != null && preserveKeys) {
092            clone.setRuleDelegationId(ruleDelegationId);
093        }
094        clone.setDelegationRule(delegationRule);
095        clone.setDelegateRuleId(delegationRule.getId());
096        if (delegationTypeCode != null) {
097            clone.setDelegationType(DelegationType.fromCode(delegationTypeCode));
098        }
099        return clone;
100    }
101
102    public String getDelegateRuleId() {
103        return delegateRuleId;
104    }
105    public void setDelegateRuleId(String delegateRuleId) {
106        this.delegateRuleId = delegateRuleId;
107    }
108
109    @Override
110    public RuleBaseValues getDelegationRule() {
111        return delegationRule;
112    }
113
114    public RuleBaseValues getDelegationRuleBaseValues() {
115        return delegationRule;
116    }
117
118    public void setDelegationRuleBaseValues(RuleBaseValues delegationRuleBaseValues) {
119        this.delegationRule = delegationRuleBaseValues;
120    }
121
122    public void setDelegationRule(RuleBaseValues delegationRule) {
123        this.delegationRule = delegationRule;
124    }
125
126    /**
127     * Setter for type code preserved for DD
128     * @param delegationTypeCode the DelegationType code
129     */
130    public void setDelegationTypeCode(String delegationTypeCode) {
131        DelegationType.fromCode(delegationTypeCode);
132        this.delegationTypeCode = delegationTypeCode;
133    }
134
135    /**
136     * Getter for type code preserved for DD
137     * @return the DelegationType code
138     */
139    public String getDelegationTypeCode() {
140        return delegationTypeCode;
141    }
142
143    @Override
144    public DelegationType getDelegationType() {
145        return DelegationType.fromCode(delegationTypeCode);
146    }
147    public void setDelegationType(DelegationType delegationType) {
148        this.delegationTypeCode = delegationType.getCode();
149    }
150    public String getRuleDelegationId() {
151        return ruleDelegationId;
152    }
153    public void setRuleDelegationId(String ruleDelegationId) {
154        this.ruleDelegationId = ruleDelegationId;
155    }
156
157    /**
158     * Returns the most recent RuleResponsibility for the responsibility
159     * id on this RuleDelegation.
160     */
161    public RuleResponsibilityBo getRuleResponsibility() {
162        if ( getResponsibilityId() == null ) {
163                return null;
164        }
165        return KEWServiceLocator.getRuleService().findRuleResponsibility(getResponsibilityId());
166    }
167
168    public DocumentType getDocumentType() {
169        return this.getDelegationRule().getDocumentType();
170    }
171
172    public String getResponsibilityId() {
173        return responsibilityId;
174    }
175    public void setResponsibilityId(String ruleResponsibilityId) {
176        this.responsibilityId = ruleResponsibilityId;
177    }
178
179    public String getGroupReviewerName() {
180        return this.groupReviewerName;
181    }
182
183    public String getGroupReviewerNamespace() {
184        return this.groupReviewerNamespace;
185    }
186
187    public String getPersonReviewer() {
188        return this.personReviewer;
189    }
190
191    public void setGroupReviewerName(String groupReviewerName) {
192        this.groupReviewerName = groupReviewerName;
193    }
194
195    public void setGroupReviewerNamespace(String groupReviewerNamespace) {
196        this.groupReviewerNamespace = groupReviewerNamespace;
197    }
198
199    public void setPersonReviewer(String personReviewer) {
200        this.personReviewer = personReviewer;
201    }
202
203    public String getPersonReviewerType() {
204        return this.personReviewerType;
205    }
206
207    public void setPersonReviewerType(String personReviewerType) {
208        this.personReviewerType = personReviewerType;
209    }
210
211    public GroupBo getGroupBo() {
212        GroupBo groupBo = null;
213        if (StringUtils.isNotBlank(getGroupReviewerName())) {
214            if ( groupBo == null ) {
215                groupBo = GroupBo.from(KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(
216                        getGroupReviewerNamespace(), getGroupReviewerName()));
217            }
218        }
219        return groupBo;
220    }
221
222    public PersonImpl getPersonImpl() {
223        return new PersonImpl();
224    }
225
226        /**
227       * An override of the refresh() method that properly preserves the RuleBaseValues instance. If the delegationRuleBaseValues property
228       * becomes null as a result of the refresh() method on the PersistableBusinessObjectBase superclass, an attempt is made to retrieve
229       * it by calling refreshReferenceObject() for the property. If that also fails, then the RuleBaseValues instance that was in-place
230       * prior to the refresh() superclass call will be used as the delegationRuleBaseValues property's value. This override is necessary
231       * in order to prevent certain exceptions during the cancellation of a rule delegation maintenance document.
232       *
233       * @see org.kuali.rice.krad.bo.PersistableBusinessObjectBase#refresh()
234       * @see org.kuali.rice.krad.bo.PersistableBusinessObjectBase#refreshReferenceObject(java.lang.String)
235       */
236        @Override
237        public void refresh() {
238                RuleBaseValues oldRuleBaseValues = this.getDelegationRule();
239                super.refresh();
240                if (this.getDelegationRule() == null) {
241                        this.refreshReferenceObject("delegationRuleBaseValues");
242                        if (this.getDelegationRule() == null) {
243                                this.setDelegationRule(oldRuleBaseValues);
244                        }
245                }
246        }
247
248    public static org.kuali.rice.kew.api.rule.RuleDelegation to(RuleDelegationBo bo) {
249        if (bo == null) {
250            return null;
251        }
252        return org.kuali.rice.kew.api.rule.RuleDelegation.Builder.create(bo).build();
253        /*org.kuali.rice.kew.api.rule.RuleDelegation.Builder builder = org.kuali.rice.kew.api.rule.RuleDelegation.Builder.create();
254        builder.setDelegationType(bo.getDelegationType());
255        builder.setDelegationRule(org.kuali.rice.kew.api.rule.Rule.Builder.create(RuleBaseValues.to(
256                bo.getDelegationRule())));
257        return builder.build();*/
258    }
259}
260