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 org.kuali.ole.fp.document.DisbursementVoucherDocument;
019import org.kuali.ole.sys.OLEConstants;
020import org.kuali.ole.sys.OLEKeyConstants;
021import org.kuali.ole.sys.OLEPropertyConstants;
022import org.kuali.ole.sys.document.AccountingDocument;
023import org.kuali.ole.sys.document.validation.GenericValidation;
024import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
025import org.kuali.rice.core.api.util.type.KualiDecimal;
026import org.kuali.rice.krad.util.GlobalVariables;
027import org.kuali.rice.krad.util.MessageMap;
028
029public class DisbursementVoucherDocumentAmountValidation extends GenericValidation {
030    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementVoucherDocumentAmountValidation.class);
031
032    private AccountingDocument accountingDocumentForValidation;
033
034    /**
035     * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
036     */
037    public boolean validate(AttributedDocumentEvent event) {
038        LOG.debug("validate start");
039        
040        boolean isValid = true;
041        DisbursementVoucherDocument disbursementVoucherDocument = (DisbursementVoucherDocument) accountingDocumentForValidation;
042        
043        MessageMap errors = GlobalVariables.getMessageMap();
044        errors.addToErrorPath(OLEPropertyConstants.DOCUMENT);
045
046        /* check total cannot be negative or zero */
047        if (!disbursementVoucherDocument.getDisbVchrCheckTotalAmount().isPositive()) {
048            errors.putError(OLEPropertyConstants.DISB_VCHR_CHECK_TOTAL_AMOUNT, OLEKeyConstants.ERROR_NEGATIVE_OR_ZERO_CHECK_TOTAL);
049            isValid = false;
050        }
051
052        /* total accounting lines cannot be negative */
053        if (KualiDecimal.ZERO.compareTo(disbursementVoucherDocument.getSourceTotal()) == 1) {
054            errors.putErrorWithoutFullErrorPath(OLEConstants.ACCOUNTING_LINE_ERRORS, OLEKeyConstants.ERROR_NEGATIVE_ACCOUNTING_TOTAL);
055            isValid = false;
056        }
057
058        /* total of accounting lines must match check total */
059        if (disbursementVoucherDocument.getDisbVchrCheckTotalAmount().compareTo(disbursementVoucherDocument.getSourceTotal()) != 0) {
060            errors.putErrorWithoutFullErrorPath(OLEConstants.ACCOUNTING_LINE_ERRORS, OLEKeyConstants.ERROR_CHECK_ACCOUNTING_TOTAL);
061            isValid = false;
062        }
063        
064        errors.removeFromErrorPath(OLEPropertyConstants.DOCUMENT);
065
066        return isValid;
067    }
068
069    /**
070     * Sets the accountingDocumentForValidation attribute value.
071     * 
072     * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
073     */
074    public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
075        this.accountingDocumentForValidation = accountingDocumentForValidation;
076    }
077
078    /**
079     * Gets the accountingDocumentForValidation attribute. 
080     * @return Returns the accountingDocumentForValidation.
081     */
082    public AccountingDocument getAccountingDocumentForValidation() {
083        return accountingDocumentForValidation;
084    }
085
086}