View Javadoc
1   /**
2    * Copyright 2005-2015 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.impl.peopleflow;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.delegation.DelegationType;
20  import org.kuali.rice.core.api.membership.MemberType;
21  import org.kuali.rice.kew.api.action.ActionRequestPolicy;
22  import org.kuali.rice.kew.api.peopleflow.PeopleFlowDelegate;
23  import org.kuali.rice.kew.api.peopleflow.PeopleFlowDelegateContract;
24  import org.kuali.rice.kim.api.identity.Person;
25  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
26  import org.kuali.rice.kim.framework.group.GroupEbo;
27  import org.kuali.rice.kim.framework.role.RoleEbo;
28  import org.kuali.rice.krad.bo.BusinessObject;
29  import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
30  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
31  import org.kuali.rice.krad.service.ModuleService;
32  
33  import javax.persistence.Column;
34  import javax.persistence.Entity;
35  import javax.persistence.GeneratedValue;
36  import javax.persistence.Id;
37  import javax.persistence.JoinColumn;
38  import javax.persistence.ManyToOne;
39  import javax.persistence.Table;
40  import javax.persistence.Transient;
41  import javax.persistence.Version;
42  import java.io.Serializable;
43  
44  @Entity
45  @Table(name = "KREW_PPL_FLW_DLGT_T")
46  public class PeopleFlowDelegateBo implements Serializable, PeopleFlowDelegateContract, BusinessObject {
47  
48      @Id
49      @GeneratedValue(generator = "KREW_PPL_FLW_DLGT_S")
50      @PortableSequenceGenerator(name = "KREW_PPL_FLW_DLGT_S")
51      @Column(name = "PPL_FLW_DLGT_ID", nullable = false)
52      private String id;
53  
54      @Column(name = "MBR_ID", nullable = false)
55      private String memberId;
56  
57      @Column(name = "MBR_TYP_CD", nullable = false)
58      private String memberTypeCode;
59  
60      @Column(name = "ACTN_RQST_PLCY_CD")
61      private String actionRequestPolicyCode;
62  
63      @Column(name = "DLGN_TYP_CD", nullable = false)
64      private String delegationTypeCode;
65  
66      @Column(name = "RSP_ID", nullable = false)
67      private String responsibilityId;
68  
69      @Version
70      @Column(name = "VER_NBR", nullable = false)
71      private Long versionNumber;
72  
73      @ManyToOne
74      @JoinColumn(name = "PPL_FLW_MBR_ID", nullable = false)
75      private PeopleFlowMemberBo peopleFlowMember;
76  
77      // not-persisted
78      @Transient
79      private String memberName;
80  
81      @Transient
82      private Person person;
83  
84      @Transient
85      private GroupEbo group;
86  
87      @Transient
88      private RoleEbo role;
89  
90      public static PeopleFlowDelegate to(PeopleFlowDelegateBo delegateBo) {
91          if (delegateBo == null) {
92              return null;
93          }
94          PeopleFlowDelegate.Builder builder = PeopleFlowDelegate.Builder.create(delegateBo);
95  
96          return builder.build();
97      }
98  
99      public static PeopleFlowDelegateBo from(PeopleFlowDelegate delegate, PeopleFlowMemberBo peopleFlowMember) {
100 
101         if (delegate == null) {
102             return null;
103         }
104         PeopleFlowDelegateBo delegateBo = new PeopleFlowDelegateBo();
105         delegateBo.setPeopleFlowMember(peopleFlowMember);
106         delegateBo.setMemberId(delegate.getMemberId());
107         delegateBo.setMemberType(delegate.getMemberType());
108         if (delegate.getActionRequestPolicy() != null) {
109             delegateBo.setActionRequestPolicyCode(delegate.getActionRequestPolicy().getCode());
110         }
111         if (delegate.getDelegationType() != null) {
112             delegateBo.setDelegationTypeCode(delegate.getDelegationType().getCode());
113         }
114         delegateBo.setResponsibilityId(delegate.getResponsibilityId());
115         return delegateBo;
116     }
117 
118     public String getId() {
119         return id;
120     }
121 
122     public void setId(String id) {
123         this.id = id;
124     }
125 
126     public String getMemberId() {
127         return memberId;
128     }
129 
130     public String getMemberTypeCode() {
131         return memberTypeCode;
132     }
133 
134     public void setMemberTypeCode(String memberTypeCode) {
135         this.memberTypeCode = memberTypeCode;
136     }
137 
138     public String getActionRequestPolicyCode() {
139         return actionRequestPolicyCode;
140     }
141 
142     public void setActionRequestPolicyCode(String actionRequestPolicyCode) {
143         this.actionRequestPolicyCode = actionRequestPolicyCode;
144     }
145 
146     public String getDelegationTypeCode() {
147         return delegationTypeCode;
148     }
149 
150     public void setDelegationTypeCode(String delegationTypeCode) {
151         this.delegationTypeCode = delegationTypeCode;
152     }
153 
154     public String getResponsibilityId() {
155         return responsibilityId;
156     }
157 
158     public void setResponsibilityId(String responsibilityId) {
159         this.responsibilityId = responsibilityId;
160     }
161 
162     public Long getVersionNumber() {
163         return versionNumber;
164     }
165 
166     public void setVersionNumber(Long versionNumber) {
167         this.versionNumber = versionNumber;
168     }
169 
170     public PeopleFlowMemberBo getPeopleFlowMember() {
171         return peopleFlowMember;
172     }
173 
174     public void setPeopleFlowMember(PeopleFlowMemberBo peopleFlowMember) {
175         this.peopleFlowMember = peopleFlowMember;
176     }
177 
178     public String getMemberName() {
179         return memberName;
180     }
181 
182     public void setPerson(Person person) {
183         this.person = person;
184     }
185 
186     public Person getPerson() {
187         if (MemberType.PRINCIPAL.getCode().equals(memberTypeCode)) {
188             if ((this.person == null) || !person.getPrincipalId().equals(memberId) || !person.getPrincipalName().equals(memberName)) {
189                 // use member name first
190                 if (StringUtils.isNotBlank(memberName)) {
191                     this.person = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(memberName);
192                 } else {
193                     this.person = KimApiServiceLocator.getPersonService().getPerson(memberId);
194                 }
195             }
196 
197             if (this.person != null) {
198                 memberId = person.getPrincipalId();
199                 memberName = person.getPrincipalName();
200 
201                 return this.person;
202             }
203         }
204 
205         Person newPerson = null;
206         // TODO - figure out a better way to do this.
207         try {
208             newPerson = KimApiServiceLocator.getPersonService().getPersonImplementationClass().newInstance();
209         } catch(InstantiationException ie) {
210             throw new IllegalStateException(ie);
211         } catch (IllegalAccessException iae) {
212             throw new IllegalStateException(iae);
213         }
214 
215         return newPerson;
216     }
217 
218     public GroupEbo getGroup() {
219         if (MemberType.GROUP.getCode().equals(memberTypeCode)) {
220             ModuleService eboModuleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(GroupEbo.class);
221             group = eboModuleService.retrieveExternalizableBusinessObjectIfNecessary(this, group, "group");
222              if (group != null) {
223                 memberId = group.getId();
224                 memberName = group.getNamespaceCode() + " : " + group.getName();
225 
226              }
227         }
228         return group;
229     }
230 
231 
232     public RoleEbo getRole() {
233         if (MemberType.ROLE.getCode().equals(memberTypeCode)) {
234             ModuleService eboModuleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(RoleEbo.class);
235             role = eboModuleService.retrieveExternalizableBusinessObjectIfNecessary(this, role, "role");
236             if (role != null) {
237                 memberId = role.getId();
238                 memberName = role.getNamespaceCode() + " : " + role.getName();
239             }
240          }
241          return role;
242     }
243 
244     public void setMemberName(String memberName) throws InstantiationException, IllegalAccessException {
245         this.memberName = memberName;
246 
247         // trigger update of related object (only person can be updated by name)
248         if (MemberType.PRINCIPAL.getCode().equals(memberTypeCode)) {
249             getPerson();
250         }
251     }
252 
253     public void setMemberId(String memberId) {
254         this.memberId = memberId;
255         updateRelatedObject();
256     }
257 
258     // trigger update of related object
259     public void updateRelatedObject() {
260         if (MemberType.PRINCIPAL.getCode().equals(memberTypeCode)) {
261             getPerson();
262         } else if (MemberType.GROUP.getCode().equals(memberTypeCode)) {
263             getGroup();
264         } else if (MemberType.ROLE.getCode().equals(memberTypeCode)) {
265             getRole();
266         }
267     }
268 
269     @Override
270     public MemberType getMemberType() {
271         return MemberType.fromCode(memberTypeCode);
272     }
273 
274     public void setMemberType(MemberType type) {
275         memberTypeCode = type.getCode();
276     }
277 
278     @Override
279     public ActionRequestPolicy getActionRequestPolicy() {
280         return ActionRequestPolicy.fromCode(actionRequestPolicyCode);
281     }
282 
283     @Override
284     public DelegationType getDelegationType() {
285         return DelegationType.fromCode(delegationTypeCode);
286     }
287 
288     public void setDelegationType(DelegationType delegationType) {
289         this.delegationTypeCode = delegationType.getCode();
290     }
291 
292     @Override
293     public void refresh() {
294         //To change body of implemented methods use File | Settings | File Templates.
295     }
296 }