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.fp.document.validation.impl;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.kfs.fp.businessobject.DisbursementVoucherPayeeDetail;
020    import org.kuali.kfs.fp.document.DisbursementVoucherDocument;
021    import org.kuali.kfs.sys.KFSKeyConstants;
022    import org.kuali.kfs.sys.KFSPropertyConstants;
023    import org.kuali.kfs.sys.context.SpringContext;
024    import org.kuali.kfs.sys.document.AccountingDocument;
025    import org.kuali.kfs.sys.document.validation.GenericValidation;
026    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
027    import org.kuali.rice.kns.bo.State;
028    import org.kuali.rice.kns.service.DataDictionaryService;
029    import org.kuali.rice.kns.service.StateService;
030    import org.kuali.rice.kns.util.GlobalVariables;
031    import org.kuali.rice.kns.util.MessageMap;
032    
033    public class DisbursementVoucherPayeeStateCodeValidation extends GenericValidation {
034        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementVoucherPayeeStateCodeValidation.class);
035    
036        private AccountingDocument accountingDocumentForValidation;
037    
038        /**
039         * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
040         */
041        public boolean validate(AttributedDocumentEvent event) {
042            LOG.debug("validate start");
043            boolean isValid = true;
044    
045            DisbursementVoucherDocument document = (DisbursementVoucherDocument) accountingDocumentForValidation;
046            DisbursementVoucherPayeeDetail payeeDetail = document.getDvPayeeDetail();
047    
048            MessageMap errors = GlobalVariables.getMessageMap();
049            errors.addToErrorPath(KFSPropertyConstants.DOCUMENT);
050    
051            DataDictionaryService dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
052            StateService stateService = SpringContext.getBean(StateService.class);
053    
054            String countryCode = payeeDetail.getDisbVchrPayeeCountryCode();
055            String stateCode = payeeDetail.getDisbVchrPayeeStateCode();
056            if (StringUtils.isNotBlank(stateCode) && StringUtils.isNotBlank(countryCode)) {
057                State state = stateService.getByPrimaryId(countryCode, stateCode);
058                if (state == null) {
059                    String label = dataDictionaryService.getAttributeLabel(DisbursementVoucherPayeeDetail.class, KFSPropertyConstants.DISB_VCHR_PAYEE_STATE_CODE);
060                    String propertyPath = KFSPropertyConstants.DV_PAYEE_DETAIL + "." + KFSPropertyConstants.DISB_VCHR_PAYEE_STATE_CODE;
061                    errors.putError(propertyPath, KFSKeyConstants.ERROR_EXISTENCE, label);
062                    isValid = false;
063                }
064            }
065            
066            countryCode = payeeDetail.getDisbVchrSpecialHandlingCountryCode();
067            stateCode = payeeDetail.getDisbVchrSpecialHandlingStateCode();
068            if (document.isDisbVchrSpecialHandlingCode() && StringUtils.isNotBlank(stateCode) && StringUtils.isNotBlank(countryCode)) {
069                State state = stateService.getByPrimaryId(countryCode, stateCode);
070                if (state == null) {
071                    String label = dataDictionaryService.getAttributeLabel(DisbursementVoucherPayeeDetail.class, KFSPropertyConstants.DISB_VCHR_SPECIAL_HANDLING_STATE_CODE);
072                    String propertyPath = KFSPropertyConstants.DV_PAYEE_DETAIL + "." + KFSPropertyConstants.DISB_VCHR_SPECIAL_HANDLING_STATE_CODE;
073                    errors.putError(propertyPath, KFSKeyConstants.ERROR_EXISTENCE, label);
074                    isValid = false;
075                }
076            }
077    
078            errors.removeFromErrorPath(KFSPropertyConstants.DOCUMENT);
079    
080            return isValid;
081        }
082    
083    
084        /**
085         * Gets the accountingDocumentForValidation attribute.
086         * 
087         * @return Returns the accountingDocumentForValidation.
088         */
089        public AccountingDocument getAccountingDocumentForValidation() {
090            return accountingDocumentForValidation;
091        }
092    
093    
094        /**
095         * Sets the accountingDocumentForValidation attribute value.
096         * 
097         * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
098         */
099        public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
100            this.accountingDocumentForValidation = accountingDocumentForValidation;
101        }
102    }