View Javadoc

1   /**
2    * Copyright 2012 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   *
15   */
16  package org.kuali.student.enrollment.class2.courseoffering.service.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.criteria.Predicate;
20  import org.kuali.rice.core.api.criteria.PredicateFactory;
21  import org.kuali.rice.core.api.criteria.QueryByCriteria;
22  import org.kuali.rice.krad.lookup.LookupableImpl;
23  import org.kuali.rice.krad.web.form.LookupForm;
24  import org.kuali.student.enrollment.class2.courseoffering.util.ActivityOfferingConstants;
25  import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingResourceLoader;
26  import org.kuali.student.enrollment.common.util.ContextBuilder;
27  import org.kuali.student.enrollment.courseoffering.dto.ActivityOfferingInfo;
28  import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
29  import org.kuali.student.r2.common.dto.ContextInfo;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  import java.util.Map;
34  
35  /**
36   * This class //TODO ...
37   *
38   * @author Kuali Student Team
39   */
40  public class ActivityOfferingLookupableImpl extends LookupableImpl {
41  
42      @Override
43      protected List<?> getSearchResults(LookupForm lookupForm, Map<String, String> fieldValues, boolean unbounded) {
44          List<ActivityOfferingInfo> activityOfferingInfos = new ArrayList<ActivityOfferingInfo>();
45  
46          try {
47              if(hasCriteria(fieldValues)){
48                  QueryByCriteria qbc = buildQueryByCriteria(fieldValues);
49                  activityOfferingInfos = getCourseOfferingService().searchForActivityOfferings(qbc, getContextInfo());
50              }
51          } catch (Exception e) {
52             throw new RuntimeException(e);
53          }
54  
55          return activityOfferingInfos;
56      }
57  
58      private QueryByCriteria buildQueryByCriteria(Map<String, String> fieldValues){
59          String aoId = fieldValues.get(ActivityOfferingConstants.ACTIVITYOFFERING_ID);
60  
61          List<Predicate> predicates = new ArrayList<Predicate>();
62          if (StringUtils.isNotBlank(aoId)) {
63              predicates.add(PredicateFactory.equalIgnoreCase("id", aoId));
64          }
65  
66          QueryByCriteria.Builder qbcBuilder = QueryByCriteria.Builder.create();
67          qbcBuilder.setPredicates(predicates.toArray(new Predicate[predicates.size()]));
68          QueryByCriteria qbc = qbcBuilder.build();
69  
70          return qbc;
71      }
72  
73      private boolean hasCriteria(Map<String, String> fieldValues){
74          return StringUtils.isNotBlank(fieldValues.get(ActivityOfferingConstants.ACTIVITYOFFERING_ID));
75      }
76  
77      public CourseOfferingService getCourseOfferingService() {
78          return CourseOfferingResourceLoader.loadCourseOfferingService();
79      }
80  
81      public ContextInfo getContextInfo() {
82          return ContextBuilder.loadContextInfo();
83      }
84  }