001/*
002 * Copyright 2009 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 */
016package org.kuali.ole.fp.document.validation.impl;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.ole.fp.businessobject.DisbursementVoucherPayeeDetail;
020import org.kuali.ole.fp.document.DisbursementVoucherDocument;
021import org.kuali.ole.sys.OLEKeyConstants;
022import org.kuali.ole.sys.OLEPropertyConstants;
023import org.kuali.ole.sys.context.SpringContext;
024import org.kuali.ole.sys.document.AccountingDocument;
025import org.kuali.ole.sys.document.validation.GenericValidation;
026import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
027import org.kuali.rice.kns.service.DataDictionaryService;
028import org.kuali.rice.krad.util.GlobalVariables;
029import org.kuali.rice.krad.util.MessageMap;
030import org.kuali.rice.location.api.state.State;
031import org.kuali.rice.location.api.state.StateService;
032
033public 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.ole.sys.document.validation.Validation#validate(org.kuali.ole.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(OLEPropertyConstants.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.getState(countryCode, stateCode);
058            if (state == null) {
059                String label = dataDictionaryService.getAttributeLabel(DisbursementVoucherPayeeDetail.class, OLEPropertyConstants.DISB_VCHR_PAYEE_STATE_CODE);
060                String propertyPath = OLEPropertyConstants.DV_PAYEE_DETAIL + "." + OLEPropertyConstants.DISB_VCHR_PAYEE_STATE_CODE;
061                errors.putError(propertyPath, OLEKeyConstants.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.getState(countryCode, stateCode);
070            if (state == null) {
071                String label = dataDictionaryService.getAttributeLabel(DisbursementVoucherPayeeDetail.class, OLEPropertyConstants.DISB_VCHR_SPECIAL_HANDLING_STATE_CODE);
072                String propertyPath = OLEPropertyConstants.DV_PAYEE_DETAIL + "." + OLEPropertyConstants.DISB_VCHR_SPECIAL_HANDLING_STATE_CODE;
073                errors.putError(propertyPath, OLEKeyConstants.ERROR_EXISTENCE, label);
074                isValid = false;
075            }
076        }
077
078        errors.removeFromErrorPath(OLEPropertyConstants.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}