1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
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          
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  }