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