001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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: */
016: package org.kuali.module.vendor.service.impl;
017:
018: import java.util.List;
019:
020: import org.kuali.core.util.ObjectUtils;
021: import org.kuali.kfs.service.ParameterService;
022: import org.kuali.module.purap.PurapParameterConstants;
023: import org.kuali.module.vendor.bo.VendorDetail;
024: import org.kuali.module.vendor.service.PhoneNumberService;
025:
026: public class PhoneNumberServiceImpl implements PhoneNumberService {
027:
028: public ParameterService parameterService;
029: public List<String> phoneNumberFormats;
030:
031: public void setParameterService(ParameterService parameterService) {
032: this .parameterService = parameterService;
033: }
034:
035: // This 1-based index is used to pick the format among those in phoneNumberFormats
036: // which is the default.
037: public final int PHONE_FORMAT_RULE_DEFAULT_INDEX = 1;
038:
039: /**
040: * Converts a valid phone number to the default format. Must be changed if the generic format changes. The string passed in is
041: * stripped of non-number chars. If it is then the right length it is formatted. If not the right length the original string is
042: * returned.
043: *
044: * @param phone The phone number String to be converted
045: * @return A String in the default valid format
046: * @see org.kuali.core.web.format.PhoneNumberFormatter
047: */
048: public String formatNumberIfPossible(String unformattedNumber) {
049: if (ObjectUtils.isNull(unformattedNumber)) {
050: return unformattedNumber;
051: }
052: String formattedNumber = unformattedNumber
053: .replaceAll("\\D", "");
054: Integer defaultPhoneNumberDigits = new Integer(
055: parameterService
056: .getParameterValue(
057: VendorDetail.class,
058: PurapParameterConstants.DEFAULT_PHONE_NUMBER_DIGITS_PARM_NM));
059: // Before moving to the parameter table:
060: // if ( formattedNumber.length() != VendorConstants.GENERIC_DEFAULT_PHONE_NUM_DIGITS ) {
061: if (formattedNumber.length() != defaultPhoneNumberDigits) {
062: return unformattedNumber;
063: } else {
064: return formattedNumber.substring(0, 3) + "-"
065: + formattedNumber.substring(3, 6) + "-"
066: + formattedNumber.substring(6, 10);
067: }
068: }
069:
070: /**
071: * A predicate to determine the validity of phone numbers, using only the formats which are common in North America (which we
072: * are calling Generic formats) as examples.
073: *
074: * @param phone A phone number String
075: * @return True if the phone number is known to be in a valid format
076: */
077: public boolean isValidPhoneNumber(String phone) {
078: String[] formats = parseFormats();
079: for (int i = 0; i < formats.length; i++) {
080: if (phone.matches(formats[i])) {
081: return true;
082: }
083: }
084: return false;
085: }
086:
087: /**
088: * Splits the set of phone number formats which are returned from the rule service as a semicolon-delimeted String into a String
089: * array.
090: *
091: * @return A String array of the phone number format regular expressions.
092: */
093: protected String[] parseFormats() {
094: if (ObjectUtils.isNull(phoneNumberFormats)) {
095: phoneNumberFormats = parameterService
096: .getParameterValues(
097: VendorDetail.class,
098: PurapParameterConstants.PHONE_NUMBER_FORMATS_PARM_NM);
099: }
100: return phoneNumberFormats.toArray(new String[] {});
101: }
102:
103: /**
104: * A predicate to determine whether the given phone number is in the default format.
105: *
106: * @param phone A phone number String
107: * @return True if the phone number is in the default format.
108: */
109: public boolean isDefaultFormatPhoneNumber(String phone) {
110: String[] formats = parseFormats();
111: String defaultPhoneFormat = formats[PHONE_FORMAT_RULE_DEFAULT_INDEX - 1];
112: if (phone.matches(defaultPhoneFormat)) {
113: return true;
114: }
115: return false;
116: }
117:
118: }
|