001/*
002 * Copyright 2013 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 1.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/ecl1.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.select.document.validation.impl;
017
018import org.kuali.ole.select.OleSelectConstant;
019import org.kuali.ole.select.businessobject.OleDisbursementVoucherAccountingLine;
020import org.kuali.ole.sys.OLEConstants;
021import org.kuali.ole.sys.document.AccountingDocument;
022import org.kuali.ole.sys.document.validation.GenericValidation;
023import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
024import org.kuali.rice.krad.util.GlobalVariables;
025
026import java.math.BigDecimal;
027import java.util.List;
028
029public class OleDVAccountingLinesPercentValidation extends GenericValidation {
030    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleDVAccountingLinesPercentValidation.class);
031
032    private AccountingDocument accountingDocumentForValidation;
033
034    @Override
035    public boolean validate(AttributedDocumentEvent event) {
036        boolean valid = true;
037        //super.validate(event);
038        if (valid) {
039            // validate that the percents total 100 for each account
040            BigDecimal sourceTotalPercent = BigDecimal.ZERO;
041            BigDecimal desiredPercent = new BigDecimal("100");
042            List sourceAccounts = getAccountingDocumentForValidation().getSourceAccountingLines();
043            for (Object account : sourceAccounts) {
044                if (((OleDisbursementVoucherAccountingLine) account).getAccountLinePercent() != null) {
045                    sourceTotalPercent = sourceTotalPercent.add(((OleDisbursementVoucherAccountingLine) account).getAccountLinePercent());
046                } else {
047                    sourceTotalPercent = sourceTotalPercent.add(BigDecimal.ZERO);
048                }
049            }
050            if ((sourceAccounts.size() > 0 && desiredPercent.compareTo(sourceTotalPercent) != 0)) {
051                GlobalVariables.getMessageMap().putError(OLEConstants.ACCOUNTING_LINE_ERRORS, OleSelectConstant.ERROR_DI_ACCOUNTING_TOTAL, "");
052                valid = false;
053            }
054        }
055        return valid;
056    }
057
058
059    /**
060     * Gets the accountingDocumentForValdation attribute.
061     *
062     * @return Returns the accountingDocumentForValdation.
063     */
064    public AccountingDocument getAccountingDocumentForValidation() {
065        return accountingDocumentForValidation;
066    }
067
068    /**
069     * Sets the accountingDocumentForValdation attribute value.
070     *
071     * @param accountingDocumentForValdation The accountingDocumentForValdation to set.
072     */
073    public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
074        this.accountingDocumentForValidation = accountingDocumentForValidation;
075    }
076}
077