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.fp.document.validation.impl;
017
018 import java.util.Iterator;
019 import java.util.List;
020
021 import org.kuali.kfs.fp.businessobject.ProcurementCardTargetAccountingLine;
022 import org.kuali.kfs.fp.businessobject.ProcurementCardTransactionDetail;
023 import org.kuali.kfs.fp.document.ProcurementCardDocument;
024 import org.kuali.kfs.sys.KFSPropertyConstants;
025 import org.kuali.kfs.sys.businessobject.AccountingLine;
026 import org.kuali.kfs.sys.document.AccountingDocument;
027 import org.kuali.rice.kns.util.GlobalVariables;
028 import org.kuali.rice.kns.util.MessageMap;
029
030 /**
031 * This class...
032 */
033 public class ProcurementCardErrorPathUtil {
034 protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProcurementCardErrorPathUtil.class);
035
036 /**
037 * Fix the GlobalVariables.getErrorMap errorPath for how procurement card documents needs them in order
038 * to properly display errors on the interface. This is different from other financial document accounting
039 * lines because instead procurement card documents have accounting lines insides of transactions.
040 * Hence the error path is slightly different.
041 *
042 * @param financialDocument The financial document the errors will be posted to.
043 * @param accountingLine The accounting line the error will be posted on.
044 */
045 public static void fixErrorPath(AccountingDocument financialDocument, AccountingLine accountingLine) {
046 List transactionEntries = ((ProcurementCardDocument) financialDocument).getTransactionEntries();
047 if (accountingLine.isTargetAccountingLine()) {
048 ProcurementCardTargetAccountingLine targetAccountingLineToBeFound = (ProcurementCardTargetAccountingLine) accountingLine;
049
050 String errorPath = KFSPropertyConstants.DOCUMENT;
051
052 // originally I used getFinancialDocumentTransactionLineNumber to determine the appropriate transaction, unfortunately
053 // this makes it dependent on the order of transactionEntries in FP_PRCRMNT_DOC_T. Hence we have two loops below.
054 boolean done = false;
055 int transactionLineIndex = 0;
056 for (Iterator iterTransactionEntries = transactionEntries.iterator(); !done && iterTransactionEntries.hasNext(); transactionLineIndex++) {
057 ProcurementCardTransactionDetail transactionEntry = (ProcurementCardTransactionDetail) iterTransactionEntries.next();
058
059 // Loop over the transactionEntry to find the accountingLine's location. Keep another counter handy.
060 int accountingLineCounter = 0;
061 for (Iterator iterTargetAccountingLines = transactionEntry.getTargetAccountingLines().iterator(); !done && iterTargetAccountingLines.hasNext(); accountingLineCounter++) {
062 ProcurementCardTargetAccountingLine targetAccountingLine = (ProcurementCardTargetAccountingLine) iterTargetAccountingLines.next();
063
064 if (targetAccountingLine.getSequenceNumber().equals(targetAccountingLineToBeFound.getSequenceNumber())) {
065 // Found the item, capture error path, and set boolean (break isn't enough for 2 loops).
066 errorPath = errorPath + "." + KFSPropertyConstants.TRANSACTION_ENTRIES + "[" + transactionLineIndex + "]." + KFSPropertyConstants.TARGET_ACCOUNTING_LINES + "[" + accountingLineCounter + "]";
067 done = true;
068 }
069 }
070 }
071
072 if (!done) {
073 LOG.warn("fixErrorPath failed to locate item accountingLine=" + accountingLine.toString());
074 }
075
076 // Clearing the error path is not a universal solution but should work for PCDO. In this case it's the only choice
077 // because KualiRuleService.applyRules will miss to remove the previous transaction added error path (only this
078 // method knows how it is called).
079 MessageMap errorMap = GlobalVariables.getMessageMap();
080 errorMap.clearErrorPath();
081 errorMap.addToErrorPath(errorPath);
082 }
083 }
084 }