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