View Javadoc

1   package org.kuali.student.enrollment.class2.courseoffering.service.impl;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.rice.core.api.util.RiceKeyConstants;
5   import org.kuali.rice.kim.impl.identity.PersonImpl;
6   import org.kuali.rice.kim.impl.identity.PersonLookupableImpl;
7   import org.kuali.rice.krad.uif.field.InputField;
8   import org.kuali.rice.krad.uif.view.LookupView;
9   import org.kuali.rice.krad.util.GlobalVariables;
10  import org.kuali.rice.krad.web.form.LookupForm;
11  
12  import java.util.ArrayList;
13  import java.util.List;
14  import java.util.Map;
15  
16  /**
17   * Created with IntelliJ IDEA.
18   * User: swedev
19   * Date: 7/3/12
20   * Time: 3:14 PM
21   * This class was created to prevent Blank KS Person Lookups from bringing down the server.
22   */
23  public class KSPersonLookupableImpl extends PersonLookupableImpl {
24  
25      @Override
26      protected List<?> getSearchResults(LookupForm form, Map<String, String> searchCriteria, boolean unbounded) {
27  
28          if(GlobalVariables.getMessageMap().hasErrors()){
29              return new ArrayList<PersonImpl>();
30          }  else{
31              return super.getSearchResults(form, searchCriteria, unbounded);
32          }
33      }
34  
35      @Override
36      public boolean validateSearchParameters(LookupForm form, Map<String, String> searchCriteria) {
37  
38          boolean valid = super.validateSearchParameters(form, searchCriteria);    //To change body of overridden methods use File | Settings | File Templates.
39  
40          Map<String, InputField> criteriaFields = getCriteriaFieldsForValidation((LookupView) form.getPostedView(),
41                  form);
42  
43          // validate something exist
44          if(this.areAllCriteriaFieldsBlank(searchCriteria)){
45              for (Map.Entry<String, String> searchKeyValue : searchCriteria.entrySet()) {
46                  String searchPropertyName = searchKeyValue.getKey();
47                  String searchPropertyValue = searchKeyValue.getValue();
48  
49                  InputField inputField = criteriaFields.get(searchPropertyName);
50                  if (inputField != null) {
51                      if (StringUtils.isBlank(searchPropertyValue)) {
52                          GlobalVariables.getMessageMap().putError(inputField.getPropertyName(),
53                                  RiceKeyConstants.ERROR_REQUIRED, inputField.getLabel());
54                      }
55                  } else {
56                      throw new RuntimeException("Invalid search field sent for property name: " + searchPropertyName);
57                  }
58              }
59  
60              if (GlobalVariables.getMessageMap().hasErrors()) {
61                  valid = false;
62              }
63          }
64  
65          return valid;
66      }
67  
68      private boolean areAllCriteriaFieldsBlank(Map<String, String> searchCriteria){
69          // validate required
70          // TODO: this will be done by the uif validation service at some point
71          int blankCount =0;
72          for (Map.Entry<String, String> searchKeyValue : searchCriteria.entrySet()) {
73              String searchPropertyValue = searchKeyValue.getValue();
74  
75              if(searchPropertyValue == null || "".equals(searchPropertyValue)){
76                  blankCount++;
77              }
78  
79          }
80          return (blankCount == searchCriteria.size());
81      }
82  
83  }