View Javadoc
1   /*
2    * Copyright 2012 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.businessobject.lookup;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.ole.fp.businessobject.DisbursementPayee;
25  import org.kuali.ole.fp.document.service.DisbursementVoucherPayeeService;
26  import org.kuali.ole.sys.OLEConstants;
27  import org.kuali.ole.sys.OLEPropertyConstants;
28  import org.kuali.ole.sys.context.SpringContext;
29  import org.kuali.ole.vnd.VendorConstants;
30  import org.kuali.ole.vnd.VendorPropertyConstants;
31  import org.kuali.ole.vnd.businessobject.VendorDetail;
32  import org.kuali.rice.kim.api.identity.Person;
33  import org.kuali.rice.kim.api.identity.PersonService;
34  import org.kuali.rice.kim.impl.KIMPropertyConstants;
35  import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
36  import org.kuali.rice.kns.lookup.Lookupable;
37  import org.kuali.rice.kns.service.DataDictionaryService;
38  import org.kuali.rice.krad.bo.BusinessObject;
39  import org.kuali.rice.krad.datadictionary.AttributeSecurity;
40  import org.kuali.rice.krad.util.ObjectUtils;
41  
42  public class AbstractPayeeLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
43  
44      protected Lookupable vendorLookupable;
45      protected DisbursementVoucherPayeeService disbursementVoucherPayeeService;
46  
47      // get the label for the given attribute of the current business object
48      protected String getAttributeLabel(String attributeName) {
49          return this.getDataDictionaryService().getAttributeLabel(getBusinessObjectClass(), attributeName);
50      }
51  
52      // perform vendor search
53      protected List<DisbursementPayee> getVendorsAsPayees(Map<String, String> fieldValues) {
54          List<DisbursementPayee> payeeList = new ArrayList<DisbursementPayee>();
55  
56          Map<String, String> fieldsForLookup = this.getVendorFieldValues(fieldValues);
57          vendorLookupable.setBusinessObjectClass(VendorDetail.class);
58          vendorLookupable.validateSearchParameters(fieldsForLookup);
59  
60          List<? extends BusinessObject> vendorList = vendorLookupable.getSearchResults(fieldsForLookup);
61          for (BusinessObject vendor : vendorList) {
62              VendorDetail vendorDetail = (VendorDetail) vendor;
63              DisbursementPayee payee = getPayeeFromVendor(vendorDetail, fieldValues);
64              payeeList.add(payee);
65          }
66  
67          return payeeList;
68      }
69  
70      // get the search criteria valid for vendor lookup
71      protected Map<String, String> getVendorFieldValues(Map<String, String> fieldValues) {
72          Map<String, String> vendorFieldValues = new HashMap<String, String>();
73          vendorFieldValues.put(OLEPropertyConstants.TAX_NUMBER, fieldValues.get(OLEPropertyConstants.TAX_NUMBER));
74          vendorFieldValues.put(OLEPropertyConstants.VENDOR_NAME, fieldValues.get(OLEPropertyConstants.VENDOR_NAME));
75          vendorFieldValues.put(OLEPropertyConstants.VENDOR_NUMBER, fieldValues.get(OLEPropertyConstants.VENDOR_NUMBER));
76          vendorFieldValues.put(OLEPropertyConstants.PERSON_FIRST_NAME, fieldValues.get(OLEPropertyConstants.PERSON_FIRST_NAME));
77          vendorFieldValues.put(OLEPropertyConstants.PERSON_LAST_NAME, fieldValues.get(OLEPropertyConstants.PERSON_LAST_NAME));
78          vendorFieldValues.put(OLEPropertyConstants.ACTIVE, fieldValues.get(OLEPropertyConstants.ACTIVE));
79  
80          Map<String, String> fieldConversionMap = disbursementVoucherPayeeService.getFieldConversionBetweenPayeeAndVendor();
81          this.replaceFieldKeys(vendorFieldValues, fieldConversionMap);
82  
83          String vendorName = this.getVendorName(vendorFieldValues);
84          if (StringUtils.isNotBlank(vendorName)) {
85              vendorFieldValues.put(OLEPropertyConstants.VENDOR_NAME, vendorName);
86          }
87  
88          vendorFieldValues.remove(VendorPropertyConstants.VENDOR_FIRST_NAME);
89          vendorFieldValues.remove(VendorPropertyConstants.VENDOR_LAST_NAME);
90  
91          return vendorFieldValues;
92      }
93  
94      // get the vendor name from the given field value map
95      protected String getVendorName(Map<String, String> vendorFieldValues) {
96          String firstName = vendorFieldValues.get(VendorPropertyConstants.VENDOR_FIRST_NAME);
97          String lastName = vendorFieldValues.get(VendorPropertyConstants.VENDOR_LAST_NAME);
98  
99          // TODO The following code has a bug: it doesn't handle blank first name right.
100         if (StringUtils.isNotBlank(lastName)) {
101             return lastName + VendorConstants.NAME_DELIM + firstName;
102         }
103         else if (StringUtils.isNotBlank(firstName)) {
104             return OLEConstants.WILDCARD_CHARACTER + VendorConstants.NAME_DELIM + firstName;
105         }
106 
107         return StringUtils.EMPTY;
108     }
109 
110     protected DisbursementPayee getPayeeFromVendor(VendorDetail vendorDetail, Map<String, String> fieldValues) {
111         DisbursementPayee payee = disbursementVoucherPayeeService.getPayeeFromVendor(vendorDetail);
112         payee.setPaymentReasonCode(fieldValues.get(OLEPropertyConstants.PAYMENT_REASON_CODE));
113 
114         //KFSMI-5497
115         //get the attributeSecurity property and mask the field so that on results screen will be shown masked.
116         DataDictionaryService dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
117         AttributeSecurity attributeSecurity =  dataDictionaryService.getAttributeSecurity(DisbursementPayee.class.getName(), OLEPropertyConstants.TAX_NUMBER);
118         if (ObjectUtils.isNotNull(attributeSecurity)) {
119             attributeSecurity.setMask(true);
120         }
121 
122         return payee;
123     }
124 
125     // perform person search
126     protected List<DisbursementPayee> getPersonAsPayees(Map<String, String> fieldValues) {
127         List<DisbursementPayee> payeeList = new ArrayList<DisbursementPayee>();
128 
129         Map<String, String> fieldsForLookup = this.getPersonFieldValues(fieldValues);
130         List<Person> persons = SpringContext.getBean(PersonService.class).findPeople(fieldsForLookup);
131 
132         for (Person personDetail : persons) {
133             DisbursementPayee payee = getPayeeFromPerson(personDetail, fieldValues);
134             payeeList.add(payee);
135         }
136 
137         return payeeList;
138     }
139 
140     // get the search criteria valid for person lookup
141     protected Map<String, String> getPersonFieldValues(Map<String, String> fieldValues) {
142         Map<String, String> personFieldValues = new HashMap<String, String>();
143         personFieldValues.put(OLEPropertyConstants.PERSON_FIRST_NAME, fieldValues.get(OLEPropertyConstants.PERSON_FIRST_NAME));
144         personFieldValues.put(OLEPropertyConstants.PERSON_LAST_NAME, fieldValues.get(OLEPropertyConstants.PERSON_LAST_NAME));
145         personFieldValues.put(OLEPropertyConstants.EMPLOYEE_ID, fieldValues.get(OLEPropertyConstants.EMPLOYEE_ID));
146         personFieldValues.put(OLEPropertyConstants.ACTIVE, fieldValues.get(OLEPropertyConstants.ACTIVE));
147         Map<String, String> fieldConversionMap = disbursementVoucherPayeeService.getFieldConversionBetweenPayeeAndPerson();
148         this.replaceFieldKeys(personFieldValues, fieldConversionMap);
149 
150         personFieldValues.put(KIMPropertyConstants.Person.EMPLOYEE_STATUS_CODE, OLEConstants.EMPLOYEE_ACTIVE_STATUS);
151 
152         return personFieldValues;
153     }
154 
155     protected DisbursementPayee getPayeeFromPerson(Person personDetail, Map<String, String> fieldValues) {
156         DisbursementPayee payee = disbursementVoucherPayeeService.getPayeeFromPerson(personDetail);
157         payee.setPaymentReasonCode(fieldValues.get(OLEPropertyConstants.PAYMENT_REASON_CODE));
158 
159         //OLEMI-5497
160         //get the attributeSecurity property and unmask the field so that on results screen, it will set as blank.
161         DataDictionaryService dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
162         AttributeSecurity attributeSecurity =  dataDictionaryService.getAttributeSecurity(DisbursementPayee.class.getName(), OLEPropertyConstants.TAX_NUMBER);
163         if (ObjectUtils.isNotNull(attributeSecurity)) {
164             attributeSecurity.setMask(false);
165         }
166 
167         return payee;
168     }
169 
170     // replace the keys in fieldValues with the corresponding values defined in fieldConversionMap
171     protected void replaceFieldKeys(Map<String, String> fieldValues, Map<String, String> fieldConversionMap) {
172         for (String key : fieldConversionMap.keySet()) {
173             if (fieldValues.containsKey(key)) {
174                 String value = fieldValues.get(key);
175                 String newKey = fieldConversionMap.get(key);
176                 fieldValues.remove(key);
177                 fieldValues.put(newKey, value);
178             }
179         }
180     }
181 
182     /**
183      * Sets the vendorLookupable attribute value.
184      *
185      * @param vendorLookupable The vendorLookupable to set.
186      */
187     public void setVendorLookupable(Lookupable vendorLookupable) {
188         this.vendorLookupable = vendorLookupable;
189     }
190 
191     /**
192      * Sets the disbursementVoucherPayeeService attribute value.
193      *
194      * @param disbursementVoucherPayeeService The disbursementVoucherPayeeService to set.
195      */
196     public void setDisbursementVoucherPayeeService(DisbursementVoucherPayeeService disbursementVoucherPayeeService) {
197         this.disbursementVoucherPayeeService = disbursementVoucherPayeeService;
198     }
199 
200 }