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.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import org.kuali.ole.fp.document.TransferOfFundsDocument;
23  import org.kuali.ole.fp.document.service.TransferOfFundsService;
24  import org.kuali.ole.sys.OLEKeyConstants;
25  import org.kuali.ole.sys.businessobject.AccountingLine;
26  import org.kuali.ole.sys.document.AccountingDocument;
27  import org.kuali.ole.sys.document.validation.GenericValidation;
28  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
29  import org.kuali.rice.core.api.util.type.KualiDecimal;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  
32  /**
33   * Transfer of Funds document validation which checks that mandatory and non-mandatory transfer totals are in balance.
34   */
35  public class TransferOfFundsTransferTotalsBalancedValidation extends GenericValidation {
36      private AccountingDocument accountingDocumentForValidation;
37      private TransferOfFundsService transferOfFundsService;
38  
39      /**
40       * This method checks the sum of all of the "From" accounting lines with mandatory transfer object codes against the sum of all
41       * of the "To" accounting lines with mandatory transfer object codes. In addition, it does the same, but for accounting lines
42       * with non-mandatory transfer object code. This is to enforce the rule that the document must balance within the object code
43       * object sub-type codes of mandatory transfers and non-mandatory transfers.
44       * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
45       */
46      public boolean validate(AttributedDocumentEvent event) {
47          TransferOfFundsDocument tofDoc = (TransferOfFundsDocument)accountingDocumentForValidation;
48          List lines = new ArrayList();
49  
50          lines.addAll(tofDoc.getSourceAccountingLines());
51          lines.addAll(tofDoc.getTargetAccountingLines());
52  
53          // sum the from lines.
54          KualiDecimal mandatoryTransferFromAmount = KualiDecimal.ZERO;
55          KualiDecimal nonMandatoryTransferFromAmount = KualiDecimal.ZERO;
56          KualiDecimal mandatoryTransferToAmount = KualiDecimal.ZERO;
57          KualiDecimal nonMandatoryTransferToAmount = KualiDecimal.ZERO;
58  
59          for (Iterator i = lines.iterator(); i.hasNext();) {
60              AccountingLine line = (AccountingLine) i.next();
61              String objectSubTypeCode = line.getObjectCode().getFinancialObjectSubTypeCode();
62  
63              if (transferOfFundsService.isNonMandatoryTransfersSubType(objectSubTypeCode)) {
64                  if (line.isSourceAccountingLine()) {
65                      nonMandatoryTransferFromAmount = nonMandatoryTransferFromAmount.add(line.getAmount());
66                  }
67                  else {
68                      nonMandatoryTransferToAmount = nonMandatoryTransferToAmount.add(line.getAmount());
69                  }
70              }
71              else if (transferOfFundsService.isMandatoryTransfersSubType(objectSubTypeCode)) {
72                  if (line.isSourceAccountingLine()) {
73                      mandatoryTransferFromAmount = mandatoryTransferFromAmount.add(line.getAmount());
74                  }
75                  else {
76                      mandatoryTransferToAmount = mandatoryTransferToAmount.add(line.getAmount());
77                  }
78              }
79          }
80  
81          // check that the amounts balance across mandatory transfers and non-mandatory transfers
82          boolean isValid = true;
83  
84          if (mandatoryTransferFromAmount.compareTo(mandatoryTransferToAmount) != 0) {
85              isValid = false;
86              GlobalVariables.getMessageMap().putError("document.sourceAccountingLines", OLEKeyConstants.ERROR_DOCUMENT_TOF_MANDATORY_TRANSFERS_DO_NOT_BALANCE);
87          }
88  
89          if (nonMandatoryTransferFromAmount.compareTo(nonMandatoryTransferToAmount) != 0) {
90              isValid = false;
91              GlobalVariables.getMessageMap().putError("document.sourceAccountingLines", OLEKeyConstants.ERROR_DOCUMENT_TOF_NON_MANDATORY_TRANSFERS_DO_NOT_BALANCE);
92          }
93  
94          return isValid;
95      }
96  
97      /**
98       * Gets the accountingDocumentForValidation attribute. 
99       * @return Returns the accountingDocumentForValidation.
100      */
101     public AccountingDocument getAccountingDocumentForValidation() {
102         return accountingDocumentForValidation;
103     }
104 
105     /**
106      * Sets the accountingDocumentForValidation attribute value.
107      * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
108      */
109     public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
110         this.accountingDocumentForValidation = accountingDocumentForValidation;
111     }
112 
113     /**
114      * Gets the transferOfFundsService attribute. 
115      * @return Returns the transferOfFundsService.
116      */
117     public TransferOfFundsService getTransferOfFundsService() {
118         return transferOfFundsService;
119     }
120 
121     /**
122      * Sets the transferOfFundsService attribute value.
123      * @param transferOfFundsService The transferOfFundsService to set.
124      */
125     public void setTransferOfFundsService(TransferOfFundsService transferOfFundsService) {
126         this.transferOfFundsService = transferOfFundsService;
127     }
128 }