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 java.util.List;
019    
020    import org.kuali.kfs.sys.KFSConstants;
021    import org.kuali.kfs.sys.KFSKeyConstants;
022    import org.kuali.kfs.sys.businessobject.AccountingLine;
023    import org.kuali.kfs.sys.document.AccountingDocument;
024    import org.kuali.kfs.sys.document.validation.GenericValidation;
025    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
026    import org.kuali.rice.kns.util.GlobalVariables;
027    
028    /**
029     * Validation for Auxiliary Voucher that checks that all the source accounting lines on the document use
030     * only one sub fund group among them.
031     */
032    public class AuxiliaryVoucherSingleSubFundValidation 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 Sub-Fund Group.
038         * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
039         */
040        public boolean validate(AttributedDocumentEvent event) {
041            boolean valid = true;
042    
043            String baseSubFundGroupCode = null;
044            int index = 0;
045    
046            List<AccountingLine> lines = getAccountingDocumentForValidation().getSourceAccountingLines();
047            for (AccountingLine line : lines) {
048                if (index == 0) {
049                    baseSubFundGroupCode = line.getAccount().getSubFundGroupCode();
050                }
051                else {
052                    String currentSubFundGroup = line.getAccount().getSubFundGroupCode();
053                    if (!currentSubFundGroup.equals(baseSubFundGroupCode)) {
054                        GlobalVariables.getMessageMap().putError(KFSConstants.ACCOUNTING_LINE_ERRORS, KFSKeyConstants.AuxiliaryVoucher.ERROR_DIFFERENT_SUB_FUND_GROUPS);
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    }