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.OLEConstants.SOURCE_ACCOUNTING_LINE_ERRORS;
19  import static org.kuali.ole.sys.OLEConstants.TARGET_ACCOUNTING_LINE_ERRORS;
20  import static org.kuali.ole.sys.OLEKeyConstants.ERROR_DOCUMENT_ACCOUNTING_LINE_TOTAL_CHANGED;
21  
22  import org.kuali.ole.fp.document.BudgetAdjustmentDocument;
23  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
24  import org.kuali.ole.sys.document.validation.impl.AccountingLineGroupTotalsUnchangedValidation;
25  import org.kuali.rice.core.api.util.type.KualiDecimal;
26  import org.kuali.rice.core.api.util.type.KualiInteger;
27  import org.kuali.rice.core.web.format.CurrencyFormatter;
28  import org.kuali.rice.krad.util.GlobalVariables;
29  
30  /**
31   * The Budget Adjustment's variation on whether accounting lines have been unchanged or not
32   */
33  public class BudgetAdjustmentAccountingLineTotalsUnchangedValidation extends AccountingLineGroupTotalsUnchangedValidation {
34  
35      /**
36       * Returns true if account line totals remains unchanged from what was entered and what was persisted before; takes into account all adjustment totals
37       * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
38       */
39      public boolean validate(AttributedDocumentEvent event) {
40          boolean isUnchanged = true;
41  
42          BudgetAdjustmentDocument persistedDocument = (BudgetAdjustmentDocument) retrievePersistedDocument(getAccountingDocumentForValidation());
43          BudgetAdjustmentDocument currentDocument = (BudgetAdjustmentDocument)getAccountingDocumentForValidation();
44  
45          if (persistedDocument == null) {
46              handleNonExistentDocumentWhenApproving(getAccountingDocumentForValidation());
47          }
48          else {
49              // retrieve the persisted totals
50              KualiDecimal persistedSourceCurrentBudgetTotal = persistedDocument.getSourceCurrentBudgetTotal();
51              KualiDecimal persistedSourceBaseBudgetTotal = persistedDocument.getSourceBaseBudgetTotal();
52              KualiDecimal persistedTargetCurrentBudgetTotal = persistedDocument.getTargetCurrentBudgetTotal();
53              KualiDecimal persistedTargetBaseBudgetTotal = persistedDocument.getTargetBaseBudgetTotal();
54  
55              // retrieve the updated totals
56              KualiDecimal currentSourceCurrentBudgetTotal = currentDocument.getSourceCurrentBudgetTotal();
57              KualiDecimal currentSourceBaseBudgetTotal = currentDocument.getSourceBaseBudgetTotal();
58              KualiDecimal currentTargetCurrentBudgetTotal = currentDocument.getTargetCurrentBudgetTotal();
59              KualiDecimal currentTargetBaseBudgetTotal = currentDocument.getTargetBaseBudgetTotal();
60  
61              // make sure that totals have remained unchanged, if not, recognize that, and
62              // generate appropriate error messages
63              if (persistedSourceCurrentBudgetTotal.compareTo(currentSourceCurrentBudgetTotal) != 0) {
64                  isUnchanged = false;
65                  buildTotalChangeErrorMessage(SOURCE_ACCOUNTING_LINE_ERRORS, "source current budget", persistedSourceCurrentBudgetTotal, currentSourceCurrentBudgetTotal);
66              }
67              if (persistedSourceBaseBudgetTotal.compareTo(currentSourceBaseBudgetTotal) != 0) {
68                  isUnchanged = false;
69                  buildTotalChangeErrorMessage(SOURCE_ACCOUNTING_LINE_ERRORS, "source base budget", persistedSourceBaseBudgetTotal, currentSourceBaseBudgetTotal);
70              }
71              if (persistedTargetCurrentBudgetTotal.compareTo(currentTargetCurrentBudgetTotal) != 0) {
72                  isUnchanged = false;
73                  buildTotalChangeErrorMessage(TARGET_ACCOUNTING_LINE_ERRORS, "target current budget", persistedTargetCurrentBudgetTotal, currentTargetCurrentBudgetTotal);
74              }
75              if (persistedTargetBaseBudgetTotal.compareTo(currentTargetBaseBudgetTotal) != 0) {
76                  isUnchanged = false;
77                  buildTotalChangeErrorMessage(TARGET_ACCOUNTING_LINE_ERRORS, "target base budget", persistedTargetBaseBudgetTotal, currentTargetBaseBudgetTotal);
78              }
79          }
80  
81          return isUnchanged;
82      }
83      
84      /**
85       * Builds the error message for when totals have changed.
86       * 
87       * @param propertyName name of property
88       * @param sectionTitle title of section
89       * @param persistedSourceLineTotal previously persisted source line total
90       * @param currentSourceLineTotal current entered source line total
91       */
92      protected void buildTotalChangeErrorMessage(String propertyName, String sectionTitle, KualiDecimal persistedSourceLineTotal, KualiDecimal currentSourceLineTotal) {
93          String persistedTotal = (String) new CurrencyFormatter().format(persistedSourceLineTotal);
94          String currentTotal = (String) new CurrencyFormatter().format(currentSourceLineTotal);
95  
96          GlobalVariables.getMessageMap().putError(propertyName, ERROR_DOCUMENT_ACCOUNTING_LINE_TOTAL_CHANGED, new String[] { sectionTitle, persistedTotal, currentTotal });
97      }
98  }