1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
38
39 public final int PHONE_FORMAT_RULE_DEFAULT_INDEX = 1;
40
41
42
43
44
45
46
47
48
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
57
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
68
69
70
71
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
85
86
87
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
98
99
100
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 }