001/**
002 * Copyright 2012 The Kuali Foundation Licensed under the
003 * Educational Community License, Version 2.0 (the "License"); you may
004 * not use this file except in compliance with the License. You may
005 * obtain a copy of the License at
006 *
007 * http://www.osedu.org/licenses/ECL-2.0
008 *
009 * Unless required by applicable law or agreed to in writing,
010 * software distributed under the License is distributed on an "AS IS"
011 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012 * or implied. See the License for the specific language governing
013 * permissions and limitations under the License.
014 *
015 */
016package org.kuali.student.enrollment.class2.courseoffering.service.impl;
017
018import org.apache.commons.lang.BooleanUtils;
019import org.apache.commons.lang.StringUtils;
020import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
021import org.kuali.rice.core.api.util.RiceKeyConstants;
022import org.kuali.rice.krad.lookup.LookupableImpl;
023import org.kuali.rice.krad.util.GlobalVariables;
024import org.kuali.rice.krad.util.KRADConstants;
025import org.kuali.rice.krad.web.form.LookupForm;
026import org.kuali.student.enrollment.class2.courseoffering.dto.ActivityOfferingWrapper;
027import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingResourceLoader;
028import org.kuali.student.enrollment.courseoffering.dto.ActivityOfferingInfo;
029import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
030import org.kuali.student.r2.common.constants.CommonServiceConstants;
031import org.kuali.student.r2.common.util.ContextUtils;
032import org.kuali.student.r2.core.class1.search.CourseOfferingManagementSearchImpl;
033import org.kuali.student.r2.core.search.dto.SearchRequestInfo;
034import org.kuali.student.r2.core.search.dto.SearchResultCellInfo;
035import org.kuali.student.r2.core.search.dto.SearchResultInfo;
036import org.kuali.student.r2.core.search.service.SearchService;
037
038import javax.xml.namespace.QName;
039import java.util.ArrayList;
040import java.util.List;
041import java.util.Map;
042
043/**
044 * This class //TODO ...
045 *
046 * @author Kuali Student Team
047 */
048public class ActivityOfferingWrapperLookupableImpl extends LookupableImpl {
049
050    private SearchService searchService;
051
052    @Override
053    public boolean validateSearchParameters(LookupForm form, Map<String, String> searchCriteria){
054        if (searchCriteria == null || searchCriteria.isEmpty() || StringUtils.isBlank(searchCriteria.get("termId")) || StringUtils.isBlank(searchCriteria.get("courseOfferingCode"))){
055            GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS,RiceKeyConstants.ERROR_CUSTOM,"Missing Course Offering code and/or term Id");
056            return false;
057        }
058        return true;
059    }
060
061    @Override
062    protected List<?> getSearchResults(LookupForm lookupForm, Map<String, String> fieldValues, boolean unbounded) {
063        List<ActivityOfferingWrapper> activityOfferingWrappers = new ArrayList<ActivityOfferingWrapper>();
064
065        String termId = fieldValues.get("termId");
066        String courseOfferingCode = fieldValues.get("courseOfferingCode");
067
068        /**
069         * Edit AO maintenace document uses this AO search to allow user to add colocated AOs.
070         */
071        if (StringUtils.isNotBlank(termId) && StringUtils.isNotBlank(courseOfferingCode)){
072
073            SearchRequestInfo searchRequest = new SearchRequestInfo(CourseOfferingManagementSearchImpl.CO_MANAGEMENT_SEARCH.getKey());
074            searchRequest.addParam(CourseOfferingManagementSearchImpl.SearchParameters.COURSE_CODE, StringUtils.upperCase(courseOfferingCode));
075            searchRequest.addParam(CourseOfferingManagementSearchImpl.SearchParameters.ATP_ID, termId);
076            searchRequest.addParam(CourseOfferingManagementSearchImpl.SearchParameters.IS_EXACT_MATCH_CO_CODE_SEARCH, BooleanUtils.toStringTrueFalse(true));
077
078            try {
079                String coId = "";
080                SearchResultInfo searchResult = getSearchService().search(searchRequest, ContextUtils.createDefaultContextInfo());
081
082                if (searchResult.getRows().isEmpty()){
083                    GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM,"Invalid CourseOfferingCode " + courseOfferingCode);
084                } else if (searchResult.getRows().size() > 1){
085                    GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM,"Multiple CourseOfferings entries found for " + courseOfferingCode);
086                } else {
087                    for(SearchResultCellInfo cellInfo : searchResult.getRows().get(0).getCells()){
088
089                        if(CourseOfferingManagementSearchImpl.SearchResultColumns.CO_ID.equals(cellInfo.getKey())){
090                            coId = cellInfo.getValue();
091                            break;
092                        }
093
094                    }
095                }
096
097                if (StringUtils.isNotBlank(coId)){
098                    List<ActivityOfferingInfo> aos = getCourseOfferingService().getActivityOfferingsByCourseOffering(coId,ContextUtils.createDefaultContextInfo());
099                    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}