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