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
19 import org.apache.commons.lang.BooleanUtils;
20 import org.apache.commons.lang.StringUtils;
21 import org.kuali.rice.core.api.util.RiceKeyConstants;
22 import org.kuali.rice.krad.uif.UifParameters;
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.common.uif.form.KSUifForm;
28 import org.kuali.student.common.uif.util.GrowlIcon;
29 import org.kuali.student.common.uif.util.KSUifUtils;
30 import org.kuali.student.enrollment.class2.acal.dto.HolidayWrapper;
31 import org.kuali.student.enrollment.class2.acal.form.HolidayCalendarForm;
32 import org.kuali.student.enrollment.class2.acal.service.HolidayCalendarViewHelperService;
33 import org.kuali.student.enrollment.class2.acal.util.AcalCommonUtils;
34 import org.kuali.student.enrollment.class2.acal.util.CalendarConstants;
35 import org.kuali.student.enrollment.common.util.EnrollConstants;
36 import org.kuali.student.r2.core.acal.dto.HolidayCalendarInfo;
37 import org.kuali.student.r2.core.constants.AcademicCalendarServiceConstants;
38 import org.kuali.student.r2.core.constants.AtpServiceConstants;
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 java.util.ArrayList;
49 import java.util.Calendar;
50 import java.util.Collections;
51 import java.util.Date;
52 import java.util.HashMap;
53 import java.util.List;
54 import java.util.Map;
55 import java.util.Properties;
56
57
58
59
60
61
62
63
64
65
66 @Controller
67 @RequestMapping(value = "/holidayCalendar")
68 public class HolidayCalendarController extends UifControllerBase {
69
70 @Override
71 protected KSUifForm createInitialForm(HttpServletRequest httpServletRequest) {
72 return new HolidayCalendarForm();
73 }
74
75
76
77
78
79
80
81
82
83
84
85 @Override
86 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addLine")
87 public ModelAndView addLine(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
88 HttpServletRequest request, HttpServletResponse response) {
89 HolidayCalendarForm hcForm = (HolidayCalendarForm) form;
90 List<HolidayWrapper> holidays = hcForm.getHolidays();
91 Map<String, Object> newCollectionLines = form.getNewCollectionLines();
92
93 if(newCollectionLines != null && !newCollectionLines.isEmpty()){
94 for (Map.Entry<String, Object> entry : newCollectionLines.entrySet()){
95 HolidayWrapper newHoliday = (HolidayWrapper)entry.getValue();
96 if(holidays != null && !holidays.isEmpty()){
97 for(HolidayWrapper holiday : holidays){
98 boolean duplicated = isDuplicateHoliday(newHoliday, holiday);
99 if(duplicated){
100 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, "ERROR: The holiday being added is already in the collection.");
101 return getUIFModelAndView(form);
102 }
103 }
104 }
105 }
106 }
107
108 return super.addLine(form, result, request, response);
109 }
110
111 private boolean isDuplicateHoliday(HolidayWrapper newHoliday, HolidayWrapper sourceHoliday){
112 return (newHoliday.getTypeKey().equals(sourceHoliday.getTypeKey()));
113 }
114
115
116
117
118
119
120
121
122
123
124
125 @Override
126 @RequestMapping(method = RequestMethod.GET, params = "methodToCall=start")
127 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
128 HttpServletRequest request, HttpServletResponse response) {
129 HolidayCalendarForm hcForm = (HolidayCalendarForm) form;
130
131 String hcId = request.getParameter(CalendarConstants.CALENDAR_ID);
132
133 String readOnlyView = request.getParameter(CalendarConstants.READ_ONLY_VIEW);
134 hcForm.getView().setReadOnly(BooleanUtils.toBoolean(readOnlyView));
135
136 if ((hcId != null) && !hcId.trim().isEmpty()) {
137 try {
138 getHolidayCalendar(hcId, hcForm);
139 } catch (Exception ex) {
140 throw new RuntimeException("unable to getHolidayCalendar ");
141 }
142 }
143
144 return getUIFModelAndView(form);
145 }
146
147
148
149
150
151
152
153
154
155
156
157 @RequestMapping(method = RequestMethod.GET, params = "methodToCall=startNew")
158 public ModelAndView startNew( @ModelAttribute("KualiForm") HolidayCalendarForm form, BindingResult result,
159 HttpServletRequest request, HttpServletResponse response) {
160 HolidayCalendarInfo hcInfo = null;
161
162 try {
163 String calendarId = request.getParameter(CalendarConstants.CALENDAR_ID);
164 if (calendarId != null && !calendarId.trim().isEmpty()) {
165 hcInfo = getHolidayCalendarFormHelper(form).getHolidayCalendar(calendarId);
166 } else if (StringUtils.equals(request.getParameter(CalendarConstants.PAGE_ID), CalendarConstants.HOLIDAYCALENDAR_COPYPAGE)) {
167 hcInfo = getHolidayCalendarFormHelper(form).getNewestHolidayCalendar();
168 }
169 }
170 catch (Exception e) {
171 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, "Unexpected error; could not get holiday to copy: " + e.getMessage());
172 }
173
174 if (null != hcInfo) {
175 form.setHolidayCalendarInfo(hcInfo);
176
177 Calendar cal = Calendar.getInstance();
178 int currentYear = cal.get(Calendar.YEAR);
179 cal.setTime(hcInfo.getEndDate());
180 int endYear = cal.get(Calendar.YEAR);
181 if (endYear == currentYear) {
182 cal.setTime(hcInfo.getStartDate());
183 int startYear = cal.get(Calendar.YEAR);
184 StringBuilder calName = new StringBuilder();
185 if (startYear < endYear) {
186 calName.append(++startYear);
187 calName.append("-");
188 }
189 calName.append(++endYear);
190 calName.append(" Holiday Calendar");
191 form.setNewCalendarName(calName.toString());
192
193
194
195
196
197
198
199 }
200
201 } else{
202 form.setHolidayCalendarInfo(new HolidayCalendarInfo());
203 }
204
205 return super.start(form, result, request, response);
206 }
207
208
209
210
211
212
213
214
215
216
217
218 @RequestMapping(method = RequestMethod.GET, params = "methodToCall=copyForNew")
219 public ModelAndView copyForNew( @ModelAttribute("KualiForm") HolidayCalendarForm form, BindingResult result,
220 HttpServletRequest request, HttpServletResponse response) {
221 HolidayCalendarInfo hcInfo = null;
222
223 try {
224 String calendarId = request.getParameter(CalendarConstants.CALENDAR_ID);
225 if (calendarId == null || calendarId.trim().isEmpty()) {
226 hcInfo = getHolidayCalendarFormHelper(form).getNewestHolidayCalendar();
227 }
228 else {
229 hcInfo = getHolidayCalendarFormHelper(form).getHolidayCalendar(calendarId);
230 }
231 }
232 catch (Exception e) {
233 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, "Unexpected error; could not get holiday to copy: " + e.getMessage());
234 }
235
236 form.setHolidayCalendarInfo(hcInfo);
237
238 return toCopy(form, result, request, response);
239 }
240
241
242
243
244
245
246
247
248
249
250
251 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=copy")
252 public ModelAndView copy( @ModelAttribute("KualiForm") HolidayCalendarForm form, BindingResult result,
253 HttpServletRequest request, HttpServletResponse response) {
254 restoreForm(form);
255 if ((null == form.getHolidayCalendarInfo()) || (null == form.getHolidayCalendarInfo().getId())) {
256
257 GlobalVariables.getMessageMap().putError( KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM,
258 "Unexpected error; the holiday to copy has been misplaced.");
259 return getUIFModelAndView(form);
260 }
261
262 List<HolidayWrapper> newHolidays;
263 try {
264 newHolidays = getHolidayCalendarFormHelper(form).getHolidayWrappersForHolidayCalendar(
265 form.getHolidayCalendarInfo().getId());
266 }
267 catch (Exception x) {
268 throw new RuntimeException(x);
269 }
270
271 Collections.sort(newHolidays);
272
273 for (HolidayWrapper holidayWrapper : newHolidays) {
274
275 holidayWrapper.getHolidayInfo().setId(null);
276 }
277
278 form.setOrgHcId(form.getHolidayCalendarInfo().getId());
279 form.getHolidayCalendarInfo().setId(null);
280 form.getHolidayCalendarInfo().setName(form.getNewCalendarName());
281 form.getHolidayCalendarInfo().setStartDate(form.getNewCalendarStartDate());
282 form.getHolidayCalendarInfo().setEndDate(form.getNewCalendarEndDate());
283 form.getHolidayCalendarInfo().setStateKey(AtpServiceConstants.ATP_DRAFT_STATE_KEY);
284
285 form.setOfficialCalendar(false);
286 form.getHolidayCalendarInfo().setDescr(AcalCommonUtils.buildDesc(form.getNewCalendarName()));
287 form.setHolidays(newHolidays);
288 form.setHcId(null);
289 form.setMeta(form.getHolidayCalendarInfo().getMeta());
290 return getUIFModelAndView(form, CalendarConstants.HOLIDAYCALENDAR_EDITPAGE);
291 }
292
293
294
295
296
297
298
299
300
301
302
303
304 @RequestMapping(params = "methodToCall=search")
305 public ModelAndView search(@ModelAttribute("KualiForm") HolidayCalendarForm form, BindingResult result,
306 HttpServletRequest request, HttpServletResponse response) throws Exception {
307
308 String controllerPath = CalendarConstants.CALENDAR_SEARCH_CONTROLLER_PATH;
309 Properties urlParameters = new Properties();
310 urlParameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.START_METHOD);
311 urlParameters.put(UifParameters.VIEW_ID, CalendarConstants.CALENDAR_SEARCH_VIEW);
312
313
314
315
316
317 urlParameters.put(CalendarConstants.CALENDAR_SEARCH_TYPE, CalendarConstants.HOLIDAYCALENDER);
318 return super.performRedirect(form,controllerPath, urlParameters);
319 }
320
321
322
323
324
325
326
327
328
329
330
331 @RequestMapping(params = "methodToCall=toCreate")
332 public ModelAndView toCreate(@ModelAttribute("KualiForm") HolidayCalendarForm hcForm, BindingResult result,
333 HttpServletRequest request, HttpServletResponse response){
334 hcForm.setHcId(null);
335 hcForm.setHolidayCalendarInfo( new HolidayCalendarInfo());
336 hcForm.setHolidays(new ArrayList<HolidayWrapper>());
337
338
339
340 Properties urlParameters = new Properties();
341 urlParameters.put(UifParameters.VIEW_ID, CalendarConstants.HOLIDAYCALENDAR_FLOWVIEW);
342 urlParameters.put("flow", hcForm.getFlowKey());
343 urlParameters.put(CalendarConstants.PAGE_ID,CalendarConstants.HOLIDAYCALENDAR_EDITPAGE);
344 urlParameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, "startNew");
345 String controllerPath = CalendarConstants.HCAL_CONTROLLER_PATH;
346 return super.performRedirect(hcForm,controllerPath, urlParameters);
347 }
348
349 private void restoreForm(HolidayCalendarForm hcForm){
350 HolidayCalendarInfo hcalInfo = hcForm.getHolidayCalendarInfo();
351
352 if (StringUtils.isBlank(hcalInfo.getId()) && StringUtils.isNotBlank(hcForm.getOrgHcId())){
353 try{
354 getHolidayCalendar(hcForm.getOrgHcId(), hcForm);
355 hcForm.setHcId(hcForm.getOrgHcId());
356 hcForm.setOrgHcId(null);
357 } catch (Exception ex) {
358 throw new RuntimeException("unable to getHolidayCalendar");
359 }
360 }
361 }
362
363 @RequestMapping(params = "methodToCall=toEdit")
364 public ModelAndView toEdit(@ModelAttribute("KualiForm") HolidayCalendarForm hcForm, BindingResult result,
365 HttpServletRequest request, HttpServletResponse response){
366 restoreForm(hcForm);
367 return getUIFModelAndView(hcForm, CalendarConstants.HOLIDAYCALENDAR_EDITPAGE);
368 }
369
370 @RequestMapping(params = "methodToCall=toCopy")
371 public ModelAndView toCopy(@ModelAttribute("KualiForm") HolidayCalendarForm hcForm, BindingResult result,
372 HttpServletRequest request, HttpServletResponse response){
373 hcForm.setNewCalendarName(null);
374 hcForm.setNewCalendarStartDate(null);
375 hcForm.setNewCalendarEndDate(null);
376 hcForm.setNewCalendar(true);
377 return copy(hcForm, result, request, response);
378 }
379
380
381
382
383
384
385
386
387
388
389
390 @RequestMapping(params = "methodToCall=save")
391 public ModelAndView save(@ModelAttribute("KualiForm") HolidayCalendarForm hcForm, BindingResult result,
392 HttpServletRequest request, HttpServletResponse response) throws Exception {
393 return updateHolidayCalendarForm(hcForm, CalendarConstants.MessageKeys.INFO_HOLIDAY_CALENDAR_SAVED,false);
394 }
395
396
397
398
399
400
401
402
403
404
405
406 @RequestMapping(params = "methodToCall=delete")
407 public ModelAndView delete(@ModelAttribute("KualiForm") HolidayCalendarForm hcForm, BindingResult result,
408 HttpServletRequest request, HttpServletResponse response) throws Exception {
409 String dialog = CalendarConstants.HOLIDAY_DELETE_CONFIRMATION_DIALOG;
410 if (!hasDialogBeenDisplayed(dialog, hcForm)) {
411
412
413 return showDialog(dialog, hcForm, request, response);
414 }else{
415 if(hasDialogBeenAnswered(dialog,hcForm)){
416 boolean confirmDelete = getBooleanDialogResponse(dialog, hcForm, request, response);
417 hcForm.getDialogManager().resetDialogStatus(dialog);
418 if(!confirmDelete){
419 return getUIFModelAndView(hcForm);
420 }
421 } else {
422
423
424 return showDialog(dialog, hcForm, request, response);
425 }
426 }
427
428 getHolidayCalendarFormHelper(hcForm).deleteHolidayCalendar(hcForm.getHolidayCalendarInfo().getId());
429 Properties urlParameters = new Properties();
430 urlParameters.put("viewId", CalendarConstants.CALENDAR_SEARCH_VIEW);
431 urlParameters.put("methodToCall", KRADConstants.SEARCH_METHOD);
432
433
434
435
436
437 HolidayCalendarInfo hCalInfo = hcForm.getHolidayCalendarInfo();
438 urlParameters.put(EnrollConstants.GROWL_MESSAGE, CalendarConstants.MessageKeys.INFO_HOLIDAY_CALENDAR_DELETED);
439 urlParameters.put(EnrollConstants.GROWL_MESSAGE_PARAMS, hCalInfo.getName());
440
441 return performRedirect(hcForm, CalendarConstants.CALENDAR_SEARCH_CONTROLLER_PATH, urlParameters);
442 }
443
444
445
446
447
448
449
450
451
452
453
454 @RequestMapping(params = "methodToCall=makeOfficial")
455 public ModelAndView makeOfficial(@ModelAttribute("KualiForm") HolidayCalendarForm hcForm, BindingResult result,
456 HttpServletRequest request, HttpServletResponse response) throws Exception {
457
458 return updateHolidayCalendarForm(hcForm, CalendarConstants.MessageKeys.INFO_HOLIDAY_CALENDAR_OFFICIAL,true);
459 }
460
461
462
463
464
465
466
467
468
469
470
471 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=deleteHoliday")
472 public ModelAndView deleteHoliday(@ModelAttribute("KualiForm") HolidayCalendarForm hcForm, BindingResult result,
473 HttpServletRequest request, HttpServletResponse response) {
474
475 String selectedCollectionPath = hcForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
476 if (StringUtils.isBlank(selectedCollectionPath)) {
477 throw new RuntimeException("unable to determine the selected collection path");
478 }
479
480 int selectedLineIndex = -1;
481 String selectedLine = hcForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
482 if (StringUtils.isNotBlank(selectedLine)) {
483 selectedLineIndex = Integer.parseInt(selectedLine);
484 }
485
486 if (selectedLineIndex == -1) {
487 throw new RuntimeException("unable to determine the selected line index");
488 }
489
490 try{
491 getHolidayCalendarFormHelper(hcForm).deleteHoliday(selectedLineIndex,hcForm);
492 }catch (Exception e){
493 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, "Error deleting holiday - " + e.getMessage());
494 e.printStackTrace();
495 }
496
497 return getUIFModelAndView(hcForm);
498 }
499
500 private ModelAndView updateHolidayCalendarForm(HolidayCalendarForm hcForm, String updateMsg, boolean isSetOfficial) throws Exception {
501
502 getHolidayCalendarFormHelper(hcForm).validateHolidayCalendar(hcForm);
503 getHolidayCalendarFormHelper(hcForm).populateHolidayCalendarDefaults(hcForm);
504
505 if (GlobalVariables.getMessageMap().getErrorCount() > 0){
506 return getUIFModelAndView(hcForm, CalendarConstants.HOLIDAYCALENDAR_EDITPAGE);
507 }
508
509 if (!isValidHolidayCalendar(hcForm.getHolidayCalendarInfo())) {
510 return getUIFModelAndView(hcForm, CalendarConstants.HOLIDAYCALENDAR_EDITPAGE);
511 }
512
513
514 getHolidayCalendarFormHelper(hcForm).saveHolidayCalendar(hcForm,isSetOfficial);
515 if (GlobalVariables.getMessageMap().hasErrors()){
516 return getUIFModelAndView(hcForm, CalendarConstants.HOLIDAYCALENDAR_EDITPAGE);
517 }
518
519 HolidayCalendarInfo hCalInfo = hcForm.getHolidayCalendarInfo();
520 hcForm.setAdminOrgName(getAdminOrgNameById(hCalInfo.getAdminOrgId()));
521 hcForm.setStateName(getHolidayCalendarFormHelper(hcForm).getHolidayCalendarState(hCalInfo.getStateKey()));
522 hcForm.setNewCalendar(false);
523 hcForm.setOfficialCalendar(hCalInfo.getStateKey().equals(AcademicCalendarServiceConstants.ACADEMIC_CALENDAR_OFFICIAL_STATE_KEY));
524 hcForm.setHcId(hCalInfo.getId());
525 hcForm.setMeta(hCalInfo.getMeta());
526
527 Collections.sort(hcForm.getHolidays());
528
529 KSUifUtils.addGrowlMessageIcon(GrowlIcon.SUCCESS, updateMsg, hCalInfo.getName());
530 return getUIFModelAndView(hcForm, CalendarConstants.HOLIDAYCALENDAR_EDITPAGE);
531
532 }
533
534 private boolean isValidHolidayCalendar(HolidayCalendarInfo hc)throws Exception {
535 boolean valid = true;
536 Date startDate = hc.getStartDate();
537 Date endDate = hc.getEndDate();
538
539 if(startDate.after(endDate)) {
540 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, "ERROR: " + hc.getName() + "start date should not be later than the end date.");
541 valid = false;
542 }
543
544 return valid;
545 }
546
547 private void getHolidayCalendar(String hcId, HolidayCalendarForm hcForm) throws Exception {
548 HolidayCalendarInfo hcInfo = getHolidayCalendarFormHelper(hcForm).getHolidayCalendar(hcId);
549 hcForm.setHolidayCalendarInfo(hcInfo);
550 hcForm.setAdminOrgName(getAdminOrgNameById(hcInfo.getAdminOrgId()));
551 hcForm.setStateName(getHolidayCalendarFormHelper(hcForm).getHolidayCalendarState(hcInfo.getStateKey()));
552 hcForm.setNewCalendar(false);
553 hcForm.setOfficialCalendar(hcInfo.getStateKey().equals(AcademicCalendarServiceConstants.ACADEMIC_CALENDAR_OFFICIAL_STATE_KEY));
554
555 List<HolidayWrapper> holidays =
556 getHolidayCalendarFormHelper(hcForm).getHolidayWrappersForHolidayCalendar(hcInfo.getId());
557 Collections.sort(holidays);
558 hcForm.setHolidays(holidays);
559 if(hcInfo!=null)hcForm.setMeta(hcInfo.getMeta());
560 }
561
562 private String getAdminOrgNameById(String id){
563
564 String adminOrgName = null;
565 Map<String, String> allHcOrgs = new HashMap<String, String>();
566 allHcOrgs.put("102", "Registrar's Office");
567
568 if(allHcOrgs.containsKey(id)){
569 adminOrgName = allHcOrgs.get(id);
570 }
571
572 return adminOrgName;
573 }
574
575 private HolidayCalendarViewHelperService getHolidayCalendarFormHelper(HolidayCalendarForm hcForm) {
576 if (hcForm.getView() != null && hcForm.getView().getViewHelperServiceClass() != null){
577 return (HolidayCalendarViewHelperService)hcForm.getView().getViewHelperService();
578 } else {
579 return (HolidayCalendarViewHelperService)hcForm.getPostedView().getViewHelperService();
580 }
581 }
582
583 }