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.apache.commons.lang.StringUtils; 019 import org.kuali.kfs.fp.businessobject.DisbursementVoucherPayeeDetail; 020 import org.kuali.kfs.fp.document.DisbursementVoucherConstants; 021 import org.kuali.kfs.fp.document.DisbursementVoucherDocument; 022 import org.kuali.kfs.sys.KFSKeyConstants; 023 import org.kuali.kfs.sys.KFSPropertyConstants; 024 import org.kuali.kfs.sys.context.SpringContext; 025 import org.kuali.kfs.sys.document.AccountingDocument; 026 import org.kuali.kfs.sys.document.validation.GenericValidation; 027 import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent; 028 import org.kuali.kfs.vnd.businessobject.VendorDetail; 029 import org.kuali.kfs.vnd.document.service.VendorService; 030 import org.kuali.rice.kim.bo.Person; 031 import org.kuali.rice.kim.service.PersonService; 032 import org.kuali.rice.kim.util.KimConstants; 033 import org.kuali.rice.kns.util.GlobalVariables; 034 import org.kuali.rice.kns.util.MessageMap; 035 036 public class DisbursementVoucherPayeeInitiatorValidation extends GenericValidation implements DisbursementVoucherConstants{ 037 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementVoucherPayeeInitiatorValidation.class); 038 039 private AccountingDocument accountingDocumentForValidation; 040 041 public static final String DV_PAYEE_ID_NUMBER_PROPERTY_PATH = KFSPropertyConstants.DV_PAYEE_DETAIL + "." + KFSPropertyConstants.DISB_VCHR_PAYEE_ID_NUMBER; 042 043 /** 044 * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent) 045 */ 046 public boolean validate(AttributedDocumentEvent event) { 047 LOG.debug("validate start"); 048 boolean isValid = true; 049 050 DisbursementVoucherDocument document = (DisbursementVoucherDocument) accountingDocumentForValidation; 051 DisbursementVoucherPayeeDetail payeeDetail = document.getDvPayeeDetail(); 052 053 MessageMap errors = GlobalVariables.getMessageMap(); 054 errors.addToErrorPath(KFSPropertyConstants.DOCUMENT); 055 056 String uuid = null; 057 // If payee is a vendor, then look up SSN and look for SSN in the employee table 058 if (payeeDetail.isVendor() && StringUtils.isNotBlank(payeeDetail.getDisbVchrVendorHeaderIdNumber())) { 059 VendorDetail dvVendor = retrieveVendorDetail(payeeDetail.getDisbVchrVendorHeaderIdNumberAsInteger(), payeeDetail.getDisbVchrVendorDetailAssignedIdNumberAsInteger()); 060 // if the vendor tax type is SSN, then check the tax number 061 if (dvVendor != null && TAX_TYPE_SSN.equals(dvVendor.getVendorHeader().getVendorTaxTypeCode())) { 062 // check ssn against employee table 063 Person user = retrieveEmployeeBySSN(dvVendor.getVendorHeader().getVendorTaxNumber()); 064 if (user != null) { 065 uuid = user.getPrincipalId(); 066 } 067 } 068 } 069 // If payee is an employee, then pull payee from employee table 070 else if(payeeDetail.isEmployee()) { 071 uuid = payeeDetail.getDisbVchrEmployeeIdNumber(); 072 } 073 074 // If a uuid was found for payee, check it against the initiator uuid 075 if (StringUtils.isNotBlank(uuid)) { 076 Person initUser = getInitiator(document); 077 if (uuid.equals(initUser.getPrincipalId())) { 078 errors.putError(DV_PAYEE_ID_NUMBER_PROPERTY_PATH, KFSKeyConstants.ERROR_PAYEE_INITIATOR); 079 isValid = false; 080 } 081 } 082 083 errors.removeFromErrorPath(KFSPropertyConstants.DOCUMENT); 084 085 return isValid; 086 } 087 088 /** 089 * Retrieves the VendorDetail object from the vendor id number. 090 * 091 * @param vendorIdNumber vendor ID number 092 * @param vendorDetailIdNumber vendor detail ID number 093 * @return <code>VendorDetail</code> 094 */ 095 protected VendorDetail retrieveVendorDetail(Integer vendorIdNumber, Integer vendorDetailIdNumber) { 096 return SpringContext.getBean(VendorService.class).getVendorDetail(vendorIdNumber, vendorDetailIdNumber); 097 } 098 099 /** 100 * Retrieves Person from SSN 101 * 102 * @param ssnNumber social security number 103 * @return <code>Person</code> 104 */ 105 protected Person retrieveEmployeeBySSN(String ssnNumber) { 106 Person person = (Person) SpringContext.getBean(PersonService.class).getPersonByExternalIdentifier(KimConstants.PersonExternalIdentifierTypes.TAX, ssnNumber).get(0); 107 if (person == null) { 108 LOG.error("User Not Found"); 109 } 110 return person; 111 } 112 113 /** 114 * Returns the initiator of the document as a KualiUser 115 * 116 * @param document submitted document 117 * @return <code>KualiUser</code> 118 */ 119 protected Person getInitiator(AccountingDocument document) { 120 Person initUser = SpringContext.getBean(PersonService.class).getPersonByPrincipalName(document.getDocumentHeader().getWorkflowDocument().getInitiatorNetworkId()); 121 if (initUser == null) { 122 throw new RuntimeException("Document Initiator not found"); 123 } 124 125 return initUser; 126 } 127 128 /** 129 * Sets the accountingDocumentForValidation attribute value. 130 * @param accountingDocumentForValidation The accountingDocumentForValidation to set. 131 */ 132 public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) { 133 this.accountingDocumentForValidation = accountingDocumentForValidation; 134 } 135 136 /** 137 * Gets the accountingDocumentForValidation attribute. 138 * @return Returns the accountingDocumentForValidation. 139 */ 140 public AccountingDocument getAccountingDocumentForValidation() { 141 return accountingDocumentForValidation; 142 } 143 144 } 145