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.ACCOUNTING_LINE_ERRORS;
019import static org.kuali.ole.sys.OLEKeyConstants.AuxiliaryVoucher.ERROR_DIFFERENT_CHARTS;
020
021import java.util.List;
022
023import org.kuali.ole.sys.businessobject.AccountingLine;
024import org.kuali.ole.sys.document.AccountingDocument;
025import org.kuali.ole.sys.document.validation.GenericValidation;
026import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
027import org.kuali.rice.krad.util.GlobalVariables;
028
029/**
030 * Validates that all accounting lines on the document use only one chart among them all.
031 */
032public class AuxiliaryVoucherSingleChartUsedValidation extends GenericValidation {
033    private AccountingDocument accountingDocumentForValidation;
034
035    /**
036     * Iterates <code>{@link AccountingLine}</code> instances in a given <code>{@link FinancialDocument}</code> instance and
037     * compares them to see if they are all in the same Chart.
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 valid = true;
042
043        String baseChartCode = null;
044        int index = 0;
045
046        List<AccountingLine> lines = accountingDocumentForValidation.getSourceAccountingLines();
047        for (AccountingLine line : lines) {
048            if (index == 0) {
049                baseChartCode = line.getChartOfAccountsCode();
050            }
051            else {
052                String currentChartCode = line.getChartOfAccountsCode();
053                if (!currentChartCode.equals(baseChartCode)) {
054                    GlobalVariables.getMessageMap().putError(ACCOUNTING_LINE_ERRORS, ERROR_DIFFERENT_CHARTS, new String[] {});
055                    return false;
056                }
057            }
058            index++;
059        }
060        return true;
061    }
062
063    /**
064     * Gets the accountingDocumentForValidation attribute. 
065     * @return Returns the accountingDocumentForValidation.
066     */
067    public AccountingDocument getAccountingDocumentForValidation() {
068        return accountingDocumentForValidation;
069    }
070
071    /**
072     * Sets the accountingDocumentForValidation attribute value.
073     * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
074     */
075    public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
076        this.accountingDocumentForValidation = accountingDocumentForValidation;
077    }
078}