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.Map;
20  
21  import org.kuali.ole.fp.document.BudgetAdjustmentDocument;
22  import org.kuali.ole.sys.OLEConstants;
23  import org.kuali.ole.sys.OLEKeyConstants;
24  import org.kuali.ole.sys.document.validation.GenericValidation;
25  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
26  import org.kuali.rice.core.api.util.type.KualiDecimal;
27  import org.kuali.rice.core.api.util.type.KualiInteger;
28  import org.kuali.rice.krad.util.GlobalVariables;
29  import org.kuali.rice.krad.util.MessageMap;
30  
31  /**
32   * A validation which checks if a Budget Adjustment document is balanced before heading to routing
33   */
34  public class BudgetAdjustmentDocumentBalancedValidation extends GenericValidation {
35      public BudgetAdjustmentDocument accountingDocumentForValidation;
36  
37      /**
38       * Validates that the budget adjustment document is balanced, based on whether the source base amount equals the target base amount
39       * and that the income stream balance map has no non-zero values.
40       * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
41       */
42      public boolean validate(AttributedDocumentEvent event) {
43          MessageMap errors = GlobalVariables.getMessageMap();
44  
45          boolean balanced = true;
46  
47          // check base amounts are equal
48          //KFSMI-3036
49          KualiInteger sourceBaseBudgetTotal = getAccountingDocumentForValidation().getSourceBaseBudgetIncomeTotal().subtract( getAccountingDocumentForValidation().getSourceBaseBudgetExpenseTotal());
50          KualiInteger targetBaseBudgetTotal = getAccountingDocumentForValidation().getTargetBaseBudgetIncomeTotal().subtract( getAccountingDocumentForValidation().getTargetBaseBudgetExpenseTotal());
51          if (sourceBaseBudgetTotal.compareTo(targetBaseBudgetTotal) != 0) {
52              GlobalVariables.getMessageMap().putError(OLEConstants.ACCOUNTING_LINE_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_BA_BASE_AMOUNTS_BALANCED);
53              balanced = false;
54          }
55  
56          // check current amounts balance, income stream balance Map should add to 0
57          Map incomeStreamMap = getAccountingDocumentForValidation().buildIncomeStreamBalanceMapForDocumentBalance();
58          KualiDecimal totalCurrentAmount = new KualiDecimal(0);
59          for (Iterator iter = incomeStreamMap.values().iterator(); iter.hasNext();) {
60              KualiDecimal streamAmount = (KualiDecimal) iter.next();
61              totalCurrentAmount = totalCurrentAmount.add(streamAmount);
62          }
63  
64          if (totalCurrentAmount.isNonZero()) {
65              GlobalVariables.getMessageMap().putError(OLEConstants.ACCOUNTING_LINE_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_BA_CURRENT_AMOUNTS_BALANCED);
66              balanced = false;
67          }
68  
69          return balanced;
70      }
71  
72      /**
73       * Gets the accountingDocumentForValidation attribute. 
74       * @return Returns the accountingDocumentForValidation.
75       */
76      public BudgetAdjustmentDocument getAccountingDocumentForValidation() {
77          return accountingDocumentForValidation;
78      }
79  
80      /**
81       * Sets the accountingDocumentForValidation attribute value.
82       * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
83       */
84      public void setAccountingDocumentForValidation(BudgetAdjustmentDocument accountingDocumentForValidation) {
85          this.accountingDocumentForValidation = accountingDocumentForValidation;
86      }
87  }