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.List;
019    
020    import org.apache.commons.lang.StringUtils;
021    import org.kuali.kfs.module.ld.LaborConstants;
022    import org.kuali.kfs.module.ld.LaborKeyConstants;
023    import org.kuali.kfs.module.ld.businessobject.ExpenseTransferSourceAccountingLine;
024    import org.kuali.kfs.module.ld.businessobject.ExpenseTransferTargetAccountingLine;
025    import org.kuali.kfs.module.ld.document.LaborExpenseTransferDocumentBase;
026    import org.kuali.kfs.module.ld.document.SalaryExpenseTransferDocument;
027    import org.kuali.kfs.sys.KFSPropertyConstants;
028    import org.kuali.kfs.sys.document.AccountingDocument;
029    import org.kuali.kfs.sys.document.validation.GenericValidation;
030    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
031    import org.kuali.rice.kns.document.Document;
032    import org.kuali.rice.kns.util.GlobalVariables;
033    
034    /**
035     * Validates that an accounting document's accounting lines have the same Employee ID 
036     */
037    public class SalaryExpenseTransferAccountingLinesSameEmployeeValidation extends GenericValidation {
038        private Document documentForValidation;
039        
040        /**
041         * Validates that the accounting lines in the accounting document have the same employee id 
042         * <strong>Expects an accounting document as the first a parameter</strong>
043         * @see org.kuali.kfs.validation.Validation#validate(java.lang.Object[])
044         */
045        public boolean validate(AttributedDocumentEvent event) {
046            boolean result = true;
047            
048            Document documentForValidation = getDocumentForValidation();
049            
050            SalaryExpenseTransferDocument salaryExpenseTransferDocument = (SalaryExpenseTransferDocument) documentForValidation;
051                    
052            String employeeID = salaryExpenseTransferDocument.getEmplid() ;
053            
054            if (StringUtils.isBlank(employeeID)) {
055                GlobalVariables.getMessageMap().putError(LaborConstants.DOCUMENT_EMPLOYEE_ID_ERRORS, LaborKeyConstants.MISSING_EMPLOYEE_ID) ;
056                result = false ;
057            }
058            
059            // ensure the employee ids in the source accounting lines are same
060            AccountingDocument accountingDocument = (AccountingDocument) documentForValidation;
061            if (!hasAccountingLinesSameEmployee(accountingDocument)) {
062                return false;
063            }
064            
065            return result ;    
066        }
067    
068        protected boolean hasAccountingLinesSameEmployee(AccountingDocument accountingDocument) {
069            
070            LaborExpenseTransferDocumentBase expenseTransferDocument = (LaborExpenseTransferDocumentBase) accountingDocument;
071            List<ExpenseTransferSourceAccountingLine> sourceAccountingLines = expenseTransferDocument.getSourceAccountingLines();
072            List<ExpenseTransferTargetAccountingLine> targetAccountingLines = expenseTransferDocument.getTargetAccountingLines();
073    
074            boolean sourceAccountingLinesValidationResult = true;
075            boolean targetAccountingLinesValidationResult = true;
076    
077            String employeeID = expenseTransferDocument.getEmplid();
078            String accountingLineEmplID = null;
079    
080            // Source Lines
081            for (ExpenseTransferSourceAccountingLine sourceAccountingLine : sourceAccountingLines) {
082                accountingLineEmplID = sourceAccountingLine.getEmplid();
083                if (accountingLineEmplID == null || !StringUtils.equals(employeeID, accountingLineEmplID)) {
084                    sourceAccountingLinesValidationResult = false;
085                    break;
086                }
087            }
088    
089            // Target lines
090            for (ExpenseTransferTargetAccountingLine targetAccountingLine : targetAccountingLines) {
091                accountingLineEmplID = targetAccountingLine.getEmplid();
092                if (accountingLineEmplID == null || !StringUtils.equals(employeeID, accountingLineEmplID)) {
093                    targetAccountingLinesValidationResult = false;
094                    break;
095                }
096            }
097    
098            if (!sourceAccountingLinesValidationResult) {
099                GlobalVariables.getMessageMap().putError(KFSPropertyConstants.SOURCE_ACCOUNTING_LINES, LaborKeyConstants.ERROR_EMPLOYEE_ID_NOT_SAME);
100            }
101    
102            if (!targetAccountingLinesValidationResult) {
103                GlobalVariables.getMessageMap().putError(KFSPropertyConstants.TARGET_ACCOUNTING_LINES, LaborKeyConstants.ERROR_EMPLOYEE_ID_NOT_SAME_IN_TARGET);
104            }
105    
106            return (sourceAccountingLinesValidationResult && targetAccountingLinesValidationResult);
107        }
108    
109        /**
110         * Gets the accountingDocumentForValidation attribute. 
111         * @return Returns the accountingDocumentForValidation.
112         */
113        public Document getDocumentForValidation() {
114            return documentForValidation;
115        }
116    
117        /**
118         * Sets the accountingDocumentForValidation attribute value.
119         * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
120         */
121        public void setDocumentForValidation(Document documentForValidation) {
122            this.documentForValidation = documentForValidation;
123        } 
124    }