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 java.text.MessageFormat;
019    
020    import org.kuali.kfs.fp.businessobject.AdvanceDepositDetail;
021    import org.kuali.kfs.fp.businessobject.Deposit;
022    import org.kuali.kfs.fp.document.AdvanceDepositDocument;
023    import org.kuali.kfs.fp.document.CashManagementDocument;
024    import org.kuali.kfs.fp.document.DisbursementVoucherDocument;
025    import org.kuali.kfs.fp.document.NonCheckDisbursementDocument;
026    import org.kuali.kfs.sys.KFSConstants;
027    import org.kuali.kfs.sys.KFSKeyConstants;
028    import org.kuali.kfs.sys.KFSPropertyConstants;
029    import org.kuali.kfs.sys.businessobject.Bank;
030    import org.kuali.kfs.sys.context.SpringContext;
031    import org.kuali.kfs.sys.service.BankService;
032    import org.kuali.rice.kns.document.Document;
033    import org.kuali.rice.kns.rules.PromptBeforeValidationBase;
034    import org.kuali.rice.kns.service.KualiConfigurationService;
035    
036    /**
037     * Performs warning checks and prompts for CashManagement.
038     */
039    public class CashManagementDocumentPreRules extends PromptBeforeValidationBase {
040    
041        @Override
042        public boolean doPrompts(Document document) {
043            boolean preRulesOK = true;
044    
045            CashManagementDocument cmDocument = (CashManagementDocument) document;
046    
047            preRulesOK &= checkBankCodeActive(cmDocument);
048    
049            return preRulesOK;
050        }
051    
052        /**
053         * If bank specification is enabled, prompts user to use the continuation bank code when the given bank code is inactive
054         * 
055         * @param cmDocument document containing bank code
056         * @return true
057         */
058        protected boolean checkBankCodeActive(CashManagementDocument cmDocument) {
059            boolean continueRules = true;
060    
061            // if bank specification is not enabled, no need to validate bank
062            if (!SpringContext.getBean(BankService.class).isBankSpecificationEnabled()) {
063                return continueRules;
064            }
065    
066            int questionIndex = 0;
067            for (Deposit deposit : cmDocument.getDeposits()) {
068                questionIndex++;
069    
070                // refresh bank reference so continuation bank can be checked for active status
071                deposit.refreshReferenceObject(KFSPropertyConstants.BANK);
072                Bank bank = deposit.getBank();
073    
074                // if bank is inactive and continuation is active, prompt user to use continuation bank
075                if (bank != null && !bank.isActive() && bank.getContinuationBank().isActive()) {
076                    String questionText = SpringContext.getBean(KualiConfigurationService.class).getPropertyString(KFSKeyConstants.QUESTION_BANK_INACTIVE);
077                    questionText = MessageFormat.format(questionText, deposit.getDepositBankCode(), bank.getContinuationBankCode());
078    
079                    boolean useContinuation = super.askOrAnalyzeYesNoQuestion(KFSConstants.USE_CONTINUATION_BANK_QUESTION + questionIndex, questionText);
080                    if (useContinuation) {
081                        deposit.setDepositBankCode(bank.getContinuationBankCode());
082                    }
083                }
084            }
085    
086            return continueRules;
087        }
088    
089    }