1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.ole.fp.businessobject.lookup;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.kuali.ole.fp.businessobject.DisbursementPayee;
27  import org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService;
28  import org.kuali.ole.sys.OLEConstants;
29  import org.kuali.ole.sys.OLEKeyConstants;
30  import org.kuali.ole.sys.OLEPropertyConstants;
31  import org.kuali.ole.vnd.VendorConstants;
32  import org.kuali.rice.kim.impl.KIMPropertyConstants;
33  import org.kuali.rice.kns.util.KNSGlobalVariables;
34  import org.kuali.rice.kns.util.MessageList;
35  import org.kuali.rice.kns.web.struts.form.LookupForm;
36  import org.kuali.rice.kns.web.ui.ResultRow;
37  import org.kuali.rice.krad.bo.BusinessObject;
38  import org.kuali.rice.krad.exception.ValidationException;
39  import org.kuali.rice.krad.lookup.CollectionIncomplete;
40  import org.kuali.rice.krad.util.BeanPropertyComparator;
41  import org.kuali.rice.krad.util.GlobalVariables;
42  
43  public class DisbursementPayeeLookupableHelperServiceImpl extends AbstractPayeeLookupableHelperServiceImpl  {
44      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementPayeeLookupableHelperServiceImpl.class);
45      private static final int NAME_REQUIRED_FILLED_WITH_WILDCARD = 4;
46  
47      protected DisbursementVoucherPaymentReasonService disbursementVoucherPaymentReasonService;
48  
49      
50  
51  
52  
53      @Deprecated
54      public boolean checkMinimumFieldsFilled(Map fieldValues) {
55          if(StringUtils.isBlank((String) fieldValues.get(OLEPropertyConstants.VENDOR_NUMBER)) && StringUtils.isBlank((String) fieldValues.get(KIMPropertyConstants.Person.EMPLOYEE_ID)) && StringUtils.isBlank((String)fieldValues.get(KIMPropertyConstants.Person.FIRST_NAME)) &&
56                  StringUtils.isBlank((String)fieldValues.get(KIMPropertyConstants.Person.LAST_NAME)) && StringUtils.isBlank((String)fieldValues.get(OLEPropertyConstants.VENDOR_NAME))) {
57              final String vendorNumberLabel = this.getAttributeLabel(OLEPropertyConstants.VENDOR_NUMBER);
58              final String vendorNameLabel = this.getAttributeLabel(OLEPropertyConstants.VENDOR_NAME);
59              final String firstNameLabel = this.getAttributeLabel(KIMPropertyConstants.Person.FIRST_NAME);
60              final String lastNameLabel = this.getAttributeLabel(KIMPropertyConstants.Person.LAST_NAME);
61              final String employeeIdLabel = this.getAttributeLabel(KIMPropertyConstants.Person.EMPLOYEE_ID);
62  
63              GlobalVariables.getMessageMap().putError(OLEPropertyConstants.VENDOR_NUMBER, OLEKeyConstants.ERROR_DV_LOOKUP_NEEDS_SOME_FIELD, new String[] {vendorNumberLabel, employeeIdLabel, vendorNameLabel, firstNameLabel, lastNameLabel});
64              return false;
65          }
66          return true;
67      }
68  
69      
70  
71  
72  
73      @Deprecated
74      public void validateVendorNameUse(Map fieldValues) {
75          final String vendorName = (String) fieldValues.get(OLEPropertyConstants.VENDOR_NAME);
76          final String vendorNumber = (String) fieldValues.get(OLEPropertyConstants.VENDOR_NUMBER);
77          final String employeeId = (String) fieldValues.get(KIMPropertyConstants.Person.EMPLOYEE_ID);
78  
79              final boolean isVendorInfoEntered = StringUtils.isNotBlank(vendorName) || StringUtils.isNotBlank(vendorNumber);
80              if (isVendorInfoEntered && StringUtils.isNotBlank(employeeId)) {
81                  
82                  String messageKey = OLEKeyConstants.ERROR_DV_VENDOR_EMPLOYEE_CONFUSION;
83                  String vendorNameLabel = this.getAttributeLabel(OLEPropertyConstants.VENDOR_NAME);
84                  String vendorNumberLabel = this.getAttributeLabel(OLEPropertyConstants.VENDOR_NUMBER);
85              GlobalVariables.getMessageMap().putError(KIMPropertyConstants.Person.EMPLOYEE_ID, messageKey, this.getAttributeLabel(KIMPropertyConstants.Person.EMPLOYEE_ID), vendorNameLabel, vendorNumberLabel);
86              }
87  
88              if (StringUtils.isBlank(vendorNumber) && !StringUtils.isBlank(vendorName) && !filledEnough(vendorName)) {
89                  final String vendorNameLabel = this.getAttributeLabel(OLEPropertyConstants.VENDOR_NAME);
90                  GlobalVariables.getMessageMap().putError(OLEPropertyConstants.VENDOR_NAME, OLEKeyConstants.ERROR_DV_NAME_NOT_FILLED_ENOUGH, new String[] {vendorNameLabel, Integer.toString(getNameLengthWithWildcardRequirement())});
91              }
92      }
93  
94      
95  
96  
97  
98      @Deprecated
99      public void validateTaxNumberCriteria(Map fieldValues) {
100         final String employeeId = (String) fieldValues.get(KIMPropertyConstants.Person.EMPLOYEE_ID);
101         final String firstName = (String)fieldValues.get(KIMPropertyConstants.Person.FIRST_NAME);
102         final String lastName = (String)fieldValues.get(KIMPropertyConstants.Person.LAST_NAME);
103         final String vendorTaxNumber = (String)fieldValues.get(OLEPropertyConstants.TAX_NUMBER);
104 
105             
106             if(StringUtils.isNotBlank(vendorTaxNumber) && StringUtils.isNotBlank(firstName)) {
107                 fieldValues.remove(OLEPropertyConstants.TAX_NUMBER);
108                 String messageKey = OLEKeyConstants.ERROR_DV_LOOKUP_TAX_NUMBER_EMPLOYEE_DETAILS_CONFUSION;
109             GlobalVariables.getMessageMap().putError(KIMPropertyConstants.Person.FIRST_NAME, messageKey, this.getAttributeLabel(OLEPropertyConstants.TAX_NUMBER), this.getAttributeLabel(KIMPropertyConstants.Person.FIRST_NAME));
110             }
111             
112             if(StringUtils.isNotBlank(vendorTaxNumber) && StringUtils.isNotBlank(lastName)) {
113                 fieldValues.remove(OLEPropertyConstants.TAX_NUMBER);
114                 String messageKey = OLEKeyConstants.ERROR_DV_LOOKUP_TAX_NUMBER_EMPLOYEE_DETAILS_CONFUSION;
115             GlobalVariables.getMessageMap().putError(KIMPropertyConstants.Person.LAST_NAME, messageKey, this.getAttributeLabel(OLEPropertyConstants.TAX_NUMBER), this.getAttributeLabel(KIMPropertyConstants.Person.LAST_NAME));
116             }
117             
118             if(StringUtils.isNotBlank(vendorTaxNumber) && StringUtils.isNotBlank(employeeId)) {
119                 fieldValues.remove(OLEPropertyConstants.TAX_NUMBER);
120                 String messageKey = OLEKeyConstants.ERROR_DV_LOOKUP_TAX_NUMBER_EMPLOYEE_DETAILS_CONFUSION;
121             GlobalVariables.getMessageMap().putError(KIMPropertyConstants.Person.EMPLOYEE_ID, messageKey, this.getAttributeLabel(KIMPropertyConstants.Person.EMPLOYEE_ID), this.getAttributeLabel(OLEPropertyConstants.TAX_NUMBER));
122         }
123             }
124 
125     
126 
127 
128 
129     @Deprecated
130     public void validateEmployeeNameUse(Map fieldValues) {
131         final String firstName = (String)fieldValues.get(KIMPropertyConstants.Person.FIRST_NAME);
132         final String lastName = (String)fieldValues.get(KIMPropertyConstants.Person.LAST_NAME);
133         final String vendorName = (String) fieldValues.get(OLEPropertyConstants.VENDOR_NAME);
134         final String employeeId = (String) fieldValues.get(KIMPropertyConstants.Person.EMPLOYEE_ID);
135         final boolean isPersonNameEntered = StringUtils.isNotBlank(firstName) || StringUtils.isNotBlank(lastName);
136 
137             if (isPersonNameEntered && StringUtils.isNotBlank(vendorName)) {
138                 
139                 String messageKey = OLEKeyConstants.ERROR_DV_VENDOR_NAME_PERSON_NAME_CONFUSION;
140 
141                 String vendorNameLabel = this.getAttributeLabel(OLEPropertyConstants.VENDOR_NAME);
142                 String firstNameLabel = this.getAttributeLabel(KIMPropertyConstants.Person.FIRST_NAME);
143                 String lastNameLabel = this.getAttributeLabel(KIMPropertyConstants.Person.LAST_NAME);
144                 GlobalVariables.getMessageMap().putError(OLEPropertyConstants.VENDOR_NAME, messageKey, vendorNameLabel, firstNameLabel, lastNameLabel);
145             }
146             if (StringUtils.isBlank(employeeId)) {
147                 if (StringUtils.isBlank(firstName) && !StringUtils.isBlank(lastName) && !filledEnough(lastName)) {
148                     final String label = getAttributeLabel(KIMPropertyConstants.Person.LAST_NAME);
149                     GlobalVariables.getMessageMap().putError(KIMPropertyConstants.Person.LAST_NAME, OLEKeyConstants.ERROR_DV_NAME_NOT_FILLED_ENOUGH, new String[] { label, Integer.toString(getNameLengthWithWildcardRequirement() ) } );
150                 } else if (StringUtils.isBlank(lastName) && !StringUtils.isBlank(firstName) && !filledEnough(firstName)) {
151                     final String label = getAttributeLabel(KIMPropertyConstants.Person.FIRST_NAME);
152                     GlobalVariables.getMessageMap().putError(KIMPropertyConstants.Person.FIRST_NAME, OLEKeyConstants.ERROR_DV_NAME_NOT_FILLED_ENOUGH, new String[] { label, Integer.toString(getNameLengthWithWildcardRequirement() ) } );
153                 }
154             }
155         }
156 
157     
158 
159 
160     @Override
161     public void validateSearchParameters(Map fieldValues) {
162         super.validateSearchParameters(fieldValues);
163 
164         String vendorNumberLabel = getAttributeLabel(OLEPropertyConstants.VENDOR_NUMBER);
165         String taxNumberLabel = getAttributeLabel(OLEPropertyConstants.TAX_NUMBER);
166         String employeeIdLabel = getAttributeLabel(OLEPropertyConstants.EMPLOYEE_ID);
167         String payeeNameLabel = getAttributeLabel(OLEPropertyConstants.PAYEE_NAME);
168         String lastNameLabel = getAttributeLabel(OLEPropertyConstants.PERSON_LAST_NAME);
169         String firstNameLabel = getAttributeLabel(OLEPropertyConstants.PERSON_FIRST_NAME);
170 
171         String vendorNumber = (String)fieldValues.get(OLEPropertyConstants.VENDOR_NUMBER);
172         String taxNumber = (String)fieldValues.get(OLEPropertyConstants.TAX_NUMBER);
173         String employeeId = (String)fieldValues.get(OLEPropertyConstants.EMPLOYEE_ID);
174         String payeeName = (String)fieldValues.get(OLEPropertyConstants.PAYEE_NAME);
175 
176         boolean vendorNumberBlank = StringUtils.isBlank(vendorNumber);
177         boolean taxNumberBlank = StringUtils.isBlank(taxNumber);
178         boolean employeeIdBlank = StringUtils.isBlank(employeeId);
179         boolean payeeNameBlank = StringUtils.isBlank(payeeName);
180         boolean vendorInfoBlank = vendorNumberBlank && taxNumberBlank;
181 
182         
183         if (vendorNumberBlank && taxNumberBlank && employeeIdBlank && payeeNameBlank) {
184             GlobalVariables.getMessageMap().putError(OLEPropertyConstants.PAYEE_NAME, OLEKeyConstants.ERROR_DV_LOOKUP_NEEDS_SOME_FIELD, new String[]{vendorNumberLabel, taxNumberLabel, employeeIdLabel, payeeNameLabel});
185         }
186         
187         else if (!(vendorNumberBlank && taxNumberBlank) && !employeeIdBlank) {
188             GlobalVariables.getMessageMap().putError(OLEPropertyConstants.EMPLOYEE_ID, OLEKeyConstants.ERROR_DV_VENDOR_EMPLOYEE_CONFUSION, new String[]{employeeIdLabel, vendorNumberLabel, taxNumberLabel});
189         }
190         
191         else if (!vendorNumberBlank && !(taxNumberBlank && payeeNameBlank)) {
192             GlobalVariables.getMessageMap().putWarning(OLEPropertyConstants.VENDOR_NUMBER, OLEKeyConstants.WARNING_DV_PAYEE_ID_UNIQUE, new String[]{vendorNumberLabel, "Vendor"});
193         }
194         
195         else if (!employeeIdBlank && !payeeNameBlank) {
196             GlobalVariables.getMessageMap().putWarning(OLEPropertyConstants.EMPLOYEE_ID, OLEKeyConstants.WARNING_DV_PAYEE_ID_UNIQUE, new String[]{employeeIdLabel, "Employee"});
197         }
198         
199         else if (!payeeNameBlank) {
200             String minlen = Integer.toString(getNameLengthWithWildcardRequirement());
201 
202             if (!canSplit(payeeName)) {
203                 
204                 if (!filledEnough(payeeName)) {
205                     GlobalVariables.getMessageMap().putError(OLEPropertyConstants.PAYEE_NAME, OLEKeyConstants.ERROR_DV_NAME_NOT_FILLED_ENOUGH, new String[]{payeeNameLabel, minlen});
206                 }
207             }
208             else {
209                 String lastName = splitLastName(payeeName);
210                 String firstName = splitFirstName(payeeName);
211 
212                 
213                 if (StringUtils.isBlank(lastName) && StringUtils.isBlank(firstName)) {
214                     GlobalVariables.getMessageMap().putError(OLEPropertyConstants.PAYEE_NAME, OLEKeyConstants.ERROR_DV_LAST_FIRST_NAME_NOT_FILLED, new String[]{payeeNameLabel, minlen});
215                 }
216 
217                 
218                 if (!filledEnough(lastName)) {
219                     GlobalVariables.getMessageMap().putError(OLEPropertyConstants.PAYEE_NAME, OLEKeyConstants.ERROR_DV_NAME_NOT_FILLED_ENOUGH, new String[]{lastNameLabel, minlen});
220                 }
221 
222                 
223                 if (!filledEnough(firstName)) {
224                     GlobalVariables.getMessageMap().putError(OLEPropertyConstants.PAYEE_NAME, OLEKeyConstants.ERROR_DV_NAME_NOT_FILLED_ENOUGH, new String[]{firstNameLabel, minlen});
225                 }
226             }
227         }
228 
229         if (GlobalVariables.getMessageMap().hasErrors()) {
230             throw new ValidationException("errors in search criteria");
231         }
232     }
233 
234      
235 
236 
237 
238     @Override
239     public Collection performLookup(LookupForm lookupForm, Collection resultTable, boolean bounded) {
240         Map<String, String> fieldValues = lookupForm.getFieldsForLookup();
241         String paymentReasonCode = fieldValues.get(OLEPropertyConstants.PAYMENT_REASON_CODE);
242 
243         List<DisbursementPayee> displayList = (List<DisbursementPayee>) super.performLookup(lookupForm, resultTable, bounded);
244         this.filterReturnUrl((List<ResultRow>) resultTable, displayList, paymentReasonCode);
245 
246         MessageList messageList = KNSGlobalVariables.getMessageList();
247         disbursementVoucherPaymentReasonService.postPaymentReasonCodeUsage(paymentReasonCode, messageList);
248 
249         return displayList;
250     }
251 
252     
253 
254 
255     @Override
256     public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
257         List<DisbursementPayee> searchResults = new ArrayList<DisbursementPayee>();
258 
259         if (StringUtils.isNotBlank(fieldValues.get(OLEPropertyConstants.VENDOR_NUMBER)) || StringUtils.isNotBlank(fieldValues.get(OLEPropertyConstants.TAX_NUMBER))) {
260             searchResults.addAll(this.getVendorsAsPayees(fieldValues));
261         }
262         else if (StringUtils.isNotBlank(fieldValues.get(OLEPropertyConstants.EMPLOYEE_ID))) {
263             searchResults.addAll(this.getPersonAsPayees(fieldValues));
264         }
265         else {
266             searchResults.addAll(this.getVendorsAsPayees(fieldValues));
267             searchResults.addAll(this.getPersonAsPayees(fieldValues));
268         }
269 
270         return sortSearchResults(searchResults);
271     }
272 
273     
274 
275 
276     protected List<? extends BusinessObject> sortSearchResults(List<DisbursementPayee> searchResults) {
277         CollectionIncomplete results = new CollectionIncomplete(searchResults, Long.valueOf(searchResults.size()));
278 
279         
280         List<String> defaultSortColumns = getDefaultSortColumns();
281         if (defaultSortColumns.size() > 0) {
282             Collections.sort(results, new BeanPropertyComparator(getDefaultSortColumns(), true));
283         }
284 
285         return results;
286     }
287 
288     
289 
290 
291     protected boolean canSplit(String payeeName) {
292         return StringUtils.contains(payeeName, OLEConstants.COMMA);
293     }
294 
295     
296 
297 
298 
299     protected String splitLastName(String payeeName) {
300         return StringUtils.strip(StringUtils.substringBefore(payeeName, OLEConstants.COMMA));
301     }
302 
303     
304 
305 
306 
307     protected String splitFirstName(String payeeName) {
308         return StringUtils.strip(StringUtils.substringAfter(payeeName, OLEConstants.COMMA));
309     }
310 
311 
312     
313 
314 
315 
316 
317     protected boolean filledEnough(String s) {
318         return !containsLookupWildcard(s) || StringUtils.length(s) >= getNameLengthWithWildcardRequirement();
319     }
320 
321     
322 
323 
324     protected int getNameLengthWithWildcardRequirement() {
325         return NAME_REQUIRED_FILLED_WITH_WILDCARD;
326     }
327 
328     
329 
330 
331 
332 
333     protected boolean containsLookupWildcard(String s) {
334         return StringUtils.contains(s, "*") || StringUtils.contains(s, "%");
335     }
336 
337     
338 
339 
340     @Override
341     protected Map<String, String> getVendorFieldValues(Map<String, String> fieldValues) {
342         String vendorNumber = fieldValues.get(OLEPropertyConstants.VENDOR_NUMBER);
343         String taxNumber = fieldValues.get(OLEPropertyConstants.TAX_NUMBER);
344         String payeeName = fieldValues.get(OLEPropertyConstants.PAYEE_NAME);
345         boolean vendorNumberBlank = StringUtils.isBlank(vendorNumber);
346         boolean taxNumberBlank = StringUtils.isBlank(taxNumber);
347         boolean payeeNameBlank = StringUtils.isBlank(payeeName);
348 
349         Map<String, String> vendorFieldValues = new HashMap<String, String>();
350         vendorFieldValues.put(OLEPropertyConstants.ACTIVE, fieldValues.get(OLEPropertyConstants.ACTIVE));
351 
352         if (!vendorNumberBlank) {
353             
354             vendorFieldValues.put(OLEPropertyConstants.VENDOR_NUMBER, vendorNumber);
355         }
356         else {
357             if (!taxNumberBlank) {
358                 vendorFieldValues.put(OLEPropertyConstants.TAX_NUMBER, taxNumber);
359             }
360             if (!payeeNameBlank) {
361                 vendorFieldValues.put(OLEPropertyConstants.PAYEE_NAME, getVendorName(payeeName));
362             }
363         }
364 
365         Map<String, String> fieldConversionMap = disbursementVoucherPayeeService.getFieldConversionBetweenPayeeAndVendor();
366         replaceFieldKeys(vendorFieldValues, fieldConversionMap);
367         return vendorFieldValues;
368     }
369 
370     
371 
372 
373     protected String getVendorName(String payeeName) {
374         String vendorName = "";
375 
376         
377         if (!canSplit(payeeName)) {
378             vendorName = payeeName;
379         }
380         
381         else {
382             
383             String lastName = splitLastName(payeeName);
384             if (StringUtils.isBlank(lastName)) {
385                 lastName = "*";
386             }
387             
388             String firstName = splitFirstName(payeeName);
389             if (StringUtils.isBlank(firstName)) {
390                 firstName = "*";
391             }
392             
393             vendorName = lastName + VendorConstants.NAME_DELIM + firstName;
394         }
395 
396         return vendorName;
397     }
398 
399     
400 
401 
402     @Override
403     protected Map<String, String> getPersonFieldValues(Map<String, String> fieldValues) {
404         String employeeId = fieldValues.get(OLEPropertyConstants.EMPLOYEE_ID);
405         String payeeName = fieldValues.get(OLEPropertyConstants.PAYEE_NAME);
406         boolean employeeIdBlank = StringUtils.isBlank(employeeId);
407         boolean payeeNameBlank = StringUtils.isBlank(payeeName);
408 
409         Map<String, String> personFieldValues = new HashMap<String, String>();
410         personFieldValues.put(OLEPropertyConstants.ACTIVE, fieldValues.get(OLEPropertyConstants.ACTIVE));
411 
412         
413         if (!employeeIdBlank) {
414             personFieldValues.put(OLEPropertyConstants.EMPLOYEE_ID, employeeId);
415         }
416         
417         else if (!payeeNameBlank) {
418             
419             String lastName = splitLastName(payeeName);
420             if (StringUtils.isNotBlank(lastName)) {
421                 personFieldValues.put(OLEPropertyConstants.PERSON_LAST_NAME, getVendorName(lastName));
422             }
423             
424             String firstName = splitFirstName(payeeName);
425             if (StringUtils.isNotBlank(firstName)) {
426                 personFieldValues.put(OLEPropertyConstants.PERSON_FIRST_NAME, getVendorName(firstName));
427             }
428         }
429 
430         Map<String, String> fieldConversionMap = disbursementVoucherPayeeService.getFieldConversionBetweenPayeeAndPerson();
431         this.replaceFieldKeys(personFieldValues, fieldConversionMap);
432         return personFieldValues;
433     }
434 
435     
436 
437 
438 
439 
440 
441 
442     protected void filterReturnUrl(List<ResultRow> resultRowList, List<DisbursementPayee> payeeList, String paymentReasonCode) {
443         Collection<String> payeeTypeCodes = disbursementVoucherPaymentReasonService.getPayeeTypesByPaymentReason(paymentReasonCode);
444         if (payeeTypeCodes == null || payeeTypeCodes.isEmpty()) {
445             return;
446         }
447 
448         for (int index = 0; index < payeeList.size(); index++) {
449             DisbursementPayee payee = payeeList.get(index);
450 
451             boolean isQualified = disbursementVoucherPaymentReasonService.isPayeeQualifiedForPayment(payee, paymentReasonCode, payeeTypeCodes);
452             if (!isQualified) {
453                 resultRowList.get(index).setReturnUrl(StringUtils.EMPTY);
454             }
455         }
456     }
457 
458     
459 
460 
461 
462 
463     public void setDisbursementVoucherPaymentReasonService(DisbursementVoucherPaymentReasonService disbursementVoucherPaymentReasonService) {
464         this.disbursementVoucherPaymentReasonService = disbursementVoucherPaymentReasonService;
465     }
466 
467 }