| 1 | |
package org.kuali.student.enrollment.class2.acal.service.impl; |
| 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.uif.UifParameters; |
| 8 | |
import org.kuali.rice.krad.uif.service.impl.ViewHelperServiceImpl; |
| 9 | |
import org.kuali.rice.krad.util.KRADConstants; |
| 10 | |
import org.kuali.student.enrollment.acal.constants.AcademicCalendarServiceConstants; |
| 11 | |
import org.kuali.student.enrollment.acal.dto.AcademicCalendarInfo; |
| 12 | |
import org.kuali.student.enrollment.acal.dto.HolidayCalendarInfo; |
| 13 | |
import org.kuali.student.enrollment.acal.dto.TermInfo; |
| 14 | |
import org.kuali.student.enrollment.acal.service.AcademicCalendarService; |
| 15 | |
import org.kuali.student.enrollment.class2.acal.service.CalendarSearchViewHelperService; |
| 16 | |
import org.kuali.student.enrollment.class2.acal.util.CalendarConstants; |
| 17 | |
import org.kuali.student.r2.common.dto.ContextInfo; |
| 18 | |
|
| 19 | |
import javax.xml.namespace.QName; |
| 20 | |
import java.text.SimpleDateFormat; |
| 21 | |
import java.util.ArrayList; |
| 22 | |
import java.util.List; |
| 23 | |
import java.util.Properties; |
| 24 | |
|
| 25 | |
import static org.kuali.rice.core.api.criteria.PredicateFactory.*; |
| 26 | |
|
| 27 | 0 | public class CalendarSearchViewHelperServiceImpl extends ViewHelperServiceImpl implements CalendarSearchViewHelperService { |
| 28 | |
|
| 29 | |
private transient AcademicCalendarService academicCalendarService; |
| 30 | |
|
| 31 | |
public final static String NAME = "name"; |
| 32 | |
public final static String START_DATE = "startDate"; |
| 33 | |
public final static String END_DATE = "endDate"; |
| 34 | |
public final static String CALENDAR_TYPE = "atpType"; |
| 35 | |
|
| 36 | |
|
| 37 | |
public List<TermInfo> searchForTerms(String name, String year,ContextInfo context)throws Exception { |
| 38 | |
|
| 39 | 0 | List<TermInfo> termInfoList = new ArrayList<TermInfo>(); |
| 40 | |
|
| 41 | 0 | QueryByCriteria.Builder query = buildQueryByCriteria(name,year,"Term"); |
| 42 | |
|
| 43 | 0 | List<TermInfo> terms = getAcademicCalendarService().searchForTerms(query.build(),context); |
| 44 | 0 | for (TermInfo term : terms) { |
| 45 | 0 | termInfoList.add(term); |
| 46 | |
} |
| 47 | |
|
| 48 | 0 | return termInfoList; |
| 49 | |
|
| 50 | |
} |
| 51 | |
|
| 52 | |
public List<AcademicCalendarInfo> searchForAcademicCalendars(String name, String year,ContextInfo context)throws Exception { |
| 53 | |
|
| 54 | 0 | List<AcademicCalendarInfo> acalInfoList = new ArrayList<AcademicCalendarInfo>(); |
| 55 | |
|
| 56 | 0 | QueryByCriteria.Builder query = buildQueryByCriteria(name,year,AcademicCalendarServiceConstants.ACADEMIC_CALENDAR_TYPE_KEY); |
| 57 | |
|
| 58 | 0 | List<AcademicCalendarInfo> acals = getAcademicCalendarService().searchForAcademicCalendars(query.build(), context); |
| 59 | 0 | for (AcademicCalendarInfo acal : acals) { |
| 60 | 0 | acalInfoList.add(acal); |
| 61 | |
} |
| 62 | |
|
| 63 | 0 | return acalInfoList; |
| 64 | |
|
| 65 | |
|
| 66 | |
} |
| 67 | |
|
| 68 | |
public List<HolidayCalendarInfo> searchForHolidayCalendars(String name, String year,ContextInfo context)throws Exception { |
| 69 | |
|
| 70 | 0 | List<HolidayCalendarInfo> hCals = new ArrayList<HolidayCalendarInfo>(); |
| 71 | |
|
| 72 | 0 | QueryByCriteria.Builder query = buildQueryByCriteria(name,year,AcademicCalendarServiceConstants.HOLIDAY_CALENDAR_TYPE_KEY); |
| 73 | |
|
| 74 | 0 | List<HolidayCalendarInfo> hcs = getAcademicCalendarService().searchForHolidayCalendars(query.build(), context); |
| 75 | 0 | for (HolidayCalendarInfo hc : hcs) { |
| 76 | 0 | hCals.add(hc); |
| 77 | |
} |
| 78 | |
|
| 79 | 0 | return hCals; |
| 80 | |
|
| 81 | |
|
| 82 | |
} |
| 83 | |
|
| 84 | |
private QueryByCriteria.Builder buildQueryByCriteria(String name, String year,String typeKey){ |
| 85 | |
|
| 86 | 0 | QueryByCriteria.Builder qBuilder = QueryByCriteria.Builder.create(); |
| 87 | 0 | List<Predicate> pList = new ArrayList<Predicate>(); |
| 88 | |
Predicate p; |
| 89 | |
|
| 90 | 0 | qBuilder.setPredicates(); |
| 91 | 0 | if (StringUtils.isNotBlank(name)){ |
| 92 | 0 | p = like(NAME, "%" + name + "%"); |
| 93 | 0 | pList.add(p); |
| 94 | |
} |
| 95 | |
|
| 96 | 0 | if (StringUtils.isNotBlank(year)){ |
| 97 | |
try { |
| 98 | |
|
| 99 | 0 | Predicate startDatePredicate = and(greaterThanOrEqual(START_DATE, new SimpleDateFormat("MM/dd/yyyy").parse("01/01/" + year)), |
| 100 | |
lessThanOrEqual(START_DATE, new SimpleDateFormat("MM/dd/yyyy").parse("12/31/" + year))); |
| 101 | |
|
| 102 | |
|
| 103 | 0 | Predicate endDatePredicate = and(greaterThanOrEqual(END_DATE, new SimpleDateFormat("MM/dd/yyyy").parse("01/01/" + year)), |
| 104 | |
lessThanOrEqual(END_DATE, new SimpleDateFormat("MM/dd/yyyy").parse("12/31/" + year))); |
| 105 | |
|
| 106 | 0 | pList.add(or(startDatePredicate, endDatePredicate)); |
| 107 | |
|
| 108 | 0 | } catch (Exception e) { |
| 109 | 0 | throw new RuntimeException(e); |
| 110 | 0 | } |
| 111 | |
|
| 112 | |
} |
| 113 | |
|
| 114 | 0 | if (StringUtils.equalsIgnoreCase(typeKey, "Term")){ |
| 115 | 0 | p = notIn(CALENDAR_TYPE,AcademicCalendarServiceConstants.ACADEMIC_CALENDAR_TYPE_KEY,AcademicCalendarServiceConstants.HOLIDAY_CALENDAR_TYPE_KEY); |
| 116 | |
}else{ |
| 117 | 0 | p = equal(CALENDAR_TYPE,typeKey); |
| 118 | |
} |
| 119 | |
|
| 120 | 0 | pList.add(p); |
| 121 | |
|
| 122 | 0 | if (!pList.isEmpty()){ |
| 123 | 0 | Predicate[] preds = new Predicate[pList.size()]; |
| 124 | 0 | pList.toArray(preds); |
| 125 | 0 | qBuilder.setPredicates(and(preds)); |
| 126 | |
} |
| 127 | 0 | return qBuilder; |
| 128 | |
} |
| 129 | |
|
| 130 | |
public Properties buildTermURLParameters(TermInfo term, String methodToCall, boolean readOnlyView, ContextInfo context){ |
| 131 | |
|
| 132 | 0 | String acalId = null; |
| 133 | |
try { |
| 134 | 0 | List<AcademicCalendarInfo> atps = getAcademicCalendarService().getAcademicCalendarsForTerm(term.getId(), context); |
| 135 | 0 | if (!atps.isEmpty()){ |
| 136 | 0 | acalId = atps.get(0).getId(); |
| 137 | |
} |
| 138 | 0 | } catch (Exception e) { |
| 139 | 0 | throw new RuntimeException(e); |
| 140 | 0 | } |
| 141 | |
|
| 142 | 0 | Properties props = new Properties(); |
| 143 | 0 | props.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, methodToCall); |
| 144 | 0 | props.put(CalendarConstants.CALENDAR_ID,acalId); |
| 145 | 0 | props.put(UifParameters.VIEW_ID, CalendarConstants.ACAL_VIEW); |
| 146 | 0 | props.put(CalendarConstants.PAGE_ID,CalendarConstants.ACADEMIC_CALENDAR_EDIT_PAGE); |
| 147 | 0 | props.put(CalendarConstants.SELECT_TAB,CalendarConstants.ACAL_TERM_TAB); |
| 148 | |
|
| 149 | 0 | if (readOnlyView){ |
| 150 | 0 | props.put(CalendarConstants.READ_ONLY_VIEW,""+ true); |
| 151 | |
} |
| 152 | |
|
| 153 | 0 | return props; |
| 154 | |
|
| 155 | |
} |
| 156 | |
|
| 157 | |
public Properties buildACalURLParameters(AcademicCalendarInfo acal, String methodToCall, boolean readOnlyView, ContextInfo context){ |
| 158 | |
|
| 159 | 0 | Properties props = new Properties(); |
| 160 | 0 | props.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, methodToCall); |
| 161 | 0 | props.put(CalendarConstants.CALENDAR_ID,acal.getId()); |
| 162 | 0 | props.put(UifParameters.VIEW_ID, CalendarConstants.ACAL_VIEW); |
| 163 | |
|
| 164 | 0 | if (StringUtils.equals(methodToCall,CalendarConstants.AC_COPY_METHOD)){ |
| 165 | 0 | props.put(CalendarConstants.PAGE_ID,CalendarConstants.ACADEMIC_CALENDAR_COPY_PAGE); |
| 166 | |
} else { |
| 167 | 0 | props.put(CalendarConstants.PAGE_ID,CalendarConstants.ACADEMIC_CALENDAR_EDIT_PAGE); |
| 168 | |
} |
| 169 | |
|
| 170 | 0 | if (readOnlyView){ |
| 171 | 0 | props.put(CalendarConstants.READ_ONLY_VIEW,""+ true); |
| 172 | |
} |
| 173 | |
|
| 174 | 0 | return props; |
| 175 | |
|
| 176 | |
} |
| 177 | |
|
| 178 | |
public Properties buildHCalURLParameters(HolidayCalendarInfo hcInfo, String methodToCall, boolean readOnlyView, ContextInfo context){ |
| 179 | |
|
| 180 | 0 | Properties props = new Properties(); |
| 181 | 0 | props.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, methodToCall); |
| 182 | 0 | props.put(CalendarConstants.CALENDAR_ID, hcInfo.getId()); |
| 183 | 0 | props.put(UifParameters.VIEW_ID, CalendarConstants.HOLIDAYCALENDAR_FLOWVIEW); |
| 184 | |
|
| 185 | 0 | if (StringUtils.equals(methodToCall,CalendarConstants.HC_COPY_METHOD)){ |
| 186 | 0 | props.put(CalendarConstants.PAGE_ID,CalendarConstants.HOLIDAYCALENDAR_COPYPAGE); |
| 187 | 0 | }else if (StringUtils.equals(methodToCall,CalendarConstants.HC_VIEW_METHOD) && readOnlyView){ |
| 188 | 0 | props.put(CalendarConstants.PAGE_ID,CalendarConstants.HOLIDAYCALENDAR_VIEWPAGE); |
| 189 | |
} else { |
| 190 | 0 | props.put(CalendarConstants.PAGE_ID,CalendarConstants.HOLIDAYCALENDAR_EDITPAGE); |
| 191 | |
} |
| 192 | |
|
| 193 | 0 | return props; |
| 194 | |
|
| 195 | |
} |
| 196 | |
|
| 197 | |
protected AcademicCalendarService getAcademicCalendarService() { |
| 198 | 0 | if(academicCalendarService == null) { |
| 199 | 0 | academicCalendarService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART)); |
| 200 | |
} |
| 201 | 0 | return this.academicCalendarService; |
| 202 | |
} |
| 203 | |
} |