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.document.NonCheckDisbursementDocument;
021import org.kuali.ole.sys.OLEConstants;
022import org.kuali.ole.sys.OLEKeyConstants;
023import org.kuali.ole.sys.OLEPropertyConstants;
024import org.kuali.ole.sys.businessobject.Bank;
025import org.kuali.ole.sys.context.SpringContext;
026import org.kuali.ole.sys.service.BankService;
027import org.kuali.rice.core.api.config.property.ConfigurationService;
028import org.kuali.rice.kns.rules.PromptBeforeValidationBase;
029import org.kuali.rice.krad.document.Document;
030
031/**
032 * Performs warning checks and prompts for NonCheckDisbursement.
033 */
034public class NonCheckDisbursementDocumentPreRules extends PromptBeforeValidationBase {
035
036    @Override
037    public boolean doPrompts(Document document) {
038        boolean preRulesOK = true;
039        
040        NonCheckDisbursementDocument nonCheckDocument = (NonCheckDisbursementDocument) document;
041        
042        preRulesOK &= checkBankCodeActive(nonCheckDocument);
043        
044        return preRulesOK;
045    }
046    
047    /**
048     * If bank specification is enabled, prompts user to use the continuation bank code when the
049     * given bank code is inactive
050     * 
051     * @param nonCheckDocument document containing bank code
052     * @return true
053     */
054    protected boolean checkBankCodeActive(NonCheckDisbursementDocument nonCheckDocument) {
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        // refresh bank reference so continuation bank can be checked for active status
063        nonCheckDocument.refreshReferenceObject(OLEPropertyConstants.BANK);
064        Bank bank = nonCheckDocument.getBank();
065
066        // if bank is inactive and continuation is active, prompt user to use continuation bank
067        if (bank != null && !bank.isActive() && bank.getContinuationBank().isActive()) {
068            String questionText = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEKeyConstants.QUESTION_BANK_INACTIVE);
069            questionText = MessageFormat.format(questionText, nonCheckDocument.getFinancialDocumentBankCode(), bank.getContinuationBankCode());
070
071            boolean useContinuation = super.askOrAnalyzeYesNoQuestion(OLEConstants.USE_CONTINUATION_BANK_QUESTION, questionText);
072            if (useContinuation) {
073                nonCheckDocument.setFinancialDocumentBankCode(bank.getContinuationBankCode());
074            }
075        }
076
077        return continueRules;
078    }
079
080}