View Javadoc

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.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       * A predicate to determine if a String field is all numbers
63       * 
64       * @param field A String tax number
65       * @return True if String is numeric
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       * A predicate to determine if a String field is null or empty
83       * 
84       * @param field A String tax number
85       * @return True if String is null or empty
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       * A predicate to determine the validity of tax numbers We're using regular expressions stored in the business rules table to
98       * validate whether the tax number is in the correct format. The regular expressions are : (please update this javadoc comment
99       * when the regular expressions change) 1. For SSN : (?!000)(?!666)(\d{3})([ \-]?)(?!00)(\d{2})([\-]?)(?!0000)(\d{4}) 2. For
100      * FEIN : (?!00)(\d{3})([ \-]?)(\d{2})([\-]?)(?!0000)(\d{4})
101      * 
102      * @param taxNbr A tax number String (SSN or FEIN)
103      * @param taxType determines SSN or FEIN tax number type
104      * @return True if the tax number is known to be in a valid format
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      * Someday we'll have to use the rules table instead of using constants. This method will return true if the tax number is an
139      * allowed tax number and return false if it's not allowed.
140      * 
141      * @param taxNbr The tax number to be processed.
142      * @return boolean true if the tax number is allowed and false otherwise.
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      * Splits the set of tax number formats which are returned from the rule service as a semicolon-delimeted String into a String
156      * array.
157      * 
158      * @return A String array of the tax number format regular expressions.
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      * Splits the set of tax fein number formats which are returned from the rule service as a semicolon-delimeted String into a
169      * String array.
170      * 
171      * @return A String array of the tax fein number format regular expressions.
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      * Splits the set of not allowed tax number formats which are returned from the rule service as a semicolon-delimeted String
182      * into a String array.
183      * 
184      * @return A String array of the not allowed tax number format regular expressions.
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 }