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.module.cg.document.validation.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.module.cg.businessobject.SubContractor;
20  import org.kuali.ole.sys.OLEKeyConstants;
21  import org.kuali.ole.sys.context.SpringContext;
22  import org.kuali.rice.kns.document.MaintenanceDocument;
23  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
24  import org.kuali.rice.location.api.country.Country;
25  import org.kuali.rice.location.api.country.CountryService;
26  import org.kuali.rice.location.api.state.State;
27  import org.kuali.rice.location.api.state.StateService;
28  
29  /**
30   *
31   */
32  public class SubcontractorRule extends MaintenanceDocumentRuleBase {
33  
34      protected SubContractor newSubcontractor;
35  
36      /**
37       * This method has been overridden to add some additional validation checks to the {@link Subcontractor} maintenance document.
38       *
39       * @param maintenanceDocument - document to be tested
40       * @return whether maintenance doc passes
41       * @throws org.kuali.rice.krad.exception.ValidationException
42       */
43      @Override
44      protected boolean validateMaintenanceDocument(MaintenanceDocument maintenanceDocument) {
45          boolean success = true;
46  
47          success = super.validateMaintenanceDocument(maintenanceDocument);
48  
49          newSubcontractor = (SubContractor) super.getNewBo();
50          success &= validateCountryCode(newSubcontractor.getSubcontractorCountryCode());
51          success &= validateStateCode(newSubcontractor.getSubcontractorCountryCode(), newSubcontractor.getSubcontractorStateCode());
52  
53          return success;
54      }
55  
56  
57      /**
58       * This method retrieves the entered state code and checks that this value is valid by comparing it against known values in the
59       * SH_STATE_T database table.
60       *
61       * @param stateCode
62       * @return Whether state code entered is valid
63       */
64      protected boolean validateStateCode(String countryCode, String stateCode) {
65          boolean valid = true;
66  
67          // Perform lookup for state code provided
68          if ( StringUtils.isNotBlank(stateCode) && StringUtils.isNotBlank(countryCode) ) {
69          State state = SpringContext.getBean(StateService.class).getState(countryCode, stateCode);
70  
71          // If no values returned, state code is invalid, throw error
72          if (state== null) {
73              putFieldError("subcontractorStateCode", OLEKeyConstants.ERROR_STATE_CODE_INVALID, stateCode);
74              valid = false;
75          }
76          }
77  
78          return valid;
79      }
80  
81      /**
82       * This method retrieves the entered country code and checks that this value is valid by comparing it against known values in
83       * the SH_COUNTRY_T database table.
84       *
85       * @param countryCode
86       * @return Whether country code entered is valid.
87       */
88      protected boolean validateCountryCode(String countryCode) {
89          boolean valid = true;
90  
91          if ( StringUtils.isNotBlank(countryCode) ) {
92          Country country = SpringContext.getBean(CountryService.class).getCountry(countryCode);
93  
94          // If no values returned, country code is invalid, throw error
95          if (country == null) {
96              putFieldError("subcontractorCountryCode", OLEKeyConstants.ERROR_COUNTRY_CODE_INVALID, countryCode);
97              valid = false;
98          }
99          }
100 
101         return valid;
102     }
103 
104 }