001/*
002 * Copyright 2013 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 1.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/ecl1.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.select.document.validation.impl;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.ole.module.purap.document.RequisitionDocument;
020import org.kuali.ole.module.purap.document.validation.impl.RequisitionDocumentPreRules;
021import org.kuali.ole.select.businessobject.OleSufficientFundCheck;
022import org.kuali.ole.select.document.OleRequisitionDocument;
023import org.kuali.ole.select.document.service.OleRequisitionDocumentService;
024import org.kuali.ole.sys.OLEConstants;
025import org.kuali.ole.sys.OLEPropertyConstants;
026import org.kuali.ole.sys.businessobject.SourceAccountingLine;
027import org.kuali.ole.sys.context.SpringContext;
028import org.kuali.rice.core.api.config.property.ConfigurationService;
029import org.kuali.rice.krad.document.Document;
030import org.kuali.rice.krad.service.BusinessObjectService;
031
032import java.util.HashMap;
033import java.util.List;
034import java.util.Map;
035
036public class OleRequisitionDocumentPreRules extends RequisitionDocumentPreRules {
037    /**
038     * Default Constructor
039     */
040    public OleRequisitionDocumentPreRules() {
041        super();
042    }
043
044    /**
045     * Main hook point to perform rules check.
046     *
047     * @see org.kuali.rice.kns.rules.PromptBeforeValidationBase#doRules(org.kuali.rice.krad.document.Document)
048     */
049    @Override
050    public boolean doPrompts(Document document) {
051        boolean preRulesOK = true;
052        OleRequisitionDocument reqDoc = null;
053        if (document instanceof RequisitionDocument) {
054            reqDoc = (OleRequisitionDocument) document;
055        }
056        OleRequisitionDocumentService oleRequisitionDocumentService = (OleRequisitionDocumentService) SpringContext
057                .getBean("oleRequisitionDocumentService");
058        StringBuilder accountNumbers = new StringBuilder();
059        if (oleRequisitionDocumentService != null && reqDoc != null) {
060            List<SourceAccountingLine> sourceAccountingLineList = reqDoc.getSourceAccountingLines();
061            for (SourceAccountingLine accLine : sourceAccountingLineList) {
062                String notificationOption = null;
063                boolean sufficientFundCheck;
064                Map<String, Object> key = new HashMap<String, Object>();
065                String chartCode = accLine.getChartOfAccountsCode();
066                String accNo = accLine.getAccountNumber();
067                String objectCd = accLine.getFinancialObjectCode();
068                key.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartCode);
069                key.put(OLEPropertyConstants.ACCOUNT_NUMBER, accNo);
070                OleSufficientFundCheck account = SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(
071                        OleSufficientFundCheck.class, key);
072                if (account != null) {
073                    notificationOption = account.getNotificationOption();
074                }
075                if (notificationOption != null && notificationOption.equals(OLEPropertyConstants.WARNING_MSG)
076                        && oleRequisitionDocumentService.hasSufficientFundsOnRequisition(accLine)) {
077                    accountNumbers.append(accLine.getAccountNumber());
078                }
079            }
080        }
081        if (accountNumbers.length() > 0) {
082            preRulesOK = askForConfirmation(OLEConstants.SufficientFundCheck.REQUISITION_SFC_CHECKING_STRING,
083                    OLEConstants.SufficientFundCheck.REQUISITION_SFC_CHECKING, accountNumbers);
084        }
085        preRulesOK &= super.doPrompts(document);
086        return preRulesOK;
087    }
088
089    /**
090     * Prompts user to confirm with a Yes or No to a question being asked.
091     *
092     * @param questionType    - type of question
093     * @param messageConstant - key to retrieve message
094     * @return - true if overriding, false otherwise
095     */
096    protected boolean askForConfirmation(String questionType, String messageConstant, StringBuilder accountNumbers) {
097
098        String questionText = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(
099                messageConstant);
100        if (questionText.contains("{")) {
101            questionText = prepareQuestionText(questionType, questionText, accountNumbers);
102        }
103
104        boolean confirmOverride = super.askOrAnalyzeYesNoQuestion(questionType, questionText);
105
106        if (!confirmOverride) {
107            event.setActionForwardName(OLEConstants.MAPPING_BASIC);
108            return false;
109        }
110        return true;
111    }
112
113    /**
114     * Creates the actual text of the question, replacing place holders like pay date threshold with an actual constant value.
115     *
116     * @param questionType - type of question
117     * @param questionText - actual text of question pulled from resource file
118     * @return - question text with place holders replaced
119     */
120    protected String prepareQuestionText(String questionType, String questionText, StringBuilder accountNumbers) {
121        if (StringUtils.equals(questionType, OLEConstants.SufficientFundCheck.REQUISITION_SFC_CHECKING_STRING)) {
122            questionText = StringUtils.replace(questionText, "{0}", accountNumbers.toString());
123        }
124        return questionText;
125    }
126
127}