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(form,searchCriteria)){
045                for (Map.Entry<String, String> searchKeyValue : searchCriteria.entrySet()) {
046                    String searchPropertyName = searchKeyValue.getKey();
047                    String searchPropertyValue = searchKeyValue.getValue();
048    
049                    LookupView lookupView = (LookupView) form.getPostedView();
050                    InputField inputField = criteriaFields.get(searchPropertyName);
051                    if (inputField != null) {
052                        if (StringUtils.isBlank(searchPropertyValue)) {
053                            GlobalVariables.getMessageMap().putError(inputField.getPropertyName(),
054                                    RiceKeyConstants.ERROR_REQUIRED, inputField.getLabel());
055                        }
056                    } else {
057                        throw new RuntimeException("Invalid search field sent for property name: " + searchPropertyName);
058                    }
059                }
060    
061                if (GlobalVariables.getMessageMap().hasErrors()) {
062                    valid = false;
063                }
064            }
065    
066            return valid;
067        }
068    
069        private boolean areAllCriteriaFieldsBlank(LookupForm form, Map<String, String> searchCriteria){
070            Map<String, InputField> criteriaFields = getCriteriaFieldsForValidation((LookupView) form.getPostedView(),
071                    form);
072    
073            // validate required
074            // TODO: this will be done by the uif validation service at some point
075            int blankCount =0;
076            for (Map.Entry<String, String> searchKeyValue : searchCriteria.entrySet()) {
077                String searchPropertyValue = searchKeyValue.getValue();
078    
079                if(searchPropertyValue == null || "".equals(searchPropertyValue)){
080                    blankCount++;
081                }
082    
083            }
084            return (blankCount == searchCriteria.size());
085        }
086    
087    }