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.sys.OLEKeyConstants;
22 import org.kuali.ole.vnd.VendorConstants;
23 import org.kuali.ole.vnd.VendorParameterConstants;
24 import org.kuali.ole.vnd.businessobject.VendorDetail;
25 import org.kuali.ole.vnd.service.TaxNumberService;
26 import org.kuali.rice.core.web.format.FormatException;
27 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
28 import org.kuali.rice.krad.util.ObjectUtils;
29
30 public class TaxNumberServiceImpl implements TaxNumberService {
31
32 public ParameterService parameterService;
33
34
35 public void setParameterService(ParameterService parameterService) {
36 this.parameterService = parameterService;
37 }
38
39
40 public static List<String> taxNumberFormats;
41 public static List<String> feinNumberFormats;
42 public static List<String> notAllowedTaxNumbers;
43
44
45 public String formatToDefaultFormat(String taxNbr) throws FormatException {
46 String digits = taxNbr.replaceAll("\\D", "");
47
48 Integer defaultTaxNumberDigits = new Integer(parameterService.getParameterValueAsString(VendorDetail.class, VendorParameterConstants.DEFAULT_TAX_NUMBER_DIGITS));
49
50 if (digits.length() < defaultTaxNumberDigits) {
51 throw new FormatException("Tax number has fewer than " + defaultTaxNumberDigits + " digits.", OLEKeyConstants.ERROR_CUSTOM, taxNbr);
52 }
53 else if (digits.length() > defaultTaxNumberDigits) {
54 throw new FormatException("Tax number has more than " + defaultTaxNumberDigits + " digits.", OLEKeyConstants.ERROR_CUSTOM, taxNbr);
55 }
56 else {
57 return digits;
58 }
59 }
60
61
62
63
64
65
66
67 public boolean isStringAllNumbers(String field) {
68 if (!isStringEmpty(field)) {
69 field = field.trim();
70 for (int x = 0; x < field.length(); x++) {
71 char c = field.charAt(x);
72 if (!Character.isDigit(c)) {
73 return false;
74 }
75 }
76 return true;
77 }
78 return false;
79 }
80
81
82
83
84
85
86
87 public boolean isStringEmpty(String field) {
88 if (field == null || field.equals("")) {
89 return true;
90 }
91 else {
92 return false;
93 }
94 }
95
96
97
98
99
100
101
102
103
104
105
106 public boolean isValidTaxNumber(String taxNbr, String taxType) {
107 String[] ssnFormats = parseSSNFormats();
108 String[] feinFormats = parseFEINFormats();
109 Integer defaultTaxNumberDigits = new Integer(parameterService.getParameterValueAsString(VendorDetail.class, "DEFAULT_TAX_NUMBER_DIGITS"));
110
111 if (taxNbr.length() != defaultTaxNumberDigits || !isStringAllNumbers(taxNbr)) {
112 return false;
113 }
114
115 if (taxType.equals(VendorConstants.TAX_TYPE_SSN)) {
116
117 for (int i = 0; i < ssnFormats.length; i++) {
118 if (taxNbr.matches(ssnFormats[i])) {
119 return true;
120 }
121 }
122 return false;
123 }
124 else if (taxType.equals(VendorConstants.TAX_TYPE_FEIN)) {
125 for (int i = 0; i < feinFormats.length; i++) {
126 if (taxNbr.matches(feinFormats[i])) {
127 return true;
128 }
129 }
130 return false;
131 }
132
133 return true;
134 }
135
136
137
138
139
140
141
142
143
144 public boolean isAllowedTaxNumber(String taxNbr) {
145 String[] notAllowedTaxNumbers = parseNotAllowedTaxNumbers();
146 for (int i = 0; i < notAllowedTaxNumbers.length; i++) {
147 if (taxNbr.matches(notAllowedTaxNumbers[i])) {
148 return false;
149 }
150 }
151 return true;
152 }
153
154
155
156
157
158
159
160 public String[] parseSSNFormats() {
161 if (ObjectUtils.isNull(taxNumberFormats)) {
162 taxNumberFormats = new ArrayList<String>( parameterService.getParameterValuesAsString(VendorDetail.class, VendorParameterConstants.TAX_SSN_NUMBER_FORMATS) );
163 }
164 return taxNumberFormats.toArray(new String[] {});
165 }
166
167
168
169
170
171
172
173 public String[] parseFEINFormats() {
174 if (ObjectUtils.isNull(feinNumberFormats)) {
175 feinNumberFormats = new ArrayList<String>( parameterService.getParameterValuesAsString(VendorDetail.class, VendorParameterConstants.TAX_FEIN_NUMBER_FORMATS) );
176 }
177 return feinNumberFormats.toArray(new String[] {});
178 }
179
180
181
182
183
184
185
186 public String[] parseNotAllowedTaxNumbers() {
187 if (ObjectUtils.isNull(notAllowedTaxNumbers)) {
188 notAllowedTaxNumbers = new ArrayList<String>( parameterService.getParameterValuesAsString(VendorDetail.class, VendorParameterConstants.NOT_ALLOWED_TAX_NUMBERS) );
189 }
190 return notAllowedTaxNumbers.toArray(new String[] {});
191 }
192
193 }