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