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.BooleanUtils;
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
21  import org.kuali.rice.core.api.util.RiceKeyConstants;
22  import org.kuali.rice.krad.lookup.LookupableImpl;
23  import org.kuali.rice.krad.util.GlobalVariables;
24  import org.kuali.rice.krad.util.KRADConstants;
25  import org.kuali.rice.krad.web.form.LookupForm;
26  import org.kuali.student.enrollment.class2.courseoffering.dto.ActivityOfferingWrapper;
27  import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingResourceLoader;
28  import org.kuali.student.enrollment.courseoffering.dto.ActivityOfferingInfo;
29  import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
30  import org.kuali.student.r2.common.constants.CommonServiceConstants;
31  import org.kuali.student.r2.common.util.ContextUtils;
32  import org.kuali.student.r2.core.class1.search.CourseOfferingManagementSearchImpl;
33  import org.kuali.student.r2.core.search.dto.SearchRequestInfo;
34  import org.kuali.student.r2.core.search.dto.SearchResultCellInfo;
35  import org.kuali.student.r2.core.search.dto.SearchResultInfo;
36  import org.kuali.student.r2.core.search.service.SearchService;
37  
38  import javax.xml.namespace.QName;
39  import java.util.ArrayList;
40  import java.util.List;
41  import java.util.Map;
42  
43  /**
44   * This class //TODO ...
45   *
46   * @author Kuali Student Team
47   */
48  public class ActivityOfferingWrapperLookupableImpl extends LookupableImpl {
49  
50      private SearchService searchService;
51  
52      @Override
53      public boolean validateSearchParameters(LookupForm form, Map<String, String> searchCriteria){
54          if (searchCriteria == null || searchCriteria.isEmpty() || StringUtils.isBlank(searchCriteria.get("termId")) || StringUtils.isBlank(searchCriteria.get("courseOfferingCode"))){
55              GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS,RiceKeyConstants.ERROR_CUSTOM,"Missing Course Offering code and/or term Id");
56              return false;
57          }
58          return true;
59      }
60  
61      @Override
62      protected List<?> getSearchResults(LookupForm lookupForm, Map<String, String> fieldValues, boolean unbounded) {
63          List<ActivityOfferingWrapper> activityOfferingWrappers = new ArrayList<ActivityOfferingWrapper>();
64  
65          String termId = fieldValues.get("termId");
66          String courseOfferingCode = fieldValues.get("courseOfferingCode");
67  
68          /**
69           * Edit AO maintenace document uses this AO search to allow user to add colocated AOs.
70           */
71          if (StringUtils.isNotBlank(termId) && StringUtils.isNotBlank(courseOfferingCode)){
72  
73              SearchRequestInfo searchRequest = new SearchRequestInfo(CourseOfferingManagementSearchImpl.CO_MANAGEMENT_SEARCH.getKey());
74              searchRequest.addParam(CourseOfferingManagementSearchImpl.SearchParameters.COURSE_CODE, StringUtils.upperCase(courseOfferingCode));
75              searchRequest.addParam(CourseOfferingManagementSearchImpl.SearchParameters.ATP_ID, termId);
76              searchRequest.addParam(CourseOfferingManagementSearchImpl.SearchParameters.IS_EXACT_MATCH_CO_CODE_SEARCH, BooleanUtils.toStringTrueFalse(true));
77  
78              try {
79                  String coId = "";
80                  SearchResultInfo searchResult = getSearchService().search(searchRequest, ContextUtils.createDefaultContextInfo());
81  
82                  if (searchResult.getRows().isEmpty()){
83                      GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM,"Invalid CourseOfferingCode " + courseOfferingCode);
84                  } else if (searchResult.getRows().size() > 1){
85                      GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM,"Multiple CourseOfferings entries found for " + courseOfferingCode);
86                  } else {
87                      for(SearchResultCellInfo cellInfo : searchResult.getRows().get(0).getCells()){
88  
89                          if(CourseOfferingManagementSearchImpl.SearchResultColumns.CO_ID.equals(cellInfo.getKey())){
90                              coId = cellInfo.getValue();
91                              break;
92                          }
93  
94                      }
95                  }
96  
97                  if (StringUtils.isNotBlank(coId)){
98                      List<ActivityOfferingInfo> aos = getCourseOfferingService().getActivityOfferingsByCourseOffering(coId,ContextUtils.createDefaultContextInfo());
99                      for (ActivityOfferingInfo activityOffering : aos) {
100                         CO_AO_RG_ViewHelperServiceImpl helper = new CO_AO_RG_ViewHelperServiceImpl();
101                         ActivityOfferingWrapper wrapper = helper.convertAOInfoToWrapper(activityOffering);
102                         wrapper.setCourseOfferingId(coId);
103                         activityOfferingWrappers.add(wrapper);
104                     }
105                 }
106 
107             } catch (Exception e){
108                 throw new RuntimeException(e);
109             }
110 
111         }
112 
113         return activityOfferingWrappers;
114     }
115 
116     public CourseOfferingService getCourseOfferingService() {
117         return CourseOfferingResourceLoader.loadCourseOfferingService();
118     }
119 
120     protected SearchService getSearchService() {
121         if(searchService == null) {
122             searchService = (SearchService) GlobalResourceLoader.getService(new QName(CommonServiceConstants.REF_OBJECT_URI_GLOBAL_PREFIX + "search", SearchService.class.getSimpleName()));
123         }
124         return searchService;
125     }
126 }