View Javadoc
1   /*
2    * Copyright 2013 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 1.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/ecl1.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.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          //super.validate(event);
38          if (valid) {
39              // validate that the percents total 100 for each account
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       * Gets the accountingDocumentForValdation attribute.
71       *
72       * @return Returns the accountingDocumentForValdation.
73       */
74      public AccountingDocument getAccountingDocumentForValidation() {
75          return accountingDocumentForValidation;
76      }
77  
78      /**
79       * Sets the accountingDocumentForValdation attribute value.
80       *
81       * @param accountingDocumentForValdation The accountingDocumentForValdation to set.
82       */
83      public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
84          this.accountingDocumentForValidation = accountingDocumentForValidation;
85      }
86  }