001 package org.kuali.student.enrollment.class2.courseoffering.service.impl; 002 003 import org.apache.commons.lang.StringUtils; 004 import org.kuali.rice.core.api.util.RiceKeyConstants; 005 import org.kuali.rice.kim.impl.identity.PersonImpl; 006 import org.kuali.rice.kim.impl.identity.PersonLookupableImpl; 007 import org.kuali.rice.krad.uif.field.InputField; 008 import org.kuali.rice.krad.uif.view.LookupView; 009 import org.kuali.rice.krad.util.GlobalVariables; 010 import org.kuali.rice.krad.web.form.LookupForm; 011 012 import java.util.ArrayList; 013 import java.util.List; 014 import java.util.Map; 015 016 /** 017 * Created with IntelliJ IDEA. 018 * User: swedev 019 * Date: 7/3/12 020 * Time: 3:14 PM 021 * This class was created to prevent Blank KS Person Lookups from bringing down the server. 022 */ 023 public class KSPersonLookupableImpl extends PersonLookupableImpl { 024 025 @Override 026 protected List<?> getSearchResults(LookupForm form, Map<String, String> searchCriteria, boolean unbounded) { 027 028 if(GlobalVariables.getMessageMap().hasErrors()){ 029 return new ArrayList<PersonImpl>(); 030 } else{ 031 return super.getSearchResults(form, searchCriteria, unbounded); 032 } 033 } 034 035 @Override 036 public boolean validateSearchParameters(LookupForm form, Map<String, String> searchCriteria) { 037 038 boolean valid = super.validateSearchParameters(form, searchCriteria); //To change body of overridden methods use File | Settings | File Templates. 039 040 Map<String, InputField> criteriaFields = getCriteriaFieldsForValidation((LookupView) form.getPostedView(), 041 form); 042 043 // validate something exist 044 if(this.areAllCriteriaFieldsBlank(searchCriteria)){ 045 for (Map.Entry<String, String> searchKeyValue : searchCriteria.entrySet()) { 046 String searchPropertyName = searchKeyValue.getKey(); 047 String searchPropertyValue = searchKeyValue.getValue(); 048 049 InputField inputField = criteriaFields.get(searchPropertyName); 050 if (inputField != null) { 051 if (StringUtils.isBlank(searchPropertyValue)) { 052 GlobalVariables.getMessageMap().putError(inputField.getPropertyName(), 053 RiceKeyConstants.ERROR_REQUIRED, inputField.getLabel()); 054 } 055 } else { 056 throw new RuntimeException("Invalid search field sent for property name: " + searchPropertyName); 057 } 058 } 059 060 if (GlobalVariables.getMessageMap().hasErrors()) { 061 valid = false; 062 } 063 } 064 065 return valid; 066 } 067 068 private boolean areAllCriteriaFieldsBlank(Map<String, String> searchCriteria){ 069 // validate required 070 // TODO: this will be done by the uif validation service at some point 071 int blankCount =0; 072 for (Map.Entry<String, String> searchKeyValue : searchCriteria.entrySet()) { 073 String searchPropertyValue = searchKeyValue.getValue(); 074 075 if(searchPropertyValue == null || "".equals(searchPropertyValue)){ 076 blankCount++; 077 } 078 079 } 080 return (blankCount == searchCriteria.size()); 081 } 082 083 }