View Javadoc
1   /*
2    * Copyright 2007-2009 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.sys.service.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.sys.OLEConstants;
20  import org.kuali.ole.sys.OLEKeyConstants;
21  import org.kuali.ole.sys.context.SpringContext;
22  import org.kuali.ole.sys.service.NonTransactional;
23  import org.kuali.ole.sys.service.PostalCodeValidationService;
24  import org.kuali.rice.kns.datadictionary.validation.fieldlevel.ZipcodeValidationPattern;
25  import org.kuali.rice.krad.util.GlobalVariables;
26  import org.kuali.rice.location.api.state.State;
27  import org.kuali.rice.location.api.state.StateService;
28  
29  /**
30   * Service implementation for the PostalCodeBase structure. This is the default implementation, that is delivered with Kuali.
31   */
32  
33  @NonTransactional
34  public class PostalCodeValidationServiceImpl implements PostalCodeValidationService {
35  
36      public boolean validateAddress(String postalCountryCode, String stateCode, String postalCode, String statePropertyConstant, String postalCodePropertyConstant) {
37          boolean valid = true;
38  
39          if (StringUtils.equals(OLEConstants.COUNTRY_CODE_UNITED_STATES, postalCountryCode)) {
40  
41              if (StringUtils.isBlank(stateCode)) {
42                  valid &= false;
43                  if (StringUtils.isNotBlank(statePropertyConstant)) {
44                      GlobalVariables.getMessageMap().putError(statePropertyConstant, OLEKeyConstants.ERROR_US_REQUIRES_STATE);
45                  }
46              }
47  
48              if (StringUtils.isBlank(postalCode)) {
49                  valid &= false;
50                  if (StringUtils.isNotBlank(postalCodePropertyConstant)) {
51                      GlobalVariables.getMessageMap().putError(postalCodePropertyConstant, OLEKeyConstants.ERROR_US_REQUIRES_ZIP);
52                  }
53              }
54              else {
55                  ZipcodeValidationPattern zipPattern = new ZipcodeValidationPattern();
56                  if (!zipPattern.matches(StringUtils.defaultString(postalCode))) {
57                      valid &= false;
58                      if (StringUtils.isNotBlank(postalCodePropertyConstant)) {
59                          GlobalVariables.getMessageMap().putError(postalCodePropertyConstant, OLEKeyConstants.ERROR_POSTAL_CODE_INVALID);
60                      }
61                  }
62              }
63  
64          }
65  
66          // verify state code exist
67          if (StringUtils.isNotBlank(postalCountryCode) && StringUtils.isNotBlank(stateCode)) {
68              State state = SpringContext.getBean(StateService.class).getState(postalCountryCode, stateCode);
69              if (state == null) {
70                  GlobalVariables.getMessageMap().putError(statePropertyConstant, OLEKeyConstants.ERROR_STATE_CODE_INVALID, stateCode);
71              }
72          }
73          
74          return valid;
75      }
76  
77  
78  }