1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.ole.select.document.validation.impl;
17  
18  import org.kuali.ole.select.OleSelectConstant;
19  import org.kuali.ole.select.businessobject.OleAccountingLine;
20  import org.kuali.ole.sys.OLEConstants;
21  import org.kuali.ole.sys.document.AccountingDocument;
22  import org.kuali.ole.sys.document.validation.GenericValidation;
23  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
24  import org.kuali.rice.krad.util.GlobalVariables;
25  
26  import java.math.BigDecimal;
27  import java.util.List;
28  
29  public class OleAccountingLinesPercentValidation extends GenericValidation {
30      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleAccountingLinesPercentValidation.class);
31  
32      private AccountingDocument accountingDocumentForValidation;
33  
34      @Override
35      public boolean validate(AttributedDocumentEvent event) {
36          boolean valid = true;
37          
38          if (valid) {
39              
40              BigDecimal sourceTotalPercent = BigDecimal.ZERO;
41              BigDecimal targetTotalPercent = BigDecimal.ZERO;
42              BigDecimal desiredPercent = new BigDecimal("100");
43              List sourceAccounts = getAccountingDocumentForValidation().getSourceAccountingLines();
44              for (Object account : sourceAccounts) {
45                  if (((OleAccountingLine) account).getAccountLinePercent() != null) {
46                      sourceTotalPercent = sourceTotalPercent.add(((OleAccountingLine) account).getAccountLinePercent());
47                  } else {
48                      sourceTotalPercent = sourceTotalPercent.add(BigDecimal.ZERO);
49                  }
50              }
51              List targetAccounts = getAccountingDocumentForValidation().getTargetAccountingLines();
52              for (Object account : targetAccounts) {
53                  if (((OleAccountingLine) account).getAccountLinePercent() != null) {
54                      targetTotalPercent = targetTotalPercent.add(((OleAccountingLine) account).getAccountLinePercent());
55                  } else {
56                      targetTotalPercent = targetTotalPercent.add(BigDecimal.ZERO);
57                  }
58              }
59              if ((sourceAccounts.size() > 0 && desiredPercent.compareTo(sourceTotalPercent) != 0) ||
60                      (targetAccounts.size() > 0 && desiredPercent.compareTo(targetTotalPercent) != 0)) {
61                  GlobalVariables.getMessageMap().putError(OLEConstants.ACCOUNTING_LINE_ERRORS, OleSelectConstant.ERROR_DI_ACCOUNTING_TOTAL, "");
62                  valid = false;
63              }
64          }
65          return valid;
66      }
67  
68  
69      
70  
71  
72  
73  
74      public AccountingDocument getAccountingDocumentForValidation() {
75          return accountingDocumentForValidation;
76      }
77  
78      
79  
80  
81  
82  
83      public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
84          this.accountingDocumentForValidation = accountingDocumentForValidation;
85      }
86  }