1 package org.kuali.student.enrollment.class2.appointment.controller;
2
3 import org.apache.commons.lang.BooleanUtils;
4 import org.apache.commons.lang.StringUtils;
5 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
6 import org.kuali.rice.krad.uif.UifConstants;
7 import org.kuali.rice.krad.uif.UifParameters;
8 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
9 import org.kuali.rice.krad.util.GlobalVariables;
10 import org.kuali.rice.krad.util.KRADConstants;
11 import org.kuali.rice.krad.web.controller.UifControllerBase;
12 import org.kuali.rice.krad.web.form.UifFormBase;
13 import org.kuali.student.r2.core.acal.dto.KeyDateInfo;
14 import org.kuali.student.r2.core.acal.dto.TermInfo;
15 import org.kuali.student.r2.core.acal.service.AcademicCalendarService;
16 import org.kuali.student.enrollment.class2.appointment.dto.AppointmentWindowWrapper;
17 import org.kuali.student.enrollment.class2.appointment.form.RegistrationWindowsManagementForm;
18 import org.kuali.student.enrollment.class2.appointment.service.AppointmentViewHelperService;
19 import org.kuali.student.enrollment.class2.appointment.util.AppointmentConstants;
20 import org.kuali.student.enrollment.class2.appointment.util.AppointmentSlotRuleTypeConversion;
21 import org.kuali.student.r2.common.dto.ContextInfo;
22 import org.kuali.student.r2.common.dto.StatusInfo;
23 import org.kuali.student.r2.common.exceptions.DoesNotExistException;
24 import org.kuali.student.r2.common.exceptions.InvalidParameterException;
25 import org.kuali.student.r2.common.exceptions.MissingParameterException;
26 import org.kuali.student.r2.common.exceptions.OperationFailedException;
27 import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
28 import org.kuali.student.r2.common.util.ContextUtils;
29 import org.kuali.student.r2.core.constants.AcademicCalendarServiceConstants;
30 import org.kuali.student.r2.common.util.date.DateFormatters;
31 import org.kuali.student.r2.core.appointment.constants.AppointmentServiceConstants;
32 import org.kuali.student.r2.core.appointment.dto.AppointmentSlotInfo;
33 import org.kuali.student.r2.core.appointment.dto.AppointmentWindowInfo;
34 import org.kuali.student.r2.core.appointment.service.AppointmentService;
35 import org.kuali.student.r2.core.constants.PopulationServiceConstants;
36 import org.kuali.student.r2.core.population.dto.PopulationInfo;
37 import org.kuali.student.r2.core.population.service.PopulationService;
38 import org.springframework.beans.factory.config.BeanDefinition;
39 import org.springframework.stereotype.Controller;
40 import org.springframework.validation.BindingResult;
41 import org.springframework.web.bind.annotation.ModelAttribute;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.bind.annotation.RequestMethod;
44 import org.springframework.web.servlet.ModelAndView;
45
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48 import javax.xml.namespace.QName;
49 import java.util.ArrayList;
50 import java.util.Collection;
51 import java.util.Date;
52 import java.util.List;
53 import java.util.Properties;
54
55
56
57
58
59
60
61 @Controller
62 @RequestMapping(value = "/registrationWindows")
63 public class RegistrationWindowsController extends UifControllerBase {
64
65 private AppointmentViewHelperService viewHelperService;
66
67 private AcademicCalendarService acalService;
68
69 private AppointmentService appointmentService;
70
71 private PopulationService populationService;
72
73 @Override
74 protected UifFormBase createInitialForm(@SuppressWarnings("unused") HttpServletRequest request) {
75 return new RegistrationWindowsManagementForm();
76 }
77
78 @Override
79 @RequestMapping(method = RequestMethod.GET, params = "methodToCall=start")
80 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, @SuppressWarnings("unused") BindingResult result,
81 @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) {
82
83 if (!(form instanceof RegistrationWindowsManagementForm)) {
84 throw new IllegalArgumentException("Expected RegistrationWindowsManagementForm, got " + form.getClass().getSimpleName());
85 }
86
87 RegistrationWindowsManagementForm theForm = (RegistrationWindowsManagementForm) form;
88 String termId = request.getParameter("termId");
89
90 if (StringUtils.isNotBlank(termId)) {
91 try {
92 getViewHelperService(theForm).loadTermAndPeriods(termId, theForm);
93 return getUIFModelAndView(theForm);
94 } catch (Exception ex) {
95 throw new RuntimeException(ex);
96 }
97 }
98
99 return super.start(theForm, result, request, response);
100 }
101
102
103
104
105 @RequestMapping(params = "methodToCall=searchForTerm")
106 public ModelAndView searchForTerm(@ModelAttribute("KualiForm") RegistrationWindowsManagementForm searchForm, @SuppressWarnings("unused") BindingResult result,
107 @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) throws Exception {
108 String termType = searchForm.getTermType();
109 String termYear = searchForm.getTermYear();
110
111 getViewHelperService(searchForm).searchForTerm(termType, termYear, searchForm);
112
113 if (GlobalVariables.getMessageMap().hasErrors()) {
114 return getUIFModelAndView(searchForm, AppointmentConstants.SELECT_TERM_PAGE);
115 }
116
117 return getUIFModelAndView(searchForm, AppointmentConstants.REGISTRATION_WINDOWS_EDIT_PAGE);
118 }
119
120 @RequestMapping(params = "methodToCall=show")
121 public ModelAndView show(@ModelAttribute("KualiForm") RegistrationWindowsManagementForm form, @SuppressWarnings("unused") BindingResult result,
122 @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) throws Exception {
123
124 if (!form.isShowAddWindows()) {
125 form.setShowAddWindows(true);
126 }
127
128 form = populatePeriodCollections(form);
129
130 return getUIFModelAndView(form);
131 }
132
133 @RequestMapping(params = "methodToCall=save")
134 public ModelAndView save(@ModelAttribute("KualiForm") RegistrationWindowsManagementForm form, @SuppressWarnings("unused") BindingResult result,
135 @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) throws Exception {
136
137
138 getViewHelperService(form).saveWindows(form);
139
140 return getUIFModelAndView(form);
141 }
142
143 @RequestMapping(params = "methodToCall=assignStudents")
144 public ModelAndView assignStudents(@ModelAttribute("KualiForm") RegistrationWindowsManagementForm uifForm, @SuppressWarnings("unused") BindingResult result,
145 @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) throws Exception {
146
147
148 AppointmentWindowWrapper window = _getSelectedWindow(uifForm, "Assign Students");
149 boolean isValid = getViewHelperService(uifForm).validateApptWidnow(window, false);
150 if (isValid) {
151 boolean isSaved = getViewHelperService(uifForm).saveApptWindow(window);
152
153
154 if (window != null && isSaved) {
155
156 List<AppointmentSlotInfo> slots = getAppointmentService().generateAppointmentSlotsByWindow(window.getAppointmentWindowInfo().getId(), new ContextInfo());
157 StatusInfo status = getAppointmentService().generateAppointmentsByWindow(window.getAppointmentWindowInfo().getId(), window.getAppointmentWindowInfo().getTypeKey(), new ContextInfo());
158
159
160 if (status.getIsSuccess()) {
161 GlobalVariables.getMessageMap().addGrowlMessage("",
162 AppointmentConstants.APPOINTMENT_MSG_INFO_ASSIGNED, window.getAppointmentWindowInfo().getName(), status.getMessage(), String.valueOf(slots.size()));
163
164 window.getAppointmentWindowInfo().setStateKey(AppointmentServiceConstants.APPOINTMENT_WINDOW_STATE_ASSIGNED_KEY);
165 } else {
166
167 GlobalVariables.getMessageMap().addGrowlMessage("",
168 AppointmentConstants.APPOINTMENT_MSG_ERROR_TOO_MANY_STUDENTS, status.getMessage());
169 }
170 }
171 }
172 return getUIFModelAndView(uifForm);
173 }
174
175 @RequestMapping(params = "methodToCall=breakAppointments")
176 public ModelAndView breakAppointments(@ModelAttribute("KualiForm") RegistrationWindowsManagementForm uifForm, @SuppressWarnings("unused") BindingResult result,
177 @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) throws Exception {
178
179 Properties urlParameters = new Properties();
180 urlParameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, "refreshAfterDialog");
181 urlParameters.put(UifParameters.VIEW_ID, AppointmentConstants.REGISTRATION_WINDOWS_MANAGEMENT_VIEW);
182 urlParameters.put(UifParameters.PAGE_ID, AppointmentConstants.REGISTRATION_WINDOWS_EDIT_PAGE);
183 urlParameters.put("periodId", uifForm.getPeriodId());
184 urlParameters.put("termType", uifForm.getTermType());
185 urlParameters.put("termYear", uifForm.getTermYear());
186
187
188
189
190 String controllerPath = AppointmentConstants.REGISTRATION_WINDOWS_CONTROLLER_PATH;
191
192 try {
193 String dialog = AppointmentConstants.Registration_Windows_ConfirmBreak_Dialog;
194 if (!hasDialogBeenDisplayed(dialog, uifForm)) {
195 AppointmentWindowWrapper window = _getSelectedWindow(uifForm, "Break Appointments");
196 uifForm.setSelectedAppointmentWindow(window);
197
198
199 return showDialog(dialog, uifForm, request, response);
200 }
201
202 boolean confirmDelete = getBooleanDialogResponse(dialog, uifForm, request, response);
203 uifForm.getDialogManager().resetDialogStatus(dialog);
204 if (!confirmDelete) {
205 return super.performRedirect(uifForm, controllerPath, urlParameters);
206 }
207 } catch (Exception e) {
208
209 return getUIFModelAndView(uifForm);
210 }
211
212 AppointmentWindowWrapper window = uifForm.getSelectedAppointmentWindow();
213 if (window != null) {
214
215 StatusInfo status = getAppointmentService().deleteAppointmentSlotsByWindowCascading(window.getAppointmentWindowInfo().getId(), new ContextInfo());
216 if (status.getIsSuccess()) {
217
218 urlParameters.put("growlMessage", AppointmentConstants.APPOINTMENT_MSG_INFO_BREAK_APPOINTMENTS_SUCCESS);
219
220
221 window.getAppointmentWindowInfo().setStateKey(AppointmentServiceConstants.APPOINTMENT_WINDOW_STATE_DRAFT_KEY);
222 } else {
223
224
225 urlParameters.put("growlMessage", AppointmentConstants.APPOINTMENT_MSG_ERROR_BREAK_APPOINTMENTS_FAILURE);
226 urlParameters.put("windowName", status.getMessage());
227 }
228 }
229
230 return super.performRedirect(uifForm, controllerPath, urlParameters);
231 }
232
233 @RequestMapping(params = "methodToCall=deleteLineThroughDialog")
234 public ModelAndView deleteLineWithDialog(@ModelAttribute("KualiForm") RegistrationWindowsManagementForm uifForm, @SuppressWarnings("unused") BindingResult result, @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) {
235 Properties urlParameters = new Properties();
236 urlParameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, "refreshAfterDialog");
237 urlParameters.put(UifParameters.VIEW_ID, AppointmentConstants.REGISTRATION_WINDOWS_MANAGEMENT_VIEW);
238 urlParameters.put(UifParameters.PAGE_ID, AppointmentConstants.REGISTRATION_WINDOWS_EDIT_PAGE);
239 urlParameters.put("periodId", uifForm.getPeriodId());
240 urlParameters.put("termType", uifForm.getTermType());
241 urlParameters.put("termYear", uifForm.getTermYear());
242
243
244
245
246 String controllerPath = AppointmentConstants.REGISTRATION_WINDOWS_CONTROLLER_PATH;
247
248
249
250 try {
251
252 AppointmentWindowWrapper window = _getSelectedWindow(uifForm, "Delete a Window");
253 uifForm.setSelectedAppointmentWindow(window);
254 if (window != null) {
255
256 if (AppointmentServiceConstants.APPOINTMENT_WINDOW_STATE_ASSIGNED_KEY.equals(window.getAppointmentWindowInfo().getStateKey())) {
257
258
259 StatusInfo status = getAppointmentService().deleteAppointmentSlotsByWindowCascading(window.getAppointmentWindowInfo().getId(), new ContextInfo());
260 if (status.getIsSuccess()) {
261 getAppointmentService().deleteAppointmentWindowCascading(window.getId(), new ContextInfo());
262 uifForm.removeSelectedAppointmentWindow(window);
263 urlParameters.put("growlMessage", AppointmentConstants.APPOINTMENT_MSG_INFO_DELETED);
264 urlParameters.put("windowName", window.getWindowName());
265
266 return getUIFModelAndView(uifForm, AppointmentConstants.REGISTRATION_WINDOWS_EDIT_PAGE);
267 } else {
268
269 urlParameters.put("growlMessage", AppointmentConstants.APPOINTMENT_MSG_ERROR_BREAK_APPOINTMENTS_FAILURE);
270 urlParameters.put("windowName", status.getMessage());
271
272 return getUIFModelAndView(uifForm, AppointmentConstants.REGISTRATION_WINDOWS_EDIT_PAGE);
273 }
274 } else {
275 getAppointmentService().deleteAppointmentWindowCascading(window.getId(), new ContextInfo());
276 uifForm.removeSelectedAppointmentWindow(window);
277 urlParameters.put("growlMessage", AppointmentConstants.APPOINTMENT_MSG_INFO_DELETED);
278 urlParameters.put("windowName", window.getWindowName());
279
280 return getUIFModelAndView(uifForm, AppointmentConstants.REGISTRATION_WINDOWS_EDIT_PAGE);
281 }
282 } else {
283
284 return getUIFModelAndView(uifForm, AppointmentConstants.REGISTRATION_WINDOWS_EDIT_PAGE);
285 }
286 } catch (Exception e) {
287
288 return getUIFModelAndView(uifForm, AppointmentConstants.REGISTRATION_WINDOWS_EDIT_PAGE);
289 }
290 }
291
292 @RequestMapping(params = "methodToCall=refreshAfterDialog")
293 public ModelAndView refreshAfterDialog(@ModelAttribute("KualiForm") RegistrationWindowsManagementForm uifForm,
294 @SuppressWarnings("unused") BindingResult result,
295 @SuppressWarnings("unused") HttpServletRequest request,
296 @SuppressWarnings("unused") HttpServletResponse response) throws Exception {
297
298 String windowName = request.getParameter("windowName");
299 String growlMessage = request.getParameter("growlMessage");
300 if (growlMessage != null) {
301 if (windowName != null) {
302 GlobalVariables.getMessageMap().addGrowlMessage("", growlMessage, windowName);
303 } else {
304 GlobalVariables.getMessageMap().addGrowlMessage("", growlMessage);
305 }
306 }
307
308 getViewHelperService(uifForm).searchForTerm(uifForm.getTermType(), uifForm.getTermYear(), uifForm);
309
310 if (!uifForm.isShowAddWindows()) {
311 uifForm.setShowAddWindows(true);
312 }
313 uifForm = populatePeriodCollections(uifForm);
314
315 return getUIFModelAndView(uifForm);
316 }
317
318 private RegistrationWindowsManagementForm populatePeriodCollections(RegistrationWindowsManagementForm uifForm)
319 throws Exception {
320 String periodId = uifForm.getPeriodId();
321 String periodInfoDetails = "";
322
323
324 uifForm.getAppointmentWindows().clear();
325
326 if (periodId == null || periodId.isEmpty()) {
327 uifForm.setPeriodInfoDetails(periodInfoDetails);
328 uifForm.setShowAddWindows(false);
329 uifForm.getAppointmentWindows().clear();
330 } else if (!periodId.isEmpty() && !periodId.equals("all")) {
331
332
333 KeyDateInfo period = getAcalService().getKeyDate(periodId, ContextUtils.getContextInfo());
334
335
336 List<KeyDateInfo> periods = new ArrayList<KeyDateInfo>();
337 periods.add(period);
338 _loadWindowsInfoForm(periods, uifForm);
339
340
341 AppointmentWindowWrapper addLine = (AppointmentWindowWrapper) uifForm.getNewCollectionLines().get("appointmentWindows");
342 if (addLine == null) {
343 addLine = new AppointmentWindowWrapper();
344 uifForm.getNewCollectionLines().put("appointmentWindows", addLine);
345 }
346
347 if (period.getName() != null) {
348 periodInfoDetails = period.getName() + " Start Date: " + _getSimpleDate(period.getStartDate()) + "<br>"
349 + period.getName() + " End Date: " + _getSimpleDate(period.getEndDate());
350 uifForm.setPeriodName(period.getName());
351 uifForm.setPeriodId(period.getId());
352 addLine.setPeriodName(period.getName());
353 addLine.setPeriodKey(period.getId());
354 } else {
355 periodInfoDetails = period.getId() + " Start Date: " + _getSimpleDate(period.getStartDate()) + "<br>"
356 + period.getId() + " End Date: " + _getSimpleDate(period.getEndDate());
357 uifForm.setPeriodName(period.getId());
358 uifForm.setPeriodId(period.getId());
359 addLine.setPeriodName(period.getId());
360 addLine.setPeriodKey(period.getId());
361 }
362 uifForm.setPeriodInfoDetails(periodInfoDetails);
363
364
365 } else if (periodId.equals("all")) {
366 List<KeyDateInfo> periodMilestones = uifForm.getPeriodMilestones();
367 if (periodMilestones.isEmpty()) {
368 TermInfo term = uifForm.getTermInfo();
369 if (term.getId() != null && !term.getId().isEmpty()) {
370 getViewHelperService(uifForm).loadPeriods(term.getId(), uifForm);
371 periodMilestones = uifForm.getPeriodMilestones();
372 }
373 }
374 for (KeyDateInfo period : periodMilestones) {
375
376 if (period.getName() != null) {
377 periodInfoDetails = periodInfoDetails.concat(period.getName() + " Start Date: " + _getSimpleDate(period.getStartDate()) + "<br>"
378 + period.getName() + " End Date: " + _getSimpleDate(period.getEndDate()) + "<br>");
379 } else {
380 periodInfoDetails = periodInfoDetails.concat(period.getId() + " Start Date: " + period.getStartDate() + "<br>"
381 + period.getId() + " End Date: " + _getSimpleDate(period.getEndDate()) + "<br>");
382 }
383 }
384 uifForm.setPeriodInfoDetails(periodInfoDetails);
385 _loadWindowsInfoForm(periodMilestones, uifForm);
386 }
387
388 return uifForm;
389 }
390
391
392 private AppointmentWindowWrapper _getSelectedWindow(RegistrationWindowsManagementForm theForm, String actionLink) {
393 String selectedCollectionPath = theForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
394 if (StringUtils.isBlank(selectedCollectionPath)) {
395 throw new RuntimeException("Selected collection was not set for " + actionLink);
396 }
397
398 int selectedLineIndex = -1;
399 String selectedLine = theForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
400 if (StringUtils.isNotBlank(selectedLine)) {
401 selectedLineIndex = Integer.parseInt(selectedLine);
402 }
403
404 if (selectedLineIndex == -1) {
405 throw new RuntimeException("Selected line index was not set");
406 }
407
408 Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(theForm, selectedCollectionPath);
409 Object window = ((List<Object>) collection).get(selectedLineIndex);
410
411 return (AppointmentWindowWrapper) window;
412 }
413
414 private String _getSimpleDate(Date date) {
415 if (date == null) {
416 return "";
417 }
418 return DateFormatters.MONTH_DAY_YEAR_DATE_FORMATTER.format(date);
419 }
420
421 private void _loadWindowsInfoForm(List<KeyDateInfo> periods, RegistrationWindowsManagementForm form) throws InvalidParameterException, MissingParameterException, DoesNotExistException, PermissionDeniedException, OperationFailedException {
422 for (KeyDateInfo period : periods) {
423 List<AppointmentWindowInfo> windows = getAppointmentService().getAppointmentWindowsByPeriod(period.getId(), new ContextInfo());
424 if (windows != null && windows.size() > 0) {
425 for (AppointmentWindowInfo window : windows) {
426
427
428 PopulationInfo population = getPopulationService().getPopulation(window.getAssignedPopulationId(), new ContextInfo());
429
430 AppointmentWindowWrapper windowWrapper = new AppointmentWindowWrapper();
431
432 windowWrapper.setAppointmentWindowInfo(window);
433 windowWrapper.setId(window.getId());
434 windowWrapper.setWindowName(window.getName());
435 windowWrapper.setPeriodKey(window.getPeriodMilestoneId());
436 windowWrapper.setPeriodName(period.getName());
437 windowWrapper.setAssignedPopulationName(population.getName());
438 windowWrapper.setWindowTypeKey(window.getTypeKey());
439 if (!AppointmentServiceConstants.APPOINTMENT_WINDOW_TYPE_ONE_SLOT_KEY.equals(window.getTypeKey())) {
440 windowWrapper.setSlotRuleEnumType(AppointmentSlotRuleTypeConversion.convToAppointmentSlotRuleCode(window.getSlotRule()));
441 }
442 windowWrapper.setStartDate(window.getStartDate());
443 windowWrapper.setStartTime(_parseTime(window.getStartDate()));
444 windowWrapper.setStartTimeAmPm(_parseAmPm(window.getStartDate()));
445
446 windowWrapper.setEndDate(window.getEndDate());
447 windowWrapper.setEndTime(_parseTime(window.getEndDate()));
448 windowWrapper.setEndTimeAmPm(_parseAmPm(window.getEndDate()));
449
450 form.getAppointmentWindows().add(windowWrapper);
451 }
452 }
453 }
454 }
455
456 private String _parseAmPm(Date date) {
457 if (date == null) {
458 return null;
459 }
460 return DateFormatters.AM_PM_TIME_FORMATTER.format(date);
461 }
462
463 private String _parseTime(Date date) {
464 if (date == null) {
465 return null;
466 }
467 return DateFormatters.HOUR_MINUTE_TIME_FORMATTER.format(date);
468 }
469
470 public AppointmentViewHelperService getViewHelperService(RegistrationWindowsManagementForm appointmentForm) {
471 if (viewHelperService == null) {
472 if (appointmentForm.getView().getViewHelperServiceClass() != null) {
473 viewHelperService = (AppointmentViewHelperService) appointmentForm.getView().getViewHelperService();
474 } else {
475 viewHelperService = (AppointmentViewHelperService) appointmentForm.getPostedView().getViewHelperService();
476 }
477 }
478 return viewHelperService;
479 }
480
481 public AcademicCalendarService getAcalService() {
482 if (acalService == null) {
483 acalService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
484 }
485 return this.acalService;
486 }
487
488
489 public AppointmentService getAppointmentService() {
490 if (appointmentService == null) {
491 appointmentService = (AppointmentService) GlobalResourceLoader.getService(new QName(AppointmentServiceConstants.NAMESPACE, AppointmentServiceConstants.SERVICE_NAME_LOCAL_PART));
492 }
493 return appointmentService;
494 }
495
496 public PopulationService getPopulationService() {
497 if (populationService == null) {
498 populationService = (PopulationService) GlobalResourceLoader.getService(new QName(PopulationServiceConstants.NAMESPACE, "PopulationMockService"));
499 }
500 return populationService;
501 }
502
503 }