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.Collection;
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.kuali.kfs.module.ld.LaborConstants;
024    import org.kuali.kfs.module.ld.LaborKeyConstants;
025    import org.kuali.kfs.module.ld.businessobject.LaborJournalVoucherDetail;
026    import org.kuali.kfs.module.ld.document.LaborJournalVoucherDocument;
027    import org.kuali.kfs.sys.KFSConstants;
028    import org.kuali.kfs.sys.KFSKeyConstants;
029    import org.kuali.kfs.sys.KFSPropertyConstants;
030    import org.kuali.kfs.sys.ObjectUtil;
031    import org.kuali.kfs.sys.businessobject.AccountingLine;
032    import org.kuali.kfs.sys.context.SpringContext;
033    import org.kuali.rice.kns.service.DataDictionaryService;
034    import org.kuali.rice.kim.service.PersonService;
035    import org.kuali.rice.kns.util.GlobalVariables;
036    import org.kuali.kfs.sys.document.validation.GenericValidation;
037    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
038    
039    /**
040     * Validates that a labor journal voucher document's accounting lines have valid Employee ID 
041     */
042    public class LaborJournalVoucherEmployeeIDExistenceCheckValidation extends GenericValidation {
043        private AccountingLine accountingLineForValidation; 
044        
045        /**
046         * Validates that the accounting line in the labor journal voucher document have an existing employee id 
047         * @see org.kuali.kfs.validation.Validation#validate(java.lang.Object[])
048         */
049        public boolean validate(AttributedDocumentEvent event) {
050            boolean result = true;
051            
052            LaborJournalVoucherDetail laborJournalVoucherDetail = (LaborJournalVoucherDetail) getAccountingLineForValidation();
053            String emplid = laborJournalVoucherDetail.getEmplid();
054            
055            if (StringUtils.isBlank(emplid) || LaborConstants.getDashEmplId().equals(emplid)) {
056                return true ;
057            }
058            if (!employeeIDExistenceCheck(emplid)) {
059                result = false ;
060            }
061            return result ;    
062        }
063    
064        /**
065         * Checks whether employee id exists
066         * 
067         * @param employeeid employee id is checked against the collection of universal users
068         * @return True if the given employee id exists, false otherwise.
069         */ 
070        protected boolean employeeIDExistenceCheck(String employeeid) {
071            
072            boolean employeeIDExists  = true ;
073            Map criteria = new HashMap();
074            criteria.put(KFSPropertyConstants.PERSON_PAYROLL_IDENTIFIER, employeeid);
075            
076            Collection emplidMatches = SpringContext.getBean(org.kuali.rice.kim.service.PersonService.class).findPeople(criteria);
077            if (emplidMatches == null || emplidMatches.isEmpty()) {
078                String label = SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getBusinessObjectEntry(LaborJournalVoucherDetail.class.getName()).getAttributeDefinition(KFSPropertyConstants.EMPLID).getLabel();
079                GlobalVariables.getMessageMap().putError(KFSPropertyConstants.EMPLID, KFSKeyConstants.ERROR_EXISTENCE, label) ;
080                employeeIDExists = false ;
081            }
082                
083            return employeeIDExists ;
084        }
085    
086        /**
087         * Gets the accountingLineForValidation attribute. 
088         * @return Returns the accountingLineForValidation.
089         */
090        public AccountingLine getAccountingLineForValidation() {
091            return accountingLineForValidation;
092        }
093    
094        /**
095         * Sets the accountingLineForValidation attribute value.
096         * @param accountingLineForValidation The accountingLineForValidation to set.
097         */
098        public void setAccountingLineForValidation(AccountingLine accountingLineForValidation) {
099            this.accountingLineForValidation = accountingLineForValidation;
100        }
101    }
102