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.module.cg.document.validation.impl;
017
018 import java.util.ArrayList;
019 import java.util.HashMap;
020 import java.util.List;
021 import java.util.Map;
022
023 import org.kuali.kfs.module.cg.businessobject.SubContractor;
024 import org.kuali.kfs.sys.KFSKeyConstants;
025 import org.kuali.rice.kns.bo.Country;
026 import org.kuali.rice.kns.bo.State;
027 import org.kuali.kfs.sys.context.SpringContext;
028 import org.kuali.rice.kns.service.CountryService;
029 import org.kuali.rice.kns.service.StateService;
030 import org.kuali.rice.kns.document.MaintenanceDocument;
031 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
032 import org.kuali.rice.kns.service.BusinessObjectService;
033
034 /**
035 *
036 */
037 public class SubcontractorRule extends MaintenanceDocumentRuleBase {
038
039 protected SubContractor newSubcontractor;
040
041 /**
042 * This method has been overridden to add some additional validation checks to the {@link Subcontractor} maintenance document.
043 *
044 * @param maintenanceDocument - document to be tested
045 * @return whether maintenance doc passes
046 * @throws org.kuali.rice.kns.exception.ValidationException
047 */
048 @Override
049 protected boolean validateMaintenanceDocument(MaintenanceDocument maintenanceDocument) {
050 boolean success = true;
051
052 success = super.validateMaintenanceDocument(maintenanceDocument);
053
054 newSubcontractor = (SubContractor) super.getNewBo();
055 success &= validateCountryCode(newSubcontractor.getSubcontractorCountryCode());
056 success &= validateStateCode(newSubcontractor.getSubcontractorCountryCode(), newSubcontractor.getSubcontractorStateCode());
057
058 return success;
059 }
060
061
062 /**
063 * This method retrieves the entered state code and checks that this value is valid by comparing it against known values in the
064 * SH_STATE_T database table.
065 *
066 * @param stateCode
067 * @return Whether state code entered is valid
068 */
069 protected boolean validateStateCode(String countryCode, String stateCode) {
070 boolean valid = true;
071
072 // Perform lookup for state code provided
073 State state = SpringContext.getBean(StateService.class).getByPrimaryId(countryCode, stateCode);
074
075 // If no values returned, state code is invalid, throw error
076 if (state== null) {
077 putFieldError("subcontractorStateCode", KFSKeyConstants.ERROR_STATE_CODE_INVALID, stateCode);
078 valid = false;
079 }
080
081 return valid;
082 }
083
084 /**
085 * This method retrieves the entered country code and checks that this value is valid by comparing it against known values in
086 * the SH_COUNTRY_T database table.
087 *
088 * @param countryCode
089 * @return Whether country code entered is valid.
090 */
091 protected boolean validateCountryCode(String countryCode) {
092 boolean valid = true;
093
094 Country country = SpringContext.getBean(CountryService.class).getByPrimaryId(countryCode);
095
096 // If no values returned, country code is invalid, throw error
097 if (country == null) {
098 putFieldError("subcontractorCountryCode", KFSKeyConstants.ERROR_COUNTRY_CODE_INVALID, countryCode);
099 valid = false;
100 }
101
102 return valid;
103 }
104
105 }