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.core.web.format.FormatException;
022: import org.kuali.kfs.KFSKeyConstants;
023: import org.kuali.kfs.service.ParameterService;
024: import org.kuali.module.vendor.VendorConstants;
025: import org.kuali.module.vendor.VendorParameterConstants;
026: import org.kuali.module.vendor.bo.VendorDetail;
027: import org.kuali.module.vendor.service.TaxNumberService;
028:
029: public class TaxNumberServiceImpl implements TaxNumberService {
030:
031: public ParameterService parameterService;
032:
033: public void setParameterService(ParameterService parameterService) {
034: this .parameterService = parameterService;
035: }
036:
037: public static List<String> taxNumberFormats;
038: public static List<String> feinNumberFormats;
039: public static List<String> notAllowedTaxNumbers;
040:
041: public String formatToDefaultFormat(String taxNbr)
042: throws FormatException {
043: String digits = taxNbr.replaceAll("\\D", "");
044:
045: Integer defaultTaxNumberDigits = new Integer(
046: parameterService
047: .getParameterValue(
048: VendorDetail.class,
049: VendorParameterConstants.DEFAULT_TAX_NUMBER_DIGITS));
050:
051: if (digits.length() < defaultTaxNumberDigits) {
052: throw new FormatException("Tax number has fewer than "
053: + defaultTaxNumberDigits + " digits.",
054: KFSKeyConstants.ERROR_CUSTOM, taxNbr);
055: } else if (digits.length() > defaultTaxNumberDigits) {
056: throw new FormatException("Tax number has more than "
057: + defaultTaxNumberDigits + " digits.",
058: KFSKeyConstants.ERROR_CUSTOM, taxNbr);
059: } else {
060: return digits;
061: }
062: }
063:
064: /**
065: * A predicate to determine if a String field is all numbers
066: *
067: * @param field A String tax number
068: * @return True if String is numeric
069: */
070: public boolean isStringAllNumbers(String field) {
071: if (!isStringEmpty(field)) {
072: field = field.trim();
073: for (int x = 0; x < field.length(); x++) {
074: char c = field.charAt(x);
075: if (!Character.isDigit(c)) {
076: return false;
077: }
078: }
079: return true;
080: }
081: return false;
082: }
083:
084: /**
085: * A predicate to determine if a String field is null or empty
086: *
087: * @param field A String tax number
088: * @return True if String is null or empty
089: */
090: public boolean isStringEmpty(String field) {
091: if (field == null || field.equals("")) {
092: return true;
093: } else {
094: return false;
095: }
096: }
097:
098: /**
099: * A predicate to determine the validity of tax numbers We're using regular expressions stored in the business rules table to
100: * validate whether the tax number is in the correct format. The regular expressions are : (please update this javadoc comment
101: * when the regular expressions change) 1. For SSN : (?!000)(?!666)(\d{3})([ \-]?)(?!00)(\d{2})([\-]?)(?!0000)(\d{4}) 2. For
102: * FEIN : (?!00)(\d{3})([ \-]?)(\d{2})([\-]?)(?!0000)(\d{4})
103: *
104: * @param taxNbr A tax number String (SSN or FEIN)
105: * @param taxType determines SSN or FEIN tax number type
106: * @return True if the tax number is known to be in a valid format
107: */
108: public boolean isValidTaxNumber(String taxNbr, String taxType) {
109: String[] ssnFormats = parseSSNFormats();
110: String[] feinFormats = parseFEINFormats();
111: Integer defaultTaxNumberDigits = new Integer(parameterService
112: .getParameterValue(VendorDetail.class,
113: "DEFAULT_TAX_NUMBER_DIGITS"));
114:
115: if (taxNbr.length() != defaultTaxNumberDigits
116: || !isStringAllNumbers(taxNbr)) {
117: return false;
118: }
119:
120: if (taxType.equals(VendorConstants.TAX_TYPE_SSN)) {
121:
122: for (int i = 0; i < ssnFormats.length; i++) {
123: if (taxNbr.matches(ssnFormats[i])) {
124: return true;
125: }
126: }
127: return false;
128: } else if (taxType.equals(VendorConstants.TAX_TYPE_FEIN)) {
129: for (int i = 0; i < feinFormats.length; i++) {
130: if (taxNbr.matches(feinFormats[i])) {
131: return true;
132: }
133: }
134: return false;
135: }
136:
137: return true;
138: }
139:
140: /**
141: * Someday we'll have to use the rules table instead of using constants. This method will return true if the tax number is an
142: * allowed tax number and return false if it's not allowed.
143: *
144: * @param taxNbr The tax number to be processed.
145: * @return boolean true if the tax number is allowed and false otherwise.
146: */
147: public boolean isAllowedTaxNumber(String taxNbr) {
148: String[] notAllowedTaxNumbers = parseNotAllowedTaxNumbers();
149: for (int i = 0; i < notAllowedTaxNumbers.length; i++) {
150: if (taxNbr.matches(notAllowedTaxNumbers[i])) {
151: return false;
152: }
153: }
154: return true;
155: }
156:
157: /**
158: * Splits the set of tax number formats which are returned from the rule service as a semicolon-delimeted String into a String
159: * array.
160: *
161: * @return A String array of the tax number format regular expressions.
162: */
163: public String[] parseSSNFormats() {
164: if (ObjectUtils.isNull(taxNumberFormats)) {
165: taxNumberFormats = parameterService.getParameterValues(
166: VendorDetail.class, "TAX_SSN_NUMBER_FORMATS");
167: }
168: return taxNumberFormats.toArray(new String[] {});
169: }
170:
171: /**
172: * Splits the set of tax fein number formats which are returned from the rule service as a semicolon-delimeted String into a
173: * String array.
174: *
175: * @return A String array of the tax fein number format regular expressions.
176: */
177: public String[] parseFEINFormats() {
178: if (ObjectUtils.isNull(feinNumberFormats)) {
179: feinNumberFormats = parameterService.getParameterValues(
180: VendorDetail.class, "TAX_FEIN_NUMBER_FORMATS");
181: }
182: return feinNumberFormats.toArray(new String[] {});
183: }
184:
185: /**
186: * Splits the set of not allowed tax number formats which are returned from the rule service as a semicolon-delimeted String
187: * into a String array.
188: *
189: * @return A String array of the not allowed tax number format regular expressions.
190: */
191: public String[] parseNotAllowedTaxNumbers() {
192: if (ObjectUtils.isNull(notAllowedTaxNumbers)) {
193: notAllowedTaxNumbers = parameterService
194: .getParameterValues(
195: VendorDetail.class,
196: VendorParameterConstants.PURAP_NOT_ALLOWED_TAX_NUMBERS);
197: }
198: return notAllowedTaxNumbers.toArray(new String[] {});
199: }
200:
201: }
|