| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.student.enrollment.class2.acal.controller; |
| 17 | |
|
| 18 | |
import org.apache.commons.lang.StringUtils; |
| 19 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; |
| 20 | |
import org.kuali.rice.core.api.util.RiceKeyConstants; |
| 21 | |
import org.kuali.rice.krad.uif.UifParameters; |
| 22 | |
import org.kuali.rice.krad.uif.util.ObjectPropertyUtils; |
| 23 | |
import org.kuali.rice.krad.util.GlobalVariables; |
| 24 | |
import org.kuali.rice.krad.util.KRADConstants; |
| 25 | |
import org.kuali.rice.krad.web.controller.UifControllerBase; |
| 26 | |
import org.kuali.rice.krad.web.form.UifFormBase; |
| 27 | |
import org.kuali.student.enrollment.acal.constants.AcademicCalendarServiceConstants; |
| 28 | |
import org.kuali.student.enrollment.acal.dto.AcademicCalendarInfo; |
| 29 | |
import org.kuali.student.enrollment.acal.dto.HolidayCalendarInfo; |
| 30 | |
import org.kuali.student.enrollment.acal.dto.TermInfo; |
| 31 | |
import org.kuali.student.enrollment.acal.service.AcademicCalendarService; |
| 32 | |
import org.kuali.student.enrollment.class2.acal.form.CalendarSearchForm; |
| 33 | |
import org.kuali.student.enrollment.class2.acal.service.CalendarSearchViewHelperService; |
| 34 | |
import org.kuali.student.enrollment.class2.acal.util.CalendarConstants; |
| 35 | |
import org.kuali.student.r2.common.dto.ContextInfo; |
| 36 | |
import org.kuali.student.r2.common.dto.StatusInfo; |
| 37 | |
import org.kuali.student.mock.utilities.TestHelper; |
| 38 | |
import org.springframework.stereotype.Controller; |
| 39 | |
import org.springframework.validation.BindingResult; |
| 40 | |
import org.springframework.web.bind.annotation.ModelAttribute; |
| 41 | |
import org.springframework.web.bind.annotation.RequestMapping; |
| 42 | |
import org.springframework.web.bind.annotation.RequestMethod; |
| 43 | |
import org.springframework.web.servlet.ModelAndView; |
| 44 | |
|
| 45 | |
import javax.servlet.http.HttpServletRequest; |
| 46 | |
import javax.servlet.http.HttpServletResponse; |
| 47 | |
import javax.xml.namespace.QName; |
| 48 | |
import java.util.ArrayList; |
| 49 | |
import java.util.Collection; |
| 50 | |
import java.util.List; |
| 51 | |
import java.util.Properties; |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
@Controller |
| 60 | |
@RequestMapping(value = "/calendarSearch") |
| 61 | 0 | public class CalendarSearchController extends UifControllerBase { |
| 62 | |
|
| 63 | |
private transient AcademicCalendarService acalService; |
| 64 | |
private ContextInfo contextInfo; |
| 65 | |
|
| 66 | |
@Override |
| 67 | |
protected UifFormBase createInitialForm(HttpServletRequest request) { |
| 68 | 0 | return new CalendarSearchForm(); |
| 69 | |
} |
| 70 | |
|
| 71 | |
|
| 72 | |
@Override |
| 73 | |
@RequestMapping(method = RequestMethod.GET, params = "methodToCall=start") |
| 74 | |
public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, |
| 75 | |
HttpServletRequest request, HttpServletResponse response) { |
| 76 | 0 | CalendarSearchForm calendarSearchForm = (CalendarSearchForm)form; |
| 77 | |
|
| 78 | 0 | String calendarSearchType = request.getParameter(CalendarConstants.CALENDAR_SEARCH_TYPE); |
| 79 | 0 | if (null != calendarSearchType) { |
| 80 | 0 | calendarSearchForm.setCalendarType(calendarSearchType); |
| 81 | |
} |
| 82 | |
|
| 83 | 0 | return super.start(form, result, request, response); |
| 84 | |
} |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
@RequestMapping(params = "methodToCall=search") |
| 90 | |
public ModelAndView search(@ModelAttribute("KualiForm") CalendarSearchForm searchForm, BindingResult result, |
| 91 | |
HttpServletRequest request, HttpServletResponse response) throws Exception { |
| 92 | 0 | String calendarType = searchForm.getCalendarType(); |
| 93 | |
|
| 94 | 0 | resetForm(searchForm); |
| 95 | 0 | if(calendarType.equals(CalendarConstants.HOLIDAYCALENDER)){ |
| 96 | 0 | List<HolidayCalendarInfo> hCals = getViewHelperService(searchForm).searchForHolidayCalendars(searchForm.getName(), searchForm.getYear(), getContextInfo()); |
| 97 | 0 | searchForm.setHolidayCalendars(hCals); |
| 98 | 0 | } else if(calendarType.equals(CalendarConstants.ACADEMICCALENDER)) { |
| 99 | 0 | List<AcademicCalendarInfo> aCals = getViewHelperService(searchForm).searchForAcademicCalendars(searchForm.getName(), searchForm.getYear(), getContextInfo()); |
| 100 | 0 | searchForm.setAcademicCalendars(aCals); |
| 101 | 0 | } else if(calendarType.equals(CalendarConstants.TERM)){ |
| 102 | 0 | List<TermInfo> terms = getViewHelperService(searchForm).searchForTerms(searchForm.getName(),searchForm.getYear(),getContextInfo()); |
| 103 | 0 | searchForm.setTerms(terms); |
| 104 | 0 | } else { |
| 105 | 0 | GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, "ERROR: invalid calendar type."); |
| 106 | |
} |
| 107 | |
|
| 108 | 0 | return getUIFModelAndView(searchForm); |
| 109 | |
} |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
@RequestMapping(params = "methodToCall=view") |
| 115 | |
public ModelAndView view(@ModelAttribute("KualiForm") CalendarSearchForm searchForm, BindingResult result, |
| 116 | |
HttpServletRequest request, HttpServletResponse response) throws Exception { |
| 117 | |
|
| 118 | 0 | Object atp = getSelectedAtp(searchForm, "view"); |
| 119 | |
Properties urlParameters; |
| 120 | |
String controllerPath; |
| 121 | 0 | if(atp instanceof HolidayCalendarInfo){ |
| 122 | 0 | urlParameters = getViewHelperService(searchForm).buildHCalURLParameters((HolidayCalendarInfo)atp,CalendarConstants.HC_VIEW_METHOD,true,getContextInfo()); |
| 123 | 0 | controllerPath = CalendarConstants.HCAL_CONTROLLER_PATH; |
| 124 | 0 | } else if(atp instanceof AcademicCalendarInfo) { |
| 125 | 0 | urlParameters = getViewHelperService(searchForm).buildACalURLParameters((AcademicCalendarInfo)atp,CalendarConstants.AC_VIEW_METHOD,true,getContextInfo()); |
| 126 | 0 | controllerPath = CalendarConstants.ACAL_CONTROLLER_PATH; |
| 127 | 0 | } else if(atp instanceof TermInfo){ |
| 128 | 0 | urlParameters = getViewHelperService(searchForm).buildTermURLParameters((TermInfo)atp,CalendarConstants.AC_VIEW_METHOD,true,getContextInfo()); |
| 129 | 0 | controllerPath = CalendarConstants.ACAL_CONTROLLER_PATH; |
| 130 | |
} else { |
| 131 | 0 | throw new RuntimeException("Invalid calendar type. This search supports Acal/HCal/Term only"); |
| 132 | |
} |
| 133 | |
|
| 134 | 0 | return super.performRedirect(searchForm,controllerPath, urlParameters); |
| 135 | |
} |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
@RequestMapping(params = "methodToCall=edit") |
| 141 | |
public ModelAndView edit(@ModelAttribute("KualiForm") CalendarSearchForm searchForm, BindingResult result, |
| 142 | |
HttpServletRequest request, HttpServletResponse response) throws Exception { |
| 143 | |
|
| 144 | 0 | Object atp = getSelectedAtp(searchForm, "edit"); |
| 145 | |
|
| 146 | |
Properties urlParameters; |
| 147 | |
String controllerPath; |
| 148 | |
|
| 149 | 0 | if(atp instanceof HolidayCalendarInfo){ |
| 150 | 0 | urlParameters = getViewHelperService(searchForm).buildHCalURLParameters((HolidayCalendarInfo) atp, CalendarConstants.HC_EDIT_METHOD, false, getContextInfo()); |
| 151 | 0 | controllerPath = CalendarConstants.HCAL_CONTROLLER_PATH; |
| 152 | 0 | } else if(atp instanceof AcademicCalendarInfo) { |
| 153 | 0 | urlParameters = getViewHelperService(searchForm).buildACalURLParameters((AcademicCalendarInfo) atp, CalendarConstants.AC_EDIT_METHOD, false, getContextInfo()); |
| 154 | 0 | controllerPath = CalendarConstants.ACAL_CONTROLLER_PATH; |
| 155 | 0 | } else if(atp instanceof TermInfo){ |
| 156 | 0 | urlParameters = getViewHelperService(searchForm).buildTermURLParameters((TermInfo)atp,CalendarConstants.AC_EDIT_METHOD, false, getContextInfo()); |
| 157 | 0 | controllerPath = CalendarConstants.ACAL_CONTROLLER_PATH; |
| 158 | |
} else { |
| 159 | 0 | throw new RuntimeException("Invalid calendar type. This search supports Acal/HCal/Term only"); |
| 160 | |
} |
| 161 | |
|
| 162 | 0 | return super.performRedirect(searchForm,controllerPath, urlParameters); |
| 163 | |
|
| 164 | |
} |
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
@RequestMapping(params = "methodToCall=copy") |
| 170 | |
public ModelAndView copy(@ModelAttribute("KualiForm") CalendarSearchForm searchForm, BindingResult result, |
| 171 | |
HttpServletRequest request, HttpServletResponse response) throws Exception { |
| 172 | |
|
| 173 | 0 | Object atp = getSelectedAtp(searchForm, "copy"); |
| 174 | |
|
| 175 | |
Properties urlParameters; |
| 176 | |
String controllerPath; |
| 177 | |
|
| 178 | 0 | if(atp instanceof HolidayCalendarInfo){ |
| 179 | 0 | controllerPath = CalendarConstants.HCAL_CONTROLLER_PATH; |
| 180 | 0 | urlParameters = getViewHelperService(searchForm).buildHCalURLParameters((HolidayCalendarInfo)atp,CalendarConstants.HC_COPY_METHOD,false,getContextInfo()); |
| 181 | 0 | }else if(atp instanceof AcademicCalendarInfo) { |
| 182 | 0 | urlParameters = getViewHelperService(searchForm).buildACalURLParameters((AcademicCalendarInfo)atp,CalendarConstants.AC_COPY_METHOD,false,getContextInfo()); |
| 183 | 0 | controllerPath = CalendarConstants.ACAL_CONTROLLER_PATH; |
| 184 | |
} else { |
| 185 | 0 | throw new RuntimeException("Invalid calendar type. This search supports Acal and HCal only"); |
| 186 | |
} |
| 187 | |
|
| 188 | 0 | return super.performRedirect(searchForm,controllerPath, urlParameters); |
| 189 | |
|
| 190 | |
} |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
@RequestMapping(params = "methodToCall=delete") |
| 196 | |
public ModelAndView delete(@ModelAttribute("KualiForm") CalendarSearchForm searchForm, BindingResult result, |
| 197 | |
HttpServletRequest request, HttpServletResponse response) throws Exception { |
| 198 | 0 | Object atp = getSelectedAtp(searchForm, "delete"); |
| 199 | |
|
| 200 | 0 | if(atp instanceof HolidayCalendarInfo){ |
| 201 | 0 | StatusInfo status = getAcademicCalendarService().deleteHolidayCalendar(((HolidayCalendarInfo)atp).getId(),getContextInfo()); |
| 202 | 0 | if (status.getIsSuccess()){ |
| 203 | 0 | GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_MESSAGES,CalendarConstants.MSG_INFO_SEARCH_DELETE_SUCCESS,((HolidayCalendarInfo) atp).getName()); |
| 204 | 0 | searchForm.getHolidayCalendars().remove(atp); |
| 205 | |
} else{ |
| 206 | 0 | GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, status.getMessage()); |
| 207 | |
} |
| 208 | 0 | } else if(atp instanceof AcademicCalendarInfo) { |
| 209 | 0 | StatusInfo status = getAcademicCalendarService().deleteAcademicCalendar(((AcademicCalendarInfo)atp).getId(),getContextInfo()); |
| 210 | 0 | if (status.getIsSuccess()){ |
| 211 | 0 | GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_MESSAGES,CalendarConstants.MSG_INFO_SEARCH_DELETE_SUCCESS,((AcademicCalendarInfo) atp).getName()); |
| 212 | 0 | searchForm.getAcademicCalendars().remove(atp); |
| 213 | |
} else{ |
| 214 | 0 | GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, status.getMessage()); |
| 215 | |
} |
| 216 | 0 | } else if(atp instanceof TermInfo){ |
| 217 | 0 | StatusInfo status = getAcademicCalendarService().deleteTerm(((TermInfo)atp).getId(),getContextInfo()); |
| 218 | 0 | if (status.getIsSuccess()){ |
| 219 | 0 | GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_MESSAGES,CalendarConstants.MSG_INFO_SEARCH_DELETE_SUCCESS,((TermInfo) atp).getName()); |
| 220 | 0 | searchForm.getTerms().remove(atp); |
| 221 | |
} else{ |
| 222 | 0 | GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, status.getMessage()); |
| 223 | |
} |
| 224 | 0 | } else { |
| 225 | 0 | GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, "ERROR: invalid calendar type."); |
| 226 | |
} |
| 227 | |
|
| 228 | 0 | return getUIFModelAndView(searchForm); |
| 229 | |
|
| 230 | |
} |
| 231 | |
|
| 232 | |
private void resetForm(CalendarSearchForm searchForm) { |
| 233 | 0 | searchForm.setHolidayCalendars(new ArrayList<HolidayCalendarInfo>()); |
| 234 | 0 | searchForm.setAcademicCalendars(new ArrayList<AcademicCalendarInfo>()); |
| 235 | 0 | searchForm.setTerms(new ArrayList<TermInfo>()); |
| 236 | 0 | } |
| 237 | |
|
| 238 | |
private Object getSelectedAtp(CalendarSearchForm searchForm, String actionLink){ |
| 239 | 0 | String selectedCollectionPath = searchForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH); |
| 240 | 0 | if (StringUtils.isBlank(selectedCollectionPath)) { |
| 241 | 0 | throw new RuntimeException("Selected collection was not set for " + actionLink); |
| 242 | |
} |
| 243 | |
|
| 244 | 0 | int selectedLineIndex = -1; |
| 245 | 0 | String selectedLine = searchForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX); |
| 246 | 0 | if (StringUtils.isNotBlank(selectedLine)) { |
| 247 | 0 | selectedLineIndex = Integer.parseInt(selectedLine); |
| 248 | |
} |
| 249 | |
|
| 250 | 0 | if (selectedLineIndex == -1) { |
| 251 | 0 | throw new RuntimeException("Selected line index was not set"); |
| 252 | |
} |
| 253 | |
|
| 254 | 0 | Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(searchForm, selectedCollectionPath); |
| 255 | 0 | Object atp = ((List<Object>) collection).get(selectedLineIndex); |
| 256 | |
|
| 257 | 0 | return atp; |
| 258 | |
} |
| 259 | |
|
| 260 | |
private ContextInfo getContextInfo() { |
| 261 | 0 | if (null == contextInfo) { |
| 262 | |
|
| 263 | 0 | contextInfo = TestHelper.getContext1(); |
| 264 | |
} |
| 265 | 0 | return contextInfo; |
| 266 | |
} |
| 267 | |
|
| 268 | |
private CalendarSearchViewHelperService getViewHelperService(CalendarSearchForm form){ |
| 269 | 0 | if (form.getView().getViewHelperServiceClassName() != null){ |
| 270 | 0 | return (CalendarSearchViewHelperService)form.getView().getViewHelperService(); |
| 271 | |
} else { |
| 272 | 0 | return (CalendarSearchViewHelperService)form.getPostedView().getViewHelperService(); |
| 273 | |
} |
| 274 | |
} |
| 275 | |
|
| 276 | |
protected AcademicCalendarService getAcademicCalendarService(){ |
| 277 | 0 | if(acalService == null) { |
| 278 | 0 | acalService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART)); |
| 279 | |
} |
| 280 | 0 | return acalService; |
| 281 | |
} |
| 282 | |
} |