1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
34  
35  public class TransferOfFundsTransferTotalsBalancedValidation extends GenericValidation {
36      private AccountingDocument accountingDocumentForValidation;
37      private TransferOfFundsService transferOfFundsService;
38  
39      
40  
41  
42  
43  
44  
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          
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          
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  
99  
100 
101     public AccountingDocument getAccountingDocumentForValidation() {
102         return accountingDocumentForValidation;
103     }
104 
105     
106 
107 
108 
109     public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
110         this.accountingDocumentForValidation = accountingDocumentForValidation;
111     }
112 
113     
114 
115 
116 
117     public TransferOfFundsService getTransferOfFundsService() {
118         return transferOfFundsService;
119     }
120 
121     
122 
123 
124 
125     public void setTransferOfFundsService(TransferOfFundsService transferOfFundsService) {
126         this.transferOfFundsService = transferOfFundsService;
127     }
128 }