| 1 | |
package org.kuali.student.enrollment.class2.acal.service; |
| 2 | |
|
| 3 | |
import org.apache.commons.lang.StringUtils; |
| 4 | |
import org.kuali.rice.core.api.criteria.Predicate; |
| 5 | |
import org.kuali.rice.core.api.criteria.QueryByCriteria; |
| 6 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; |
| 7 | |
import org.kuali.rice.krad.lookup.LookupableImpl; |
| 8 | |
import org.kuali.rice.krad.uif.UifParameters; |
| 9 | |
import org.kuali.rice.krad.util.KRADConstants; |
| 10 | |
import org.kuali.rice.krad.util.UrlFactory; |
| 11 | |
import org.kuali.rice.krad.web.form.LookupForm; |
| 12 | |
import org.kuali.student.enrollment.acal.constants.AcademicCalendarServiceConstants; |
| 13 | |
import org.kuali.student.enrollment.acal.dto.AcademicCalendarInfo; |
| 14 | |
import org.kuali.student.enrollment.acal.dto.TermInfo; |
| 15 | |
import org.kuali.student.enrollment.acal.service.AcademicCalendarService; |
| 16 | |
import org.kuali.student.r2.common.dto.ContextInfo; |
| 17 | |
import org.kuali.student.mock.utilities.TestHelper; |
| 18 | |
|
| 19 | |
import javax.xml.namespace.QName; |
| 20 | |
import java.text.ParseException; |
| 21 | |
import java.text.SimpleDateFormat; |
| 22 | |
import java.util.*; |
| 23 | |
|
| 24 | |
import static org.kuali.rice.core.api.criteria.PredicateFactory.*; |
| 25 | |
|
| 26 | 0 | public class AcademicTermLookupableImpl extends LookupableImpl { |
| 27 | |
|
| 28 | |
public final static String TERM_TYPE = "typeKey"; |
| 29 | |
public final static String TERM_NAME = "name"; |
| 30 | |
public final static String TERM_START_DATE = "startDate"; |
| 31 | |
public final static String TERM_END_DATE = "endDate"; |
| 32 | |
public final static String TERM_STATE = "stateKey"; |
| 33 | |
|
| 34 | |
private transient AcademicCalendarService academicCalendarService; |
| 35 | |
|
| 36 | |
@Override |
| 37 | |
protected List<?> getSearchResults(LookupForm lookupForm, Map<String, String> fieldValues, boolean unbounded) { |
| 38 | |
|
| 39 | 0 | TermInfo termInfo = null; |
| 40 | 0 | List<TermInfo> termInfoList = new ArrayList<TermInfo>(); |
| 41 | 0 | List<AcademicCalendarInfo> acalInfoList = new ArrayList<AcademicCalendarInfo>(); |
| 42 | |
|
| 43 | 0 | String termName = fieldValues.get(TERM_NAME); |
| 44 | 0 | String termType = fieldValues.get(TERM_TYPE); |
| 45 | 0 | String termState = fieldValues.get(TERM_STATE); |
| 46 | 0 | String termStartDate = fieldValues.get(TERM_START_DATE); |
| 47 | 0 | String termEndDate = fieldValues.get(TERM_END_DATE); |
| 48 | |
|
| 49 | 0 | ContextInfo context = new ContextInfo(); |
| 50 | |
|
| 51 | |
|
| 52 | 0 | QueryByCriteria.Builder qBuilder = QueryByCriteria.Builder.create(); |
| 53 | 0 | List<Predicate> pList = new ArrayList<Predicate>(); |
| 54 | |
Predicate p; |
| 55 | |
|
| 56 | 0 | qBuilder.setPredicates(); |
| 57 | 0 | if (StringUtils.isNotBlank(termName)){ |
| 58 | 0 | p = equal(TERM_NAME, termName); |
| 59 | 0 | pList.add(p); |
| 60 | |
} |
| 61 | |
|
| 62 | 0 | if (StringUtils.isNotBlank(termType)){ |
| 63 | 0 | p = equal("atpType", termType); |
| 64 | 0 | pList.add(p); |
| 65 | |
} |
| 66 | |
|
| 67 | 0 | if (StringUtils.isNotBlank(termState)){ |
| 68 | 0 | p = equal("atpState", termState); |
| 69 | 0 | pList.add(p); |
| 70 | |
} |
| 71 | |
|
| 72 | 0 | if (StringUtils.isNotBlank(termStartDate)){ |
| 73 | |
try { |
| 74 | 0 | p = greaterThanOrEqual(TERM_START_DATE, new SimpleDateFormat("MM/dd/yyyy").parse(termStartDate)); |
| 75 | 0 | } catch (ParseException e) { |
| 76 | 0 | throw new RuntimeException(e); |
| 77 | 0 | } |
| 78 | 0 | pList.add(p); |
| 79 | |
} |
| 80 | |
|
| 81 | 0 | if (StringUtils.isNotBlank(termEndDate)){ |
| 82 | |
try{ |
| 83 | 0 | p = lessThanOrEqual(TERM_END_DATE, new SimpleDateFormat("MM/dd/yyyy").parse(termEndDate)); |
| 84 | 0 | } catch (ParseException e) { |
| 85 | 0 | throw new RuntimeException(e); |
| 86 | 0 | } |
| 87 | 0 | pList.add(p); |
| 88 | |
} |
| 89 | |
|
| 90 | 0 | if (!pList.isEmpty()){ |
| 91 | 0 | Predicate[] preds = new Predicate[pList.size()]; |
| 92 | 0 | pList.toArray(preds); |
| 93 | 0 | qBuilder.setPredicates(and(preds)); |
| 94 | |
} |
| 95 | |
|
| 96 | |
try { |
| 97 | 0 | termInfoList = getAcademicCalendarService().searchForTerms(qBuilder.build(),context); |
| 98 | 0 | } catch (Exception e) { |
| 99 | 0 | e.printStackTrace(); |
| 100 | 0 | } |
| 101 | |
|
| 102 | 0 | return termInfoList; |
| 103 | |
} |
| 104 | |
|
| 105 | |
|
| 106 | |
@Override |
| 107 | |
public boolean allowsMaintenanceNewOrCopyAction() { |
| 108 | 0 | return false; |
| 109 | |
} |
| 110 | |
|
| 111 | |
@Override |
| 112 | |
protected String getActionUrlHref(LookupForm lookupForm, Object dataObject, String methodToCall, |
| 113 | |
List<String> pkNames) { |
| 114 | |
|
| 115 | 0 | if (dataObject == null){ |
| 116 | 0 | return StringUtils.EMPTY; |
| 117 | |
} |
| 118 | |
|
| 119 | 0 | TermInfo term = (TermInfo)dataObject; |
| 120 | |
|
| 121 | 0 | String acalId = null; |
| 122 | |
try { |
| 123 | 0 | List<AcademicCalendarInfo> atps = getAcademicCalendarService().getAcademicCalendarsForTerm(term.getId(), TestHelper.getContext1()); |
| 124 | 0 | if (!atps.isEmpty()){ |
| 125 | 0 | acalId = atps.get(0).getId(); |
| 126 | |
} |
| 127 | 0 | } catch (Exception e) { |
| 128 | 0 | throw new RuntimeException(e); |
| 129 | 0 | } |
| 130 | |
|
| 131 | 0 | if (StringUtils.isBlank(acalId)){ |
| 132 | 0 | return StringUtils.EMPTY; |
| 133 | |
} |
| 134 | |
|
| 135 | 0 | Properties props = new Properties(); |
| 136 | 0 | props.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, "start"); |
| 137 | 0 | props.put("id",acalId); |
| 138 | 0 | props.put(UifParameters.VIEW_ID, "academicCalendarEditView"); |
| 139 | |
|
| 140 | 0 | if (StringUtils.isNotBlank(lookupForm.getReturnLocation())) { |
| 141 | 0 | props.put(KRADConstants.RETURN_LOCATION_PARAMETER, lookupForm.getReturnLocation()); |
| 142 | |
} |
| 143 | |
|
| 144 | 0 | return UrlFactory.parameterizeUrl("academicCalendar", props); |
| 145 | |
} |
| 146 | |
|
| 147 | |
protected AcademicCalendarService getAcademicCalendarService() { |
| 148 | 0 | if(academicCalendarService == null) { |
| 149 | 0 | academicCalendarService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART)); |
| 150 | |
} |
| 151 | 0 | return this.academicCalendarService; |
| 152 | |
} |
| 153 | |
|
| 154 | |
} |