001    /*
002     * Copyright 2011 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     */
016    package org.kuali.kfs.fp.document.validation.impl;
017    
018    import static org.kuali.kfs.sys.KFSConstants.SOURCE_ACCOUNTING_LINE_ERRORS;
019    import static org.kuali.kfs.sys.KFSConstants.TARGET_ACCOUNTING_LINE_ERRORS;
020    import static org.kuali.kfs.sys.KFSKeyConstants.ERROR_DOCUMENT_ACCOUNTING_LINE_TOTAL_CHANGED;
021    
022    import org.kuali.kfs.fp.document.BudgetAdjustmentDocument;
023    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
024    import org.kuali.kfs.sys.document.validation.impl.AccountingLineGroupTotalsUnchangedValidation;
025    import org.kuali.rice.kns.util.GlobalVariables;
026    import org.kuali.rice.kns.util.KualiDecimal;
027    import org.kuali.rice.kns.util.KualiInteger;
028    import org.kuali.rice.kns.web.format.CurrencyFormatter;
029    
030    /**
031     * The Budget Adjustment's variation on whether accounting lines have been unchanged or not
032     */
033    public class BudgetAdjustmentAccountingLineTotalsUnchangedValidation extends AccountingLineGroupTotalsUnchangedValidation {
034    
035        /**
036         * Returns true if account line totals remains unchanged from what was entered and what was persisted before; takes into account all adjustment totals
037         * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
038         */
039        public boolean validate(AttributedDocumentEvent event) {
040            boolean isUnchanged = true;
041    
042            BudgetAdjustmentDocument persistedDocument = (BudgetAdjustmentDocument) retrievePersistedDocument(getAccountingDocumentForValidation());
043            BudgetAdjustmentDocument currentDocument = (BudgetAdjustmentDocument)getAccountingDocumentForValidation();
044    
045            if (persistedDocument == null) {
046                handleNonExistentDocumentWhenApproving(getAccountingDocumentForValidation());
047            }
048            else {
049                // retrieve the persisted totals
050                KualiDecimal persistedSourceCurrentBudgetTotal = persistedDocument.getSourceCurrentBudgetTotal();
051                KualiInteger persistedSourceBaseBudgetTotal = persistedDocument.getSourceBaseBudgetTotal();
052                KualiDecimal persistedTargetCurrentBudgetTotal = persistedDocument.getTargetCurrentBudgetTotal();
053                KualiInteger persistedTargetBaseBudgetTotal = persistedDocument.getTargetBaseBudgetTotal();
054    
055                // retrieve the updated totals
056                KualiDecimal currentSourceCurrentBudgetTotal = currentDocument.getSourceCurrentBudgetTotal();
057                KualiInteger currentSourceBaseBudgetTotal = currentDocument.getSourceBaseBudgetTotal();
058                KualiDecimal currentTargetCurrentBudgetTotal = currentDocument.getTargetCurrentBudgetTotal();
059                KualiInteger currentTargetBaseBudgetTotal = currentDocument.getTargetBaseBudgetTotal();
060    
061                // make sure that totals have remained unchanged, if not, recognize that, and
062                // generate appropriate error messages
063                if (persistedSourceCurrentBudgetTotal.compareTo(currentSourceCurrentBudgetTotal) != 0) {
064                    isUnchanged = false;
065                    buildTotalChangeErrorMessage(SOURCE_ACCOUNTING_LINE_ERRORS, "source current budget", persistedSourceCurrentBudgetTotal, currentSourceCurrentBudgetTotal);
066                }
067                if (persistedSourceBaseBudgetTotal.compareTo(currentSourceBaseBudgetTotal) != 0) {
068                    isUnchanged = false;
069                    buildTotalChangeErrorMessage(SOURCE_ACCOUNTING_LINE_ERRORS, "source base budget", persistedSourceBaseBudgetTotal.kualiDecimalValue(), currentSourceBaseBudgetTotal.kualiDecimalValue());
070                }
071                if (persistedTargetCurrentBudgetTotal.compareTo(currentTargetCurrentBudgetTotal) != 0) {
072                    isUnchanged = false;
073                    buildTotalChangeErrorMessage(TARGET_ACCOUNTING_LINE_ERRORS, "target current budget", persistedTargetCurrentBudgetTotal, currentTargetCurrentBudgetTotal);
074                }
075                if (persistedTargetBaseBudgetTotal.compareTo(currentTargetBaseBudgetTotal) != 0) {
076                    isUnchanged = false;
077                    buildTotalChangeErrorMessage(TARGET_ACCOUNTING_LINE_ERRORS, "target base budget", persistedTargetBaseBudgetTotal.kualiDecimalValue(), currentTargetBaseBudgetTotal.kualiDecimalValue());
078                }
079            }
080    
081            return isUnchanged;
082        }
083        
084        /**
085         * Builds the error message for when totals have changed.
086         * 
087         * @param propertyName name of property
088         * @param sectionTitle title of section
089         * @param persistedSourceLineTotal previously persisted source line total
090         * @param currentSourceLineTotal current entered source line total
091         */
092        protected void buildTotalChangeErrorMessage(String propertyName, String sectionTitle, KualiDecimal persistedSourceLineTotal, KualiDecimal currentSourceLineTotal) {
093            String persistedTotal = (String) new CurrencyFormatter().format(persistedSourceLineTotal);
094            String currentTotal = (String) new CurrencyFormatter().format(currentSourceLineTotal);
095    
096            GlobalVariables.getMessageMap().putError(propertyName, ERROR_DOCUMENT_ACCOUNTING_LINE_TOTAL_CHANGED, new String[] { sectionTitle, persistedTotal, currentTotal });
097        }
098    }