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.HashMap;
019    import java.util.List;
020    import java.util.Map;
021    
022    import org.kuali.kfs.module.ld.LaborConstants;
023    import org.kuali.kfs.module.ld.LaborKeyConstants;
024    import org.kuali.kfs.module.ld.LaborPropertyConstants;
025    import org.kuali.kfs.module.ld.businessobject.ExpenseTransferAccountingLine;
026    import org.kuali.kfs.module.ld.document.LaborExpenseTransferDocumentBase;
027    import org.kuali.kfs.module.ld.document.SalaryExpenseTransferDocument;
028    import org.kuali.kfs.module.ld.service.LaborLedgerPendingEntryService;
029    import org.kuali.kfs.sys.KFSConstants;
030    import org.kuali.kfs.sys.KFSPropertyConstants;
031    import org.kuali.kfs.sys.context.SpringContext;
032    import org.kuali.kfs.sys.document.AccountingDocument;
033    import org.kuali.kfs.sys.document.validation.GenericValidation;
034    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
035    import org.kuali.rice.kns.document.Document;
036    import org.kuali.rice.kns.util.GlobalVariables;
037    import org.kuali.rice.kns.util.KNSConstants;
038    
039    /**
040     * Validates that an accounting document does not have any pending
041     * labor ledger entries with the same emplID, periodCode, accountNumber, objectCode 
042     */
043    public class SalaryExpenseTransferPendingLegerEntryValidation extends GenericValidation {
044        private Document documentForValidation;
045        
046        /**
047         * Validates that the accounting lines in the accounting document does not have 
048         * any pending labor ledger entries with the same emplID, periodCode, accountNumber, objectCode 
049         * <strong>Expects an accounting document as the first a parameter</strong>
050         * @see org.kuali.kfs.validation.Validation#validate(java.lang.Object[])
051         */
052        public boolean validate(AttributedDocumentEvent event) {
053            boolean result = true;
054            
055            Document documentForValidation = getDocumentForValidation();
056            AccountingDocument accountingDocument = (AccountingDocument) documentForValidation;
057            
058            result = !hasPendingLedgerEntry(accountingDocument);
059            return result ;    
060        }
061    
062        /**
063         * Checks whether amounts by object codes are unchanged
064         * 
065         * @param accountingDocumentForValidation The accounting document from which the amounts by objects codes are checked
066         * @return True if the given accounting documents amounts by object code are unchanged, false otherwise.
067         */ 
068        protected boolean hasPendingLedgerEntry(AccountingDocument accountingDocument) {
069            boolean entriesExist = false ;
070            
071            LaborExpenseTransferDocumentBase expenseTransferDocument = (LaborExpenseTransferDocumentBase) accountingDocument;
072            List<ExpenseTransferAccountingLine> sourceAccountingLines = expenseTransferDocument.getSourceAccountingLines();
073    
074            Map<String, String> fieldValues = new HashMap<String, String>();
075            for (ExpenseTransferAccountingLine sourceAccountingLine : sourceAccountingLines) {
076                String payPeriodCode = sourceAccountingLine.getPayrollEndDateFiscalPeriodCode();
077                String accountNumber = sourceAccountingLine.getAccountNumber();
078                String objectCode = sourceAccountingLine.getFinancialObjectCode();
079                String emplId = sourceAccountingLine.getEmplid();
080                String documentNumber = accountingDocument.getDocumentNumber();
081    
082                fieldValues.put(LaborPropertyConstants.PAYROLL_END_DATE_FISCAL_PERIOD_CODE, payPeriodCode);
083                fieldValues.put(KFSPropertyConstants.ACCOUNT_NUMBER, accountNumber);
084                fieldValues.put(KFSPropertyConstants.FINANCIAL_OBJECT_CODE, objectCode);
085                fieldValues.put(KFSPropertyConstants.EMPLID, emplId);
086                fieldValues.put(KFSPropertyConstants.DOCUMENT_NUMBER, KNSConstants.NOT_LOGICAL_OPERATOR + documentNumber);
087                
088                if (SpringContext.getBean(LaborLedgerPendingEntryService.class).hasPendingLaborLedgerEntry(fieldValues)) {
089                    GlobalVariables.getMessageMap().putError(LaborConstants.EMPLOYEE_LOOKUP_ERRORS, LaborKeyConstants.PENDING_SALARY_TRANSFER_ERROR, emplId, payPeriodCode, accountNumber, objectCode);
090                    return true;
091                }                        
092            }
093            return entriesExist ;
094        }
095    
096        /**
097         * Gets the documentForValidation attribute. 
098         * @return Returns the documentForValidation.
099         */
100        public Document getDocumentForValidation() {
101            return documentForValidation;
102        }
103    
104        /**
105         * Sets the accountingDocumentForValidation attribute value.
106         * @param documentForValidation The documentForValidation to set.
107         */
108        public void setDocumentForValidation(Document documentForValidation) {
109            this.documentForValidation = documentForValidation;
110        }    
111    }