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.kim.impl.identity.entity;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.persistence.Cacheable;
22  import javax.persistence.CascadeType;
23  import javax.persistence.Column;
24  import javax.persistence.Convert;
25  import javax.persistence.GeneratedValue;
26  import javax.persistence.Id;
27  import javax.persistence.JoinColumn;
28  import javax.persistence.OneToMany;
29  import javax.persistence.OneToOne;
30  import javax.persistence.PrimaryKeyJoinColumn;
31  import javax.persistence.Table;
32  
33  import org.apache.commons.collections.CollectionUtils;
34  import org.apache.commons.lang.StringUtils;
35  import org.eclipse.persistence.annotations.BatchFetch;
36  import org.eclipse.persistence.annotations.JoinFetch;
37  import org.eclipse.persistence.annotations.JoinFetchType;
38  import org.kuali.rice.kim.api.KimConstants;
39  import org.kuali.rice.kim.api.identity.EntityUtils;
40  import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliation;
41  import org.kuali.rice.kim.api.identity.citizenship.EntityCitizenship;
42  import org.kuali.rice.kim.api.identity.employment.EntityEmployment;
43  import org.kuali.rice.kim.api.identity.entity.Entity;
44  import org.kuali.rice.kim.api.identity.entity.EntityContract;
45  import org.kuali.rice.kim.api.identity.entity.EntityDefault;
46  import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifier;
47  import org.kuali.rice.kim.api.identity.name.EntityName;
48  import org.kuali.rice.kim.api.identity.name.EntityNameContract;
49  import org.kuali.rice.kim.api.identity.personal.EntityEthnicity;
50  import org.kuali.rice.kim.api.identity.principal.Principal;
51  import org.kuali.rice.kim.api.identity.residency.EntityResidency;
52  import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfo;
53  import org.kuali.rice.kim.api.identity.visa.EntityVisa;
54  import org.kuali.rice.kim.impl.identity.affiliation.EntityAffiliationBo;
55  import org.kuali.rice.kim.impl.identity.citizenship.EntityCitizenshipBo;
56  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
57  import org.kuali.rice.kim.impl.identity.external.EntityExternalIdentifierBo;
58  import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
59  import org.kuali.rice.kim.impl.identity.personal.EntityBioDemographicsBo;
60  import org.kuali.rice.kim.impl.identity.personal.EntityEthnicityBo;
61  import org.kuali.rice.kim.impl.identity.principal.PrincipalBo;
62  import org.kuali.rice.kim.impl.identity.privacy.EntityPrivacyPreferencesBo;
63  import org.kuali.rice.kim.impl.identity.residency.EntityResidencyBo;
64  import org.kuali.rice.kim.impl.identity.type.EntityTypeContactInfoBo;
65  import org.kuali.rice.kim.impl.identity.visa.EntityVisaBo;
66  import org.kuali.rice.kim.impl.services.KimImplServiceLocator;
67  import org.kuali.rice.krad.bo.DataObjectBase;
68  import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
69  import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
70  import org.kuali.rice.krad.data.platform.MaxValueIncrementerFactory;
71  import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
72  
73  @javax.persistence.Entity
74  @Cacheable(false)
75  @Table(name = "KRIM_ENTITY_T")
76  public class EntityBo extends DataObjectBase implements EntityContract {
77  
78      private static final long serialVersionUID = -2448541334029932773L;
79  
80      @PortableSequenceGenerator(name = "KRIM_ENTITY_ID_S")
81      @GeneratedValue(generator = "KRIM_ENTITY_ID_S")
82      @Id
83      @Column(name = "ENTITY_ID")
84      private String id;
85  
86      @JoinFetch(value= JoinFetchType.OUTER)
87      @OneToMany(targetEntity = EntityNameBo.class, orphanRemoval = true, cascade = CascadeType.ALL)
88      @JoinColumn(name = "ENTITY_ID", referencedColumnName = "ENTITY_ID", insertable = false, updatable = false)
89      private List<EntityNameBo> names = new ArrayList<EntityNameBo>();
90  
91      @JoinFetch(value= JoinFetchType.OUTER)
92      @OneToMany(targetEntity = PrincipalBo.class, orphanRemoval = true, cascade = CascadeType.ALL)
93      @JoinColumn(name = "ENTITY_ID", referencedColumnName = "ENTITY_ID", insertable = false, updatable = false)
94      private List<PrincipalBo> principals = new ArrayList<PrincipalBo>();
95  
96      @JoinFetch(value= JoinFetchType.OUTER)
97      @OneToMany(targetEntity = EntityExternalIdentifierBo.class, orphanRemoval = true, cascade = CascadeType.ALL)
98      @JoinColumn(name = "ENTITY_ID", referencedColumnName = "ENTITY_ID", insertable = false, updatable = false)
99      private List<EntityExternalIdentifierBo> externalIdentifiers = new ArrayList<EntityExternalIdentifierBo>();
100 
101     @JoinFetch(value= JoinFetchType.OUTER)
102     @OneToMany(targetEntity = EntityAffiliationBo.class, orphanRemoval = true, cascade = CascadeType.ALL)
103     @JoinColumn(name = "ENTITY_ID", referencedColumnName = "ENTITY_ID", insertable = false, updatable = false)
104     private List<EntityAffiliationBo> affiliations = new ArrayList<EntityAffiliationBo>();
105 
106     @JoinFetch(value= JoinFetchType.OUTER)
107     @OneToMany(targetEntity = EntityEmploymentBo.class, orphanRemoval = true, cascade = CascadeType.ALL)
108     @JoinColumn(name = "ENTITY_ID", referencedColumnName = "ENTITY_ID", insertable = false, updatable = false)
109     private List<EntityEmploymentBo> employmentInformation = new ArrayList<EntityEmploymentBo>();
110 
111     @JoinFetch(value= JoinFetchType.OUTER)
112     @OneToMany(targetEntity = EntityTypeContactInfoBo.class, orphanRemoval = true, cascade = CascadeType.ALL)
113     @JoinColumn(name = "ENTITY_ID", referencedColumnName = "ENTITY_ID", insertable = false, updatable = false)
114     private List<EntityTypeContactInfoBo> entityTypeContactInfos = new ArrayList<EntityTypeContactInfoBo>();
115 
116     @JoinFetch(value= JoinFetchType.OUTER)
117     @OneToOne(targetEntity = EntityPrivacyPreferencesBo.class, orphanRemoval = true, cascade = CascadeType.ALL)
118     @PrimaryKeyJoinColumn(name = "ENTITY_ID", referencedColumnName = "ENTITY_ID")
119     private EntityPrivacyPreferencesBo privacyPreferences;
120 
121     @JoinFetch(value= JoinFetchType.OUTER)
122     @OneToOne(targetEntity = EntityBioDemographicsBo.class, orphanRemoval = true, cascade = CascadeType.ALL)
123     @PrimaryKeyJoinColumn(name = "ENTITY_ID", referencedColumnName = "ENTITY_ID")
124     private EntityBioDemographicsBo bioDemographics;
125 
126     @JoinFetch(value= JoinFetchType.OUTER)
127     @OneToMany(targetEntity = EntityCitizenshipBo.class, orphanRemoval = true, cascade = CascadeType.ALL)
128     @JoinColumn(name = "ENTITY_ID", referencedColumnName = "ENTITY_ID", insertable = false, updatable = false)
129     private List<EntityCitizenshipBo> citizenships = new ArrayList<EntityCitizenshipBo>();
130 
131     @JoinFetch(value= JoinFetchType.OUTER)
132     @OneToMany(targetEntity = EntityEthnicityBo.class, orphanRemoval = true, cascade = CascadeType.ALL)
133     @JoinColumn(name = "ENTITY_ID", referencedColumnName = "ENTITY_ID", insertable = false, updatable = false)
134     private List<EntityEthnicityBo> ethnicities = new ArrayList<EntityEthnicityBo>();
135 
136     @JoinFetch(value= JoinFetchType.OUTER)
137     @OneToMany(targetEntity = EntityResidencyBo.class, orphanRemoval = true, cascade = CascadeType.ALL)
138     @JoinColumn(name = "ENTITY_ID", referencedColumnName = "ENTITY_ID", insertable = false, updatable = false)
139     private List<EntityResidencyBo> residencies = new ArrayList<EntityResidencyBo>();
140 
141     @JoinFetch(value= JoinFetchType.OUTER)
142     @OneToMany(targetEntity = EntityVisaBo.class, orphanRemoval = true, cascade = CascadeType.ALL)
143     @JoinColumn(name = "ENTITY_ID", referencedColumnName = "ENTITY_ID", insertable = false, updatable = false)
144     private List<EntityVisaBo> visas = new ArrayList<EntityVisaBo>();
145 
146     @Column(name = "ACTV_IND")
147     @Convert(converter = BooleanYNConverter.class)
148     private boolean active;
149 
150     public static org.kuali.rice.kim.api.identity.entity.Entity to(EntityBo bo) {
151         if (bo == null) {
152             return null;
153         }
154         return Entity.Builder.create(bo).build();
155     }
156 
157     public static EntityDefault toDefault(EntityBo bo) {
158         if (bo == null) {
159             return null;
160         }
161         return EntityDefault.Builder.create(bo).build();
162     }
163 
164     public static EntityBo from(org.kuali.rice.kim.api.identity.entity.Entity immutable) {
165         return fromAndUpdate(immutable, null);
166     }
167 
168     /**
169      * Creates a EntityBo business object from an immutable representation of a Entity.
170      *
171      * @param immutable an immutable Entity
172      * @return a EntityBo
173      */
174     public static EntityBo fromAndUpdate(Entity immutable, EntityBo toUpdate) {
175         String entityId;
176 
177         if (immutable == null) {
178             return null;
179         }
180         EntityBo bo = toUpdate;
181         if (toUpdate == null) {
182             bo = new EntityBo();
183         }
184         bo.active = immutable.isActive();
185         bo.id = immutable.getId();
186         if (StringUtils.isBlank(bo.id)) {
187             DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(
188                     KimImplServiceLocator.getDataSource(), KimConstants.SequenceNames.KRIM_ENTITY_ID_S);
189             entityId = incrementer.nextStringValue();
190             bo.id = entityId;
191         } else {
192             entityId = bo.id;
193         }
194 
195         bo.names = new ArrayList<EntityNameBo>();
196         if (CollectionUtils.isNotEmpty(immutable.getNames())) {
197             for (EntityName name : immutable.getNames()) {
198                 bo.names.add(EntityNameBo.from(name));
199             }
200 
201             for (EntityNameBo nameBo : bo.getNames()) {
202                 if (StringUtils.isBlank(nameBo.getEntityId())) {
203                     nameBo.setEntityId(entityId);
204                 }
205             }
206         }
207 
208         bo.principals = new ArrayList<PrincipalBo>();
209         if (CollectionUtils.isNotEmpty(immutable.getPrincipals())) {
210             for (Principal principal : immutable.getPrincipals()) {
211                 bo.principals.add(PrincipalBo.from(principal));
212             }
213 
214             for (PrincipalBo principalBo : bo.getPrincipals()) {
215                 if (StringUtils.isBlank(principalBo.getEntityId())) {
216                     principalBo.setEntityId(entityId);
217                 }
218             }
219         }
220 
221         bo.externalIdentifiers = new ArrayList<EntityExternalIdentifierBo>();
222         if (CollectionUtils.isNotEmpty(immutable.getExternalIdentifiers())) {
223             for (EntityExternalIdentifier externalId : immutable.getExternalIdentifiers()) {
224                 bo.externalIdentifiers.add(EntityExternalIdentifierBo.from(externalId));
225             }
226 
227             for (EntityExternalIdentifierBo EntityExternalIdentifierBo : bo.getExternalIdentifiers()) {
228                 if (StringUtils.isBlank(EntityExternalIdentifierBo.getEntityId())) {
229                     EntityExternalIdentifierBo.setEntityId(entityId);
230                 }
231             }
232         }
233 
234         bo.affiliations = new ArrayList<EntityAffiliationBo>();
235         if (CollectionUtils.isNotEmpty(immutable.getAffiliations())) {
236             for (EntityAffiliation affiliation : immutable.getAffiliations()) {
237                 bo.affiliations.add(EntityAffiliationBo.from(affiliation));
238             }
239 
240             for (EntityAffiliationBo entityAffiliationBo : bo.getAffiliations()) {
241                 if (StringUtils.isBlank(entityAffiliationBo.getEntityId())) {
242                     entityAffiliationBo.setEntityId(entityId);
243                 }
244             }
245         }
246 
247         bo.employmentInformation = new ArrayList<EntityEmploymentBo>();
248         if (CollectionUtils.isNotEmpty(immutable.getEmploymentInformation())) {
249             for (EntityEmployment employment : immutable.getEmploymentInformation()) {
250                 bo.employmentInformation.add(EntityEmploymentBo.from(employment));
251             }
252             for (EntityEmploymentBo employmentBo : bo.getEmploymentInformation()) {
253                 if (StringUtils.isBlank(employmentBo.getEntityId())) {
254                     employmentBo.setEntityId(entityId);
255                 }
256 
257                 EntityAffiliationBo entityAffiliationBo = employmentBo.getEntityAffiliation();
258                 if (entityAffiliationBo != null && StringUtils.isBlank(employmentBo.getEntityAffiliationId())) {
259                     DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(
260                             KimImplServiceLocator.getDataSource(), "KRIM_ENTITY_AFLTN_ID_S");
261                     String affiliationId = incrementer.nextStringValue();
262                     employmentBo.setEntityAffiliationId(affiliationId);
263                     entityAffiliationBo.setId(affiliationId);
264                     if (StringUtils.isBlank(entityAffiliationBo.getEntityId())) {
265                         entityAffiliationBo.setEntityId(entityId);
266                     }
267                 }
268             }
269         }
270 
271         bo.entityTypeContactInfos = new ArrayList<EntityTypeContactInfoBo>();
272         if (CollectionUtils.isNotEmpty(immutable.getEntityTypeContactInfos())) {
273             for (EntityTypeContactInfo entityType : immutable.getEntityTypeContactInfos()) {
274                 bo.entityTypeContactInfos.add(EntityTypeContactInfoBo.from(entityType));
275             }
276         }
277 
278         if (immutable.getPrivacyPreferences() != null) {
279             bo.privacyPreferences = EntityPrivacyPreferencesBo.from(immutable.getPrivacyPreferences());
280         }
281 
282         if (immutable.getBioDemographics() != null) {
283             bo.bioDemographics = EntityBioDemographicsBo.from(immutable.getBioDemographics());
284         }
285 
286         bo.citizenships = new ArrayList<EntityCitizenshipBo>();
287         if (CollectionUtils.isNotEmpty(immutable.getCitizenships())) {
288             for (EntityCitizenship citizenship : immutable.getCitizenships()) {
289                 bo.citizenships.add(EntityCitizenshipBo.from(citizenship));
290             }
291 
292             for (EntityCitizenshipBo citizenshipBo : bo.getCitizenships()) {
293                 if (StringUtils.isBlank(citizenshipBo.getEntityId())) {
294                     citizenshipBo.setEntityId(entityId);
295                 }
296             }
297         }
298 
299         bo.ethnicities = new ArrayList<EntityEthnicityBo>();
300         if (CollectionUtils.isNotEmpty(immutable.getEthnicities())) {
301             for (EntityEthnicity ethnicity : immutable.getEthnicities()) {
302                 bo.ethnicities.add(EntityEthnicityBo.from(ethnicity));
303             }
304         }
305 
306         bo.residencies = new ArrayList<EntityResidencyBo>();
307         if (CollectionUtils.isNotEmpty(immutable.getResidencies())) {
308             for (EntityResidency residency : immutable.getResidencies()) {
309                 bo.residencies.add(EntityResidencyBo.from(residency));
310             }
311 
312             for (EntityResidencyBo entityResidencyBo : bo.getResidencies()) {
313                 if (StringUtils.isBlank(entityResidencyBo.getEntityId())) {
314                     entityResidencyBo.setEntityId(entityId);
315                 }
316             }
317         }
318 
319         bo.visas = new ArrayList<EntityVisaBo>();
320         if (CollectionUtils.isNotEmpty(immutable.getVisas())) {
321             for (EntityVisa visa : immutable.getVisas()) {
322                 bo.visas.add(EntityVisaBo.from(visa));
323             }
324 
325             for (EntityVisaBo entityVisaBo : bo.getVisas()) {
326                 if (StringUtils.isBlank(entityVisaBo.getEntityId())) {
327                     entityVisaBo.setEntityId(entityId);
328                 }
329             }
330         }
331         bo.setVersionNumber(immutable.getVersionNumber());
332         bo.setObjectId(immutable.getObjectId());
333 
334         return bo;
335     }
336 
337     @Override
338     public EntityTypeContactInfoBo getEntityTypeContactInfoByTypeCode(String entityTypeCode) {
339         if (CollectionUtils.isEmpty(this.entityTypeContactInfos)) {
340             return null;
341         }
342         for (EntityTypeContactInfoBo entType : this.entityTypeContactInfos) {
343             if (entType.getEntityTypeCode().equals(entityTypeCode)) {
344                 return entType;
345             }
346         }
347         return null;
348     }
349 
350     @Override
351     public EntityEmploymentBo getPrimaryEmployment() {
352         if (CollectionUtils.isEmpty(this.employmentInformation)) {
353             return null;
354         }
355         for (EntityEmploymentBo employment : this.employmentInformation) {
356             if (employment.isPrimary() && employment.isActive()) {
357                 return employment;
358             }
359         }
360         return null;
361     }
362 
363     @Override
364     public EntityAffiliationBo getDefaultAffiliation() {
365         return EntityUtils.getDefaultItem(this.affiliations);
366     }
367 
368     @Override
369     public EntityExternalIdentifierBo getEntityExternalIdentifier(String externalIdentifierTypeCode) {
370         if (CollectionUtils.isEmpty(this.externalIdentifiers)) {
371             return null;
372         }
373         for (EntityExternalIdentifierBo externalId : this.externalIdentifiers) {
374             if (externalId.getExternalIdentifierTypeCode().equals(externalIdentifierTypeCode)) {
375                 return externalId;
376             }
377         }
378         return null;
379     }
380 
381     @Override
382     public EntityNameContract getDefaultName() {
383         return EntityUtils.getDefaultItem(this.names);
384     }
385 
386     @Override
387     public EntityPrivacyPreferencesBo getPrivacyPreferences() {
388         return this.privacyPreferences;
389     }
390 
391     @Override
392     public EntityBioDemographicsBo getBioDemographics() {
393         return this.bioDemographics;
394     }
395 
396     @Override
397     public String getId() {
398         return id;
399     }
400 
401     public void setId(String id) {
402         this.id = id;
403     }
404 
405     @Override
406     public List<EntityNameBo> getNames() {
407         return names;
408     }
409 
410     public void setNames(List<EntityNameBo> names) {
411         this.names = names;
412     }
413 
414     @Override
415     public List<PrincipalBo> getPrincipals() {
416         return principals;
417     }
418 
419     public void setPrincipals(List<PrincipalBo> principals) {
420         this.principals = principals;
421     }
422 
423     @Override
424     public List<EntityExternalIdentifierBo> getExternalIdentifiers() {
425         return externalIdentifiers;
426     }
427 
428     public void setExternalIdentifiers(List<EntityExternalIdentifierBo> externalIdentifiers) {
429         this.externalIdentifiers = externalIdentifiers;
430     }
431 
432     @Override
433     public List<EntityAffiliationBo> getAffiliations() {
434         return affiliations;
435     }
436 
437     public void setAffiliations(List<EntityAffiliationBo> affiliations) {
438         this.affiliations = affiliations;
439     }
440 
441     @Override
442     public List<EntityEmploymentBo> getEmploymentInformation() {
443         return employmentInformation;
444     }
445 
446     public void setEmploymentInformation(List<EntityEmploymentBo> employmentInformation) {
447         this.employmentInformation = employmentInformation;
448     }
449 
450     @Override
451     public List<EntityTypeContactInfoBo> getEntityTypeContactInfos() {
452         return entityTypeContactInfos;
453     }
454 
455     public void setEntityTypeContactInfos(List<EntityTypeContactInfoBo> entityTypeContactInfos) {
456         this.entityTypeContactInfos = entityTypeContactInfos;
457     }
458 
459     public void setPrivacyPreferences(EntityPrivacyPreferencesBo privacyPreferences) {
460         this.privacyPreferences = privacyPreferences;
461     }
462 
463     public void setBioDemographics(EntityBioDemographicsBo bioDemographics) {
464         this.bioDemographics = bioDemographics;
465     }
466 
467     @Override
468     public List<EntityCitizenshipBo> getCitizenships() {
469         return citizenships;
470     }
471 
472     public void setCitizenships(List<EntityCitizenshipBo> citizenships) {
473         this.citizenships = citizenships;
474     }
475 
476     @Override
477     public List<EntityEthnicityBo> getEthnicities() {
478         return ethnicities;
479     }
480 
481     public void setEthnicities(List<EntityEthnicityBo> ethnicities) {
482         this.ethnicities = ethnicities;
483     }
484 
485     @Override
486     public List<EntityResidencyBo> getResidencies() {
487         return residencies;
488     }
489 
490     public void setResidencies(List<EntityResidencyBo> residencies) {
491         this.residencies = residencies;
492     }
493 
494     @Override
495     public List<EntityVisaBo> getVisas() {
496         return visas;
497     }
498 
499     public void setVisas(List<EntityVisaBo> visas) {
500         this.visas = visas;
501     }
502 
503     public boolean getActive() {
504         return active;
505     }
506 
507     @Override
508     public boolean isActive() {
509         return active;
510     }
511 
512     public void setActive(boolean active) {
513         this.active = active;
514     }
515 }