View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.fp.document.validation.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.fp.businessobject.DisbursementVoucherPayeeDetail;
20  import org.kuali.ole.fp.document.DisbursementVoucherConstants;
21  import org.kuali.ole.fp.document.DisbursementVoucherDocument;
22  import org.kuali.ole.sys.OLEKeyConstants;
23  import org.kuali.ole.sys.OLEPropertyConstants;
24  import org.kuali.ole.sys.context.SpringContext;
25  import org.kuali.ole.sys.document.AccountingDocument;
26  import org.kuali.ole.sys.document.validation.GenericValidation;
27  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
28  import org.kuali.ole.vnd.businessobject.VendorDetail;
29  import org.kuali.ole.vnd.document.service.VendorService;
30  import org.kuali.rice.kim.api.KimConstants;
31  import org.kuali.rice.kim.api.identity.Person;
32  import org.kuali.rice.kim.api.identity.PersonService;
33  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
34  import org.kuali.rice.krad.util.GlobalVariables;
35  import org.kuali.rice.krad.util.MessageMap;
36  
37  public class DisbursementVoucherPayeeInitiatorValidation extends GenericValidation implements DisbursementVoucherConstants{
38      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementVoucherPayeeInitiatorValidation.class);
39      
40      private AccountingDocument accountingDocumentForValidation;
41      
42      public static final String DV_PAYEE_ID_NUMBER_PROPERTY_PATH = OLEPropertyConstants.DV_PAYEE_DETAIL + "." + OLEPropertyConstants.DISB_VCHR_PAYEE_ID_NUMBER;
43  
44      /**
45       * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
46       */
47      public boolean validate(AttributedDocumentEvent event) {
48          LOG.debug("validate start");        
49          boolean isValid = true;
50          
51          DisbursementVoucherDocument document = (DisbursementVoucherDocument) accountingDocumentForValidation;
52          DisbursementVoucherPayeeDetail payeeDetail = document.getDvPayeeDetail();
53          
54          MessageMap errors = GlobalVariables.getMessageMap();
55          errors.addToErrorPath(OLEPropertyConstants.DOCUMENT);
56  
57          String uuid = null;
58          // If payee is a vendor, then look up SSN and look for SSN in the employee table
59          if (payeeDetail.isVendor() && StringUtils.isNotBlank(payeeDetail.getDisbVchrVendorHeaderIdNumber())) {
60              VendorDetail dvVendor = retrieveVendorDetail(payeeDetail.getDisbVchrVendorHeaderIdNumberAsInteger(), payeeDetail.getDisbVchrVendorDetailAssignedIdNumberAsInteger());
61              // if the vendor tax type is SSN, then check the tax number
62              if (dvVendor != null && TAX_TYPE_SSN.equals(dvVendor.getVendorHeader().getVendorTaxTypeCode())) {
63                  // check ssn against employee table
64                  Person user = retrieveEmployeeBySSN(dvVendor.getVendorHeader().getVendorTaxNumber());
65                  if (user != null) {
66                      uuid = user.getPrincipalId();
67                  }
68              }
69          }
70          // If payee is an employee, then pull payee from employee table
71          else if(payeeDetail.isEmployee()) {
72              uuid = payeeDetail.getDisbVchrEmployeeIdNumber();
73          }
74  
75          // If a uuid was found for payee, check it against the initiator uuid
76          if (StringUtils.isNotBlank(uuid)) {
77              Person initUser = getInitiator(document);
78              if (uuid.equals(initUser.getPrincipalId())) {
79                  errors.putError(DV_PAYEE_ID_NUMBER_PROPERTY_PATH, OLEKeyConstants.ERROR_PAYEE_INITIATOR);
80                  isValid = false;
81              }
82          }
83          
84          errors.removeFromErrorPath(OLEPropertyConstants.DOCUMENT);   
85          
86          return isValid;
87      }
88  
89      /**
90       * Retrieves the VendorDetail object from the vendor id number.
91       * 
92       * @param vendorIdNumber vendor ID number
93       * @param vendorDetailIdNumber vendor detail ID number
94       * @return <code>VendorDetail</code>
95       */
96      protected VendorDetail retrieveVendorDetail(Integer vendorIdNumber, Integer vendorDetailIdNumber) {
97          return SpringContext.getBean(VendorService.class).getVendorDetail(vendorIdNumber, vendorDetailIdNumber);
98      }
99  
100     /**
101      * Retrieves Person from SSN
102      * 
103      * @param ssnNumber social security number
104      * @return <code>Person</code>
105      */
106     protected Person retrieveEmployeeBySSN(String ssnNumber) {
107         Person person = (Person) SpringContext.getBean(PersonService.class).getPersonByExternalIdentifier(KimConstants.PersonExternalIdentifierTypes.TAX, ssnNumber).get(0);
108         if (person == null) {
109             LOG.error("User Not Found");
110         }
111         return person;
112     }
113     
114     /**
115      * Returns the initiator of the document as a KualiUser
116      * 
117      * @param document submitted document
118      * @return <code>KualiUser</code>
119      */
120     protected Person getInitiator(AccountingDocument document) {
121         Person initUser = KimApiServiceLocator.getPersonService().getPerson(document.getDocumentHeader().getWorkflowDocument().getInitiatorPrincipalId());
122         if (initUser == null) {
123             throw new RuntimeException("Document Initiator not found");
124         }
125 
126         return initUser;
127     }
128     
129     /**
130      * Sets the accountingDocumentForValidation attribute value.
131      * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
132      */
133     public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
134         this.accountingDocumentForValidation = accountingDocumentForValidation;
135     }
136 
137     /**
138      * Gets the accountingDocumentForValidation attribute. 
139      * @return Returns the accountingDocumentForValidation.
140      */
141     public AccountingDocument getAccountingDocumentForValidation() {
142         return accountingDocumentForValidation;
143     }
144 
145 }
146