View Javadoc

1   package org.kuali.student.enrollment.class2.courseoffering.service.impl;
2   
3   import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
4   import org.kuali.rice.krad.lookup.LookupableImpl;
5   import org.kuali.rice.krad.web.form.LookupForm;
6   import org.kuali.student.r1.common.search.dto.SearchParam;
7   import org.kuali.student.r1.common.search.dto.SearchRequest;
8   import org.kuali.student.r1.common.search.dto.SearchResult;
9   import org.kuali.student.r1.common.search.dto.SearchResultCell;
10  import org.kuali.student.r1.common.search.dto.SearchResultRow;
11  import org.kuali.student.r2.common.util.ContextUtils;
12  import org.kuali.student.r2.lum.clu.service.CluService;
13  import org.kuali.student.r2.lum.course.dto.CourseInfo;
14  import org.kuali.student.r2.lum.course.service.CourseService;
15  import org.kuali.student.r2.lum.util.constants.CluServiceConstants;
16  import org.kuali.student.r2.lum.util.constants.CourseServiceConstants;
17  
18  import javax.xml.namespace.QName;
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.Map;
22  
23  import static org.apache.commons.lang.StringUtils.isEmpty;
24  
25  public class CourseInfoLookupableImpl extends LookupableImpl {
26  	private static final long serialVersionUID = 1L;	
27  	
28      private transient CluService cluService;
29      private transient CourseService courseService;
30  
31      public enum QueryParamEnum {
32          ID("lu.queryParam.luOptionalId","id"),
33          SUBJECT("lu.queryParam.luOptionalStudySubjectArea", "subjectArea"),
34          CODE("lu.queryParam.luOptionalCode", "code"),
35          TITLE("lu.queryParam.luOptionalLongName", "courseTitle");
36  
37          private final String fieldValue;
38          private final String queryKey;
39  
40          QueryParamEnum(String qKey, String fValue) {
41              this.queryKey = qKey;
42              this.fieldValue = fValue;
43          }
44  
45          public String getFieldValue() {
46              return fieldValue;
47          }
48  
49          public String getQueryKey() {
50              return queryKey;
51          }
52      }
53  
54      @Override
55      protected List<?> getSearchResults(LookupForm lookupForm, Map<String, String> fieldValues, boolean unbounded) {
56          List <CourseInfo> courseInfoList = new ArrayList<CourseInfo>();
57          String courseId;
58          List<SearchParam> searchParams = new ArrayList<SearchParam>();
59          SearchParam qpv1 = new SearchParam();
60          qpv1.setKey("lu.queryParam.luOptionalType");
61          qpv1.setValue("kuali.lu.type.CreditCourse");
62          searchParams.add(qpv1);
63          for (QueryParamEnum qpEnum : QueryParamEnum.values()) {
64              String fieldValue = fieldValues.get(qpEnum.getFieldValue());
65              if ( ! isEmpty(fieldValue) ) {
66                  SearchParam qpv = new SearchParam();
67                  qpv.setKey(qpEnum.getQueryKey());
68                  qpv.setValue(fieldValue);
69                  searchParams.add(qpv);
70              }
71          }
72  
73          SearchRequest searchRequest = new SearchRequest();
74          searchRequest.setParams(searchParams);
75          searchRequest.setSearchKey("lu.search.mostCurrent.union");
76  
77          try {
78              SearchResult searchResult = getCluService().search(searchRequest);
79  
80              if (searchResult.getRows().size() > 0) {
81                  for(SearchResultRow srrow : searchResult.getRows()){
82                      List<SearchResultCell> srCells = srrow.getCells();
83                      if(srCells != null && srCells.size() > 0){
84                          for(SearchResultCell srcell : srCells){
85                              if (srcell.getKey().equals("lu.resultColumn.cluId")) {
86                                  courseId = srcell.getValue();
87                                  CourseInfo course = getCourseService().getCourse(courseId, ContextUtils.getContextInfo());
88                                  courseInfoList.add(course);
89                              }
90                          }
91                      }
92                  }
93              }
94  
95              return courseInfoList;
96          } catch (Exception e) {
97              throw new RuntimeException(e);
98          }
99      }
100 
101     //Note: here I am using r1 CluService implementation!!!
102     protected CluService getCluService() {
103         if(cluService == null) {
104             cluService = (CluService)GlobalResourceLoader.getService(new QName(CluServiceConstants.CLU_NAMESPACE,CluServiceConstants.SERVICE_NAME_LOCAL_PART));
105         }
106         return this.cluService;
107     }
108 
109     protected CourseService getCourseService() {
110         if(courseService == null) {
111             courseService = (CourseService)GlobalResourceLoader.getService(new QName(CourseServiceConstants.COURSE_NAMESPACE,"CourseService"));
112         }
113         return this.courseService;
114     }
115 }