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.sys.document.validation.impl;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.kfs.sys.KFSKeyConstants;
020    import org.kuali.kfs.sys.KFSPropertyConstants;
021    import org.kuali.kfs.sys.businessobject.Bank;
022    import org.kuali.kfs.sys.context.SpringContext;
023    import org.kuali.kfs.sys.service.BankService;
024    import org.kuali.rice.kns.service.DataDictionaryService;
025    import org.kuali.rice.kns.util.GlobalVariables;
026    import org.kuali.rice.kns.util.ObjectUtils;
027    
028    /**
029     * Performs bank code validation.
030     */
031    public class BankCodeValidation {
032    
033        /**
034         * Performs required, exists, and active validation of bank code. Also validates bank for deposit or disbursement indicator if
035         * requested. .
036         * 
037         * @param bankCode value to validate
038         * @param bankCodeProperty property to associate errors with
039         * @param requireDeposit true if the bank code should support deposits
040         * @param requireDisbursement true if the bank code should support disbursements
041         * @return true if bank code passes all validations, false if any fail
042         */
043        public static boolean validate(String bankCode, String bankCodeProperty, boolean requireDeposit, boolean requireDisbursement) {
044            String bankCodeLabel = SpringContext.getBean(DataDictionaryService.class).getAttributeLabel(Bank.class, KFSPropertyConstants.BANK_CODE);
045    
046            // if bank specification is not enabled, no need to validate bank code
047            if (!SpringContext.getBean(BankService.class).isBankSpecificationEnabled()) {
048                return true;
049            }
050    
051            // required check
052            if (StringUtils.isBlank(bankCode)) {
053                GlobalVariables.getMessageMap().putError(bankCodeProperty, KFSKeyConstants.ERROR_REQUIRED, bankCodeLabel);
054    
055                return false;
056            }
057    
058            Bank bank = SpringContext.getBean(BankService.class).getByPrimaryId(bankCode);
059            
060            if (ObjectUtils.isNull(bank)) {
061                GlobalVariables.getMessageMap().putError(bankCodeProperty, KFSKeyConstants.ERROR_DOCUMENT_BANKACCMAINT_INVALID_BANK);
062                return false;
063            }
064            
065            // validate deposit
066            if (requireDeposit && !bank.isBankDepositIndicator()) {
067                GlobalVariables.getMessageMap().putError(bankCodeProperty, KFSKeyConstants.Bank.ERROR_DEPOSIT_NOT_SUPPORTED);
068    
069                return false;
070            }
071    
072            // validate disbursement
073            if (requireDisbursement && !bank.isBankDisbursementIndicator()) {
074                GlobalVariables.getMessageMap().putError(bankCodeProperty, KFSKeyConstants.Bank.ERROR_DISBURSEMENT_NOT_SUPPORTED);
075    
076                return false;
077            }
078    
079            return true;
080        }
081    
082    }