001/*
002 * Copyright 2008 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.sys.document.validation.impl;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.ole.sys.OLEKeyConstants;
020import org.kuali.ole.sys.OLEPropertyConstants;
021import org.kuali.ole.sys.businessobject.Bank;
022import org.kuali.ole.sys.context.SpringContext;
023import org.kuali.ole.sys.service.BankService;
024import org.kuali.rice.kns.service.DataDictionaryService;
025import org.kuali.rice.krad.document.Document;
026import org.kuali.rice.krad.util.GlobalVariables;
027import org.kuali.rice.krad.util.ObjectUtils;
028
029/**
030 * Performs bank code validation.
031 */
032public class BankCodeValidation {
033    protected static volatile DataDictionaryService dataDictionaryService;
034    protected static volatile BankService bankService;
035
036    /**
037     * Performs required, exists, and active validation of bank code. Also validates bank for deposit or disbursement indicator if
038     * requested. .
039     * 
040     * @param bankCode value to validate
041     * @param bankCodeProperty property to associate errors with
042     * @param requireDeposit true if the bank code should support deposits
043     * @param requireDisbursement true if the bank code should support disbursements
044     * @return true if bank code passes all validations, false if any fail
045     */
046    public static boolean validate(String bankCode, String bankCodeProperty, boolean requireDeposit, boolean requireDisbursement) {
047        String bankCodeLabel = getDataDictionaryService().getAttributeLabel(Bank.class, OLEPropertyConstants.BANK_CODE);
048
049        // if bank specification is not enabled, no need to validate bank code
050        if (!getBankService().isBankSpecificationEnabled()) {
051            return true;
052        }
053
054        // required check
055        if (StringUtils.isBlank(bankCode)) {
056            GlobalVariables.getMessageMap().putError(bankCodeProperty, OLEKeyConstants.ERROR_REQUIRED, bankCodeLabel);
057
058            return false;
059        }
060
061        Bank bank = getBankService().getByPrimaryId(bankCode);
062        
063        if (ObjectUtils.isNull(bank)) {
064            GlobalVariables.getMessageMap().putError(bankCodeProperty, OLEKeyConstants.ERROR_DOCUMENT_BANKACCMAINT_INVALID_BANK);
065            return false;
066        }
067        
068        // validate deposit
069        if (requireDeposit && !bank.isBankDepositIndicator()) {
070            GlobalVariables.getMessageMap().putError(bankCodeProperty, OLEKeyConstants.Bank.ERROR_DEPOSIT_NOT_SUPPORTED);
071
072            return false;
073        }
074
075        // validate disbursement
076        if (requireDisbursement && !bank.isBankDisbursementIndicator()) {
077            GlobalVariables.getMessageMap().putError(bankCodeProperty, OLEKeyConstants.Bank.ERROR_DISBURSEMENT_NOT_SUPPORTED);
078
079            return false;
080        }
081
082        return true;
083    }
084
085    /**
086     * Performs required, exists, and active validation of bank code. Also validates bank for deposit or disbursement indicator if
087     * requested.
088     *
089     * @param document the document that is being validated
090     * @param bankCode value to validate
091     * @param bankCodeProperty property to associate errors with
092     * @param requireDeposit true if the bank code should support deposits
093     * @param requireDisbursement true if the bank code should support disbursements
094     * @return true if bank code passes all validations, false if any fail
095     */
096    public static boolean validate(Document document, String bankCode, String bankCodeProperty, boolean requireDeposit, boolean requireDisbursement) {
097        if (document != null && !getBankService().isBankSpecificationEnabledForDocument(document.getClass())) {
098            return true;
099        }
100        return BankCodeValidation.validate(bankCode, bankCodeProperty, requireDeposit, requireDisbursement);
101    }
102
103    /**
104     * @return the default implementatino of the DataDictionaryService
105     */
106    protected static DataDictionaryService getDataDictionaryService() {
107        if (dataDictionaryService == null) {
108            dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
109        }
110        return dataDictionaryService;
111    }
112
113    /**
114     * @return the default implementation of the BankService
115     */
116    protected static BankService getBankService() {
117        if (bankService == null) {
118            bankService = SpringContext.getBean(BankService.class);
119        }
120        return bankService;
121    }
122}