View Javadoc

1   /*
2    * Copyright 2008 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.fp.document.validation.impl;
17  
18  import java.util.Iterator;
19  import java.util.List;
20  
21  import org.kuali.ole.fp.businessobject.ProcurementCardTargetAccountingLine;
22  import org.kuali.ole.fp.businessobject.ProcurementCardTransactionDetail;
23  import org.kuali.ole.fp.document.ProcurementCardDocument;
24  import org.kuali.ole.sys.OLEPropertyConstants;
25  import org.kuali.ole.sys.businessobject.AccountingLine;
26  import org.kuali.ole.sys.document.AccountingDocument;
27  import org.kuali.rice.krad.util.GlobalVariables;
28  import org.kuali.rice.krad.util.MessageMap;
29  
30  /**
31   * This class...
32   */
33  public class ProcurementCardErrorPathUtil {
34      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProcurementCardErrorPathUtil.class);
35      
36      /**
37       * Fix the GlobalVariables.getMessageMap errorPath for how procurement card documents needs them in order 
38       * to properly display errors on the interface.  This is different from other financial document accounting 
39       * lines because instead procurement card documents have accounting lines insides of transactions. 
40       * Hence the error path is slightly different.
41       * 
42       * @param financialDocument The financial document the errors will be posted to.
43       * @param accountingLine The accounting line the error will be posted on.
44       */
45      public static void fixErrorPath(AccountingDocument financialDocument, AccountingLine accountingLine) {
46          List transactionEntries = ((ProcurementCardDocument) financialDocument).getTransactionEntries();
47          if (accountingLine.isTargetAccountingLine()) {
48              ProcurementCardTargetAccountingLine targetAccountingLineToBeFound = (ProcurementCardTargetAccountingLine) accountingLine;
49      
50              String errorPath = OLEPropertyConstants.DOCUMENT;
51      
52              // originally I used getFinancialDocumentTransactionLineNumber to determine the appropriate transaction, unfortunately
53              // this makes it dependent on the order of transactionEntries in FP_PRCRMNT_DOC_T. Hence we have two loops below.
54              boolean done = false;
55              int transactionLineIndex = 0;
56              for (Iterator iterTransactionEntries = transactionEntries.iterator(); !done && iterTransactionEntries.hasNext(); transactionLineIndex++) {
57                  ProcurementCardTransactionDetail transactionEntry = (ProcurementCardTransactionDetail) iterTransactionEntries.next();
58      
59                  // Loop over the transactionEntry to find the accountingLine's location. Keep another counter handy.
60                  int accountingLineCounter = 0;
61                  for (Iterator iterTargetAccountingLines = transactionEntry.getTargetAccountingLines().iterator(); !done && iterTargetAccountingLines.hasNext(); accountingLineCounter++) {
62                      ProcurementCardTargetAccountingLine targetAccountingLine = (ProcurementCardTargetAccountingLine) iterTargetAccountingLines.next();
63      
64                      if (targetAccountingLine.getSequenceNumber().equals(targetAccountingLineToBeFound.getSequenceNumber())) {
65                          // Found the item, capture error path, and set boolean (break isn't enough for 2 loops).
66                          errorPath = errorPath + "." + OLEPropertyConstants.TRANSACTION_ENTRIES + "[" + transactionLineIndex + "]." + OLEPropertyConstants.TARGET_ACCOUNTING_LINES + "[" + accountingLineCounter + "]";
67                          done = true;
68                      }
69                  }
70              }
71      
72              if (!done) {
73                  LOG.warn("fixErrorPath failed to locate item accountingLine=" + accountingLine.toString());
74              }
75      
76              // Clearing the error path is not a universal solution but should work for PCDO. In this case it's the only choice
77              // because KualiRuleService.applyRules will miss to remove the previous transaction added error path (only this
78              // method knows how it is called).
79              MessageMap messageMap = GlobalVariables.getMessageMap();
80              messageMap.clearErrorPath();
81              messageMap.addToErrorPath(errorPath);
82          }
83      }
84  }