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.sys.service.impl;
017
018 import org.apache.commons.lang.StringUtils;
019 import org.kuali.kfs.sys.KFSConstants;
020 import org.kuali.kfs.sys.KFSKeyConstants;
021 import org.kuali.kfs.sys.context.SpringContext;
022 import org.kuali.kfs.sys.service.NonTransactional;
023 import org.kuali.kfs.sys.service.PostalCodeValidationService;
024 import org.kuali.rice.kns.bo.State;
025 import org.kuali.rice.kns.datadictionary.validation.fieldlevel.ZipcodeValidationPattern;
026 import org.kuali.rice.kns.service.StateService;
027 import org.kuali.rice.kns.util.GlobalVariables;
028
029 /**
030 * Service implementation for the PostalCodeBase structure. This is the default implementation, that is delivered with Kuali.
031 */
032
033 @NonTransactional
034 public class PostalCodeValidationServiceImpl implements PostalCodeValidationService {
035
036 public boolean validateAddress(String postalCountryCode, String postalStateCode, String postalCode, String statePropertyConstant, String postalCodePropertyConstant) {
037 boolean valid = true;
038
039 if (StringUtils.equals(KFSConstants.COUNTRY_CODE_UNITED_STATES, postalCountryCode)) {
040
041 if (StringUtils.isBlank(postalStateCode)) {
042 valid &= false;
043 if (StringUtils.isNotBlank(statePropertyConstant)) {
044 GlobalVariables.getMessageMap().putError(statePropertyConstant, KFSKeyConstants.ERROR_US_REQUIRES_STATE);
045 }
046 }
047
048 if (StringUtils.isBlank(postalCode)) {
049 valid &= false;
050 if (StringUtils.isNotBlank(postalCodePropertyConstant)) {
051 GlobalVariables.getMessageMap().putError(postalCodePropertyConstant, KFSKeyConstants.ERROR_US_REQUIRES_ZIP);
052 }
053 }
054 else {
055 ZipcodeValidationPattern zipPattern = new ZipcodeValidationPattern();
056 if (!zipPattern.matches(StringUtils.defaultString(postalCode))) {
057 valid &= false;
058 if (StringUtils.isNotBlank(postalCodePropertyConstant)) {
059 GlobalVariables.getMessageMap().putError(postalCodePropertyConstant, KFSKeyConstants.ERROR_POSTAL_CODE_INVALID);
060 }
061 }
062 }
063
064 }
065
066 // verify state code exist
067 if (StringUtils.isNotBlank(postalCountryCode) && StringUtils.isNotBlank(postalStateCode)) {
068 State state = SpringContext.getBean(StateService.class).getByPrimaryId(postalCountryCode, postalStateCode);
069 if (state == null) {
070 GlobalVariables.getMessageMap().putError(statePropertyConstant, KFSKeyConstants.ERROR_STATE_CODE_INVALID, postalStateCode);
071 }
072 }
073
074 return valid;
075 }
076
077
078 }