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 static org.kuali.ole.sys.OLEConstants.ACCOUNTING_LINE_ERRORS;
19  import static org.kuali.ole.sys.OLEKeyConstants.ERROR_DOCUMENT_PC_TRANSACTION_TOTAL_ACCTING_LINE_TOTAL_NOT_EQUAL;
20  
21  import java.util.List;
22  import java.util.Set;
23  
24  import org.kuali.ole.fp.businessobject.ProcurementCardTargetAccountingLine;
25  import org.kuali.ole.fp.businessobject.ProcurementCardTransactionDetail;
26  import org.kuali.ole.fp.document.ProcurementCardDocument;
27  import org.kuali.ole.sys.OLEConstants;
28  import org.kuali.ole.sys.businessobject.TargetAccountingLine;
29  import org.kuali.ole.sys.document.validation.GenericValidation;
30  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
31  import org.kuali.rice.core.api.util.type.KualiDecimal;
32  import org.kuali.rice.kew.api.WorkflowDocument;
33  import org.kuali.rice.krad.util.GlobalVariables;
34  
35  /**
36   * Validates that an accounting line does not have a capital object object code 
37   */
38  public class ProcurementCardAccountAccessibilityValidation extends GenericValidation {
39      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProcurementCardAccountAccessibilityValidation.class);
40  
41      protected ProcurementCardDocument accountingDocumentForValidation;
42  
43      /**
44       * Validates that an accounting line does not have a capital object object code
45       * <strong>Expects an accounting line as the first a parameter</strong>
46       * @see org.kuali.ole.sys.document.validation.Validation#validate(java.lang.Object[])
47       */
48      @Override
49      public boolean validate(AttributedDocumentEvent event) {
50          boolean isValid = false;
51          ProcurementCardDocument pcDocument = getAccountingDocumentForValidation();
52  
53          WorkflowDocument workflowDocument = pcDocument.getDocumentHeader().getWorkflowDocument();
54  
55          Set<String> activeNodes = workflowDocument.getCurrentNodeNames();
56          if (workflowDocument.isEnroute() && activeNodes.contains(OLEConstants.RouteLevelNames.ACCOUNT_REVIEW_FULL_EDIT)) {
57              isValid = true;
58          }
59          return isValid;
60      }
61  
62      /**
63       * This method validates the balance of the transaction given.  A procurement card transaction is in balance if 
64       * the total amount of the transaction equals the total of the target accounting lines corresponding to the transaction.
65       * 
66       * @param pcTransaction The transaction detail used to retrieve the procurement card transaction and target accounting 
67       *                      lines used to check for in balance.
68       * @return True if the amounts are equal and the transaction is in balance, false otherwise.
69       */
70      protected boolean isTransactionBalanceValid(ProcurementCardTransactionDetail pcTransactionDetail) {
71          boolean inBalance = true;
72          KualiDecimal transAmount = pcTransactionDetail.getTransactionTotalAmount();
73          List<ProcurementCardTargetAccountingLine> targetAcctingLines = pcTransactionDetail.getTargetAccountingLines();
74  
75          KualiDecimal targetLineTotal = KualiDecimal.ZERO;
76  
77          for (TargetAccountingLine targetLine : targetAcctingLines) {
78              targetLineTotal = targetLineTotal.add(targetLine.getAmount());
79          }
80  
81          // perform absolute value check because current system has situations where amounts may be opposite in sign
82          // This will no longer be necessary following completion of KULFDBCK-1290
83          inBalance = transAmount.abs().equals(targetLineTotal.abs());
84  
85          if (!inBalance) {
86              GlobalVariables.getMessageMap().putError(ACCOUNTING_LINE_ERRORS, ERROR_DOCUMENT_PC_TRANSACTION_TOTAL_ACCTING_LINE_TOTAL_NOT_EQUAL, new String[] { transAmount.toString(), targetLineTotal.toString() });
87          }
88  
89          return inBalance;
90      }
91  
92      public ProcurementCardDocument getAccountingDocumentForValidation() {
93          return accountingDocumentForValidation;
94      }
95  
96      public void setAccountingDocumentForValidation(ProcurementCardDocument accountingDocumentForValidation) {
97          this.accountingDocumentForValidation = accountingDocumentForValidation;
98      }
99  }