001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kim.impl.identity.personal;
017
018import org.joda.time.DateTime;
019import org.joda.time.Years;
020import org.joda.time.format.DateTimeFormat;
021import org.kuali.rice.kim.api.KimApiConstants;
022import org.kuali.rice.kim.api.identity.personal.EntityBioDemographics;
023import org.kuali.rice.kim.api.identity.personal.EntityBioDemographicsContract;
024import org.kuali.rice.kim.api.identity.privacy.EntityPrivacyPreferences;
025import org.kuali.rice.kim.api.services.KimApiServiceLocator;
026import org.kuali.rice.krad.bo.DataObjectBase;
027
028import javax.persistence.Column;
029import javax.persistence.Entity;
030import javax.persistence.Id;
031import javax.persistence.Table;
032import javax.persistence.Temporal;
033import javax.persistence.TemporalType;
034import javax.persistence.Transient;
035import java.text.SimpleDateFormat;
036import java.util.List;
037
038@Entity
039@Table(name = "KRIM_ENTITY_BIO_T")
040public class EntityBioDemographicsBo extends DataObjectBase implements EntityBioDemographicsContract {
041
042    private static final long serialVersionUID = 1L;
043
044    @Id
045    @Column(name = "ENTITY_ID")
046    private String entityId;
047
048    @Temporal(TemporalType.DATE)
049    @Column(name = "BIRTH_DT")
050    private java.util.Date birthDateValue;
051
052    @Column(name = "GNDR_CD")
053    private String genderCode;
054
055    @Column(name = "GNDR_CHG_CD")
056    private String genderChangeCode;
057
058    @Temporal(TemporalType.DATE)
059    @Column(name = "DECEASED_DT")
060    private java.util.Date deceasedDateValue;
061
062    @Column(name = "MARITAL_STATUS")
063    private String maritalStatusCode;
064
065    @Column(name = "PRIM_LANG_CD")
066    private String primaryLanguageCode;
067
068    @Column(name = "SEC_LANG_CD")
069    private String secondaryLanguageCode;
070
071    @Column(name = "BIRTH_CNTRY_CD")
072    private String birthCountry;
073
074    @Column(name = "BIRTH_STATE_PVC_CD")
075    private String birthStateProvinceCode;
076
077    @Column(name = "BIRTH_CITY")
078    private String birthCity;
079
080    @Column(name = "GEO_ORIGIN")
081    private String geographicOrigin;
082
083    @Column(name = "NOTE_MSG")
084    private String noteMessage;
085
086    @Transient
087    private boolean suppressPersonal;
088
089    public static EntityBioDemographics to(EntityBioDemographicsBo bo) {
090        if (bo == null) {
091            return null;
092        }
093        return EntityBioDemographics.Builder.create(bo).build();
094    }
095
096    /**
097     * Creates a EntityBioDemographicsBo business object from an immutable representation of a EntityBioDemographics.
098     *
099     * @param immutable an immutable EntityBioDemographics
100     * @return a EntityBioDemographicsBo
101     */
102    public static EntityBioDemographicsBo from(EntityBioDemographics immutable) {
103        if (immutable == null) {
104            return null;
105        }
106        EntityBioDemographicsBo bo = new EntityBioDemographicsBo();
107        bo.entityId = immutable.getEntityId();
108        if (immutable.getBirthDateUnmasked() != null) {
109            bo.birthDateValue = DateTimeFormat.forPattern(EntityBioDemographicsContract.BIRTH_DATE_FORMAT).parseDateTime(immutable.getBirthDateUnmasked()).toDate();
110        }
111        bo.birthStateProvinceCode = immutable.getBirthStateProvinceCodeUnmasked();
112        bo.birthCity = immutable.getBirthCityUnmasked();
113        bo.birthCountry = immutable.getBirthCountryUnmasked();
114        if (immutable.getDeceasedDate() != null) {
115            bo.deceasedDateValue = DateTimeFormat.forPattern(EntityBioDemographicsContract.DECEASED_DATE_FORMAT).parseDateTime(immutable.getDeceasedDate()).toDate();
116        }
117        bo.genderCode = immutable.getGenderCodeUnmasked();
118        bo.geographicOrigin = immutable.getGeographicOriginUnmasked();
119        bo.maritalStatusCode = immutable.getMaritalStatusCodeUnmasked();
120        bo.primaryLanguageCode = immutable.getPrimaryLanguageCodeUnmasked();
121        bo.secondaryLanguageCode = immutable.getSecondaryLanguageCodeUnmasked();
122        bo.noteMessage = immutable.getNoteMessage();
123        bo.suppressPersonal = immutable.isSuppressPersonal();
124        bo.setVersionNumber(immutable.getVersionNumber());
125        bo.setObjectId(immutable.getObjectId());
126        return bo;
127    }
128
129    @Override
130    public String getBirthDate() {
131        if (this.birthDateValue != null) {
132            if (isSuppressPersonal()) {
133                return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK;
134            }
135            return new SimpleDateFormat(BIRTH_DATE_FORMAT).format(this.birthDateValue);
136        }
137        return null;
138    }
139
140    @Override
141    public Integer getAge() {
142        if (this.birthDateValue != null && !isSuppressPersonal()) {
143            DateTime endDate;
144            if (this.deceasedDateValue != null) {
145                endDate = new DateTime(this.deceasedDateValue);
146            } else {
147                endDate = new DateTime();
148            }
149            return Years.yearsBetween(new DateTime(this.birthDateValue), endDate).getYears();
150        }
151        return null;
152    }
153
154    @Override
155    public String getDeceasedDate() {
156        if (this.deceasedDateValue != null) {
157            return new SimpleDateFormat(DECEASED_DATE_FORMAT).format(this.deceasedDateValue);
158        }
159        return null;
160    }
161
162    @Override
163    public String getBirthDateUnmasked() {
164        if (this.birthDateValue != null) {
165            return new SimpleDateFormat(BIRTH_DATE_FORMAT).format(this.birthDateValue);
166        }
167        return null;
168    }
169
170    @Override
171    public boolean isSuppressPersonal() {
172        try {
173            EntityPrivacyPreferences privacy = KimApiServiceLocator.getIdentityService().getEntityPrivacyPreferences(getEntityId());
174            if (privacy != null) {
175                this.suppressPersonal = privacy.isSuppressPersonal();
176            } else {
177                this.suppressPersonal = false;
178            }
179        } catch (NullPointerException e) {
180            return false;
181        } catch (ClassCastException c) {
182            return false;
183        }
184        return suppressPersonal;
185    }
186
187    @Override
188    public String getGenderCode() {
189        if (isSuppressPersonal()) {
190            return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK_CODE;
191        }
192        return this.genderCode;
193    }
194
195    @Override
196    public String getGenderChangeCode() {
197        if (isSuppressPersonal()) {
198            return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK_CODE;
199        }
200        return this.genderChangeCode;
201    }
202
203    @Override
204    public String getMaritalStatusCode() {
205        if (isSuppressPersonal()) {
206            return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK_CODE;
207        }
208        return this.maritalStatusCode;
209    }
210
211    @Override
212    public String getPrimaryLanguageCode() {
213        if (isSuppressPersonal()) {
214            return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK_CODE;
215        }
216        return this.primaryLanguageCode;
217    }
218
219    @Override
220    public String getSecondaryLanguageCode() {
221        if (isSuppressPersonal()) {
222            return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK_CODE;
223        }
224        return this.secondaryLanguageCode;
225    }
226
227    @Override
228    public String getBirthCountry() {
229        if (isSuppressPersonal()) {
230            return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK_CODE;
231        }
232        return this.birthCountry;
233    }
234
235    @Override
236    public String getBirthStateProvinceCode() {
237        if (isSuppressPersonal()) {
238            return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK_CODE;
239        }
240        return this.birthStateProvinceCode;
241    }
242
243    @Override
244    public String getBirthCity() {
245        if (isSuppressPersonal()) {
246            return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK_CODE;
247        }
248        return this.birthCity;
249    }
250
251    @Override
252    public String getGeographicOrigin() {
253        if (isSuppressPersonal()) {
254            return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK_CODE;
255        }
256        return this.geographicOrigin;
257    }
258
259    @Override
260    public String getGenderCodeUnmasked() {
261        return this.genderCode;
262    }
263
264    @Override
265    public String getGenderChangeCodeUnmasked() {
266        return this.genderChangeCode;
267    }
268
269    @Override
270    public String getMaritalStatusCodeUnmasked() {
271        return this.maritalStatusCode;
272    }
273
274    @Override
275    public String getPrimaryLanguageCodeUnmasked() {
276        return this.primaryLanguageCode;
277    }
278
279    @Override
280    public String getSecondaryLanguageCodeUnmasked() {
281        return this.secondaryLanguageCode;
282    }
283
284    @Override
285    public String getBirthCountryUnmasked() {
286        return this.birthCountry;
287    }
288
289    @Override
290    public String getBirthStateProvinceCodeUnmasked() {
291        return this.birthStateProvinceCode;
292    }
293
294    @Override
295    public String getBirthCityUnmasked() {
296        return this.birthCity;
297    }
298
299    @Override
300    public String getGeographicOriginUnmasked() {
301        return this.geographicOrigin;
302    }
303
304    @Override
305    public String getEntityId() {
306        return entityId;
307    }
308
309    public void setEntityId(String entityId) {
310        this.entityId = entityId;
311    }
312
313    public java.util.Date getBirthDateValue() {
314        return birthDateValue;
315    }
316
317    public void setBirthDateValue(java.util.Date birthDateValue) {
318        this.birthDateValue = birthDateValue;
319    }
320
321    public void setGenderCode(String genderCode) {
322        this.genderCode = genderCode;
323    }
324
325    public void setGenderChangeCode(String genderChangeCode) {
326        this.genderChangeCode = genderChangeCode;
327    }
328
329    public java.util.Date getDeceasedDateValue() {
330        return deceasedDateValue;
331    }
332
333    public void setDeceasedDateValue(java.util.Date deceasedDateValue) {
334        this.deceasedDateValue = deceasedDateValue;
335    }
336
337    public void setMaritalStatusCode(String maritalStatusCode) {
338        this.maritalStatusCode = maritalStatusCode;
339    }
340
341    public void setPrimaryLanguageCode(String primaryLanguageCode) {
342        this.primaryLanguageCode = primaryLanguageCode;
343    }
344
345    public void setSecondaryLanguageCode(String secondaryLanguageCode) {
346        this.secondaryLanguageCode = secondaryLanguageCode;
347    }
348
349    public void setBirthCountry(String birthCountry) {
350        this.birthCountry = birthCountry;
351    }
352
353    public void setBirthStateProvinceCode(String birthStateProvinceCode) {
354        this.birthStateProvinceCode = birthStateProvinceCode;
355    }
356
357    public void setBirthCity(String birthCity) {
358        this.birthCity = birthCity;
359    }
360
361    public void setGeographicOrigin(String geographicOrigin) {
362        this.geographicOrigin = geographicOrigin;
363    }
364
365    @Override
366    public String getNoteMessage() {
367        return noteMessage;
368    }
369
370    public void setNoteMessage(String noteMessage) {
371        this.noteMessage = noteMessage;
372    }
373
374    public void setSuppressPersonal(boolean suppressPersonal) {
375        this.suppressPersonal = suppressPersonal;
376    }
377}