View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.ld.document.validation.impl;
20  
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.kuali.kfs.module.ld.LaborKeyConstants;
25  import org.kuali.kfs.module.ld.businessobject.ExpenseTransferAccountingLine;
26  import org.kuali.kfs.module.ld.document.LaborExpenseTransferDocumentBase;
27  import org.kuali.kfs.sys.KFSPropertyConstants;
28  import org.kuali.kfs.sys.businessobject.AccountingLine;
29  import org.kuali.kfs.sys.document.validation.GenericValidation;
30  import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
31  import org.kuali.rice.core.api.util.type.KualiDecimal;
32  import org.kuali.rice.krad.document.Document;
33  import org.kuali.rice.krad.util.GlobalVariables;
34  
35  /**
36   * determine whether the given accounting line has already been in the given document
37   * 
38   * @param accountingDocument the given document
39   * @param accountingLine the given accounting line
40   * @return true if the given accounting line has already been in the given document; otherwise, false
41   */
42  public class LaborExpenseTransferAccountLineTotalsMatchValidation extends GenericValidation {
43      private Document documentForValidation;
44      
45      /**
46       * Validates before the document routes 
47       * @see org.kuali.kfs.validation.Validation#validate(java.lang.Object[])
48       */
49      public boolean validate(AttributedDocumentEvent event) {
50          boolean result = true;
51             
52          Document documentForValidation = getDocumentForValidation();
53          
54          LaborExpenseTransferDocumentBase expenseTransferDocument = (LaborExpenseTransferDocumentBase) documentForValidation;
55          
56          List sourceLines = expenseTransferDocument.getSourceAccountingLines();
57          List targetLines = expenseTransferDocument.getTargetAccountingLines();
58  
59          // check to ensure totals of accounting lines in source and target sections match
60          if (!isAccountingLineTotalsMatch(sourceLines, targetLines)) {
61              GlobalVariables.getMessageMap().putError(KFSPropertyConstants.SOURCE_ACCOUNTING_LINES, LaborKeyConstants.ACCOUNTING_LINE_TOTALS_MISMATCH_ERROR);
62              return false;
63          }
64  
65          return result;       
66      }
67  
68      /**
69       * This method checks if the total sum amount of the source accounting line matches the total sum amount of the target
70       * accounting line, return true if the totals match, false otherwise.
71       * 
72       * @param sourceLines
73       * @param targetLines
74       * @return
75       */
76      public boolean isAccountingLineTotalsMatch(List sourceLines, List targetLines) {
77          boolean isValid = true;
78  
79          AccountingLine line = null;
80  
81          // totals for the from and to lines.
82          KualiDecimal sourceLinesAmount = KualiDecimal.ZERO;
83          KualiDecimal targetLinesAmount = KualiDecimal.ZERO;
84  
85          // sum source lines
86          for (Iterator i = sourceLines.iterator(); i.hasNext();) {
87              line = (ExpenseTransferAccountingLine) i.next();
88              sourceLinesAmount = sourceLinesAmount.add(line.getAmount());
89          }
90  
91          // sum target lines
92          for (Iterator i = targetLines.iterator(); i.hasNext();) {
93              line = (ExpenseTransferAccountingLine) i.next();
94              targetLinesAmount = targetLinesAmount.add(line.getAmount());
95          }
96  
97          // if totals don't match, then add error message
98          if (sourceLinesAmount.compareTo(targetLinesAmount) != 0) {
99              isValid = false;
100         }
101 
102         return isValid;
103     }
104        
105     /**
106      * Gets the documentForValidation attribute. 
107      * @return Returns the documentForValidation.
108      */
109     public Document getDocumentForValidation() {
110         return documentForValidation;
111     }
112 
113     /**
114      * Sets the accountingDocumentForValidation attribute value.
115      * @param documentForValidation The documentForValidation to set.
116      */
117     public void setDocumentForValidation(Document documentForValidation) {
118         this.documentForValidation = documentForValidation;
119     }    
120 }