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 org.kuali.kfs.fp.businessobject.DisbursementVoucherPayeeDetail;
019    import org.kuali.kfs.fp.document.DisbursementVoucherDocument;
020    import org.kuali.kfs.sys.KFSConstants;
021    import org.kuali.kfs.sys.KFSKeyConstants;
022    import org.kuali.kfs.sys.KFSPropertyConstants;
023    import org.kuali.kfs.sys.context.SpringContext;
024    import org.kuali.kfs.sys.document.AccountingDocument;
025    import org.kuali.kfs.sys.document.validation.GenericValidation;
026    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
027    import org.kuali.rice.kim.bo.Person;
028    import org.kuali.rice.kim.service.PersonService;
029    import org.kuali.rice.kns.service.DataDictionaryService;
030    import org.kuali.rice.kns.util.GlobalVariables;
031    import org.kuali.rice.kns.util.MessageMap;
032    
033    public class DisbursementVoucherEmployeeInformationValidation extends GenericValidation {
034        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementVoucherEmployeeInformationValidation.class);
035    
036        private AccountingDocument accountingDocumentForValidation;
037        
038        public static final String DV_PAYEE_ID_NUMBER_PROPERTY_PATH = KFSPropertyConstants.DV_PAYEE_DETAIL + "." + KFSPropertyConstants.DISB_VCHR_PAYEE_ID_NUMBER;
039    
040        /**
041         * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
042         */
043        public boolean validate(AttributedDocumentEvent event) {
044            LOG.debug("validate start");
045            boolean isValid = true;
046            
047            DisbursementVoucherDocument document = (DisbursementVoucherDocument) accountingDocumentForValidation;
048            DisbursementVoucherPayeeDetail payeeDetail = document.getDvPayeeDetail();
049            
050            if(!payeeDetail.isEmployee() || payeeDetail.isVendor()) {
051                return true;
052            }
053            
054            String employeeId = payeeDetail.getDisbVchrPayeeIdNumber();
055            Person employee = SpringContext.getBean(PersonService.class).getPersonByEmployeeId(employeeId);
056            
057            MessageMap errors = GlobalVariables.getMessageMap();
058            errors.addToErrorPath(KFSPropertyConstants.DOCUMENT);
059    
060            // check existence of employee
061            if (employee == null) { // If employee is not found, report existence error
062                String label = SpringContext.getBean(DataDictionaryService.class).getAttributeLabel(DisbursementVoucherPayeeDetail.class, KFSPropertyConstants.DISB_VCHR_PAYEE_ID_NUMBER);
063                errors.putError(DV_PAYEE_ID_NUMBER_PROPERTY_PATH, KFSKeyConstants.ERROR_EXISTENCE, label);
064                isValid = false;
065            } 
066            else if(!KFSConstants.EMPLOYEE_ACTIVE_STATUS.equals(employee.getEmployeeStatusCode())) {
067                // If employee is found, then check that employee is active
068                String label = SpringContext.getBean(DataDictionaryService.class).getAttributeLabel(DisbursementVoucherPayeeDetail.class, KFSPropertyConstants.DISB_VCHR_PAYEE_ID_NUMBER);
069                errors.putError(DV_PAYEE_ID_NUMBER_PROPERTY_PATH, KFSKeyConstants.ERROR_INACTIVE, label);
070                isValid = false;
071            }
072            
073            errors.removeFromErrorPath(KFSPropertyConstants.DOCUMENT); 
074    
075            return isValid;
076        }
077    
078        /**
079         * Gets the accountingDocumentForValidation attribute. 
080         * @return Returns the accountingDocumentForValidation.
081         */
082        public AccountingDocument getAccountingDocumentForValidation() {
083            return accountingDocumentForValidation;
084        }
085    
086        /**
087         * Sets the accountingDocumentForValidation attribute value.
088         * 
089         * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
090         */
091        public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
092            this.accountingDocumentForValidation = accountingDocumentForValidation;
093        }
094    }
095