001/**
002 * Copyright 2014 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 * Created by venkat on 7/22/14
016 */
017package org.kuali.student.cm.course.service.impl;
018
019import org.apache.commons.lang.StringUtils;
020import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
021import org.kuali.rice.krad.lookup.LookupForm;
022import org.kuali.rice.krad.uif.UifConstants;
023import org.kuali.rice.krad.uif.element.Action;
024import org.kuali.rice.krad.util.KRADConstants;
025import org.kuali.rice.krad.util.UrlFactory;
026import org.kuali.student.cm.common.util.CMUtils;
027import org.kuali.student.cm.common.util.CurriculumManagementConstants;
028import org.kuali.student.cm.course.service.CourseLookupable;
029import org.kuali.student.cm.course.util.CourseProposalUtil;
030import org.kuali.student.common.uif.service.impl.KSLookupableImpl;
031import org.kuali.student.common.util.security.ContextUtils;
032import org.kuali.student.lum.lu.ui.krms.dto.CluInformation;
033import org.kuali.student.lum.lu.ui.krms.util.CluSearchUtil;
034import org.kuali.student.r2.core.search.dto.SearchParamInfo;
035import org.kuali.student.r2.core.search.dto.SearchRequestInfo;
036import org.kuali.student.r2.core.search.dto.SearchResultInfo;
037import org.kuali.student.r2.lum.clu.service.CluService;
038import org.kuali.student.r2.lum.util.constants.CluServiceConstants;
039
040import javax.xml.namespace.QName;
041import java.util.ArrayList;
042import java.util.List;
043import java.util.Map;
044import java.util.Properties;
045
046import static org.apache.commons.lang.StringUtils.isEmpty;
047
048/**
049 *
050 * @author Kuali Student Team
051 */
052public class CourseLookupableImpl extends KSLookupableImpl implements CourseLookupable {
053
054    private static final long serialVersionUID = 1L;
055
056    private transient CluService cluService;
057
058    public enum QueryParamEnum {
059        ID("lu.queryParam.luOptionalId","id"),
060        TITLE("lu.queryParam.luOptionalLongName", "title"),
061        CODE("lu.queryParam.luOptionalCode", "code"),
062        DESCRIPTION("lu.queryParam.luOptionalDescr", "description");
063
064        private final String fieldValue;
065        private final String queryKey;
066
067        QueryParamEnum(String qKey, String fValue) {
068            this.queryKey = qKey;
069            this.fieldValue = fValue;
070        }
071
072        public String getFieldValue() {
073            return fieldValue;
074        }
075
076        public String getQueryKey() {
077            return queryKey;
078        }
079    }
080
081    @Override
082    public List<?> performSearch(LookupForm lookupForm, Map<String, String> searchCriteria, boolean bounded) {
083
084        List<SearchParamInfo> searchParams = new ArrayList<SearchParamInfo>();
085        searchParams.add(CluSearchUtil.getTypeSearchParamForCourse());
086        searchParams.add(CluSearchUtil.getApprovedStateSearchParam());
087        for (QueryParamEnum qpEnum : QueryParamEnum.values()) {
088            String fieldValue = searchCriteria.get(qpEnum.getFieldValue());
089            if ( ! isEmpty(fieldValue) ) {
090                SearchParamInfo qpv = new SearchParamInfo();
091                qpv.setKey(qpEnum.getQueryKey());
092                qpv.getValues().add(fieldValue);
093                searchParams.add(qpv);
094            }
095        }
096
097        SearchRequestInfo searchRequest = new SearchRequestInfo();
098        searchRequest.setParams(searchParams);
099        searchRequest.setSearchKey("lu.search.mostCurrent.union");
100        searchRequest.setSortColumn("lu.resultColumn.luOptionalCode");
101
102        try {
103            SearchResultInfo searchResult = getCluService().search(searchRequest, ContextUtils.createDefaultContextInfo());
104            return CluSearchUtil.resolveCluSearchResultSet(searchResult);
105        } catch (Exception e) {
106            throw new RuntimeException(e);
107        }
108    }
109
110    public void buildViewCourseUrl(Action actionLink, Object model) {
111
112        Object dataObject = actionLink.getContext().get(UifConstants.ContextVariableNames.LINE);
113
114        String cluId = ((CluInformation)dataObject).getCluId();
115
116        String href = CourseProposalUtil.getViewCourseUrl(cluId);
117
118        if (StringUtils.isBlank(href)) {
119            actionLink.setRender(false);
120            return;
121        }
122
123        actionLink.setActionScript("window.open('" + href + "', '_self');");
124    }
125
126    protected CluService getCluService() {
127        if (cluService == null) {
128            cluService = GlobalResourceLoader.getService(new QName(CluServiceConstants.CLU_NAMESPACE, CluServiceConstants.SERVICE_NAME_LOCAL_PART));
129        }
130        return cluService;
131    }
132
133}