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.phone;
17  
18  import javax.persistence.Column;
19  import javax.persistence.MappedSuperclass;
20  import javax.persistence.Transient;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.kuali.rice.kim.api.KimApiConstants;
24  import org.kuali.rice.kim.api.identity.phone.EntityPhone;
25  import org.kuali.rice.kim.api.identity.phone.EntityPhoneContract;
26  import org.kuali.rice.kim.api.identity.privacy.EntityPrivacyPreferences;
27  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
28  import org.kuali.rice.krad.bo.DataObjectBase;
29  import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
30  
31  @MappedSuperclass
32  public abstract class EntityPhoneBase extends DataObjectBase implements EntityPhoneContract {
33  
34      @Column(name = "ENTITY_ID")
35      private String entityId;
36  
37      @Column(name = "ENT_TYP_CD")
38      private String entityTypeCode;
39  
40      @Column(name = "PHONE_TYP_CD")
41      private String phoneTypeCode;
42  
43      @Column(name = "PHONE_NBR")
44      private String phoneNumber;
45  
46      @Column(name = "PHONE_EXTN_NBR")
47      private String extensionNumber;
48  
49      @Column(name = "POSTAL_CNTRY_CD")
50      private String countryCode;
51  
52      @Transient
53      private boolean suppressPhone;
54  
55      @javax.persistence.Convert(converter=BooleanYNConverter.class)
56      @Column(name = "ACTV_IND")
57      private boolean active;
58  
59      @javax.persistence.Convert(converter=BooleanYNConverter.class)
60      @Column(name = "DFLT_IND")
61      private boolean defaultValue;
62  
63      public String getPhoneTypeCode() {
64          return this.phoneTypeCode;
65      }
66  
67      public static EntityPhone to(EntityPhoneBase bo) {
68          if (bo == null) {
69              return null;
70          }
71  
72          return EntityPhone.Builder.create(bo).build();
73      }
74  
75      @Override
76      public boolean isSuppressPhone() {
77          try {
78              EntityPrivacyPreferences privacy = KimApiServiceLocator.getIdentityService().getEntityPrivacyPreferences(
79                      getEntityId());
80              if (privacy != null) {
81                  this.suppressPhone = privacy.isSuppressPhone();
82              } else {
83                  this.suppressPhone = false;
84              }
85  
86          } catch (NullPointerException e) {
87              return false;
88          } catch (ClassCastException c) {
89              return false;
90          }
91          return this.suppressPhone;
92      }
93  
94      @Override
95      public String getFormattedPhoneNumber() {
96          if (isSuppressPhone()) {
97              return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK;
98          }
99  
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 }