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.membership.MemberType;
20  import org.kuali.rice.kew.api.action.ActionRequestPolicy;
21  import org.kuali.rice.kew.api.peopleflow.PeopleFlowDelegate;
22  import org.kuali.rice.kew.api.peopleflow.PeopleFlowMember;
23  import org.kuali.rice.kew.api.peopleflow.PeopleFlowMemberContract;
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.data.jpa.converters.Boolean01Converter;
31  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
32  import org.kuali.rice.krad.service.ModuleService;
33  
34  import javax.persistence.CascadeType;
35  import javax.persistence.Column;
36  import javax.persistence.Convert;
37  import javax.persistence.Entity;
38  import javax.persistence.GeneratedValue;
39  import javax.persistence.Id;
40  import javax.persistence.JoinColumn;
41  import javax.persistence.ManyToOne;
42  import javax.persistence.OneToMany;
43  import javax.persistence.Table;
44  import javax.persistence.Transient;
45  import javax.persistence.Version;
46  import java.io.Serializable;
47  import java.util.ArrayList;
48  import java.util.List;
49  
50  @Entity
51  @Table(name = "KREW_PPL_FLW_MBR_T")
52  public class PeopleFlowMemberBo implements Serializable, PeopleFlowMemberContract,BusinessObject {
53  
54      @Id
55      @GeneratedValue(generator = "KREW_PPL_FLW_S")
56      @PortableSequenceGenerator(name = "KREW_PPL_FLW_MBR_S")
57      @Column(name = "PPL_FLW_MBR_ID", nullable = false)
58      private String id;
59  
60      @Column(name = "MBR_ID", nullable = false)
61      private String memberId;
62  
63      @Column(name = "MBR_TYP_CD", nullable = false)
64      private String memberTypeCode;
65  
66      @Column(name = "ACTN_RQST_PLCY_CD")
67      private String actionRequestPolicyCode;
68  
69      @Column(name = "RSP_ID", nullable = false)
70      private String responsibilityId;
71  
72      @Column(name = "PRIO")
73      int priority = 1;
74  
75      @Version
76      @Column(name = "VER_NBR", nullable = false)
77      Long versionNumber;
78  
79      @ManyToOne
80      @JoinColumn(name = "PPL_FLW_ID", nullable = false)
81      private PeopleFlowBo peopleFlow;
82  
83      @OneToMany(cascade = CascadeType.ALL, mappedBy = "peopleFlowMember", orphanRemoval = true)
84      List<PeopleFlowDelegateBo> delegates = new ArrayList<PeopleFlowDelegateBo>();
85  
86      @Column(name = "FRC_ACTN", nullable = false)
87      @Convert(converter = Boolean01Converter.class)
88      private Boolean forceAction = true;
89  
90      // non-persisted
91      @Transient
92      private String memberName;
93  
94      @Transient
95      private Person person;
96  
97      @Transient
98      private GroupEbo group;
99  
100     @Transient
101     private RoleEbo role;
102 
103     public String getId() {
104         return id;
105     }
106 
107     public void setId(String id) {
108         this.id = id;
109     }
110 
111     public PeopleFlowBo getPeopleFlow() {
112         return peopleFlow;
113     }
114 
115     public void setPeopleFlow(PeopleFlowBo peopleFlow) {
116         this.peopleFlow = peopleFlow;
117     }
118 
119     public String getMemberId() {
120         return memberId;
121     }
122 
123     public String getMemberTypeCode() {
124         return memberTypeCode;
125     }
126 
127     public void setMemberTypeCode(String memberTypeCode) {
128         this.memberTypeCode = memberTypeCode;
129     }
130 
131     public String getActionRequestPolicyCode() {
132         return actionRequestPolicyCode;
133     }
134 
135     public void setActionRequestPolicyCode(String actionRequestPolicyCode) {
136         this.actionRequestPolicyCode = actionRequestPolicyCode;
137     }
138 
139     public String getResponsibilityId() {
140         return responsibilityId;
141     }
142 
143     public void setResponsibilityId(String responsibilityId) {
144         this.responsibilityId = responsibilityId;
145     }
146 
147     public int getPriority() {
148         return priority;
149     }
150 
151     public void setPriority(int priority) {
152         this.priority = priority;
153     }
154 
155     public Long getVersionNumber() {
156         return versionNumber;
157     }
158 
159     public void setVersionNumber(Long versionNumber) {
160         this.versionNumber = versionNumber;
161     }
162 
163     public String getMemberName() {
164         return memberName;
165     }
166 
167     public void setPerson(Person person) {
168         this.person = person;
169     }
170 
171 
172     public List<PeopleFlowDelegateBo> getDelegates() {
173         return delegates;
174     }
175 
176     public void setDelegates(List<PeopleFlowDelegateBo> delegates) {
177         this.delegates = delegates;
178     }
179 
180     public Boolean getForceAction() {
181         return forceAction;
182     }
183 
184     public void setForceAction(Boolean forceAction) {
185         this.forceAction = forceAction;
186 
187         if (forceAction == null) {
188             forceAction = true;
189         }
190     }
191 
192     public Person getPerson() {
193         if (MemberType.PRINCIPAL.getCode().equals(this.memberTypeCode)) {
194             if ((this.person == null) || !this.person.getPrincipalId().equals(this.memberId) ||
195                     !this.person.getPrincipalName().equals(this.memberName)) {
196 
197                 // use member name first
198                 if (StringUtils.isNotBlank(this.memberName)) {
199                     this.person = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(this.memberName);
200                 } else {
201                     this.person = KimApiServiceLocator.getPersonService().getPerson(this.memberId);
202                 }
203             }
204 
205             if (this.person != null) {
206                 this.memberId = this.person.getPrincipalId();
207                 this.memberName = this.person.getPrincipalName();
208 
209                 return this.person;
210             }
211         }
212 
213 
214 
215         Person newPerson = null;
216 
217         try {
218             newPerson = KimApiServiceLocator.getPersonService().getPersonImplementationClass().newInstance();
219         } catch (InstantiationException ie) {
220             throw new IllegalStateException(ie);
221         } catch (IllegalAccessException iae) {
222             throw new IllegalStateException(iae);
223         }
224         return newPerson;
225     }
226 
227     public GroupEbo getGroup() {
228         if (MemberType.GROUP.getCode().equals(memberTypeCode)) {
229             ModuleService eboModuleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(GroupEbo.class);
230             group = eboModuleService.retrieveExternalizableBusinessObjectIfNecessary(this, group, "group");
231             if (group != null) {
232                 memberId = group.getId();
233                 memberName = group.getNamespaceCode() + " : " + group.getName();
234 
235             }
236         }
237         return group;
238     }
239 
240 
241     public RoleEbo getRole() {
242         if (MemberType.ROLE.getCode().equals(memberTypeCode)) {
243             ModuleService eboModuleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(RoleEbo.class);
244             role = eboModuleService.retrieveExternalizableBusinessObjectIfNecessary(this, role, "role");
245             if (role != null) {
246                 memberId = role.getId();
247                 memberName = role.getNamespaceCode() + " : " + role.getName();
248             }
249         }
250         return role;
251     }
252 
253     public void setMemberName(String memberName) throws InstantiationException, IllegalAccessException {
254         this.memberName = memberName;
255 
256         // trigger update of related object (only person can be updated by name)
257         if (MemberType.PRINCIPAL.getCode().equals(memberTypeCode)) {
258             getPerson();
259         }
260     }
261 
262     public void setMemberId(String memberId) {
263         this.memberId = memberId;
264         updateRelatedObject();
265     }
266 
267     // trigger update of related object
268     public void updateRelatedObject() {
269         if (MemberType.PRINCIPAL.getCode().equals(memberTypeCode)) {
270             getPerson();
271         } else if (MemberType.GROUP.getCode().equals(memberTypeCode)) {
272             getGroup();
273         } else if (MemberType.ROLE.getCode().equals(memberTypeCode)) {
274             getRole();
275         }
276     }
277 
278     @Override
279     public MemberType getMemberType() {
280         return MemberType.fromCode(memberTypeCode);
281     }
282 
283     public void setMemberType(MemberType type) {
284         memberTypeCode = type.getCode();
285     }
286 
287     @Override
288     public ActionRequestPolicy getActionRequestPolicy() {
289         return ActionRequestPolicy.fromCode(actionRequestPolicyCode);
290     }
291 
292     public static PeopleFlowMember to(PeopleFlowMemberBo memberBo) {
293         if (memberBo == null) {
294             return null;
295         }
296         PeopleFlowMember.Builder member = PeopleFlowMember.Builder.create(memberBo);
297         return member.build();
298     }
299 
300     public static PeopleFlowMemberBo from(PeopleFlowMember member, PeopleFlowBo peopleFlow) {
301 
302         if (member == null) {
303             return null;
304         }
305 
306         PeopleFlowMemberBo memberBo = new PeopleFlowMemberBo();
307         memberBo.setPeopleFlow(peopleFlow);
308         memberBo.setMemberId(member.getMemberId());
309         memberBo.setMemberType(member.getMemberType());
310 
311         if (member.getActionRequestPolicy() != null) {
312             memberBo.setActionRequestPolicyCode(member.getActionRequestPolicy().getCode());
313         }
314 
315         memberBo.setResponsibilityId(member.getResponsibilityId());
316         memberBo.setPriority(member.getPriority());
317         memberBo.setDelegates(new ArrayList<PeopleFlowDelegateBo>());
318 
319         for (PeopleFlowDelegate delegate : member.getDelegates()) {
320             memberBo.getDelegates().add(PeopleFlowDelegateBo.from(delegate, memberBo));
321         }
322 
323         memberBo.setForceAction(member.getForceAction());
324 
325         return memberBo;
326     }
327 
328 
329     @Override
330     public void refresh() {
331         //To change body of implemented methods use File | Settings | File Templates.
332     }
333 }