1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.document;
17
18 import org.apache.commons.collections.CollectionUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.hibernate.annotations.Fetch;
21 import org.hibernate.annotations.FetchMode;
22 import org.hibernate.annotations.GenericGenerator;
23 import org.hibernate.annotations.Parameter;
24 import org.hibernate.annotations.Type;
25 import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
26 import org.kuali.rice.kim.api.KimConstants;
27 import org.kuali.rice.kim.api.identity.employment.EntityEmployment;
28 import org.kuali.rice.kim.api.role.Role;
29 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
30 import org.kuali.rice.kim.api.type.KimType;
31 import org.kuali.rice.kim.bo.ui.KimDocumentRoleMember;
32 import org.kuali.rice.kim.bo.ui.KimDocumentRoleQualifier;
33 import org.kuali.rice.kim.bo.ui.PersonDocumentAddress;
34 import org.kuali.rice.kim.bo.ui.PersonDocumentAffiliation;
35 import org.kuali.rice.kim.bo.ui.PersonDocumentCitizenship;
36 import org.kuali.rice.kim.bo.ui.PersonDocumentEmail;
37 import org.kuali.rice.kim.bo.ui.PersonDocumentEmploymentInfo;
38 import org.kuali.rice.kim.bo.ui.PersonDocumentGroup;
39 import org.kuali.rice.kim.bo.ui.PersonDocumentName;
40 import org.kuali.rice.kim.bo.ui.PersonDocumentPhone;
41 import org.kuali.rice.kim.bo.ui.PersonDocumentPrivacy;
42 import org.kuali.rice.kim.bo.ui.PersonDocumentRole;
43 import org.kuali.rice.kim.bo.ui.RoleDocumentDelegationMember;
44 import org.kuali.rice.kim.bo.ui.RoleDocumentDelegationMemberQualifier;
45 import org.kuali.rice.kim.impl.type.KimTypeAttributesHelper;
46 import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
47 import org.kuali.rice.kim.service.UiDocumentService;
48 import org.kuali.rice.krad.service.DocumentHelperService;
49 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
50 import org.kuali.rice.krad.service.SequenceAccessorService;
51 import org.kuali.rice.krad.util.GlobalVariables;
52
53 import javax.persistence.AssociationOverride;
54 import javax.persistence.AssociationOverrides;
55 import javax.persistence.AttributeOverride;
56 import javax.persistence.AttributeOverrides;
57 import javax.persistence.CascadeType;
58 import javax.persistence.Column;
59 import javax.persistence.Entity;
60 import javax.persistence.FetchType;
61 import javax.persistence.GeneratedValue;
62 import javax.persistence.JoinColumn;
63 import javax.persistence.OneToMany;
64 import javax.persistence.OneToOne;
65 import javax.persistence.Table;
66 import javax.persistence.Transient;
67 import java.util.ArrayList;
68 import java.util.HashMap;
69 import java.util.List;
70 import java.util.Map;
71
72
73
74
75
76
77
78
79 @Entity
80 @AttributeOverrides({
81 @AttributeOverride(name="documentNumber",column=@Column(name="FDOC_NBR"))
82 })
83 @AssociationOverrides({
84 @AssociationOverride(name="documentHeader",joinColumns=@JoinColumn(name="FDOC_NBR",referencedColumnName="DOC_HDR_ID",insertable=false,updatable=false))
85 })
86 @Table(name="KRIM_PERSON_DOCUMENT_T")
87 public class IdentityManagementPersonDocument extends IdentityManagementKimDocument {
88
89 protected static final long serialVersionUID = -534993712085516925L;
90
91
92 @GeneratedValue(generator="KRIM_PRNCPL_ID_S")
93 @GenericGenerator(name="KRIM_PRNCPL_ID_S",strategy="org.kuali.rice.core.jpa.spring.RiceNumericStringSequenceStyleGenerator",parameters={
94 @Parameter(name="sequence_name",value="KRIM_PRNCPL_ID_S"),
95 @Parameter(name="value_column",value="id")
96 })
97 @Column(name="PRNCPL_ID")
98 protected String principalId;
99 @Column(name="PRNCPL_NM")
100 protected String principalName;
101 @GeneratedValue(generator="KRIM_ENTITY_ID_S")
102 @GenericGenerator(name="KRIM_ENTITY_ID_S",strategy="org.kuali.rice.core.jpa.spring.RiceNumericStringSequenceStyleGenerator",parameters={
103 @Parameter(name="sequence_name",value="KRIM_ENTITY_ID_S"),
104 @Parameter(name="value_column",value="id")
105 })
106 @Column(name="ENTITY_ID")
107 protected String entityId;
108 @Type(type="org.kuali.rice.krad.util.HibernateKualiHashType")
109 @Column(name="PRNCPL_PSWD")
110 protected String password;
111
112 protected String univId = "";
113
114 @OneToMany(targetEntity=PersonDocumentAffiliation.class, fetch=FetchType.EAGER, cascade={CascadeType.ALL})
115 @Fetch(value = FetchMode.SELECT)
116 @JoinColumn(name="FDOC_NBR",insertable=false,updatable=false)
117 protected List<PersonDocumentAffiliation> affiliations;
118
119 @Transient
120 protected String campusCode = "";
121
122 @Transient
123 protected Map<String, String> externalIdentifiers = null;
124
125 @Column(name="ACTV_IND")
126 @Type(type="yes_no")
127 protected boolean active;
128
129
130 @Transient
131 protected List<PersonDocumentCitizenship> citizenships;
132
133 @OneToMany(targetEntity=PersonDocumentName.class, fetch=FetchType.EAGER, cascade={CascadeType.ALL})
134 @Fetch(value = FetchMode.SELECT)
135 @JoinColumn(name="FDOC_NBR",insertable=false,updatable=false)
136 protected List<PersonDocumentName> names;
137 @OneToMany(targetEntity=PersonDocumentAddress.class, fetch=FetchType.EAGER, cascade={CascadeType.ALL})
138 @Fetch(value = FetchMode.SELECT)
139 @JoinColumn(name="FDOC_NBR",insertable=false,updatable=false)
140 protected List<PersonDocumentAddress> addrs;
141 @OneToMany(targetEntity=PersonDocumentPhone.class, fetch=FetchType.EAGER, cascade={CascadeType.ALL})
142 @Fetch(value = FetchMode.SELECT)
143 @JoinColumn(name="FDOC_NBR")
144 protected List<PersonDocumentPhone> phones;
145 @OneToMany(targetEntity=PersonDocumentEmail.class, fetch=FetchType.EAGER, cascade={CascadeType.ALL})
146 @Fetch(value = FetchMode.SELECT)
147 @JoinColumn(name="FDOC_NBR",insertable=false,updatable=false)
148 protected List<PersonDocumentEmail> emails;
149 @OneToMany(targetEntity=PersonDocumentGroup.class, fetch=FetchType.EAGER, cascade={CascadeType.ALL})
150 @Fetch(value = FetchMode.SELECT)
151 @JoinColumn(name="FDOC_NBR",insertable=false,updatable=false)
152 protected List<PersonDocumentGroup> groups;
153 @OneToMany(targetEntity=PersonDocumentRole.class, fetch=FetchType.EAGER, cascade={CascadeType.ALL})
154 @Fetch(value = FetchMode.SELECT)
155 @JoinColumn(name="FDOC_NBR",insertable=false,updatable=false)
156 protected List<PersonDocumentRole> roles;
157
158 @OneToOne(targetEntity=PersonDocumentPrivacy.class, fetch=FetchType.EAGER, cascade={CascadeType.ALL})
159 @JoinColumn(name="FDOC_NBR",insertable=false,updatable=false)
160 protected PersonDocumentPrivacy privacy;
161
162 public IdentityManagementPersonDocument() {
163 affiliations = new ArrayList<PersonDocumentAffiliation>();
164 citizenships = new ArrayList<PersonDocumentCitizenship>();
165
166 names = new ArrayList<PersonDocumentName>();
167 addrs = new ArrayList<PersonDocumentAddress>();
168 phones = new ArrayList<PersonDocumentPhone>();
169 emails = new ArrayList<PersonDocumentEmail>();
170 groups = new ArrayList<PersonDocumentGroup>();
171 roles = new ArrayList<PersonDocumentRole>();
172 privacy = new PersonDocumentPrivacy();
173 this.active = true;
174
175 }
176
177 public String getPrincipalId() {
178 return this.principalId;
179 }
180
181 public void setPrincipalId(String principalId) {
182 this.principalId = principalId;
183 }
184
185 public String getPrincipalName() {
186 return this.principalName;
187 }
188
189
190
191
192
193 public void setPrincipalName(String principalName) {
194 this.principalName = principalName;
195 }
196
197 public String getEntityId() {
198 return this.entityId;
199 }
200
201 public void setEntityId(String entityId) {
202 this.entityId = entityId;
203 }
204
205 public List<PersonDocumentAffiliation> getAffiliations() {
206 return this.affiliations;
207 }
208
209 public void setAffiliations(List<PersonDocumentAffiliation> affiliations) {
210 this.affiliations = affiliations;
211 }
212
213 public String getCampusCode() {
214 return this.campusCode;
215 }
216
217 public void setCampusCode(String campusCode) {
218 this.campusCode = campusCode;
219 }
220
221 public Map<String, String> getExternalIdentifiers() {
222 return this.externalIdentifiers;
223 }
224
225 public void setExternalIdentifiers(Map<String, String> externalIdentifiers) {
226 this.externalIdentifiers = externalIdentifiers;
227 }
228
229 public String getPassword() {
230 return this.password;
231 }
232
233 public void setPassword(String password) {
234 this.password = password;
235 }
236
237 public boolean isActive() {
238 return this.active;
239 }
240
241 public void setActive(boolean active) {
242 this.active = active;
243 }
244
245 public List<PersonDocumentCitizenship> getCitizenships() {
246 return this.citizenships;
247 }
248
249 public void setCitizenships(List<PersonDocumentCitizenship> citizenships) {
250 this.citizenships = citizenships;
251 }
252
253 public List<PersonDocumentName> getNames() {
254 return this.names;
255 }
256
257 public void setNames(List<PersonDocumentName> names) {
258 this.names = names;
259 }
260
261 public List<PersonDocumentAddress> getAddrs() {
262 return this.addrs;
263 }
264
265 public void setAddrs(List<PersonDocumentAddress> addrs) {
266 this.addrs = addrs;
267 }
268
269 public List<PersonDocumentPhone> getPhones() {
270 return this.phones;
271 }
272
273 public void setPhones(List<PersonDocumentPhone> phones) {
274 this.phones = phones;
275 }
276
277 public List<PersonDocumentEmail> getEmails() {
278 return this.emails;
279 }
280
281 public void setEmails(List<PersonDocumentEmail> emails) {
282 this.emails = emails;
283 }
284
285 public void setGroups(List<PersonDocumentGroup> groups) {
286 this.groups = groups;
287 }
288
289 public List<PersonDocumentRole> getRoles() {
290 return this.roles;
291 }
292
293 public void setRoles(List<PersonDocumentRole> roles) {
294 this.roles = roles;
295 }
296
297 public List<PersonDocumentGroup> getGroups() {
298 return this.groups;
299 }
300
301 public String getUnivId() {
302 return this.univId;
303 }
304
305 public void setUnivId(String univId) {
306 this.univId = univId;
307 }
308
309 public PersonDocumentPrivacy getPrivacy() {
310 return this.privacy;
311 }
312
313 public void setPrivacy(PersonDocumentPrivacy privacy) {
314 this.privacy = privacy;
315 }
316
317 public void initializeDocumentForNewPerson() {
318 if(StringUtils.isBlank(this.principalId)){
319 this.principalId = getSequenceAccessorService().getNextAvailableSequenceNumber(KimConstants.SequenceNames.KRIM_PRNCPL_ID_S).toString();
320 }
321 if(StringUtils.isBlank(this.entityId)){
322 this.entityId = getSequenceAccessorService().getNextAvailableSequenceNumber(KimConstants.SequenceNames.KRIM_ENTITY_ID_S).toString();
323 }
324 }
325
326 @SuppressWarnings("unchecked")
327 @Override
328 public List buildListOfDeletionAwareLists() {
329 List managedLists = super.buildListOfDeletionAwareLists();
330 List<PersonDocumentEmploymentInfo> empInfos = new ArrayList<PersonDocumentEmploymentInfo>();
331 for (PersonDocumentAffiliation affiliation : getAffiliations()) {
332 empInfos.addAll(affiliation.getEmpInfos());
333 }
334
335 managedLists.add(empInfos);
336 managedLists.add(getAffiliations());
337 managedLists.add(getCitizenships());
338 managedLists.add(getPhones());
339 managedLists.add(getAddrs());
340 managedLists.add(getEmails());
341 managedLists.add(getNames());
342 managedLists.add(getGroups());
343 managedLists.add(getRoles());
344 return managedLists;
345 }
346
347
348
349
350 @Override
351 public void doRouteStatusChange(DocumentRouteStatusChange statusChangeEvent) {
352 super.doRouteStatusChange(statusChangeEvent);
353 if (getDocumentHeader().getWorkflowDocument().isProcessed()) {
354 setIfRolesEditable();
355 KIMServiceLocatorInternal.getUiDocumentService().saveEntityPerson(this);
356 }
357 }
358
359
360 @Override
361 public void prepareForSave(){
362 if (StringUtils.isBlank(getPrivacy().getDocumentNumber())) {
363 getPrivacy().setDocumentNumber(
364 getDocumentNumber());
365 }
366 setEmployeeRecordIds();
367 for (PersonDocumentRole role : getRoles()) {
368 role.setDocumentNumber(getDocumentNumber());
369
370
371
372
373
374
375
376 for (KimDocumentRoleMember rolePrncpl : role.getRolePrncpls()) {
377 rolePrncpl.setDocumentNumber(getDocumentNumber());
378 rolePrncpl.setRoleId(role.getRoleId());
379 if (StringUtils.isEmpty(rolePrncpl.getRoleMemberId())) {
380 SequenceAccessorService sas = getSequenceAccessorService();
381 Long nextSeq = sas.getNextAvailableSequenceNumber(
382 "KRIM_ROLE_MBR_ID_S", this.getClass());
383 String roleMemberId = nextSeq.toString();
384 rolePrncpl.setRoleMemberId(roleMemberId);
385 }
386 for (KimDocumentRoleQualifier qualifier : rolePrncpl.getQualifiers()) {
387 qualifier.setDocumentNumber(getDocumentNumber());
388 qualifier.setKimTypId(role.getKimTypeId());
389 }
390 }
391 }
392 if(getDelegationMembers()!=null){
393 for(RoleDocumentDelegationMember delegationMember: getDelegationMembers()){
394 delegationMember.setDocumentNumber(getDocumentNumber());
395 for (RoleDocumentDelegationMemberQualifier qualifier: delegationMember.getQualifiers()) {
396 qualifier.setDocumentNumber(getDocumentNumber());
397 qualifier.setKimTypId(delegationMember.getRoleBo().getKimTypeId());
398 }
399 addDelegationMemberToDelegation(delegationMember);
400 }
401 }
402 if (getAddrs() != null) {
403 String entityAddressId;
404 for(PersonDocumentAddress address : getAddrs()) {
405 address.setDocumentNumber(getDocumentNumber());
406 if (StringUtils.isEmpty(address.getEntityAddressId())) {
407 SequenceAccessorService sas = getSequenceAccessorService();
408 Long nextSeq = sas.getNextAvailableSequenceNumber(
409 "KRIM_ENTITY_ADDR_ID_S", this.getClass());
410 entityAddressId = nextSeq.toString();
411 address.setEntityAddressId(entityAddressId);
412 }
413 }
414 }
415 if (getAffiliations() != null) {
416 String affiliationId;
417 for(PersonDocumentAffiliation affiliation : getAffiliations()) {
418 affiliation.setDocumentNumber(getDocumentNumber());
419 if (StringUtils.isEmpty(affiliation.getEntityAffiliationId())) {
420 SequenceAccessorService sas = getSequenceAccessorService();
421 Long nextSeq = sas.getNextAvailableSequenceNumber(
422 "KRIM_ENTITY_AFLTN_ID_S", this.getClass());
423 affiliationId = nextSeq.toString();
424 affiliation.setEntityAffiliationId(affiliationId);
425 }
426 }
427 }
428 if (getEmails() != null) {
429 String entityEmailId;
430 for(PersonDocumentEmail email : getEmails()) {
431 email.setDocumentNumber(getDocumentNumber());
432 if (StringUtils.isEmpty(email.getEntityEmailId())) {
433 SequenceAccessorService sas = getSequenceAccessorService();
434 Long nextSeq = sas.getNextAvailableSequenceNumber(
435 "KRIM_ENTITY_EMAIL_ID_S", this.getClass());
436 entityEmailId = nextSeq.toString();
437 email.setEntityEmailId(entityEmailId);
438 }
439 }
440 }
441 if (getGroups() != null) {
442 String groupMemberId;
443 for(PersonDocumentGroup group : getGroups()) {
444 group.setDocumentNumber(getDocumentNumber());
445 if (StringUtils.isEmpty(group.getGroupMemberId())) {
446 SequenceAccessorService sas = getSequenceAccessorService();
447 Long nextSeq = sas.getNextAvailableSequenceNumber(
448 "KRIM_GRP_MBR_ID_S", this.getClass());
449 groupMemberId = nextSeq.toString();
450 group.setGroupMemberId(groupMemberId);
451 }
452 }
453 }
454 if (getNames() != null) {
455 String entityNameId;
456 for(PersonDocumentName name : getNames()) {
457 name.setDocumentNumber(getDocumentNumber());
458 if (StringUtils.isEmpty(name.getEntityNameId())) {
459 SequenceAccessorService sas = getSequenceAccessorService();
460 Long nextSeq = sas.getNextAvailableSequenceNumber(
461 "KRIM_ENTITY_NM_ID_S", this.getClass());
462 entityNameId = nextSeq.toString();
463 name.setEntityNameId(entityNameId);
464 }
465 }
466 }
467 if (getPhones() != null) {
468 String entityPhoneId;
469 for(PersonDocumentPhone phone : getPhones()) {
470 phone.setDocumentNumber(getDocumentNumber());
471 if (StringUtils.isEmpty(phone.getEntityPhoneId())) {
472 SequenceAccessorService sas = getSequenceAccessorService();
473 Long nextSeq = sas.getNextAvailableSequenceNumber(
474 "KRIM_ENTITY_PHONE_ID_S", this.getClass());
475 entityPhoneId = nextSeq.toString();
476 phone.setEntityPhoneId(entityPhoneId);
477 }
478 }
479 }
480
481 }
482
483 protected void setEmployeeRecordIds(){
484 List<EntityEmployment> empInfos = getUiDocumentService().getEntityEmploymentInformationInfo(getEntityId());
485 for(PersonDocumentAffiliation affiliation: getAffiliations()) {
486 int employeeRecordCounter = CollectionUtils.isEmpty(empInfos) ? 0 : empInfos.size();
487 for(PersonDocumentEmploymentInfo empInfo: affiliation.getEmpInfos()){
488 if(CollectionUtils.isNotEmpty(empInfos)){
489 for(EntityEmployment origEmpInfo: empInfos){
490 if (origEmpInfo.getEmployeeId().equals(empInfo.getEntityEmploymentId())) {
491 empInfo.setEmploymentRecordId(origEmpInfo.getEmploymentRecordId());
492 }
493 }
494 }
495 if(StringUtils.isEmpty(empInfo.getEmploymentRecordId())){
496 employeeRecordCounter++;
497 empInfo.setEmploymentRecordId(employeeRecordCounter+"");
498 }
499 }
500 }
501 }
502
503 public KimTypeAttributesHelper getKimTypeAttributesHelper(String roleId) {
504 Role role = KimApiServiceLocator.getRoleService().getRole(roleId);
505 KimType kimTypeInfo = KimApiServiceLocator.getKimTypeInfoService().getKimType(role.getKimTypeId());
506 return new KimTypeAttributesHelper(kimTypeInfo);
507
508 }
509
510 public void setIfRolesEditable(){
511 if(CollectionUtils.isNotEmpty(getRoles())){
512 for(PersonDocumentRole role: getRoles()){
513 role.setEditable(validAssignRole(role));
514 }
515 }
516 }
517
518 public boolean validAssignRole(PersonDocumentRole role){
519 boolean rulePassed = true;
520 if(StringUtils.isNotEmpty(role.getNamespaceCode())){
521 Map<String,String> additionalPermissionDetails = new HashMap<String,String>();
522 additionalPermissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, role.getNamespaceCode());
523 additionalPermissionDetails.put(KimConstants.AttributeConstants.ROLE_NAME, role.getRoleName());
524 if (!getDocumentHelperService().getDocumentAuthorizer(this).isAuthorizedByTemplate(
525 this,
526 KimConstants.NAMESPACE_CODE,
527 KimConstants.PermissionTemplateNames.ASSIGN_ROLE,
528 GlobalVariables.getUserSession().getPrincipalId(),
529 additionalPermissionDetails, null)){
530 rulePassed = false;
531 }
532 }
533 return rulePassed;
534 }
535
536 @Transient
537 protected transient DocumentHelperService documentHelperService;
538 @Transient
539 protected transient UiDocumentService uiDocumentService;
540
541 protected DocumentHelperService getDocumentHelperService() {
542 if ( documentHelperService == null ) {
543 documentHelperService = KRADServiceLocatorWeb.getDocumentHelperService();
544 }
545 return this.documentHelperService;
546 }
547
548 protected UiDocumentService getUiDocumentService() {
549 if (uiDocumentService == null ) {
550 uiDocumentService = KIMServiceLocatorInternal.getUiDocumentService();
551 }
552 return this.uiDocumentService;
553 }
554
555 }