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.phone;
017
018import javax.persistence.Column;
019import javax.persistence.MappedSuperclass;
020import javax.persistence.Transient;
021
022import org.apache.commons.lang.StringUtils;
023import org.kuali.rice.kim.api.KimApiConstants;
024import org.kuali.rice.kim.api.identity.phone.EntityPhone;
025import org.kuali.rice.kim.api.identity.phone.EntityPhoneContract;
026import org.kuali.rice.kim.api.identity.privacy.EntityPrivacyPreferences;
027import org.kuali.rice.kim.api.services.KimApiServiceLocator;
028import org.kuali.rice.krad.bo.DataObjectBase;
029import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
030
031@MappedSuperclass
032public abstract class EntityPhoneBase extends DataObjectBase implements EntityPhoneContract {
033
034    @Column(name = "ENTITY_ID")
035    private String entityId;
036
037    @Column(name = "ENT_TYP_CD")
038    private String entityTypeCode;
039
040    @Column(name = "PHONE_TYP_CD")
041    private String phoneTypeCode;
042
043    @Column(name = "PHONE_NBR")
044    private String phoneNumber;
045
046    @Column(name = "PHONE_EXTN_NBR")
047    private String extensionNumber;
048
049    @Column(name = "POSTAL_CNTRY_CD")
050    private String countryCode;
051
052    @Transient
053    private boolean suppressPhone;
054
055    @javax.persistence.Convert(converter=BooleanYNConverter.class)
056    @Column(name = "ACTV_IND")
057    private boolean active;
058
059    @javax.persistence.Convert(converter=BooleanYNConverter.class)
060    @Column(name = "DFLT_IND")
061    private boolean defaultValue;
062
063    public String getPhoneTypeCode() {
064        return this.phoneTypeCode;
065    }
066
067    public static EntityPhone to(EntityPhoneBase bo) {
068        if (bo == null) {
069            return null;
070        }
071
072        return EntityPhone.Builder.create(bo).build();
073    }
074
075    @Override
076    public boolean isSuppressPhone() {
077        try {
078            EntityPrivacyPreferences privacy = KimApiServiceLocator.getIdentityService().getEntityPrivacyPreferences(
079                    getEntityId());
080            if (privacy != null) {
081                this.suppressPhone = privacy.isSuppressPhone();
082            } else {
083                this.suppressPhone = false;
084            }
085
086        } catch (NullPointerException e) {
087            return false;
088        } catch (ClassCastException c) {
089            return false;
090        }
091        return this.suppressPhone;
092    }
093
094    @Override
095    public String getFormattedPhoneNumber() {
096        if (isSuppressPhone()) {
097            return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK;
098        }
099
100        return getFormattedPhoneNumberUnmasked();
101    }
102
103    @Override
104    public String getPhoneNumberUnmasked() {
105        return this.phoneNumber;
106    }
107
108    @Override
109    public String getExtensionNumberUnmasked() {
110        return this.extensionNumber;
111    }
112
113    @Override
114    public String getCountryCodeUnmasked() {
115        return this.countryCode;
116    }
117
118    @Override
119    public String getFormattedPhoneNumberUnmasked() {
120        StringBuffer sb = new StringBuffer(30);
121
122        // TODO: get extension from country code table
123        // TODO: append "+xxx" if country is not the default country
124        sb.append(this.phoneNumber);
125        if (StringUtils.isNotBlank(this.extensionNumber)) {
126            sb.append(" x");
127            sb.append(this.extensionNumber);
128        }
129
130        return sb.toString();
131    }
132
133    @Override
134    public String getPhoneNumber() {
135        if (isSuppressPhone()) {
136            return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK_PHONE;
137        }
138
139        return this.phoneNumber;
140    }
141
142    @Override
143    public String getCountryCode() {
144        if (isSuppressPhone()) {
145            return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK_CODE;
146        }
147
148        return this.countryCode;
149    }
150
151    @Override
152    public String getExtensionNumber() {
153        if (isSuppressPhone()) {
154            return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK;
155        }
156
157        return this.extensionNumber;
158    }
159
160    @Override
161    public String getEntityId() {
162        return entityId;
163    }
164
165    public void setEntityId(String entityId) {
166        this.entityId = entityId;
167    }
168
169    @Override
170    public String getEntityTypeCode() {
171        return entityTypeCode;
172    }
173
174    public void setEntityTypeCode(String entityTypeCode) {
175        this.entityTypeCode = entityTypeCode;
176    }
177
178    public void setPhoneTypeCode(String phoneTypeCode) {
179        this.phoneTypeCode = phoneTypeCode;
180    }
181
182    public void setPhoneNumber(String phoneNumber) {
183        this.phoneNumber = phoneNumber;
184    }
185
186    public void setExtensionNumber(String extensionNumber) {
187        this.extensionNumber = extensionNumber;
188    }
189
190    public void setCountryCode(String countryCode) {
191        this.countryCode = countryCode;
192    }
193
194    public boolean getSuppressPhone() {
195        return suppressPhone;
196    }
197
198    public void setSuppressPhone(boolean suppressPhone) {
199        this.suppressPhone = suppressPhone;
200    }
201
202    public boolean getActive() {
203        return active;
204    }
205
206    @Override
207    public boolean isActive() {
208        return active;
209    }
210
211    public void setActive(boolean active) {
212        this.active = active;
213    }
214
215    public boolean getDefaultValue() {
216        return defaultValue;
217    }
218
219    @Override
220    public boolean isDefaultValue() {
221        return defaultValue;
222    }
223
224    public void setDefaultValue(boolean defaultValue) {
225        this.defaultValue = defaultValue;
226    }
227
228    private static final long serialVersionUID = 1L;
229
230}