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.module.ld.document.validation.impl;
017    
018    import java.util.HashSet;
019    import java.util.List;
020    import java.util.Set;
021    
022    import org.kuali.kfs.module.ld.LaborConstants;
023    import org.kuali.kfs.module.ld.LaborKeyConstants;
024    import org.kuali.kfs.module.ld.document.LaborExpenseTransferDocumentBase;
025    import org.kuali.kfs.sys.KFSPropertyConstants;
026    import org.kuali.kfs.sys.businessobject.AccountingLine;
027    import org.kuali.kfs.sys.document.AccountingDocument;
028    import org.kuali.kfs.sys.document.validation.GenericValidation;
029    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
030    import org.kuali.rice.kns.util.GlobalVariables;
031    import org.kuali.rice.kns.document.Document;
032    
033    /**
034     * benefit transfers cannot be made between two different fringe benefit labor object codes 
035     */
036    public class BenefitExpenseTransferSameFringeBenefitObjectCodeValidation extends GenericValidation {
037        private Document documentForValidation;
038        
039        /**
040         * Validates that the accounting lines in the accounting document have the same employee id 
041         * <strong>Expects an accounting document as the first a parameter</strong>
042         * @see org.kuali.kfs.validation.Validation#validate(java.lang.Object[])
043         */
044        public boolean validate(AttributedDocumentEvent event) {
045            boolean result = true;
046            
047            Document documentForValidation = getDocumentForValidation();
048            
049            AccountingDocument accountingDocument = (AccountingDocument) documentForValidation;
050            
051            // benefit transfers cannot be made between two different fringe benefit labor object codes.
052            boolean sameFringeBenefitObjectCodes = hasSameFringeBenefitObjectCodes(accountingDocument);
053            if (!sameFringeBenefitObjectCodes) {
054                GlobalVariables.getMessageMap().putError(KFSPropertyConstants.TARGET_ACCOUNTING_LINES, LaborKeyConstants.DISTINCT_OBJECT_CODE_ERROR);
055                result = false;
056            }
057    
058            return result;
059        }
060    
061        /**
062         * Determines whether target accounting lines have the same fringe benefit object codes as source accounting lines
063         * 
064         * @param accountingDocument the given accounting document
065         * @return true if target accounting lines have the same fringe benefit object codes as source accounting lines; otherwise, false
066         */
067        protected boolean hasSameFringeBenefitObjectCodes(AccountingDocument accountingDocument) {
068            LaborExpenseTransferDocumentBase expenseTransferDocument = (LaborExpenseTransferDocumentBase) accountingDocument;
069    
070            Set<String> objectCodesFromSourceLine = new HashSet<String>();
071            for (Object sourceAccountingLine : expenseTransferDocument.getSourceAccountingLines()) {
072                AccountingLine line = (AccountingLine) sourceAccountingLine;
073                objectCodesFromSourceLine.add(line.getFinancialObjectCode());
074            }
075    
076            Set<String> objectCodesFromTargetLine = new HashSet<String>();
077            for (Object targetAccountingLine : expenseTransferDocument.getTargetAccountingLines()) {
078                AccountingLine line = (AccountingLine) targetAccountingLine;
079                objectCodesFromTargetLine.add(line.getFinancialObjectCode());
080            }
081    
082            if (objectCodesFromSourceLine.size() != objectCodesFromTargetLine.size()) {
083                return false;
084            }
085    
086            return objectCodesFromSourceLine.containsAll(objectCodesFromTargetLine);
087        }
088    
089        /**
090         * Gets the accountingDocumentForValidation attribute. 
091         * @return Returns the accountingDocumentForValidation.
092         */
093        public Document getDocumentForValidation() {
094            return documentForValidation;
095        }
096    
097        /**
098         * Sets the accountingDocumentForValidation attribute value.
099         * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
100         */
101        public void setDocumentForValidation(Document documentForValidation) {
102            this.documentForValidation = documentForValidation;
103        } 
104    }