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 javax.persistence.Column;
019import javax.persistence.Entity;
020import javax.persistence.GeneratedValue;
021import javax.persistence.Id;
022import javax.persistence.Table;
023import javax.persistence.Transient;
024import org.kuali.rice.kim.api.KimApiConstants;
025import org.kuali.rice.kim.api.identity.personal.EntityEthnicity;
026import org.kuali.rice.kim.api.identity.personal.EntityEthnicityContract;
027import org.kuali.rice.kim.api.identity.privacy.EntityPrivacyPreferences;
028import org.kuali.rice.kim.api.services.KimApiServiceLocator;
029import org.kuali.rice.krad.bo.DataObjectBase;
030import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
031
032@Entity
033@Table(name = "KRIM_ENTITY_ETHNIC_T")
034public class EntityEthnicityBo extends DataObjectBase implements EntityEthnicityContract {
035
036    private static final long serialVersionUID = 1L;
037
038    @PortableSequenceGenerator(name = "KRIM_ENTITY_ETHNIC_ID_S")
039    @GeneratedValue(generator = "KRIM_ENTITY_ETHNIC_ID_S")
040    @Id
041    @Column(name = "ID")
042    private String id;
043
044    @Column(name = "ENTITY_ID")
045    private String entityId;
046
047    @Column(name = "ETHNCTY_CD")
048    private String ethnicityCode;
049
050    @Column(name = "SUB_ETHNCTY_CD")
051    private String subEthnicityCode;
052
053    @Transient
054    private boolean suppressPersonal;
055
056    public static EntityEthnicity to(EntityEthnicityBo bo) {
057        if (bo == null) {
058            return null;
059        }
060        return EntityEthnicity.Builder.create(bo).build();
061    }
062
063    /**
064     * Creates a EntityEthnicityBo business object from an immutable representation of a EntityEthnicity.
065     *
066     * @param immutable an immutable EntityEthnicity
067     * @return a EntityEthnicityBo
068     */
069    public static EntityEthnicityBo from(EntityEthnicity immutable) {
070        if (immutable == null) {
071            return null;
072        }
073        EntityEthnicityBo bo = new EntityEthnicityBo();
074        bo.entityId = immutable.getEntityId();
075        bo.id = immutable.getId();
076        bo.ethnicityCode = immutable.getEthnicityCodeUnmasked();
077        bo.subEthnicityCode = immutable.getSubEthnicityCodeUnmasked();
078        bo.setVersionNumber(immutable.getVersionNumber());
079        bo.setObjectId(immutable.getObjectId());
080        return bo;
081    }
082
083    @Override
084    public boolean isSuppressPersonal() {
085        try {
086            EntityPrivacyPreferences privacy = KimApiServiceLocator.getIdentityService().getEntityPrivacyPreferences(getEntityId());
087            if (privacy != null) {
088                this.suppressPersonal = privacy.isSuppressPersonal();
089            } else {
090                this.suppressPersonal = false;
091            }
092        } catch (NullPointerException e) {
093            return false;
094        } catch (ClassCastException c) {
095            return false;
096        }
097        return suppressPersonal;
098    }
099
100    @Override
101    public String getEthnicityCode() {
102        if (isSuppressPersonal()) {
103            return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK;
104        }
105        return this.ethnicityCode;
106    }
107
108    @Override
109    public String getSubEthnicityCode() {
110        if (isSuppressPersonal()) {
111            return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK;
112        }
113        return this.subEthnicityCode;
114    }
115
116    @Override
117    public String getEthnicityCodeUnmasked() {
118        return this.ethnicityCode;
119    }
120
121    @Override
122    public String getSubEthnicityCodeUnmasked() {
123        return this.subEthnicityCode;
124    }
125
126    @Override
127    public String getId() {
128        return id;
129    }
130
131    public void setId(String id) {
132        this.id = id;
133    }
134
135    @Override
136    public String getEntityId() {
137        return entityId;
138    }
139
140    public void setEntityId(String entityId) {
141        this.entityId = entityId;
142    }
143
144    public void setEthnicityCode(String ethnicityCode) {
145        this.ethnicityCode = ethnicityCode;
146    }
147
148    public void setSubEthnicityCode(String subEthnicityCode) {
149        this.subEthnicityCode = subEthnicityCode;
150    }
151
152    public boolean getSuppressPersonal() {
153        return suppressPersonal;
154    }
155
156    public void setSuppressPersonal(boolean suppressPersonal) {
157        this.suppressPersonal = suppressPersonal;
158    }
159}