View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.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/ecl2.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.fp.document.validation.impl;
17  
18  import static org.kuali.ole.sys.OLEKeyConstants.ERROR_DOCUMENT_FUND_GROUP_SET_DOES_NOT_BALANCE;
19  
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.kuali.ole.fp.document.TransferOfFundsDocument;
25  import org.kuali.ole.sys.businessobject.AccountingLine;
26  import org.kuali.ole.sys.context.SpringContext;
27  import org.kuali.ole.sys.document.AccountingDocument;
28  import org.kuali.ole.sys.document.validation.GenericValidation;
29  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
30  import org.kuali.ole.sys.document.validation.impl.AccountingDocumentRuleBaseConstants;
31  import org.kuali.rice.core.api.parameter.ParameterEvaluator;
32  import org.kuali.rice.core.api.parameter.ParameterEvaluatorService;
33  import org.kuali.rice.core.api.util.type.KualiDecimal;
34  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
35  import org.kuali.rice.krad.util.GlobalVariables;
36  
37  /**
38   * Validation for Transfer of Funds document that tests if the fund groups represented by a given document are in balance.
39   */
40  public class TransferOfFundsFundGroupsBalancedValidation extends GenericValidation {
41      private AccountingDocument accountingDocumentForValidation;
42      private ParameterService parameterService;
43  
44      /**
45       * This is a helper method that wraps the fund group balancing check. This check can be configured by updating the 
46       * application parameter table that is associated with this check. See the document's specification for details.
47       * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
48       */
49      public boolean validate(AttributedDocumentEvent event) {
50          return isFundGroupSetBalanceValid(accountingDocumentForValidation, TransferOfFundsDocument.class, AccountingDocumentRuleBaseConstants.APPLICATION_PARAMETER.FUND_GROUP_BALANCING_SET);
51      }
52      
53      /**
54       * This method will make sure that totals for a specified set of fund groups is valid across the two different accounting line
55       * sections.
56       * 
57       * @param tranDoc
58       * @param fundGroupCodes An array of the fund group codes that will be considered for balancing.
59       * @return True if they balance; false otherwise.
60       */
61      protected boolean isFundGroupSetBalanceValid(AccountingDocument tranDoc, Class componentClass, String parameterName) {
62          // don't need to do any of this if there's no parameter
63          if (!getParameterService().parameterExists(componentClass, parameterName)) {
64              return true;
65          }
66  
67          List lines = new ArrayList();
68  
69          lines.addAll(tranDoc.getSourceAccountingLines());
70          lines.addAll(tranDoc.getTargetAccountingLines());
71  
72          KualiDecimal sourceLinesTotal = KualiDecimal.ZERO;
73          KualiDecimal targetLinesTotal = KualiDecimal.ZERO;
74  
75          // iterate over each accounting line and if it has an account with a
76          // fund group that should be balanced, then add that lines amount to the bucket
77          for (Iterator i = lines.iterator(); i.hasNext();) {
78              AccountingLine line = (AccountingLine) i.next();
79              String fundGroupCode = line.getAccount().getSubFundGroup().getFundGroupCode();
80  
81              ParameterEvaluator evaluator = /*REFACTORME*/SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(componentClass, parameterName, fundGroupCode);
82              if (evaluator.evaluationSucceeds()) {
83                  KualiDecimal glpeLineAmount = tranDoc.getGeneralLedgerPendingEntryAmountForDetail(line);
84                  if (line.isSourceAccountingLine()) {
85                      sourceLinesTotal = sourceLinesTotal.add(glpeLineAmount);
86                  }
87                  else {
88                      targetLinesTotal = targetLinesTotal.add(glpeLineAmount);
89                  }
90              }
91          }
92  
93          // check that the amounts balance across sections
94          boolean isValid = true;
95  
96          if (sourceLinesTotal.compareTo(targetLinesTotal) != 0) {
97              isValid = false;
98  
99              // creating an evaluator to just format the fund codes into a nice string
100             ParameterEvaluator evaluator = /*REFACTORME*/SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(componentClass, parameterName, "");
101             GlobalVariables.getMessageMap().putError("document.sourceAccountingLines", ERROR_DOCUMENT_FUND_GROUP_SET_DOES_NOT_BALANCE, new String[] { tranDoc.getSourceAccountingLinesSectionTitle(), tranDoc.getTargetAccountingLinesSectionTitle(), evaluator.getParameterValuesForMessage() });
102         }
103 
104         return isValid;
105     }
106 
107     /**
108      * Gets the accountingDocumentForValidation attribute. 
109      * @return Returns the accountingDocumentForValidation.
110      */
111     public AccountingDocument getAccountingDocumentForValidation() {
112         return accountingDocumentForValidation;
113     }
114 
115     /**
116      * Sets the accountingDocumentForValidation attribute value.
117      * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
118      */
119     public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
120         this.accountingDocumentForValidation = accountingDocumentForValidation;
121     }
122 
123     /**
124      * Gets the parameterService attribute. 
125      * @return Returns the parameterService.
126      */
127     public ParameterService getParameterService() {
128         return parameterService;
129     }
130 
131     /**
132      * Sets the parameterService attribute value.
133      * @param parameterService The parameterService to set.
134      */
135     public void setParameterService(ParameterService parameterService) {
136         this.parameterService = parameterService;
137     }
138 }