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 */
016 package org.kuali.student.enrollment.class2.courseoffering.service.impl;
017
018 import org.apache.commons.lang.StringUtils;
019 import org.kuali.rice.core.api.criteria.Predicate;
020 import org.kuali.rice.core.api.criteria.PredicateFactory;
021 import org.kuali.rice.core.api.criteria.QueryByCriteria;
022 import org.kuali.rice.krad.lookup.LookupableImpl;
023 import org.kuali.rice.krad.web.form.LookupForm;
024 import org.kuali.student.enrollment.class2.courseoffering.util.ActivityOfferingConstants;
025 import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingResourceLoader;
026 import org.kuali.student.enrollment.common.util.ContextBuilder;
027 import org.kuali.student.enrollment.courseoffering.dto.ActivityOfferingInfo;
028 import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
029 import org.kuali.student.r2.common.dto.ContextInfo;
030 import org.kuali.student.r2.common.exceptions.*;
031
032 import java.util.*;
033
034 /**
035 * This class //TODO ...
036 *
037 * @author Kuali Student Team
038 */
039 public class ActivityOfferingLookupableImpl extends LookupableImpl {
040
041 @Override
042 protected List<?> getSearchResults(LookupForm lookupForm, Map<String, String> fieldValues, boolean unbounded) {
043 List<ActivityOfferingInfo> activityOfferingInfos = new ArrayList<ActivityOfferingInfo>();
044
045 try {
046 if(hasCriteria(fieldValues)){
047 QueryByCriteria qbc = buildQueryByCriteria(fieldValues);
048 activityOfferingInfos = getCourseOfferingService().searchForActivityOfferings(qbc, getContextInfo());
049 }
050 } catch (Exception e) {
051 throw new RuntimeException(e);
052 }
053
054 return activityOfferingInfos;
055 }
056
057 private QueryByCriteria buildQueryByCriteria(Map<String, String> fieldValues){
058 String aoId = fieldValues.get(ActivityOfferingConstants.ACTIVITYOFFERING_ID);
059
060 List<Predicate> predicates = new ArrayList<Predicate>();
061 if (StringUtils.isNotBlank(aoId)) {
062 predicates.add(PredicateFactory.equalIgnoreCase("id", aoId));
063 }
064
065 QueryByCriteria.Builder qbcBuilder = QueryByCriteria.Builder.create();
066 qbcBuilder.setPredicates(predicates.toArray(new Predicate[predicates.size()]));
067 QueryByCriteria qbc = qbcBuilder.build();
068
069 return qbc;
070 }
071
072 private boolean hasCriteria(Map<String, String> fieldValues){
073 return StringUtils.isNotBlank(fieldValues.get(ActivityOfferingConstants.ACTIVITYOFFERING_ID));
074 }
075
076 public CourseOfferingService getCourseOfferingService() {
077 return CourseOfferingResourceLoader.loadCourseOfferingService();
078 }
079
080 public ContextInfo getContextInfo() {
081 return ContextBuilder.loadContextInfo();
082 }
083 }