View Javadoc

1   /**
2    * Copyright 2005-2011 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.api.identity.phone;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.CoreConstants;
20  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
21  import org.kuali.rice.core.api.mo.ModelBuilder;
22  import org.kuali.rice.kim.api.KimApiConstants;
23  import org.kuali.rice.kim.api.identity.CodedAttribute;
24  import org.w3c.dom.Element;
25  
26  import javax.xml.bind.annotation.XmlAccessType;
27  import javax.xml.bind.annotation.XmlAccessorType;
28  import javax.xml.bind.annotation.XmlAnyElement;
29  import javax.xml.bind.annotation.XmlElement;
30  import javax.xml.bind.annotation.XmlRootElement;
31  import javax.xml.bind.annotation.XmlType;
32  import java.io.Serializable;
33  import java.util.Collection;
34  
35  @XmlRootElement(name = EntityPhone.Constants.ROOT_ELEMENT_NAME)
36  @XmlAccessorType(XmlAccessType.NONE)
37  @XmlType(name = EntityPhone.Constants.TYPE_NAME, propOrder = {
38      EntityPhone.Elements.ID,
39      EntityPhone.Elements.ENTITY_TYPE_CODE,
40      EntityPhone.Elements.ENTITY_ID,
41      EntityPhone.Elements.PHONE_TYPE,
42      EntityPhone.Elements.COUNTRY_CODE,
43      EntityPhone.Elements.PHONE_NUMBER,
44      EntityPhone.Elements.EXTENSION_NUMBER,
45      EntityPhone.Elements.FORMATTED_PHONE_NUMBER,
46      EntityPhone.Elements.COUNTRY_CODE_UNMASKED,
47      EntityPhone.Elements.PHONE_NUMBER_UNMASKED,
48      EntityPhone.Elements.EXTENSION_NUMBER_UNMASKED,
49      EntityPhone.Elements.FORMATTED_PHONE_NUMBER_UNMASKED,
50      EntityPhone.Elements.SUPPRESS_PHONE,
51      EntityPhone.Elements.DEFAULT_VALUE,
52      EntityPhone.Elements.ACTIVE,
53      CoreConstants.CommonElements.VERSION_NUMBER,
54      CoreConstants.CommonElements.OBJECT_ID,
55      CoreConstants.CommonElements.FUTURE_ELEMENTS
56  })
57  public final class EntityPhone extends AbstractDataTransferObject
58      implements EntityPhoneContract
59  {
60  
61      @XmlElement(name = Elements.ID, required = false)
62      private final String id;
63      @XmlElement(name = Elements.ENTITY_TYPE_CODE, required = false)
64      private final String entityTypeCode;
65      @XmlElement(name = Elements.ENTITY_ID, required = false)
66      private final String entityId;
67      @XmlElement(name = Elements.PHONE_TYPE, required = false)
68      private final CodedAttribute phoneType;
69      @XmlElement(name = Elements.PHONE_NUMBER, required = false)
70      private final String phoneNumber;
71      @XmlElement(name = Elements.EXTENSION_NUMBER, required = false)
72      private final String extensionNumber;
73      @XmlElement(name = Elements.COUNTRY_CODE, required = false)
74      private final String countryCode;
75      @XmlElement(name = Elements.PHONE_NUMBER_UNMASKED, required = false)
76      private final String phoneNumberUnmasked;
77      @XmlElement(name = Elements.EXTENSION_NUMBER_UNMASKED, required = false)
78      private final String extensionNumberUnmasked;
79      @XmlElement(name = Elements.COUNTRY_CODE_UNMASKED, required = false)
80      private final String countryCodeUnmasked;
81      @XmlElement(name = Elements.FORMATTED_PHONE_NUMBER, required = false)
82      private final String formattedPhoneNumber;
83      @XmlElement(name = Elements.FORMATTED_PHONE_NUMBER_UNMASKED, required = false)
84      private final String formattedPhoneNumberUnmasked;
85      @XmlElement(name = Elements.SUPPRESS_PHONE, required = false)
86      private final boolean suppressPhone;
87      @XmlElement(name = Elements.DEFAULT_VALUE, required = false)
88      private final boolean defaultValue;
89      @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
90      private final Long versionNumber;
91      @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
92      private final String objectId;
93      @XmlElement(name = Elements.ACTIVE, required = false)
94      private final boolean active;
95      @SuppressWarnings("unused")
96      @XmlAnyElement
97      private final Collection<Element> _futureElements = null;
98  
99      /**
100      * Private constructor used only by JAXB.
101      *
102      */
103     private EntityPhone() {
104         this.id = null;
105         this.entityId = null;
106         this.entityTypeCode = null;
107         this.phoneType = null;
108         this.phoneNumber = null;
109         this.extensionNumber = null;
110         this.countryCode = null;
111         this.phoneNumberUnmasked = null;
112         this.extensionNumberUnmasked = null;
113         this.countryCodeUnmasked = null;
114         this.formattedPhoneNumber = null;
115         this.formattedPhoneNumberUnmasked = null;
116         this.suppressPhone = false;
117         this.defaultValue = false;
118         this.versionNumber = null;
119         this.objectId = null;
120         this.active = false;
121     }
122 
123     private EntityPhone(Builder builder) {
124         this.id = builder.getId();
125         this.entityId = builder.getEntityId();
126         this.entityTypeCode = builder.getEntityTypeCode();
127         this.phoneType = (builder.getPhoneType() != null) ? builder.getPhoneType().build() : null;
128         this.phoneNumber = builder.getPhoneNumber();
129         this.extensionNumber = builder.getExtensionNumber();
130         this.countryCode = builder.getCountryCode();
131         this.phoneNumberUnmasked = builder.getPhoneNumberUnmasked();
132         this.extensionNumberUnmasked = builder.getExtensionNumberUnmasked();
133         this.countryCodeUnmasked = builder.getCountryCodeUnmasked();
134         this.formattedPhoneNumber = builder.getFormattedPhoneNumber();
135         this.formattedPhoneNumberUnmasked = builder.getFormattedPhoneNumberUnmasked();
136         this.suppressPhone = builder.isSuppressPhone();
137         this.defaultValue = builder.isDefaultValue();
138         this.versionNumber = builder.getVersionNumber();
139         this.objectId = builder.getObjectId();
140         this.active = builder.isActive();
141     }
142 
143     @Override
144     public String getId() {
145         return this.id;
146     }
147 
148     @Override
149     public String getEntityId() {
150         return this.entityId;
151     }
152 
153     @Override
154     public String getEntityTypeCode() {
155         return this.entityTypeCode;
156     }
157 
158     @Override
159     public CodedAttribute getPhoneType() {
160         return this.phoneType;
161     }
162 
163     @Override
164     public String getPhoneNumber() {
165         return this.phoneNumber;
166     }
167 
168     @Override
169     public String getExtensionNumber() {
170         return this.extensionNumber;
171     }
172 
173     @Override
174     public String getCountryCode() {
175         return this.countryCode;
176     }
177 
178     @Override
179     public String getPhoneNumberUnmasked() {
180         return this.phoneNumberUnmasked;
181     }
182 
183     @Override
184     public String getExtensionNumberUnmasked() {
185         return this.extensionNumberUnmasked;
186     }
187 
188     @Override
189     public String getCountryCodeUnmasked() {
190         return this.countryCodeUnmasked;
191     }
192 
193     @Override
194     public String getFormattedPhoneNumber() {
195         return this.formattedPhoneNumber;
196     }
197 
198     @Override
199     public String getFormattedPhoneNumberUnmasked() {
200         return this.formattedPhoneNumberUnmasked;
201     }
202 
203     @Override
204     public boolean isSuppressPhone() {
205         return this.suppressPhone;
206     }
207 
208     @Override
209     public boolean isDefaultValue() {
210         return this.defaultValue;
211     }
212 
213     @Override
214     public Long getVersionNumber() {
215         return this.versionNumber;
216     }
217 
218     @Override
219     public String getObjectId() {
220         return this.objectId;
221     }
222 
223     @Override
224     public boolean isActive() {
225         return this.active;
226     }
227 
228     /**
229      * A builder which can be used to construct {@link EntityPhone} instances.  Enforces the constraints of the {@link EntityPhoneContract}.
230      *
231      */
232     public final static class Builder
233         implements Serializable, ModelBuilder, EntityPhoneContract
234     {
235 
236         private String id;
237         private String entityId;
238         private String entityTypeCode;
239         private CodedAttribute.Builder phoneType;
240         //private String phoneNumber;
241         //private String extensionNumber;
242         //private String countryCode;
243         private String phoneNumberUnmasked;
244         private String extensionNumberUnmasked;
245         private String countryCodeUnmasked;
246         //private String formattedPhoneNumber;
247         //private String formattedPhoneNumberUnmasked;
248         private boolean suppressPhone;
249         private boolean defaultValue;
250         private Long versionNumber;
251         private String objectId;
252         private boolean active;
253 
254         private Builder() {
255 
256         }
257 
258         public static Builder create() {
259             return new Builder();
260         }
261 
262         public static Builder create(EntityPhoneContract contract) {
263             if (contract == null) {
264                 throw new IllegalArgumentException("contract was null");
265             }
266             Builder builder = create();
267             builder.setId(contract.getId());
268             builder.setEntityId(contract.getEntityId());
269             builder.setEntityTypeCode(contract.getEntityTypeCode());
270             builder.setSuppressPhone(contract.isSuppressPhone());
271             if (contract.getPhoneType() != null) {
272                 builder.setPhoneType(CodedAttribute.Builder.create(contract.getPhoneType()));
273             }
274             builder.setPhoneNumber(contract.getPhoneNumberUnmasked());
275             builder.setExtensionNumber(contract.getExtensionNumberUnmasked());
276             builder.setCountryCode(contract.getCountryCodeUnmasked());
277             builder.setDefaultValue(contract.isDefaultValue());
278             builder.setVersionNumber(contract.getVersionNumber());
279             builder.setObjectId(contract.getObjectId());
280             builder.setActive(contract.isActive());
281             return builder;
282         }
283 
284         public EntityPhone build() {
285             return new EntityPhone(this);
286         }
287 
288         @Override
289         public String getId() {
290             return this.id;
291         }
292 
293         @Override
294         public String getEntityTypeCode() {
295             return this.entityTypeCode;
296         }
297 
298         @Override
299         public String getEntityId() {
300             return this.entityId;
301         }
302 
303         @Override
304         public CodedAttribute.Builder getPhoneType() {
305             return this.phoneType;
306         }
307 
308         @Override
309         public String getPhoneNumber() {
310             if (isSuppressPhone()) {
311                 return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK_PHONE;
312             }
313             return this.phoneNumberUnmasked;
314         }
315 
316         @Override
317         public String getExtensionNumber() {
318             if (isSuppressPhone()) {
319                 return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK;
320             }
321             return this.extensionNumberUnmasked;
322         }
323 
324         @Override
325         public String getCountryCode() {
326             if (isSuppressPhone()) {
327                 return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK_CODE;
328             }
329             return this.countryCodeUnmasked;
330         }
331 
332         @Override
333         public String getPhoneNumberUnmasked() {
334             return this.phoneNumberUnmasked;
335         }
336 
337         @Override
338         public String getExtensionNumberUnmasked() {
339             return this.extensionNumberUnmasked;
340         }
341 
342         @Override
343         public String getCountryCodeUnmasked() {
344             return this.countryCodeUnmasked;
345         }
346 
347         @Override
348         public String getFormattedPhoneNumber() {
349             if (isSuppressPhone()) {
350                 return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK;
351             }
352             return this.getFormattedPhoneNumberUnmasked();
353         }
354 
355         @Override
356         public String getFormattedPhoneNumberUnmasked() {
357             StringBuffer sb = new StringBuffer( 30 );
358 
359             // TODO: get extension from country code table
360             // TODO: append "+xxx" if country is not the default country
361             sb.append( this.phoneNumberUnmasked );
362             if ( StringUtils.isNotBlank( this.extensionNumberUnmasked) ) {
363                 sb.append( " x" );
364                 sb.append( this.extensionNumberUnmasked );
365             }
366 
367             return sb.toString();
368         }
369 
370         @Override
371         public boolean isSuppressPhone() {
372             return this.suppressPhone;
373         }
374 
375         @Override
376         public boolean isDefaultValue() {
377             return this.defaultValue;
378         }
379 
380         @Override
381         public Long getVersionNumber() {
382             return this.versionNumber;
383         }
384 
385         @Override
386         public String getObjectId() {
387             return this.objectId;
388         }
389 
390         @Override
391         public boolean isActive() {
392             return this.active;
393         }
394 
395         public void setId(String id) {
396             if (StringUtils.isWhitespace(id)) {
397                 throw new IllegalArgumentException("id is blank");
398             }
399             this.id = id;
400         }
401 
402         public void setEntityTypeCode(String entityTypeCode) {
403             this.entityTypeCode = entityTypeCode;
404         }
405 
406         public void setEntityId(String entityId) {
407             this.entityId = entityId;
408         }
409 
410         public void setPhoneType(CodedAttribute.Builder phoneType) {
411             this.phoneType = phoneType;
412         }
413 
414         public void setPhoneNumber(String phoneNumber) {
415             this.phoneNumberUnmasked = phoneNumber;
416         }
417 
418         public void setExtensionNumber(String extensionNumber) {
419             this.extensionNumberUnmasked = extensionNumber;
420         }
421 
422         public void setCountryCode(String countryCode) {
423             this.countryCodeUnmasked = countryCode;
424         }
425 
426         private void setSuppressPhone(boolean suppressPhone) {
427             this.suppressPhone = suppressPhone;
428         }
429 
430         public void setDefaultValue(boolean defaultValue) {
431             this.defaultValue = defaultValue;
432         }
433 
434         public void setVersionNumber(Long versionNumber) {
435             this.versionNumber = versionNumber;
436         }
437 
438         public void setObjectId(String objectId) {
439             this.objectId = objectId;
440         }
441 
442         public void setActive(boolean active) {
443             this.active = active;
444         }
445 
446     }
447 
448 
449     /**
450      * Defines some internal constants used on this class.
451      *
452      */
453     static class Constants {
454 
455         final static String ROOT_ELEMENT_NAME = "entityPhone";
456         final static String TYPE_NAME = "EntityPhoneType";
457     }
458 
459 
460     /**
461      * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
462      *
463      */
464     static class Elements {
465 
466         final static String ID = "id";
467         final static String ENTITY_TYPE_CODE = "entityTypeCode";
468         final static String ENTITY_ID = "entityId";
469         final static String PHONE_TYPE = "phoneType";
470         final static String PHONE_NUMBER = "phoneNumber";
471         final static String EXTENSION_NUMBER = "extensionNumber";
472         final static String COUNTRY_CODE = "countryCode";
473         final static String PHONE_NUMBER_UNMASKED = "phoneNumberUnmasked";
474         final static String EXTENSION_NUMBER_UNMASKED = "extensionNumberUnmasked";
475         final static String COUNTRY_CODE_UNMASKED = "countryCodeUnmasked";
476         final static String FORMATTED_PHONE_NUMBER = "formattedPhoneNumber";
477         final static String FORMATTED_PHONE_NUMBER_UNMASKED = "formattedPhoneNumberUnmasked";
478         final static String SUPPRESS_PHONE = "suppressPhone";
479         final static String DEFAULT_VALUE = "defaultValue";
480         final static String ACTIVE = "active";
481 
482     }
483 
484 }