1 /*
2 * Copyright 2007 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.ole.vnd.service.impl;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.kuali.ole.vnd.VendorParameterConstants;
22 import org.kuali.ole.vnd.businessobject.VendorDetail;
23 import org.kuali.ole.vnd.service.PhoneNumberService;
24 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
25 import org.kuali.rice.krad.util.ObjectUtils;
26
27 public class PhoneNumberServiceImpl implements PhoneNumberService {
28
29 public ParameterService parameterService;
30 public List<String> phoneNumberFormats;
31
32 public void setParameterService(ParameterService parameterService) {
33 this.parameterService = parameterService;
34 }
35
36
37 // This 1-based index is used to pick the format among those in phoneNumberFormats
38 // which is the default.
39 public final int PHONE_FORMAT_RULE_DEFAULT_INDEX = 1;
40
41 /**
42 * Converts a valid phone number to the default format. Must be changed if the generic format changes. The string passed in is
43 * stripped of non-number chars. If it is then the right length it is formatted. If not the right length the original string is
44 * returned.
45 *
46 * @param phone The phone number String to be converted
47 * @return A String in the default valid format
48 * @see org.kuali.rice.core.web.format.PhoneNumberFormatter
49 */
50 public String formatNumberIfPossible(String unformattedNumber) {
51 if (ObjectUtils.isNull(unformattedNumber)) {
52 return unformattedNumber;
53 }
54 String formattedNumber = unformattedNumber.replaceAll("\\D", "");
55 Integer defaultPhoneNumberDigits = new Integer(parameterService.getParameterValueAsString(VendorDetail.class, VendorParameterConstants.DEFAULT_PHONE_NUMBER_DIGITS));
56 // Before moving to the parameter table:
57 // if ( formattedNumber.length() != VendorConstants.GENERIC_DEFAULT_PHONE_NUM_DIGITS ) {
58 if (formattedNumber.length() != defaultPhoneNumberDigits) {
59 return unformattedNumber;
60 }
61 else {
62 return formattedNumber.substring(0, 3) + "-" + formattedNumber.substring(3, 6) + "-" + formattedNumber.substring(6, 10);
63 }
64 }
65
66 /**
67 * A predicate to determine the validity of phone numbers, using only the formats which are common in North America (which we
68 * are calling Generic formats) as examples.
69 *
70 * @param phone A phone number String
71 * @return True if the phone number is known to be in a valid format
72 */
73 public boolean isValidPhoneNumber(String phone) {
74 String[] formats = parseFormats();
75 for (int i = 0; i < formats.length; i++) {
76 if (phone.matches(formats[i])) {
77 return true;
78 }
79 }
80 return false;
81 }
82
83 /**
84 * Splits the set of phone number formats which are returned from the rule service as a semicolon-delimeted String into a String
85 * array.
86 *
87 * @return A String array of the phone number format regular expressions.
88 */
89 protected String[] parseFormats() {
90 if (ObjectUtils.isNull(phoneNumberFormats)) {
91 phoneNumberFormats = new ArrayList<String>( parameterService.getParameterValuesAsString(VendorDetail.class, VendorParameterConstants.PHONE_NUMBER_FORMATS) );
92 }
93 return phoneNumberFormats.toArray(new String[] {});
94 }
95
96 /**
97 * A predicate to determine whether the given phone number is in the default format.
98 *
99 * @param phone A phone number String
100 * @return True if the phone number is in the default format.
101 */
102 public boolean isDefaultFormatPhoneNumber(String phone) {
103 String[] formats = parseFormats();
104 String defaultPhoneFormat = formats[PHONE_FORMAT_RULE_DEFAULT_INDEX - 1];
105 if (phone.matches(defaultPhoneFormat)) {
106 return true;
107 }
108 return false;
109 }
110
111 }