View Javadoc
1   /*
2    * Copyright 2008-2009 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.purap.document.validation.impl;
17  
18  
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.ole.integration.cab.CapitalAssetBuilderModuleService;
21  import org.kuali.ole.module.purap.PurapConstants;
22  import org.kuali.ole.module.purap.PurapKeyConstants;
23  import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
24  import org.kuali.ole.module.purap.businessobject.PurApItem;
25  import org.kuali.ole.module.purap.document.PurchasingAccountsPayableDocument;
26  import org.kuali.ole.sys.OLEConstants;
27  import org.kuali.ole.sys.context.SpringContext;
28  import org.kuali.rice.core.api.config.property.ConfigurationService;
29  import org.kuali.rice.kns.rules.PromptBeforeValidationBase;
30  import org.kuali.rice.kns.util.KNSGlobalVariables;
31  import org.kuali.rice.kns.util.MessageList;
32  import org.kuali.rice.krad.document.Document;
33  import org.kuali.rice.krad.util.ErrorMessage;
34  import org.kuali.rice.krad.util.ObjectUtils;
35  
36  import java.util.List;
37  
38  public abstract class PurapDocumentPreRulesBase extends PromptBeforeValidationBase {
39  
40      public PurapDocumentPreRulesBase() {
41          super();
42      }
43  
44      @Override
45      public boolean doPrompts(Document document) {
46          PurchasingAccountsPayableDocument purapDocument = (PurchasingAccountsPayableDocument) document;
47  
48          boolean preRulesValid = true;
49  
50          //refresh accounts in each item....
51          List<PurApItem> items = purapDocument.getItems();
52  
53          for (PurApItem item : items) {
54              //refresh the accounts if they do exist...
55              for (PurApAccountingLine account : item.getSourceAccountingLines()) {
56                  account.refreshNonUpdateableReferences();
57              }
58          }
59  
60          if (StringUtils.isBlank(event.getQuestionContext()) || StringUtils.equals(question, PurapConstants.FIX_CAPITAL_ASSET_WARNINGS)) {
61              preRulesValid &= confirmFixCapitalAssetWarningConditions(purapDocument);
62          }
63  
64          return preRulesValid;
65      }
66  
67      public boolean confirmFixCapitalAssetWarningConditions(PurchasingAccountsPayableDocument purapDocument) {
68          boolean proceed = true;
69  
70          //check appropriate status first if not in an appropriate status return true
71          if (!checkCAMSWarningStatus(purapDocument)) {
72              return true;
73          }
74  
75          StringBuffer questionText = new StringBuffer();
76          if (StringUtils.isBlank(event.getQuestionContext())) {
77              if (!SpringContext.getBean(CapitalAssetBuilderModuleService.class).warningObjectLevelCapital(purapDocument)) {
78                  proceed &= false;
79                  questionText.append(SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(
80                          PurapKeyConstants.REQ_QUESTION_FIX_CAPITAL_ASSET_WARNINGS));
81  
82                  MessageList warnings = KNSGlobalVariables.getMessageList();
83                  if (!warnings.isEmpty()) {
84                      questionText.append("[p]");
85                      for (ErrorMessage warning : warnings) {
86                          // the following two lines should be used but org.kuali.rice.krad.util.ErrorMessage (line 83) has a bug
87                          //questionText.append(warning);
88                          //questionText.append("[br]");                        
89                          // so, to remove parenthesis in case no params exist   
90                          questionText.append(warning.getErrorKey());
91                          String[] params = warning.getMessageParameters();
92                          if (params != null && params.length > 0) {
93                              questionText.append("(");
94                              for (int i = 0; i < params.length; ++i) {
95                                  if (i > 0) {
96                                      questionText.append(", ");
97                                  }
98                                  questionText.append(params[i]);
99                              }
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 }