View Javadoc

1   package org.kuali.student.enrollment.state.service.impl;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import java.util.Map;
6   import javax.xml.namespace.QName;
7   import org.kuali.rice.core.api.criteria.Predicate;
8   import org.kuali.rice.core.api.criteria.PredicateFactory;
9   import org.kuali.rice.core.api.criteria.QueryByCriteria;
10  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
11  import org.kuali.rice.krad.lookup.LookupableImpl;
12  import org.kuali.rice.krad.web.form.LookupForm;
13  import org.kuali.student.enrollment.common.util.ContextBuilder;
14  import org.kuali.student.r2.common.dto.ContextInfo;
15  import org.kuali.student.r2.common.dto.RichTextInfo;
16  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
17  import org.kuali.student.r2.common.exceptions.MissingParameterException;
18  import org.kuali.student.r2.common.exceptions.OperationFailedException;
19  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
20  import org.kuali.student.r2.core.constants.StateServiceConstants;
21  import org.kuali.student.r2.core.class1.state.dto.StateInfo;
22  import org.kuali.student.r2.core.class1.state.service.StateService;
23  
24  public class StateInfoAdminLookupableImpl
25          extends LookupableImpl {
26  
27      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(StateInfoAdminLookupableImpl.class);
28      private transient StateService stateService;
29      private final static String SEARCH_VALUE = "searchValue";
30  
31      @Override
32      protected List<StateInfo> getSearchResults(LookupForm lookupForm, Map<String, String> fieldValues, boolean unbounded) {
33  
34          String searchValue = fieldValues.get(SEARCH_VALUE);
35          searchValue = searchValue.toLowerCase();
36          return this.findMatching(searchValue);
37      }
38  
39      private List<StateInfo> findMatching(String searchValue) {
40          QueryByCriteria.Builder qBuilder = QueryByCriteria.Builder.create();
41          List<Predicate> pList = new ArrayList<Predicate>();
42          pList.add (PredicateFactory.equal("keywordSearch",searchValue));
43          qBuilder.setPredicates(PredicateFactory.and(pList.toArray(new Predicate[pList.size()])));
44          try {
45              List<StateInfo> list = this.getStateService().searchForStates(qBuilder.build(), getContextInfo());
46  //            System.out.println ("Found " + list.size() + " lifecycles");
47  //            for (StateInfo info : list) {
48  //                System.out.println (info.getKey() + " " + info.getName());
49  //            }
50              return list;
51          } catch (InvalidParameterException ex) {
52              throw new RuntimeException(ex);
53          } catch (MissingParameterException ex) {
54              throw new RuntimeException(ex);
55          } catch (OperationFailedException ex) {
56              throw new RuntimeException(ex);
57          } catch (PermissionDeniedException ex) {
58              throw new RuntimeException(ex);
59          }
60          // replaced below with keyword search
61  //        List<StateInfo> allStates = this.getAll();
62  //        List<StateInfo> list = new ArrayList<StateInfo>();
63  //        for (StateInfo info : allStates) {
64  //            if (this.matches(info, searchValue)) {
65  //                list.add(info);
66  //            }
67  //        }
68  //        return list;
69      }
70  
71      private boolean matches(StateInfo info, String searchValue) {
72          if (searchValue == null) {
73              return true;
74          }
75          if (searchValue.isEmpty()) {
76              return true;
77          }
78          if (contains(info.getKey(), searchValue)) {
79              return true;
80          }
81          if (contains(info.getName(), searchValue)) {
82              return true;
83          }
84          if (contains(info.getDescr(), searchValue)) {
85              return true;
86          }
87          if (contains(info.getLifecycleKey(), searchValue)) {
88              return true;
89          }
90          return false;
91      }
92  
93      private boolean contains(RichTextInfo fieldValue, String searchValue) {
94          if (fieldValue == null) {
95              return false;
96          }
97          if (contains(fieldValue.getPlain(), searchValue)) {
98              return true;
99          }
100         if (contains(fieldValue.getFormatted(), searchValue)) {
101             return true;
102         }
103         return false;
104     }
105 
106     private boolean contains(Object fieldValue, String searchValue) {
107         if (fieldValue == null) {
108             return false;
109         }
110         String fieldValueString = fieldValue.toString().toLowerCase();
111         if (fieldValueString.contains(searchValue)) {
112             return true;
113         }
114         return false;
115     }
116 
117     private List<StateInfo> getAll() {
118         QueryByCriteria.Builder qBuilder = QueryByCriteria.Builder.create();
119 //        List<Predicate> pList = new ArrayList<Predicate>();
120 //
121 //        pList.add (PredicateFactory.equal("keywordSearch","testing");
122 //        pList.add(p);
123 //
124 //        Predicate[] preds = new Predicate[pList.size()];
125 //        pList.toArray(preds);
126 //        qBuilder.setPredicates(PredicateFactory.and(preds));
127         try {
128             return getStateService ().searchForStates(qBuilder.build(), getContextInfo());
129         } catch (InvalidParameterException ex) {
130             throw new RuntimeException(ex);
131         } catch (MissingParameterException ex) {
132             throw new RuntimeException(ex);
133         } catch (OperationFailedException ex) {
134             throw new RuntimeException(ex);
135         } catch (PermissionDeniedException ex) {
136             throw new RuntimeException(ex);
137         }
138     }
139 
140     public void setStateService(StateService stateService) {
141         this.stateService = stateService;
142     }
143 
144     public StateService getStateService() {
145         if (stateService == null) {
146             stateService = (StateService) GlobalResourceLoader.getService(new QName(StateServiceConstants.NAMESPACE,
147                     StateServiceConstants.SERVICE_NAME_LOCAL_PART));
148         }
149         return this.stateService;
150     }
151 
152     private ContextInfo getContextInfo() {
153         return ContextBuilder.loadContextInfo();
154     }
155 }