001/*
002 * Copyright 2008 The Kuali Foundation
003 * 
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 * http://www.opensource.org/licenses/ecl2.php
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.fp.document.validation.impl;
017
018import static org.kuali.ole.sys.OLEConstants.SOURCE_ACCOUNTING_LINE_ERRORS;
019import static org.kuali.ole.sys.OLEConstants.TARGET_ACCOUNTING_LINE_ERRORS;
020import static org.kuali.ole.sys.OLEKeyConstants.ERROR_DOCUMENT_ACCOUNTING_LINE_TOTAL_CHANGED;
021
022import org.kuali.ole.fp.document.BudgetAdjustmentDocument;
023import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
024import org.kuali.ole.sys.document.validation.impl.AccountingLineGroupTotalsUnchangedValidation;
025import org.kuali.ole.sys.document.validation.impl.OLEAccountingLineGroupTotalsUnchangedValidation;
026import org.kuali.rice.core.api.util.type.KualiDecimal;
027import org.kuali.rice.core.api.util.type.KualiInteger;
028import org.kuali.rice.core.web.format.CurrencyFormatter;
029import org.kuali.rice.krad.util.GlobalVariables;
030
031/**
032 * The Budget Adjustment's variation on whether accounting lines have been unchanged or not
033 */
034public class BudgetAdjustmentAccountingLineTotalsUnchangedValidation extends OLEAccountingLineGroupTotalsUnchangedValidation {
035
036    /**
037     * Returns true if account line totals remains unchanged from what was entered and what was persisted before; takes into account all adjustment totals
038     * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
039     */
040    public boolean validate(AttributedDocumentEvent event) {
041        boolean isUnchanged = true;
042
043        BudgetAdjustmentDocument persistedDocument = (BudgetAdjustmentDocument) retrievePersistedDocument(getAccountingDocumentForValidation());
044        BudgetAdjustmentDocument currentDocument = (BudgetAdjustmentDocument)getAccountingDocumentForValidation();
045
046        if (persistedDocument == null) {
047            handleNonExistentDocumentWhenApproving(getAccountingDocumentForValidation());
048        }
049        else {
050            // retrieve the persisted totals
051            KualiDecimal persistedSourceCurrentBudgetTotal = persistedDocument.getSourceCurrentBudgetTotal();
052            KualiDecimal persistedSourceBaseBudgetTotal = persistedDocument.getSourceBaseBudgetTotal();
053            KualiDecimal persistedTargetCurrentBudgetTotal = persistedDocument.getTargetCurrentBudgetTotal();
054            KualiDecimal persistedTargetBaseBudgetTotal = persistedDocument.getTargetBaseBudgetTotal();
055
056            // retrieve the updated totals
057            KualiDecimal currentSourceCurrentBudgetTotal = currentDocument.getSourceCurrentBudgetTotal();
058            KualiDecimal currentSourceBaseBudgetTotal = currentDocument.getSourceBaseBudgetTotal();
059            KualiDecimal currentTargetCurrentBudgetTotal = currentDocument.getTargetCurrentBudgetTotal();
060            KualiDecimal currentTargetBaseBudgetTotal = currentDocument.getTargetBaseBudgetTotal();
061
062            // make sure that totals have remained unchanged, if not, recognize that, and
063            // generate appropriate error messages
064            if (persistedSourceCurrentBudgetTotal.compareTo(currentSourceCurrentBudgetTotal) != 0) {
065                isUnchanged = false;
066                buildTotalChangeErrorMessage(SOURCE_ACCOUNTING_LINE_ERRORS, "source current budget", persistedSourceCurrentBudgetTotal, currentSourceCurrentBudgetTotal);
067            }
068            if (persistedSourceBaseBudgetTotal.compareTo(currentSourceBaseBudgetTotal) != 0) {
069                isUnchanged = false;
070                buildTotalChangeErrorMessage(SOURCE_ACCOUNTING_LINE_ERRORS, "source base budget", persistedSourceBaseBudgetTotal, currentSourceBaseBudgetTotal);
071            }
072            if (persistedTargetCurrentBudgetTotal.compareTo(currentTargetCurrentBudgetTotal) != 0) {
073                isUnchanged = false;
074                buildTotalChangeErrorMessage(TARGET_ACCOUNTING_LINE_ERRORS, "target current budget", persistedTargetCurrentBudgetTotal, currentTargetCurrentBudgetTotal);
075            }
076            if (persistedTargetBaseBudgetTotal.compareTo(currentTargetBaseBudgetTotal) != 0) {
077                isUnchanged = false;
078                buildTotalChangeErrorMessage(TARGET_ACCOUNTING_LINE_ERRORS, "target base budget", persistedTargetBaseBudgetTotal, currentTargetBaseBudgetTotal);
079            }
080        }
081
082        return isUnchanged;
083    }
084    
085    /**
086     * Builds the error message for when totals have changed.
087     * 
088     * @param propertyName name of property
089     * @param sectionTitle title of section
090     * @param persistedSourceLineTotal previously persisted source line total
091     * @param currentSourceLineTotal current entered source line total
092     */
093    protected void buildTotalChangeErrorMessage(String propertyName, String sectionTitle, KualiDecimal persistedSourceLineTotal, KualiDecimal currentSourceLineTotal) {
094        String persistedTotal = (String) new CurrencyFormatter().format(persistedSourceLineTotal);
095        String currentTotal = (String) new CurrencyFormatter().format(currentSourceLineTotal);
096
097        GlobalVariables.getMessageMap().putError(propertyName, ERROR_DOCUMENT_ACCOUNTING_LINE_TOTAL_CHANGED, new String[] { sectionTitle, persistedTotal, currentTotal });
098    }
099}