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