001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.cg.rules;
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.core.document.MaintenanceDocument;
024: import org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase;
025: import org.kuali.core.service.BusinessObjectService;
026: import org.kuali.kfs.KFSKeyConstants;
027: import org.kuali.kfs.bo.Country;
028: import org.kuali.kfs.bo.State;
029: import org.kuali.kfs.context.SpringContext;
030: import org.kuali.module.cg.bo.Subcontractor;
031:
032: /**
033: *
034: */
035: public class SubcontractorRule extends MaintenanceDocumentRuleBase {
036:
037: private Subcontractor newSubcontractor;
038:
039: /**
040: * This method has been overridden to add some additional validation checks to the {@link Subcontractor} maintenance document.
041: *
042: * @param maintenanceDocument - document to be tested
043: * @return whether maintenance doc passes
044: * @throws org.kuali.core.exceptions.ValidationException
045: */
046: @Override
047: protected boolean validateMaintenanceDocument(
048: MaintenanceDocument maintenanceDocument) {
049: boolean success = true;
050:
051: success = super
052: .validateMaintenanceDocument(maintenanceDocument);
053:
054: newSubcontractor = (Subcontractor) super .getNewBo();
055: success &= validateStateCode(newSubcontractor
056: .getSubcontractorStateCode());
057: success &= validateCountryCode(newSubcontractor
058: .getSubcontractorCountryCode());
059:
060: return success;
061: }
062:
063: /**
064: * This method retrieves the entered state code and checks that this value is valid by comparing it against known values in the
065: * SH_STATE_T database table.
066: *
067: * @param stateCode
068: * @return Whether state code entered is valid
069: */
070: private boolean validateStateCode(String stateCode) {
071: boolean valid = true;
072:
073: // Create a query to do a lookup on.
074: Map criteria = new HashMap();
075: List<String> criteriaValues = new ArrayList<String>();
076: criteriaValues.add(stateCode);
077: criteria.put("postalStateCode", criteriaValues);
078:
079: // Perform lookup for state code provided
080: List boList = (List) SpringContext.getBean(
081: BusinessObjectService.class).findMatching(State.class,
082: criteria);
083:
084: // If no values returned, state code is invalid, throw error
085: if (boList.size() < 1) {
086: putFieldError("subcontractorStateCode",
087: KFSKeyConstants.ERROR_STATE_CODE_INVALID, stateCode);
088: valid = false;
089: }
090:
091: return valid;
092: }
093:
094: /**
095: * This method retrieves the entered country code and checks that this value is valid by comparing it against known values in
096: * the SH_COUNTRY_T database table.
097: *
098: * @param countryCode
099: * @return Whether country code entered is valid.
100: */
101: private boolean validateCountryCode(String countryCode) {
102: boolean valid = true;
103:
104: // Create a query to do a lookup on.
105: Map criteria = new HashMap();
106:
107: List<String> criteriaValues = new ArrayList<String>();
108: criteriaValues.add(countryCode);
109: criteria.put("postalCountryCode", criteriaValues);
110:
111: // Perform lookup for country code provided
112: List boList = (List) SpringContext.getBean(
113: BusinessObjectService.class).findMatching(
114: Country.class, criteria);
115:
116: // If no values returned, country code is invalid, throw error
117: if (boList.size() < 1) {
118: putFieldError("subcontractorCountryCode",
119: KFSKeyConstants.ERROR_COUNTRY_CODE_INVALID,
120: countryCode);
121: valid = false;
122: }
123:
124: return valid;
125: }
126:
127: }
|