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.OleDisbursementVoucherAccountingLine;
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 OleDVAccountingLinesPercentValidation extends GenericValidation {
30      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleDVAccountingLinesPercentValidation.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 desiredPercent = new BigDecimal("100");
42              List sourceAccounts = getAccountingDocumentForValidation().getSourceAccountingLines();
43              for (Object account : sourceAccounts) {
44                  if (((OleDisbursementVoucherAccountingLine) account).getAccountLinePercent() != null) {
45                      sourceTotalPercent = sourceTotalPercent.add(((OleDisbursementVoucherAccountingLine) account).getAccountLinePercent());
46                  } else {
47                      sourceTotalPercent = sourceTotalPercent.add(BigDecimal.ZERO);
48                  }
49              }
50              if ((sourceAccounts.size() > 0 && desiredPercent.compareTo(sourceTotalPercent) != 0)) {
51                  GlobalVariables.getMessageMap().putError(OLEConstants.ACCOUNTING_LINE_ERRORS, OleSelectConstant.ERROR_DI_ACCOUNTING_TOTAL, "");
52                  valid = false;
53              }
54          }
55          return valid;
56      }
57  
58  
59      /**
60       * Gets the accountingDocumentForValdation attribute.
61       *
62       * @return Returns the accountingDocumentForValdation.
63       */
64      public AccountingDocument getAccountingDocumentForValidation() {
65          return accountingDocumentForValidation;
66      }
67  
68      /**
69       * Sets the accountingDocumentForValdation attribute value.
70       *
71       * @param accountingDocumentForValdation The accountingDocumentForValdation to set.
72       */
73      public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
74          this.accountingDocumentForValidation = accountingDocumentForValidation;
75      }
76  }
77