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  import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
19  import org.kuali.ole.module.purap.businessobject.PurApItem;
20  import org.kuali.ole.module.purap.document.PurchasingAccountsPayableDocument;
21  import org.kuali.ole.sys.OLEPropertyConstants;
22  import org.kuali.ole.sys.businessobject.AccountingLine;
23  import org.kuali.ole.sys.document.AccountingDocument;
24  import org.kuali.rice.krad.util.GlobalVariables;
25  import org.kuali.rice.krad.util.MessageMap;
26  
27  import java.util.Iterator;
28  import java.util.List;
29  
30  /**
31   * Utility class to set error path for Payment Request validations
32   */
33  public class PurchasingAccountsPayableErrorPathUtil {
34      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PurchasingAccountsPayableErrorPathUtil.class);
35  
36      /**
37       * Fix the GlobalVariables.getMessageMap errorPath for how payment request documents needs them in order to properly display
38       * errors on the interface. This is different from other financial document accounting lines because instead payment request
39       * documents have accounting lines insides of items. Hence the error path is slightly different.
40       *
41       * @param financialDocument The financial document the errors will be posted to.
42       * @param accountingLine    The accounting line the error will be posted on.
43       */
44      public static void fixErrorPath(AccountingDocument financialDocument, AccountingLine accountingLine) {
45          List<PurApItem> items = ((PurchasingAccountsPayableDocument) financialDocument).getItems();
46  
47          if (accountingLine.isSourceAccountingLine()) {
48              PurApAccountingLine targetAccountingLineToBeFound = (PurApAccountingLine) accountingLine;
49  
50              String errorPath = OLEPropertyConstants.DOCUMENT;
51  
52              boolean done = false;
53              int itemLineIndex = 0;
54              for (Iterator iterItemEntries = items.iterator(); !done && iterItemEntries.hasNext(); itemLineIndex++) {
55                  PurApItem item = (PurApItem) iterItemEntries.next();
56  
57                  // Loop over the item to find the accountingLine's location. Keep another counter handy.
58                  int accountingLineCounter = 0;
59                  for (Iterator iterSourceAccountingLines = item.getSourceAccountingLines().iterator(); !done && iterSourceAccountingLines.hasNext(); accountingLineCounter++) {
60                      PurApAccountingLine sourceAccountingLine = (PurApAccountingLine) iterSourceAccountingLines.next();
61  
62                      // Only targetAccountingLineToBeFound has sequenceNumber always not null. We should put it in the preceding place of this comparison. Otherwise, it may run into NPE.
63                      if (targetAccountingLineToBeFound.getSequenceNumber().equals(sourceAccountingLine.getSequenceNumber())) {
64                          // Found the item, capture error path, and set boolean (break isn't enough for 2 loops).
65                          errorPath = errorPath + "." + OLEPropertyConstants.ITEM + "[" + itemLineIndex + "]." + OLEPropertyConstants.SOURCE_ACCOUNTING_LINES + "[" + accountingLineCounter + "]";
66                          done = true;
67                      }
68                  }
69              }
70  
71              if (!done) {
72                  LOG.warn("fixErrorPath failed to locate item accountingLine=" + accountingLine.toString());
73              }
74  
75              // Clearing the error path is not a universal solution but should work. In this case it's the only choice
76              // because KualiRuleService.applyRules will miss to remove the previous transaction added error path (only this
77              // method knows how it is called).
78              MessageMap errorMap = GlobalVariables.getMessageMap();
79              errorMap.clearErrorPath();
80              errorMap.addToErrorPath(errorPath);
81          }
82      }
83  }