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.module.purap.document.validation.impl;
017
018
019import org.apache.commons.lang.StringUtils;
020import org.kuali.ole.integration.cab.CapitalAssetBuilderModuleService;
021import org.kuali.ole.module.purap.PurapConstants;
022import org.kuali.ole.module.purap.PurapKeyConstants;
023import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
024import org.kuali.ole.module.purap.businessobject.PurApItem;
025import org.kuali.ole.module.purap.document.PurchasingAccountsPayableDocument;
026import org.kuali.ole.sys.OLEConstants;
027import org.kuali.ole.sys.context.SpringContext;
028import org.kuali.rice.core.api.config.property.ConfigurationService;
029import org.kuali.rice.kns.rules.PromptBeforeValidationBase;
030import org.kuali.rice.kns.util.KNSGlobalVariables;
031import org.kuali.rice.kns.util.MessageList;
032import org.kuali.rice.krad.document.Document;
033import org.kuali.rice.krad.util.ErrorMessage;
034import org.kuali.rice.krad.util.ObjectUtils;
035
036import java.util.List;
037
038public abstract class PurapDocumentPreRulesBase extends PromptBeforeValidationBase {
039
040    public PurapDocumentPreRulesBase() {
041        super();
042    }
043
044    @Override
045    public boolean doPrompts(Document document) {
046        PurchasingAccountsPayableDocument purapDocument = (PurchasingAccountsPayableDocument) document;
047
048        boolean preRulesValid = true;
049
050        //refresh accounts in each item....
051        List<PurApItem> items = purapDocument.getItems();
052
053        for (PurApItem item : items) {
054            //refresh the accounts if they do exist...
055            for (PurApAccountingLine account : item.getSourceAccountingLines()) {
056                account.refreshNonUpdateableReferences();
057            }
058        }
059
060        if (StringUtils.isBlank(event.getQuestionContext()) || StringUtils.equals(question, PurapConstants.FIX_CAPITAL_ASSET_WARNINGS)) {
061            preRulesValid &= confirmFixCapitalAssetWarningConditions(purapDocument);
062        }
063
064        return preRulesValid;
065    }
066
067    public boolean confirmFixCapitalAssetWarningConditions(PurchasingAccountsPayableDocument purapDocument) {
068        boolean proceed = true;
069
070        //check appropriate status first if not in an appropriate status return true
071        if (!checkCAMSWarningStatus(purapDocument)) {
072            return true;
073        }
074
075        StringBuffer questionText = new StringBuffer();
076        if (StringUtils.isBlank(event.getQuestionContext())) {
077            if (!SpringContext.getBean(CapitalAssetBuilderModuleService.class).warningObjectLevelCapital(purapDocument)) {
078                proceed &= false;
079                questionText.append(SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(
080                        PurapKeyConstants.REQ_QUESTION_FIX_CAPITAL_ASSET_WARNINGS));
081
082                MessageList warnings = KNSGlobalVariables.getMessageList();
083                if (!warnings.isEmpty()) {
084                    questionText.append("[p]");
085                    for (ErrorMessage warning : warnings) {
086                        // the following two lines should be used but org.kuali.rice.krad.util.ErrorMessage (line 83) has a bug
087                        //questionText.append(warning);
088                        //questionText.append("[br]");                        
089                        // so, to remove parenthesis in case no params exist   
090                        questionText.append(warning.getErrorKey());
091                        String[] params = warning.getMessageParameters();
092                        if (params != null && params.length > 0) {
093                            questionText.append("(");
094                            for (int i = 0; i < params.length; ++i) {
095                                if (i > 0) {
096                                    questionText.append(", ");
097                                }
098                                questionText.append(params[i]);
099                            }
100                            questionText.append(")");
101                        }
102                    }
103                    questionText.append("[/p]");
104                }
105            }
106        }
107
108        if (!proceed || ((ObjectUtils.isNotNull(question)) && (question.equals(PurapConstants.FIX_CAPITAL_ASSET_WARNINGS)))) {
109            proceed = askOrAnalyzeYesNoQuestion(PurapConstants.FIX_CAPITAL_ASSET_WARNINGS, questionText.toString());
110        }
111        // Set a marker to record that this method has been used.
112        event.setQuestionContext(PurapConstants.FIX_CAPITAL_ASSET_WARNINGS);
113        event.setActionForwardName(OLEConstants.MAPPING_BASIC);
114        if (proceed) {
115            KNSGlobalVariables.getMessageList().clear();
116        }
117
118        return proceed;
119    }
120
121    protected abstract boolean checkCAMSWarningStatus(PurchasingAccountsPayableDocument purapDocument);
122
123}