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.BooleanUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.apache.log4j.Logger;
21 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
22 import org.kuali.rice.core.api.util.KeyValue;
23 import org.kuali.rice.core.api.util.RiceKeyConstants;
24 import org.kuali.rice.krad.uif.UifParameters;
25 import org.kuali.rice.krad.uif.component.Component;
26 import org.kuali.rice.krad.uif.control.SelectControl;
27 import org.kuali.rice.krad.uif.util.ComponentFactory;
28 import org.kuali.rice.krad.uif.util.UifKeyValue;
29 import org.kuali.rice.krad.uif.view.DialogManager;
30 import org.kuali.rice.krad.util.GlobalVariables;
31 import org.kuali.rice.krad.util.KRADConstants;
32 import org.kuali.rice.krad.web.controller.UifControllerBase;
33 import org.kuali.rice.krad.web.form.UifFormBase;
34 import org.kuali.student.common.uif.util.KSControllerHelper;
35 import org.kuali.student.enrollment.class2.acal.dto.AcademicTermWrapper;
36 import org.kuali.student.enrollment.class2.acal.dto.AcalEventWrapper;
37 import org.kuali.student.enrollment.class2.acal.dto.ExamPeriodWrapper;
38 import org.kuali.student.enrollment.class2.acal.dto.HolidayCalendarWrapper;
39 import org.kuali.student.enrollment.class2.acal.dto.HolidayWrapper;
40 import org.kuali.student.enrollment.class2.acal.dto.KeyDateWrapper;
41 import org.kuali.student.enrollment.class2.acal.dto.KeyDatesGroupWrapper;
42 import org.kuali.student.enrollment.class2.acal.form.AcademicCalendarForm;
43 import org.kuali.student.enrollment.class2.acal.service.AcademicCalendarViewHelperService;
44 import org.kuali.student.enrollment.class2.acal.util.CalendarConstants;
45 import org.kuali.student.enrollment.class2.acal.util.AcalCommonUtils;
46 import org.kuali.student.enrollment.common.util.EnrollConstants;
47 import org.kuali.student.r2.common.dto.AttributeInfo;
48 import org.kuali.student.r2.common.dto.RichTextInfo;
49 import org.kuali.student.r2.common.dto.StatusInfo;
50 import org.kuali.student.r2.common.exceptions.OperationFailedException;
51 import org.kuali.student.r2.common.exceptions.VersionMismatchException;
52 import org.kuali.student.r2.core.acal.dto.AcademicCalendarInfo;
53 import org.kuali.student.r2.core.acal.dto.AcalEventInfo;
54 import org.kuali.student.r2.core.acal.dto.ExamPeriodInfo;
55 import org.kuali.student.r2.core.acal.dto.HolidayInfo;
56 import org.kuali.student.r2.core.acal.dto.KeyDateInfo;
57 import org.kuali.student.r2.core.acal.dto.TermInfo;
58 import org.kuali.student.r2.core.acal.service.AcademicCalendarService;
59 import org.kuali.student.r2.core.acal.service.facade.AcademicCalendarServiceFacade;
60 import org.kuali.student.r2.core.constants.AcademicCalendarServiceConstants;
61 import org.kuali.student.r2.core.constants.AtpServiceConstants;
62 import org.springframework.stereotype.Controller;
63 import org.springframework.validation.BindingResult;
64 import org.springframework.web.bind.annotation.ModelAttribute;
65 import org.springframework.web.bind.annotation.RequestMapping;
66 import org.springframework.web.bind.annotation.RequestMethod;
67 import org.springframework.web.servlet.ModelAndView;
68
69 import javax.servlet.http.HttpServletRequest;
70 import javax.servlet.http.HttpServletResponse;
71 import javax.xml.namespace.QName;
72 import java.lang.reflect.InvocationTargetException;
73 import java.lang.reflect.Method;
74 import java.util.ArrayList;
75 import java.util.Date;
76 import java.util.List;
77 import java.util.Properties;
78
79
80
81
82
83
84
85
86
87
88
89
90
91 @Controller
92 @RequestMapping(value = "/academicCalendar")
93 public class AcademicCalendarController extends UifControllerBase {
94
95 private static final Logger LOG = Logger.getLogger(AcademicCalendarController.class);
96
97 private AcademicCalendarService acalService;
98 private AcademicCalendarServiceFacade academicCalendarServiceFacade;
99
100 @Override
101 protected UifFormBase createInitialForm(HttpServletRequest request) {
102 return new AcademicCalendarForm();
103 }
104
105
106
107
108
109
110
111
112
113
114 @Override
115 @RequestMapping(method = RequestMethod.GET, params = "methodToCall=start")
116 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
117 HttpServletRequest request, HttpServletResponse response) {
118 AcademicCalendarForm acalForm = (AcademicCalendarForm) form;
119
120 String acalId = request.getParameter(CalendarConstants.CALENDAR_ID);
121
122 if (StringUtils.isNotBlank(acalId)){
123 getAcalViewHelperService(acalForm).populateAcademicCalendar(acalId, acalForm);
124 }
125
126 String readOnlyView = request.getParameter(CalendarConstants.READ_ONLY_VIEW);
127 acalForm.getView().setReadOnly(BooleanUtils.toBoolean(readOnlyView));
128
129 if (StringUtils.isNotBlank(request.getParameter(CalendarConstants.SELECT_TAB))) {
130 acalForm.setDefaultTabToShow(request.getParameter(CalendarConstants.SELECT_TAB));
131 }
132
133 return getUIFModelAndView(form);
134 }
135
136 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=reload")
137 public ModelAndView reload(@ModelAttribute("KualiForm") AcademicCalendarForm acalForm, BindingResult result,
138 HttpServletRequest request, HttpServletResponse response) {
139 String acalId = acalForm.getAcademicCalendarInfo().getId();
140 getAcalViewHelperService(acalForm).populateAcademicCalendar(acalId, acalForm);
141 acalForm.setReload(false);
142 return getUIFModelAndView(acalForm, CalendarConstants.ACADEMIC_CALENDAR_EDIT_PAGE);
143 }
144
145
146
147
148 @RequestMapping(params = "methodToCall=createBlankCalendar")
149 public ModelAndView createBlankCalendar(@ModelAttribute("KualiForm") AcademicCalendarForm acalForm, BindingResult result,
150 HttpServletRequest request, HttpServletResponse response){
151
152 acalForm.setAcademicCalendarInfo(new AcademicCalendarInfo());
153 acalForm.setEvents(new ArrayList<AcalEventWrapper>());
154 acalForm.setHolidayCalendarList(new ArrayList<HolidayCalendarWrapper>());
155 acalForm.setTermWrapperList(new ArrayList<AcademicTermWrapper>());
156
157 return getUIFModelAndView(acalForm, CalendarConstants.ACADEMIC_CALENDAR_EDIT_PAGE);
158 }
159
160
161
162
163
164
165 @RequestMapping(method = RequestMethod.GET, params = "methodToCall=startNew")
166 public ModelAndView startNew( @ModelAttribute("KualiForm") AcademicCalendarForm acalForm, BindingResult result,
167 HttpServletRequest request, HttpServletResponse response) {
168
169 try {
170 AcademicCalendarInfo acalInfo = getAcalViewHelperService(acalForm).getLatestAcademicCalendar();
171 acalForm.setCopyFromAcal(acalInfo);
172 acalForm.setMeta(acalInfo.getMeta());
173 } catch (Exception e) {
174 throw getAcalViewHelperService(acalForm).convertServiceExceptionsToUI(e);
175 }
176
177 return super.start(acalForm, result, request, response);
178 }
179
180
181
182
183
184
185
186
187
188
189 @RequestMapping(params = "methodToCall=toEdit")
190 public ModelAndView toEdit(@ModelAttribute("KualiForm") AcademicCalendarForm acalForm, BindingResult result,
191 HttpServletRequest request, HttpServletResponse response){
192 AcademicCalendarInfo acalInfo = acalForm.getAcademicCalendarInfo();
193 AcademicCalendarInfo orgAcalInfo = acalForm.getCopyFromAcal();
194
195 if (StringUtils.isBlank(acalInfo.getId()) && StringUtils.isNotBlank(orgAcalInfo.getId())){
196 getAcalViewHelperService(acalForm).populateAcademicCalendar(orgAcalInfo.getId(), acalForm);
197 acalForm.setCopyFromAcal(new AcademicCalendarInfo());
198 }
199
200 acalForm.getView().setReadOnly(false);
201
202 return getUIFModelAndView(acalForm, CalendarConstants.ACADEMIC_CALENDAR_EDIT_PAGE);
203 }
204
205
206
207
208
209
210
211
212
213
214
215 @RequestMapping(params = "methodToCall=toCopy")
216 public ModelAndView toCopy(@ModelAttribute("KualiForm") AcademicCalendarForm acalForm, BindingResult result,
217 HttpServletRequest request, HttpServletResponse response){
218
219 AcademicCalendarInfo acalInfo = null;
220
221 String acalId = request.getParameter(CalendarConstants.CALENDAR_ID);
222 if (acalId != null && !acalId.trim().isEmpty()) {
223 String pageId = request.getParameter(CalendarConstants.PAGE_ID);
224 if (CalendarConstants.ACADEMIC_CALENDAR_COPY_PAGE.equals(pageId)) {
225
226 try {
227 acalInfo= getAcalService().getAcademicCalendar(acalId, getAcalViewHelperService(acalForm).createContextInfo());
228 acalForm.setCopyFromAcal(acalInfo);
229 } catch (Exception ex) {
230 throw getAcalViewHelperService(acalForm).convertServiceExceptionsToUI(ex);
231 }
232 }
233 }
234 else {
235
236 try {
237 acalInfo = acalForm.getAcademicCalendarInfo();
238 acalForm.reset();
239 acalForm.setNewCalendar(true);
240 acalForm.setCopyFromAcal(acalInfo);
241 }
242 catch (Exception e) {
243 throw getAcalViewHelperService(acalForm).convertServiceExceptionsToUI(e);
244 }
245
246 }
247
248
249 if(acalInfo != null){
250 acalForm.setMeta(acalInfo.getMeta());
251 }
252
253 return copy(acalForm, result, request, response);
254 }
255
256
257
258
259
260
261
262
263
264
265 @RequestMapping(method = RequestMethod.POST, params="methodToCall=copy")
266 public ModelAndView copy( @ModelAttribute("KualiForm") AcademicCalendarForm acalForm, BindingResult result,
267 HttpServletRequest request, HttpServletResponse response) {
268
269 getAcalViewHelperService(acalForm).copyToCreateAcademicCalendar(acalForm);
270
271 return getUIFModelAndView(acalForm, CalendarConstants.ACADEMIC_CALENDAR_EDIT_PAGE);
272
273 }
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324 @RequestMapping(params = "methodToCall=search")
325 public ModelAndView search(@ModelAttribute("KualiForm") AcademicCalendarForm acalForm, BindingResult result,
326 HttpServletRequest request, HttpServletResponse response) {
327
328 String controllerPath = CalendarConstants.CALENDAR_SEARCH_CONTROLLER_PATH;
329 Properties urlParameters = new Properties();
330 urlParameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.START_METHOD);
331 urlParameters.put(UifParameters.VIEW_ID, CalendarConstants.CALENDAR_SEARCH_VIEW);
332
333
334
335
336 return super.performRedirect(acalForm,controllerPath, urlParameters);
337 }
338
339
340
341
342
343
344
345
346
347
348
349 @RequestMapping(params = "methodToCall=save")
350 public ModelAndView save(@ModelAttribute("KualiForm") AcademicCalendarForm academicCalendarForm, BindingResult result,
351 HttpServletRequest request, HttpServletResponse response) {
352 AcademicCalendarForm acal = saveAcademicCalendarDirtyFields(academicCalendarForm);
353 return getUIFModelAndView(acal);
354 }
355
356
357
358
359
360
361
362
363
364
365 @RequestMapping(params = "methodToCall=delete")
366 public ModelAndView delete(@ModelAttribute("KualiForm") AcademicCalendarForm acalForm, BindingResult result,
367 HttpServletRequest request, HttpServletResponse response) {
368 String dialog = CalendarConstants.ACADEMIC_DELETE_CONFIRMATION_DIALOG;
369 if (!hasDialogBeenDisplayed(dialog, acalForm)) {
370
371
372 return showDialog(dialog, acalForm, request, response);
373 }else{
374 if(hasDialogBeenAnswered(dialog,acalForm)){
375 boolean confirmDelete = getBooleanDialogResponse(dialog, acalForm, request, response);
376 acalForm.getDialogManager().resetDialogStatus(dialog);
377 if(!confirmDelete){
378 return getUIFModelAndView(acalForm);
379 }
380 } else {
381
382
383 return showDialog(dialog, acalForm, request, response);
384 }
385 }
386
387 StatusInfo statusInfo;
388 try {
389 statusInfo = getAcademicCalendarServiceFacade().deleteCalendarCascaded(acalForm.getAcademicCalendarInfo().getId(), getAcalViewHelperService(acalForm).createContextInfo());
390 } catch (Exception e) {
391 throw getAcalViewHelperService(acalForm).convertServiceExceptionsToUI(e);
392 }
393
394 if (statusInfo.getIsSuccess()){
395
396 Properties urlParameters = new Properties();
397 urlParameters.put(EnrollConstants.GROWL_TITLE,"");
398 urlParameters.put(EnrollConstants.GROWL_MESSAGE, CalendarConstants.MessageKeys.INFO_ACADEMIC_CALENDAR_DELETED);
399 urlParameters.put(EnrollConstants.GROWL_MESSAGE_PARAMS,acalForm.getAcademicCalendarInfo().getName());
400 return redirectToSearch(acalForm,request,urlParameters);
401 } else {
402 GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_MESSAGES, CalendarConstants.MessageKeys.ERROR_DELETING, acalForm.getAcademicCalendarInfo().getName(),statusInfo.getMessage());
403 return getUIFModelAndView(acalForm);
404 }
405
406
407 }
408
409
410
411
412
413
414
415
416
417
418
419 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=cancelTerm")
420 public ModelAndView cancelTerm(@ModelAttribute("KualiForm") AcademicCalendarForm academicCalendarForm, BindingResult result,
421 HttpServletRequest request, HttpServletResponse response) {
422 academicCalendarForm.setFieldsToSave(processDirtyFields(academicCalendarForm));
423 int selectedLineIndex = KSControllerHelper.getSelectedCollectionLineIndex(academicCalendarForm);
424
425 AcademicTermWrapper termWrapper = academicCalendarForm.getTermWrapperList().get(selectedLineIndex);
426
427 AcademicCalendarViewHelperService viewHelperService = getAcalViewHelperService(academicCalendarForm);
428
429 if (termWrapper.isNew()){
430 academicCalendarForm.getTermWrapperList().remove(selectedLineIndex);
431 if (termWrapper.isHasSubterm()){
432 List<AcademicTermWrapper> subTerms = termWrapper.getSubterms();
433 for(AcademicTermWrapper subTerm : subTerms){
434 academicCalendarForm.getTermWrapperList().remove(subTerm);
435 }
436 }
437 }else{
438 try {
439 TermInfo termInfo = getAcalService().getTerm(termWrapper.getTermInfo().getId(), viewHelperService.createContextInfo());
440 boolean calculateInstrDays = !academicCalendarForm.getHolidayCalendarList().isEmpty();
441 AcademicTermWrapper termWrapperFromDB = viewHelperService.populateTermWrapper(termInfo, false,calculateInstrDays);
442 academicCalendarForm.getTermWrapperList().set(selectedLineIndex,termWrapperFromDB);
443 } catch (Exception e) {
444 throw getAcalViewHelperService(academicCalendarForm).convertServiceExceptionsToUI(e);
445 }
446 }
447
448 return getUIFModelAndView(academicCalendarForm);
449 }
450
451
452
453
454
455
456
457
458
459
460 @RequestMapping(params = "methodToCall=makeTermOfficial")
461 public ModelAndView makeTermOfficial(@ModelAttribute("KualiForm") AcademicCalendarForm academicCalendarForm, BindingResult result,
462 HttpServletRequest request, HttpServletResponse response) {
463 academicCalendarForm.setFieldsToSave(processDirtyFields(academicCalendarForm));
464
465 int selectedLineIndex;
466
467 AcademicTermWrapper termWrapper;
468
469 String dialog=null;
470 if(academicCalendarForm.isOfficialCalendar()){
471 dialog = CalendarConstants.ACADEMIC_TERM_OFFICIAL_CONFIRMATION_DIALOG;
472 }else{
473 dialog = CalendarConstants.ACADEMIC_TERM_AND_CALENDAR_OFFICIAL_CONFIRMATION_DIALOG;
474 }
475
476
477 if (!hasDialogBeenDisplayed(dialog, academicCalendarForm)) {
478 selectedLineIndex= KSControllerHelper.getSelectedCollectionLineIndex(academicCalendarForm);
479 termWrapper= academicCalendarForm.getTermWrapperList().get(selectedLineIndex);
480 academicCalendarForm.setSelectedCollectionPath(academicCalendarForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH));
481 academicCalendarForm.setSelectedLineIndex(academicCalendarForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
482 academicCalendarForm.setMakeOfficialName(termWrapper.getName());
483 academicCalendarForm.setMakeOfficialIsSubterm(termWrapper.isSubTerm());
484 if(termWrapper.getParentTermInfo()!=null){
485 academicCalendarForm.setMakeOfficialParentTermName(termWrapper.getParentTermInfo().getName());
486 academicCalendarForm.setOfficialParentTerm(false);
487 for(AcademicTermWrapper term : academicCalendarForm.getTermWrapperList()){
488 if(term.getTermInfo().getId().equals(termWrapper.getParentTermInfo().getId())){
489 academicCalendarForm.setOfficialParentTerm(term.isOfficial());
490 }
491 }
492 }
493
494
495 return showDialog(dialog, academicCalendarForm, request, response);
496 }else{
497 if(hasDialogBeenAnswered(dialog,academicCalendarForm)){
498 boolean confirm = getBooleanDialogResponse(dialog, academicCalendarForm, request, response);
499 academicCalendarForm.getDialogManager().resetDialogStatus(dialog);
500 if(!confirm){
501 return getUIFModelAndView(academicCalendarForm);
502 }
503 } else {
504 selectedLineIndex= KSControllerHelper.getSelectedCollectionLineIndex(academicCalendarForm);
505 termWrapper= academicCalendarForm.getTermWrapperList().get(selectedLineIndex);
506 academicCalendarForm.setSelectedCollectionPath(academicCalendarForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH));
507 academicCalendarForm.setSelectedLineIndex(academicCalendarForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
508 academicCalendarForm.setMakeOfficialName(termWrapper.getName());
509 academicCalendarForm.setMakeOfficialIsSubterm(termWrapper.isSubTerm());
510 if(termWrapper.getParentTermInfo()!=null){
511 academicCalendarForm.setMakeOfficialParentTermName(termWrapper.getParentTermInfo().getName());
512 academicCalendarForm.setOfficialParentTerm(false);
513 for(AcademicTermWrapper term : academicCalendarForm.getTermWrapperList()){
514 if(term.getTermInfo().getId().equals(termWrapper.getParentTermInfo().getId())){
515 academicCalendarForm.setOfficialParentTerm(term.isOfficial());
516 }
517 }
518 }
519
520
521
522 return showDialog(dialog, academicCalendarForm, request, response);
523 }
524 }
525
526 academicCalendarForm.getActionParameters().put(UifParameters.SELLECTED_COLLECTION_PATH, academicCalendarForm.getSelectedCollectionPath());
527 if (academicCalendarForm.getSelectedLineIndex() != null && academicCalendarForm.getSelectedLineIndex().indexOf(",") > -1) {
528 academicCalendarForm.getActionParameters().put(UifParameters.SELECTED_LINE_INDEX, academicCalendarForm.getSelectedLineIndex().substring(0,academicCalendarForm.getSelectedLineIndex().indexOf(",")));
529 } else {
530 academicCalendarForm.getActionParameters().put(UifParameters.SELECTED_LINE_INDEX, academicCalendarForm.getSelectedLineIndex());
531 }
532 selectedLineIndex= KSControllerHelper.getSelectedCollectionLineIndex(academicCalendarForm);
533 termWrapper= academicCalendarForm.getTermWrapperList().get(selectedLineIndex);
534
535 boolean acalOfficial=true;
536
537 if(!academicCalendarForm.isOfficialCalendar()){
538 acalOfficial = makeAcalOfficial(academicCalendarForm);
539 }
540
541 if(acalOfficial){
542 makeTermOfficial(termWrapper,academicCalendarForm);
543 }
544 getAcalViewHelperService(academicCalendarForm).populateAcademicCalendar(academicCalendarForm.getAcademicCalendarInfo().getId(), academicCalendarForm);
545 academicCalendarForm.setDefaultTabToShow(CalendarConstants.ACAL_TERM_TAB);
546 return getUIFModelAndView(academicCalendarForm);
547 }
548
549
550
551
552
553
554
555
556
557
558
559 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=cancelAddingHoliday")
560 public ModelAndView cancelAddingHoliday(@ModelAttribute("KualiForm") AcademicCalendarForm academicCalendarForm, BindingResult result,
561 HttpServletRequest request, HttpServletResponse response) {
562 academicCalendarForm.setFieldsToSave(processDirtyFields(academicCalendarForm));
563 ((HolidayCalendarWrapper)academicCalendarForm.getNewCollectionLines().get("holidayCalendarList")).setId(StringUtils.EMPTY);
564
565 return getUIFModelAndView(academicCalendarForm);
566 }
567
568 private void setDeleteTermMessageWithContext(AcademicCalendarForm academicCalendarForm){
569 academicCalendarForm.setSelectedCollectionPath(academicCalendarForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH));
570 academicCalendarForm.setSelectedLineIndex(academicCalendarForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX));
571 academicCalendarForm.getActionParameters().put(UifParameters.SELLECTED_COLLECTION_PATH, academicCalendarForm.getSelectedCollectionPath());
572 if (academicCalendarForm.getSelectedLineIndex() != null && academicCalendarForm.getSelectedLineIndex().indexOf(",") > -1) {
573 academicCalendarForm.getActionParameters().put(UifParameters.SELECTED_LINE_INDEX, academicCalendarForm.getSelectedLineIndex().substring(0,academicCalendarForm.getSelectedLineIndex().indexOf(",")));
574 } else {
575 academicCalendarForm.getActionParameters().put(UifParameters.SELECTED_LINE_INDEX, academicCalendarForm.getSelectedLineIndex());
576 }
577
578 int selectedLineIndex = KSControllerHelper.getSelectedCollectionLineIndex(academicCalendarForm);
579
580 AcademicTermWrapper selectedTermWrapper = academicCalendarForm.getTermWrapperList().get(selectedLineIndex);
581 if(selectedTermWrapper.isSubTerm()){
582 academicCalendarForm.setMessageForDeleteTermOrSubterm(CalendarConstants.MESSAGE_CONFIRM_TO_DELETE_SUBTERM);
583 }else if(selectedTermWrapper.isHasSubterm()){
584 academicCalendarForm.setMessageForDeleteTermOrSubterm(CalendarConstants.MESSAGE_CONFIRM_TO_DELETE_TERM_WITH_SUBTERM);
585 }else{
586 academicCalendarForm.setMessageForDeleteTermOrSubterm(CalendarConstants.MESSAGE_CONFIRM_TO_DELETE_TERM_ONLY);
587 }
588 }
589
590
591
592
593
594
595
596
597
598
599
600 @RequestMapping(params = "methodToCall=deleteTerm")
601 public ModelAndView deleteTerm(@ModelAttribute("KualiForm") AcademicCalendarForm academicCalendarForm, BindingResult result,
602 HttpServletRequest request, HttpServletResponse response) {
603 academicCalendarForm.setFieldsToSave(processDirtyFields(academicCalendarForm));
604
605 String dialog = CalendarConstants.TERM_DELETE_CONFIRMATION_DIALOG;
606 if (!hasDialogBeenDisplayed(dialog, academicCalendarForm)) {
607 setDeleteTermMessageWithContext(academicCalendarForm);
608
609 return showDialog(dialog, academicCalendarForm, request, response);
610 }else{
611 if(hasDialogBeenAnswered(dialog,academicCalendarForm)){
612 boolean confirmDelete = getBooleanDialogResponse(dialog, academicCalendarForm, request, response);
613 academicCalendarForm.getDialogManager().resetDialogStatus(dialog);
614 if(!confirmDelete){
615 return getUIFModelAndView(academicCalendarForm);
616 }
617 } else {
618 setDeleteTermMessageWithContext(academicCalendarForm);
619
620 return showDialog(dialog, academicCalendarForm, request, response);
621 }
622 }
623
624 academicCalendarForm.getActionParameters().put(UifParameters.SELLECTED_COLLECTION_PATH, academicCalendarForm.getSelectedCollectionPath());
625 if (academicCalendarForm.getSelectedLineIndex() != null && academicCalendarForm.getSelectedLineIndex().indexOf(",") > -1) {
626 academicCalendarForm.getActionParameters().put(UifParameters.SELECTED_LINE_INDEX, academicCalendarForm.getSelectedLineIndex().substring(0,academicCalendarForm.getSelectedLineIndex().indexOf(",")));
627 } else {
628 academicCalendarForm.getActionParameters().put(UifParameters.SELECTED_LINE_INDEX, academicCalendarForm.getSelectedLineIndex());
629 }
630
631 int selectedLineIndex = KSControllerHelper.getSelectedCollectionLineIndex(academicCalendarForm);
632
633 AcademicTermWrapper theTermWrapper = academicCalendarForm.getTermWrapperList().get(selectedLineIndex);
634
635 if (!theTermWrapper.isNew()){
636 academicCalendarForm.getTermsToDeleteOnSave().add(theTermWrapper);
637 }
638 academicCalendarForm.getTermWrapperList().remove(selectedLineIndex);
639
640 if(theTermWrapper.isHasSubterm()) {
641 List<AcademicTermWrapper> subTerms = theTermWrapper.getSubterms();
642 for(AcademicTermWrapper subTerm : subTerms){
643 academicCalendarForm.getTermWrapperList().remove(subTerm);
644 }
645 }
646 return getUIFModelAndView(academicCalendarForm);
647 }
648
649
650
651
652
653
654
655
656
657
658 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=deleteKeyDate")
659 public ModelAndView deleteKeyDate(@ModelAttribute("KualiForm") AcademicCalendarForm academicCalendarForm, BindingResult result,
660 HttpServletRequest request, HttpServletResponse response) {
661 academicCalendarForm.setFieldsToSave(processDirtyFields(academicCalendarForm));
662 String selectedCollectionPath = academicCalendarForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
663 if (StringUtils.isBlank(selectedCollectionPath)) {
664 throw new RuntimeException("unable to determine the selected collection path");
665 }
666
667 int selectedLineIndex = KSControllerHelper.getSelectedCollectionLineIndex(academicCalendarForm);
668
669 String selectedTermIndex = StringUtils.substringBetween(selectedCollectionPath,"termWrapperList[","]");
670 String selectedKeyDateGroup = StringUtils.substringBetween(selectedCollectionPath,"keyDatesGroupWrappers[","]");
671
672 AcademicTermWrapper termWrapper = academicCalendarForm.getTermWrapperList().get(Integer.parseInt(selectedTermIndex));
673 KeyDatesGroupWrapper keydateGroup = termWrapper.getKeyDatesGroupWrappers().get(Integer.parseInt(selectedKeyDateGroup));
674 KeyDateWrapper keyDateWrapper = keydateGroup.getKeydates().get(selectedLineIndex);
675
676 if (StringUtils.isNotBlank(keyDateWrapper.getKeyDateInfo().getId())){
677 termWrapper.getKeyDatesToDeleteOnSave().add(keyDateWrapper);
678 }
679
680 keydateGroup.getKeydates().remove(selectedLineIndex);
681
682 return getUIFModelAndView(academicCalendarForm);
683
684 }
685
686
687
688
689
690
691
692
693
694
695 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=markExamPeriodtoDelete")
696 public ModelAndView markExamPeriodtoDelete(@ModelAttribute("KualiForm") AcademicCalendarForm academicCalendarForm, BindingResult result,
697 HttpServletRequest request, HttpServletResponse response) {
698 academicCalendarForm.setFieldsToSave(processDirtyFields(academicCalendarForm));
699 String selectedCollectionPath = academicCalendarForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
700 if (StringUtils.isBlank(selectedCollectionPath)) {
701 throw new RuntimeException("unable to determine the selected collection path");
702 }
703
704 int selectedLineIndex = KSControllerHelper.getSelectedCollectionLineIndex(academicCalendarForm);
705
706 String selectedTermIndex = StringUtils.substringBetween(selectedCollectionPath,"termWrapperList[","]");
707 AcademicTermWrapper termWrapper = academicCalendarForm.getTermWrapperList().get(Integer.parseInt(selectedTermIndex));
708 ExamPeriodWrapper examPeriodWrapper = termWrapper.getExamdates().get(selectedLineIndex);
709
710 if (StringUtils.isNotBlank(examPeriodWrapper.getExamPeriodInfo().getId())){
711 termWrapper.getExamPeriodsToDeleteOnSave().add(examPeriodWrapper);
712 }
713 termWrapper.getExamdates().remove(selectedLineIndex);
714
715 return getUIFModelAndView(academicCalendarForm);
716
717 }
718
719 @RequestMapping(params = "methodToCall=deleteKeyDateGroup")
720 public ModelAndView deleteKeyDateGroup(@ModelAttribute("KualiForm") AcademicCalendarForm academicCalendarForm, BindingResult result,
721 HttpServletRequest request, HttpServletResponse response) {
722 academicCalendarForm.setFieldsToSave(processDirtyFields(academicCalendarForm));
723 String selectedCollectionPath = academicCalendarForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
724 if (StringUtils.isBlank(selectedCollectionPath)) {
725 throw new RuntimeException("unable to determine the selected collection path");
726 }
727
728 int selectedLineIndex = KSControllerHelper.getSelectedCollectionLineIndex(academicCalendarForm);
729
730 String selectedTermIndex = StringUtils.substringBetween(selectedCollectionPath, "termWrapperList[", "]");
731
732 AcademicTermWrapper termWrapper = academicCalendarForm.getTermWrapperList().get(Integer.parseInt(selectedTermIndex));
733 KeyDatesGroupWrapper keydateGroup = termWrapper.getKeyDatesGroupWrappers().get(selectedLineIndex);
734 for (KeyDateWrapper keyDateWrapper : keydateGroup.getKeydates()) {
735 if (StringUtils.isNotBlank(keyDateWrapper.getKeyDateInfo().getId())){
736 termWrapper.getKeyDatesToDeleteOnSave().add(keyDateWrapper);
737 }
738 }
739
740 termWrapper.getKeyDatesGroupWrappers().remove(keydateGroup);
741
742 return getUIFModelAndView(academicCalendarForm);
743
744 }
745
746
747
748
749
750
751
752
753
754
755
756 @RequestMapping(params = "methodToCall=deleteHolidayCalendar")
757 public ModelAndView deleteHolidayCalendar(@ModelAttribute("KualiForm") AcademicCalendarForm academicCalendarForm, BindingResult result,
758 HttpServletRequest request, HttpServletResponse response) {
759 academicCalendarForm.setFieldsToSave(processDirtyFields(academicCalendarForm));
760 String selectedCollectionPath = academicCalendarForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
761 if (StringUtils.isBlank(selectedCollectionPath)) {
762 throw new RuntimeException("unable to determine the selected collection path");
763 }
764
765 int selectedLineIndex = KSControllerHelper.getSelectedCollectionLineIndex(academicCalendarForm);
766
767 academicCalendarForm.getHolidayCalendarList().remove(selectedLineIndex);
768
769 return getUIFModelAndView(academicCalendarForm);
770
771 }
772
773
774
775
776
777
778
779
780
781
782
783
784 @RequestMapping(params = "methodToCall=deleteAcalEvent")
785 public ModelAndView deleteAcalEvent(@ModelAttribute("KualiForm") AcademicCalendarForm academicCalendarForm, BindingResult result,
786 HttpServletRequest request, HttpServletResponse response) {
787 academicCalendarForm.setFieldsToSave(processDirtyFields(academicCalendarForm));
788 String selectedCollectionPath = academicCalendarForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
789 if (StringUtils.isBlank(selectedCollectionPath)) {
790 throw new RuntimeException("unable to determine the selected collection path");
791 }
792
793 int selectedLineIndex = KSControllerHelper.getSelectedCollectionLineIndex(academicCalendarForm);
794 AcalEventWrapper deletedEvent = academicCalendarForm.getEvents().get(selectedLineIndex);
795 if(deletedEvent.getAcalEventInfo().getId()!=null){
796 academicCalendarForm.getEventsToDeleteOnSave().add(deletedEvent);
797 }
798
799 academicCalendarForm.getEvents().remove(selectedLineIndex);
800
801 return getUIFModelAndView(academicCalendarForm);
802
803 }
804
805
806
807
808 @RequestMapping(params = "methodToCall=makeAcalOfficial")
809 public ModelAndView makeAcalOfficial(@ModelAttribute("KualiForm") AcademicCalendarForm acalForm, BindingResult result,
810 HttpServletRequest request, HttpServletResponse response) {
811
812 String dialog = CalendarConstants.ACADEMIC_CALENDAR_OFFICIAL_CONFIRMATION_DIALOG;
813 if (!hasDialogBeenDisplayed(dialog, acalForm)) {
814 acalForm.setMakeOfficialName(acalForm.getAcademicCalendarInfo().getName());
815
816 return showDialog(dialog, acalForm, request, response);
817 }else{
818 if(hasDialogBeenAnswered(dialog,acalForm)){
819 boolean confirm = getBooleanDialogResponse(dialog, acalForm, request, response);
820 acalForm.getDialogManager().resetDialogStatus(dialog);
821 if(!confirm){
822 return getUIFModelAndView(acalForm);
823 }
824 } else {
825
826
827 return showDialog(dialog, acalForm, request, response);
828 }
829 }
830
831 makeAcalOfficial(acalForm);
832
833 return getUIFModelAndView(acalForm);
834 }
835
836 public AcademicCalendarService getAcalService() {
837 if(acalService == null) {
838 acalService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
839 }
840 return this.acalService;
841 }
842
843 public AcademicCalendarServiceFacade getAcademicCalendarServiceFacade() {
844 if(academicCalendarServiceFacade == null) {
845 academicCalendarServiceFacade = (AcademicCalendarServiceFacade) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.FACADE_NAMESPACE, AcademicCalendarServiceConstants.FACADE_SERVICE_NAME_LOCAL_PART));
846 }
847 return this.academicCalendarServiceFacade;
848 }
849
850 protected AcademicCalendarViewHelperService getAcalViewHelperService(AcademicCalendarForm acalForm){
851 AcademicCalendarViewHelperService viewHelperService = (AcademicCalendarViewHelperService) KSControllerHelper.getViewHelperService(acalForm);
852 return viewHelperService;
853 }
854
855
856
857
858
859
860
861
862
863 private ModelAndView redirectToSearch(AcademicCalendarForm academicCalendarForm,HttpServletRequest request, Properties urlParameters){
864 urlParameters.put("viewId", CalendarConstants.CALENDAR_SEARCH_VIEW);
865 urlParameters.put("methodToCall", KRADConstants.START_METHOD);
866
867
868
869
870 String uri = request.getRequestURL().toString().replace("academicCalendar","calendarSearch");
871 return performRedirect(academicCalendarForm, uri, urlParameters);
872 }
873
874
875
876
877
878
879
880
881 private boolean makeTermOfficial(AcademicTermWrapper term, AcademicCalendarForm acalForm){
882 AcademicCalendarViewHelperService viewHelperService = getAcalViewHelperService(acalForm);
883 StatusInfo statusInfo;
884 try {
885
886 statusInfo = getAcademicCalendarServiceFacade().makeTermOfficialCascaded(term.getTermInfo().getId(), viewHelperService.createContextInfo());
887
888 if (!statusInfo.getIsSuccess()) {
889 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_MESSAGES, RiceKeyConstants.ERROR_CUSTOM, statusInfo.getMessage());
890 return false;
891 }
892 term.setTermInfo(getAcalService().getTerm(term.getTermInfo().getId(),viewHelperService.createContextInfo()));
893 if(term.isSubTerm()){
894 for(AcademicTermWrapper termWrapper : acalForm.getTermWrapperList()){
895 if(termWrapper.getTermInfo().getId().equals(term.getParentTermInfo().getId())){
896 TermInfo updatedParentTerm = getAcalService().getTerm(term.getParentTermInfo().getId(),viewHelperService.createContextInfo());
897
898 term.setParentTermInfo(updatedParentTerm);
899 termWrapper.setTermInfo(updatedParentTerm);
900 }
901 }
902 }
903 for (KeyDatesGroupWrapper groupWrapper : term.getKeyDatesGroupWrappers()){
904 for (KeyDateWrapper keyDateWrapper : groupWrapper.getKeydates()) {
905
906
907 if (keyDateWrapper.getKeyDateInfo() != null && keyDateWrapper.getKeyDateInfo().getId()!=null) {
908 keyDateWrapper.setKeyDateInfo(getAcalService().getKeyDate(keyDateWrapper.getKeyDateInfo().getId(),viewHelperService.createContextInfo()));
909 }
910 }
911 }
912 } catch (Exception e) {
913 LOG.error("Make Official Failed for Term",e);
914 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_MESSAGES,CalendarConstants.MessageKeys.ERROR_ACAL_SAVE_TERM_OFFICIAL_FAILED,e.getMessage());
915 return false;
916 }
917 return true;
918 }
919
920
921
922
923
924
925
926 private boolean makeAcalOfficial(AcademicCalendarForm acalForm){
927 AcademicCalendarViewHelperService viewHelperService = getAcalViewHelperService(acalForm);
928 try {
929 StatusInfo statusInfo = null;
930 statusInfo = getAcalService().changeAcademicCalendarState(acalForm.getAcademicCalendarInfo().getId(), AcademicCalendarServiceConstants.ACADEMIC_CALENDAR_OFFICIAL_STATE_KEY,viewHelperService.createContextInfo());
931 if (!statusInfo.getIsSuccess()){
932 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_MESSAGES, RiceKeyConstants.ERROR_CUSTOM, statusInfo.getMessage());
933 return false;
934 } else{
935
936 acalForm.setAcademicCalendarInfo(getAcalService().getAcademicCalendar(acalForm.getAcademicCalendarInfo().getId(), viewHelperService.createContextInfo()));
937 acalForm.setOfficialCalendar(true);
938 for (AcalEventWrapper eventWrapper : acalForm.getEvents()) {
939 eventWrapper.setAcalEventInfo(getAcalService().getAcalEvent(eventWrapper.getAcalEventInfo().getId(),viewHelperService.createContextInfo()));
940 }
941 }
942 } catch (Exception e) {
943 LOG.error("Make Official Failed for Acal",e);
944 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_MESSAGES, CalendarConstants.MessageKeys.ERROR_ACAL_OFFICIAL_FAILED ,e.getMessage());
945 return false;
946 }
947 return true;
948 }
949
950
951
952
953
954
955
956 private AcademicCalendarForm saveAcademicCalendarDirtyFields(AcademicCalendarForm academicCalendarForm){
957 AcademicCalendarViewHelperService viewHelperService = getAcalViewHelperService(academicCalendarForm);
958
959
960 viewHelperService.populateAcademicCalendarDefaults(academicCalendarForm);
961
962 viewHelperService.validateAcademicCalendar(academicCalendarForm);
963
964 if (GlobalVariables.getMessageMap().getErrorCount() > 0){
965
966 return academicCalendarForm;
967 }
968
969
970 List<String> dirtyFields = processDirtyFields(academicCalendarForm);
971
972
973 AcademicCalendarInfo newAcal = saveAcal(academicCalendarForm.getAcademicCalendarInfo(), academicCalendarForm, viewHelperService);
974 academicCalendarForm.setAcademicCalendarInfo(newAcal);
975
976
977 academicCalendarForm = saveDirtyFieldChanges(academicCalendarForm, dirtyFields,viewHelperService);
978
979
980 academicCalendarForm = saveAcalEvents(academicCalendarForm,viewHelperService);
981
982
983 deleteAcalEvents(academicCalendarForm,viewHelperService);
984
985
986 academicCalendarForm = saveTerms(academicCalendarForm,viewHelperService);
987
988
989 deleteTerms(academicCalendarForm, viewHelperService);
990
991
992 for(int i = 0; i < academicCalendarForm.getTermWrapperList().size();i++){
993
994 academicCalendarForm = saveKeyDates(academicCalendarForm,i,viewHelperService);
995
996
997 AcademicTermWrapper term = academicCalendarForm.getTermWrapperList().get(i);
998 deleteKeyDates(term, viewHelperService);
999
1000
1001 try{
1002 term.setInstructionalDays(getAcalService().getInstructionalDaysForTerm(term.getTermInfo().getId(), viewHelperService.createContextInfo()));
1003 }catch (Exception ex){
1004
1005 LOG.error("Unable to load instructional days",ex);
1006 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_MESSAGES, CalendarConstants.MessageKeys.ERROR_CALCULATING_INSTRUCTIONAL_DAYS, term.getStartDate().toString(), term.getEndDate().toString());
1007 }
1008
1009
1010 academicCalendarForm = processExamPeriods(academicCalendarForm,i,viewHelperService);
1011 }
1012
1013
1014 academicCalendarForm.getEventsToDeleteOnSave().clear();
1015 academicCalendarForm.getFieldsToSave().clear();
1016 academicCalendarForm.setDirtyFields("");
1017 academicCalendarForm.getTermsToDeleteOnSave().clear();
1018 academicCalendarForm.setNewCalendar(false);
1019
1020
1021 if(!(GlobalVariables.getMessageMap().getErrorCount()>0)){
1022 GlobalVariables.getMessageMap().addGrowlMessage("", CalendarConstants.MessageKeys.INFO_ACADEMIC_CALENDAR_SAVED, academicCalendarForm.getAcademicCalendarInfo().getName());
1023 }
1024
1025 academicCalendarForm.setMeta(academicCalendarForm.getAcademicCalendarInfo().getMeta());
1026
1027
1028 academicCalendarForm.setAddedCollectionItems(new ArrayList<Object>());
1029
1030
1031 viewHelperService.sortTermWrappers(academicCalendarForm.getTermWrapperList());
1032
1033
1034 return academicCalendarForm;
1035 }
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045 private AcademicCalendarForm saveDirtyFieldChanges(AcademicCalendarForm form, List<String>dirtyFields, AcademicCalendarViewHelperService helperService){
1046 List<String> updatedFields = new ArrayList<String>();
1047
1048
1049 for(String field : dirtyFields){
1050 if(field.isEmpty()) continue;
1051
1052
1053 field = field.substring(0,field.lastIndexOf("."));
1054
1055
1056 boolean alreadyUpdated = false;
1057 for(int j=0;j<updatedFields.size();j++){
1058 if(field.compareTo(updatedFields.get(j))==0){
1059 alreadyUpdated=true;
1060 break;
1061 }
1062 }
1063 if(alreadyUpdated) continue;
1064
1065
1066 updatedFields.add(field);
1067
1068
1069 if(field.contains("newCollectionLines")){
1070
1071 continue;
1072 }else if(field.contains("academicCalendarInfo")){
1073
1074 continue;
1075 } else if(field.contains("events")){
1076
1077
1078 int index = processFieldIndex(field);
1079 AcalEventWrapper event = form.getEvents().get(index);
1080 AcalEventWrapper newEvent = saveAcalEvent(event,form, helperService);
1081 form.getEvents().set(index, newEvent);
1082
1083 } else if(field.contains("keydates")){
1084
1085
1086 int termIndex = processFieldIndex(field.substring(0, field.indexOf(".")));
1087 int keyDateGroupIndex = processFieldIndex(field.substring(field.indexOf("."), field.lastIndexOf(".")));
1088 int keyDateIndex = processFieldIndex( field.substring(field.lastIndexOf(".")));
1089 KeyDateWrapper keyDateWrapper = form.getTermWrapperList().get(termIndex).getKeyDatesGroupWrappers().get(keyDateGroupIndex).getKeydates().get(keyDateIndex);
1090 KeyDateWrapper newKeyDateWrapper = saveKeyDate(keyDateWrapper, form.getTermWrapperList().get(termIndex), helperService);
1091 form.getTermWrapperList().get(termIndex).getKeyDatesGroupWrappers().get(keyDateGroupIndex).getKeydates().set(keyDateIndex,newKeyDateWrapper);
1092
1093 } else if (field.contains("examdates")) {
1094
1095 continue;
1096 } else if(field.contains("termWrapperList")){
1097
1098
1099 int index = processFieldIndex(field);
1100 AcademicTermWrapper term = form.getTermWrapperList().get(index);
1101 AcademicTermWrapper newTerm = saveTerm(term, form, helperService);
1102 form.getTermWrapperList().set(index,newTerm);
1103
1104 } else {
1105
1106 LOG.warn("Unknown field encounter during save: "+field);
1107 }
1108 }
1109 return form;
1110 }
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120 private AcademicCalendarInfo saveAcal(AcademicCalendarInfo acal, AcademicCalendarForm form, AcademicCalendarViewHelperService helperService){
1121
1122 AcademicCalendarInfo acalInfo = processHolidayCalendars(form);
1123
1124
1125 try {
1126 if (StringUtils.isBlank(acal.getId())) {
1127
1128 acalInfo.setStateKey(AcademicCalendarServiceConstants.ACADEMIC_CALENDAR_DRAFT_STATE_KEY);
1129 acalInfo.setTypeKey(AcademicCalendarServiceConstants.ACADEMIC_CALENDAR_TYPE_KEY);
1130 RichTextInfo rti = new RichTextInfo();
1131 rti.setPlain(acalInfo.getName());
1132 acalInfo.setDescr(rti);
1133
1134
1135 AcademicCalendarInfo newAcal = getAcalService().createAcademicCalendar(AcademicCalendarServiceConstants.ACADEMIC_CALENDAR_TYPE_KEY, acalInfo, helperService.createContextInfo());
1136 return newAcal;
1137 } else {
1138
1139
1140 AcademicCalendarInfo updatedAcal = getAcalService().updateAcademicCalendar(acal.getId(), acal, helperService.createContextInfo());
1141 return updatedAcal;
1142
1143 }
1144 } catch (VersionMismatchException e){
1145
1146
1147 form.setReload(true);
1148 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_MESSAGES, RiceKeyConstants.ERROR_CUSTOM,"You are saving an older version of this calendar. Please click on the reload button to get the newer version.");
1149 return acal;
1150
1151 } catch(Exception e) {
1152
1153
1154 LOG.error("Add/update Academic calendar failed", e);
1155 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_MESSAGES, CalendarConstants.MessageKeys.ERROR_ACAL_SAVE_FAILED);
1156 return acal;
1157 }
1158 }
1159
1160
1161
1162
1163
1164
1165
1166
1167 private AcademicCalendarForm saveTerms(AcademicCalendarForm form, AcademicCalendarViewHelperService helperService){
1168
1169 List<AcademicTermWrapper> subTermsToSave = new ArrayList<AcademicTermWrapper>();
1170 for(int i=0;i<form.getTermWrapperList().size();i++){
1171 AcademicTermWrapper term = form.getTermWrapperList().get(i);
1172 if(term.getTermInfo().getId()==null){
1173 if(term.isSubTerm()){
1174
1175 subTermsToSave.add(term);
1176 } else{
1177
1178 AcademicTermWrapper newTerm = saveTerm(term,form,helperService);
1179 form.getTermWrapperList().set(i,newTerm);
1180 }
1181 }
1182 }
1183
1184 for(AcademicTermWrapper subterm : subTermsToSave){
1185 int index = form.getTermWrapperList().indexOf(subterm);
1186
1187 AcademicTermWrapper newTerm = saveTerm(subterm,form,helperService);
1188 form.getTermWrapperList().set(index,newTerm);
1189 }
1190
1191 return form;
1192 }
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202 private AcademicTermWrapper saveTerm(AcademicTermWrapper termWrapper, AcademicCalendarForm form, AcademicCalendarViewHelperService helperService){
1203
1204 TermInfo term = termWrapper.getTermInfo();
1205
1206
1207 term.setEndDate(termWrapper.getEndDate());
1208 term.setStartDate(termWrapper.getStartDate());
1209 term.setName(termWrapper.getName());
1210 term.setTypeKey(termWrapper.getTermType());
1211
1212
1213 try{
1214 if (termWrapper.isNew() && !termWrapper.isSubTerm()){
1215
1216
1217 TermInfo newTerm = getAcalService().createTerm(termWrapper.getTermType(),term,helperService.createContextInfo());
1218 termWrapper.setTermInfo(newTerm);
1219
1220 getAcalService().addTermToAcademicCalendar(form.getAcademicCalendarInfo().getId(),termWrapper.getTermInfo().getId(),helperService.createContextInfo());
1221 }else if(termWrapper.isNew() && termWrapper.isSubTerm()){
1222
1223
1224
1225 String parentTermTypeKey = termWrapper.getParentTerm();
1226 TermInfo parentTermInfo = getParentTerm(form.getAcademicCalendarInfo().getId(), parentTermTypeKey,helperService);
1227
1228
1229 if(parentTermInfo == null){
1230
1231 throw new Exception("Parent Term does not exist. Therefor unable to save subterm.");
1232 }else{
1233
1234 termWrapper.setParentTermInfo(parentTermInfo);
1235
1236 TermInfo newTerm = getAcalService().createTerm(termWrapper.getTermType(),term,helperService.createContextInfo());
1237 termWrapper.setTermInfo(newTerm);
1238
1239 getAcalService().addTermToTerm(termWrapper.getParentTermInfo().getId(), termWrapper.getTermInfo().getId(), helperService.createContextInfo());
1240
1241
1242 getAcalService().addTermToAcademicCalendar(form.getAcademicCalendarInfo().getId(),termWrapper.getTermInfo().getId(),helperService.createContextInfo());
1243 }
1244 }else {
1245
1246
1247 TermInfo updatedTerm = getAcalService().updateTerm(term.getId(),term,helperService.createContextInfo());
1248 termWrapper.setTermInfo(updatedTerm);
1249 }
1250 }catch(Exception e){
1251 LOG.error("Save term has failed", e);
1252 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_MESSAGES, CalendarConstants.MessageKeys.ERROR_ACAL_SAVE_TERM_SAVE_FAILED,
1253 termWrapper.getName(), e.getLocalizedMessage());
1254 }
1255
1256 return termWrapper;
1257 }
1258
1259
1260
1261
1262
1263
1264
1265 private void deleteTerms(AcademicCalendarForm form, AcademicCalendarViewHelperService helperService){
1266 for (AcademicTermWrapper term : form.getTermsToDeleteOnSave()){
1267 try{
1268 getAcademicCalendarServiceFacade().deleteTermCascaded(term.getTermInfo().getId(),helperService.createContextInfo());
1269 }catch(Exception e){
1270 LOG.error("Delete term has failed",e);
1271 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_MESSAGES, CalendarConstants.MessageKeys.ERROR_DELETING,"Term",term.getName());
1272
1273 }
1274 }
1275 }
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285 private AcademicCalendarForm saveKeyDates(AcademicCalendarForm form, int termIndex, AcademicCalendarViewHelperService helperService){
1286 AcademicTermWrapper term = form.getTermWrapperList().get(termIndex);
1287 for( int j = 0; j<term.getKeyDatesGroupWrappers().size();j++){
1288 KeyDatesGroupWrapper keyDateGroup = term.getKeyDatesGroupWrappers().get(j);
1289 for(int k = 0; k<keyDateGroup.getKeydates().size();k++){
1290 KeyDateWrapper keyDate = keyDateGroup.getKeydates().get(k);
1291 if(keyDate.getKeyDateInfo().getId() == null){
1292 KeyDateWrapper newKeyDate= saveKeyDate(keyDate,term,helperService);
1293 form.getTermWrapperList().get(termIndex).getKeyDatesGroupWrappers().get(j).getKeydates().set(k,newKeyDate);
1294 }
1295 }
1296 }
1297 return form;
1298 }
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308 private KeyDateWrapper saveKeyDate(KeyDateWrapper keyDateWrapper, AcademicTermWrapper term, AcademicCalendarViewHelperService helperService){
1309
1310 KeyDateInfo keyDate = keyDateWrapper.getKeyDateInfo();
1311
1312
1313 keyDate.setTypeKey(keyDateWrapper.getKeyDateType());
1314 keyDate.setName(keyDateWrapper.getKeyDateNameUI());
1315 keyDate.setIsAllDay(keyDateWrapper.isAllDay());
1316 keyDate.setIsDateRange(keyDateWrapper.isDateRange());
1317 keyDate.setStartDate(getDateInfoForKeyDate(keyDateWrapper.isAllDay(),keyDateWrapper.getStartDate(),keyDateWrapper.getStartTime(),keyDateWrapper.getStartTimeAmPm()));
1318 if (keyDateWrapper.isDateRange()){
1319 keyDate.setEndDate(getDateInfoForKeyDate(keyDateWrapper.isAllDay(),keyDateWrapper.getEndDate(),keyDateWrapper.getEndTime(),keyDateWrapper.getEndTimeAmPm()));
1320 } else{
1321 keyDate.setEndDate(null);
1322 }
1323
1324
1325 try{
1326 if (keyDateWrapper.isNew()){
1327
1328 keyDate.setStateKey(AtpServiceConstants.MILESTONE_DRAFT_STATE_KEY);
1329 KeyDateInfo createdKeyDate = getAcalService().createKeyDate(term.getTermInfo().getId(),keyDate.getTypeKey(),keyDate,helperService.createContextInfo());
1330 keyDateWrapper.setKeyDateInfo(createdKeyDate);
1331 } else {
1332
1333 KeyDateInfo updatedKeyDate = getAcalService().updateKeyDate(keyDate.getId(), keyDate, helperService.createContextInfo());
1334 keyDateWrapper.setKeyDateInfo(updatedKeyDate);
1335 }
1336 }catch(Exception e){
1337 LOG.error("Save keydate has failed",e);
1338 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_MESSAGES, CalendarConstants.MessageKeys.ERROR_ACAL_SAVE_TERM_KEYDATE_FAILED,keyDateWrapper.getKeyDateNameUI(),term.getName());
1339
1340 }
1341
1342 return keyDateWrapper;
1343 }
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353 private AcademicCalendarForm processExamPeriods(AcademicCalendarForm form, int termIndex, AcademicCalendarViewHelperService helperService){
1354
1355 AcademicTermWrapper term = form.getTermWrapperList().get(termIndex);
1356 for(int i=0; i<term.getExamdates().size(); i++ ) {
1357 ExamPeriodWrapper examPeriodWrapper = term.getExamdates().get(i);
1358
1359 ExamPeriodWrapper newExamPeriodWrapper = saveExamPeriod(form, examPeriodWrapper, term, termIndex, helperService);
1360 form.getTermWrapperList().get(termIndex).getExamdates().set(i,newExamPeriodWrapper);
1361 }
1362
1363
1364 for (ExamPeriodWrapper examPeriodToDelete : term.getExamPeriodsToDeleteOnSave()) {
1365 try {
1366 getAcalService().deleteExamPeriod(examPeriodToDelete.getExamPeriodInfo().getId(), helperService.createContextInfo());
1367 } catch (Exception e) {
1368 LOG.error("Delete exam period has failed",e);
1369 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_MESSAGES, CalendarConstants.MessageKeys.ERROR_DELETING,term.getName(),examPeriodToDelete.getExamPeriodNameUI());
1370 }
1371 }
1372 term.getExamPeriodsToDeleteOnSave().clear();
1373
1374 return form;
1375 }
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385 private ExamPeriodWrapper saveExamPeriod(AcademicCalendarForm form, ExamPeriodWrapper examPeriodWrapper, AcademicTermWrapper term, int termIndex, AcademicCalendarViewHelperService helperService){
1386
1387 ExamPeriodInfo examPeriodInfo = examPeriodWrapper.getExamPeriodInfo();
1388 String examPeriodName = examPeriodWrapper.getExamPeriodNameUI() + " " + term.getName();
1389
1390 examPeriodInfo.setTypeKey(examPeriodWrapper.getExamPeriodType());
1391 examPeriodInfo.setName(examPeriodName);
1392 examPeriodInfo.setEndDate(examPeriodWrapper.getEndDate());
1393 examPeriodInfo.setStartDate(examPeriodWrapper.getStartDate());
1394 examPeriodInfo.setStateKey(term.getTermInfo().getStateKey());
1395 setExamPeriodAttr(examPeriodInfo, AcademicCalendarServiceConstants.EXAM_PERIOD_EXCLUDE_SATURDAY_ATTR, String.valueOf(examPeriodWrapper.isExcludeSaturday()));
1396 setExamPeriodAttr(examPeriodInfo, AcademicCalendarServiceConstants.EXAM_PERIOD_EXCLUDE_SUNDAY_ATTR, String.valueOf(examPeriodWrapper.isExcludeSunday()));
1397
1398 RichTextInfo rti = new RichTextInfo();
1399 rti.setPlain(examPeriodName);
1400 rti.setFormatted(examPeriodName);
1401 examPeriodInfo.setDescr(rti);
1402
1403
1404 try{
1405 if (examPeriodWrapper.isNew()){
1406
1407 List<String> termTypeKeyList = new ArrayList<String>();
1408 termTypeKeyList.add(term.getTermType());
1409 ExamPeriodInfo createdExamPeriodInfo = getAcademicCalendarServiceFacade().addExamPeriod(examPeriodInfo.getTypeKey(), termTypeKeyList, examPeriodInfo, helperService.createContextInfo());
1410 getAcalService().addExamPeriodToTerm(term.getTermInfo().getId(), createdExamPeriodInfo.getId(), helperService.createContextInfo());
1411 examPeriodWrapper.setExamPeriodInfo(createdExamPeriodInfo);
1412 } else {
1413
1414 ExamPeriodInfo updatedExamPeriodInfo = getAcalService().updateExamPeriod(examPeriodInfo.getId(), examPeriodInfo, helperService.createContextInfo());
1415 examPeriodWrapper.setExamPeriodInfo(updatedExamPeriodInfo);
1416 }
1417 } catch (OperationFailedException oe){
1418 LOG.error("Save exam period has failed",oe);
1419 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_MESSAGES, CalendarConstants.MessageKeys.ERROR_ACAL_SAVE_TERM_EXAMPERIOD_FAILED,examPeriodWrapper.getExamPeriodNameUI(),term.getName() +". FEP is not allowed for the selected term.");
1420 }
1421 catch(Exception e){
1422 LOG.error("Save exam period has failed",e);
1423 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_MESSAGES, CalendarConstants.MessageKeys.ERROR_ACAL_SAVE_TERM_EXAMPERIOD_FAILED,examPeriodWrapper.getExamPeriodNameUI(),term.getName());
1424 }
1425
1426 return examPeriodWrapper;
1427 }
1428
1429
1430
1431
1432
1433
1434
1435
1436 private void deleteKeyDates(AcademicTermWrapper term, AcademicCalendarViewHelperService helperService){
1437 for(KeyDateWrapper keyDate : term.getKeyDatesToDeleteOnSave()){
1438 try{
1439 getAcalService().deleteKeyDate(keyDate.getKeyDateInfo().getId(),helperService.createContextInfo());
1440 }catch(Exception e){
1441 LOG.error("Delete key date has failed",e);
1442 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_MESSAGES, CalendarConstants.MessageKeys.ERROR_DELETING,term.getName(),keyDate.getKeyDateNameUI());
1443 }
1444 }
1445 term.getKeyDatesToDeleteOnSave().clear();
1446 }
1447
1448
1449
1450
1451
1452
1453
1454
1455 private AcademicCalendarForm saveAcalEvents(AcademicCalendarForm form, AcademicCalendarViewHelperService helperService){
1456 for(int i=0;i<form.getEvents().size();i++){
1457 AcalEventWrapper event = form.getEvents().get(i);
1458 if(event.getAcalEventInfo().getId()==null){
1459 AcalEventWrapper newEvent = saveAcalEvent(event,form,helperService);
1460 form.getEvents().set(i,newEvent);
1461 }
1462 }
1463 return form;
1464 }
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474 private AcalEventWrapper saveAcalEvent(AcalEventWrapper event, AcademicCalendarForm form, AcademicCalendarViewHelperService helperService){
1475
1476 AcalEventInfo eventInfo = event.getAcalEventInfo();
1477
1478
1479 RichTextInfo rti = new RichTextInfo();
1480 rti.setPlain(event.getEventTypeKey());
1481 eventInfo.setDescr(rti);
1482 eventInfo.setTypeKey(event.getEventTypeKey());
1483 eventInfo.setIsAllDay(event.isAllDay());
1484 eventInfo.setIsDateRange(event.isDateRange());
1485 eventInfo.setStartDate(getDateInfoForKeyDate(event.isAllDay(),event.getStartDate(),event.getStartTime(),event.getStartTimeAmPm()));
1486 if(event.isDateRange()){
1487 eventInfo.setEndDate(getDateInfoForKeyDate(event.isAllDay(),event.getEndDate(),event.getEndTime(),event.getEndTimeAmPm()));
1488 } else{
1489 eventInfo.setEndDate(null);
1490 }
1491
1492 if (!form.isOfficialCalendar()){
1493 eventInfo.setStateKey(AtpServiceConstants.MILESTONE_DRAFT_STATE_KEY);
1494 } else {
1495 eventInfo.setStateKey(AtpServiceConstants.MILESTONE_OFFICIAL_STATE_KEY);
1496 }
1497
1498
1499 try{
1500 if(eventInfo.getId()==null){
1501
1502 AcalEventInfo createdEventInfo = getAcalService().createAcalEvent(form.getAcademicCalendarInfo().getId(), eventInfo.getTypeKey(), eventInfo, helperService.createContextInfo());
1503 event.setAcalEventInfo(createdEventInfo);
1504 }else{
1505
1506 AcalEventInfo updatedEventInfo = getAcalService().updateAcalEvent(eventInfo.getId(), eventInfo, helperService.createContextInfo());
1507 event.setAcalEventInfo(updatedEventInfo);
1508 }
1509 }catch(Exception e){
1510 LOG.error("Save calendar event has failed" , e);
1511 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_MESSAGES, CalendarConstants.MessageKeys.ERROR_ACAL_SAVE_EVENT_FAILED,event.getEventTypeName());
1512
1513 }
1514
1515 return event;
1516 }
1517
1518
1519
1520
1521
1522
1523
1524 private void deleteAcalEvents(AcademicCalendarForm form, AcademicCalendarViewHelperService helperService){
1525 for(AcalEventWrapper event : form.getEventsToDeleteOnSave()){
1526 try{
1527 getAcalService().deleteAcalEvent(event.getAcalEventInfo().getId(),helperService.createContextInfo());
1528 }catch(Exception e){
1529 LOG.error("Delete calendar event has failed" , e);
1530 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_MESSAGES, CalendarConstants.MessageKeys.ERROR_DELETING,"Calendar event",event.getEventTypeName());
1531 }
1532 }
1533 }
1534
1535
1536
1537
1538
1539
1540
1541 private int processFieldIndex(String field){
1542 String indexChar = field.substring(field.indexOf("[")+1, field.lastIndexOf("]"));
1543 return Integer.parseInt(indexChar);
1544 }
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555 private TermInfo getParentTerm(String acalId, String parentTermTypeKey, AcademicCalendarViewHelperService helperService) throws Exception{
1556
1557 List<TermInfo> termInfoList = getAcalService().getTermsForAcademicCalendar(acalId, helperService.createContextInfo());
1558 for(TermInfo termInfo : termInfoList){
1559 if (parentTermTypeKey.equals(termInfo.getTypeKey())) {
1560 return termInfo;
1561 }
1562 }
1563 return null;
1564 }
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575 private Date getDateInfoForKeyDate(boolean isAllDay, Date date, String time, String ampm){
1576 if(!isAllDay){
1577 return AcalCommonUtils.getDateWithTime(date, time, ampm);
1578 }
1579 return date;
1580 }
1581
1582
1583
1584
1585
1586
1587
1588 private AcademicCalendarInfo processHolidayCalendars(AcademicCalendarForm academicCalendarForm) {
1589 AcademicCalendarInfo acalInfo = academicCalendarForm.getAcademicCalendarInfo();
1590 List<HolidayCalendarWrapper> holidayCalendarList = academicCalendarForm.getHolidayCalendarList();
1591 List<String> holidayCalendarIds = new ArrayList<String>();
1592 if (holidayCalendarList != null && !holidayCalendarList.isEmpty()) {
1593 for (HolidayCalendarWrapper hcWrapper : holidayCalendarList){
1594 holidayCalendarIds.add(hcWrapper.getHolidayCalendarInfo().getId());
1595 }
1596 }
1597
1598
1599 acalInfo.setHolidayCalendarIds(holidayCalendarIds);
1600 academicCalendarForm.setAcademicCalendarInfo(acalInfo);
1601
1602 return acalInfo;
1603 }
1604
1605
1606
1607
1608
1609
1610
1611
1612 private List<String> processDirtyFields(AcademicCalendarForm academicCalendarForm){
1613 String[] tempFields = academicCalendarForm.getDirtyFields().split(",");
1614 List<String> dirtyFields = academicCalendarForm.getFieldsToSave();
1615 StringBuilder completeDirtyFields = new StringBuilder("");
1616 for(String field : tempFields){
1617 boolean alreadySeen = false;
1618 for(String field2 : dirtyFields){
1619 if(field2.compareTo(field)==0){
1620 alreadySeen=true;
1621 break;
1622 }
1623 }
1624 if(!alreadySeen){
1625 dirtyFields.add(field);
1626
1627 }
1628 }
1629 for(String field : dirtyFields){
1630 completeDirtyFields.append(field);
1631 completeDirtyFields.append(",");
1632 }
1633 academicCalendarForm.setDirtyFields(completeDirtyFields.toString());
1634 return dirtyFields;
1635
1636 }
1637
1638
1639
1640
1641
1642 @Override
1643 @RequestMapping(params = "methodToCall=returnFromLightbox")
1644 public ModelAndView returnFromLightbox(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1645 HttpServletRequest request, HttpServletResponse response) {
1646
1647 String newMethodToCall;
1648
1649
1650 DialogManager dm = form.getDialogManager();
1651 String dialogId = dm.getCurrentDialogId();
1652 if (dialogId == null) {
1653 newMethodToCall = "start";
1654 } else {
1655 dm.setDialogAnswer(dialogId, form.getDialogResponse());
1656 dm.setDialogExplanation(dialogId, form.getDialogExplanation());
1657 newMethodToCall = dm.getDialogReturnMethod(dialogId);
1658 dm.setCurrentDialogId(null);
1659 }
1660
1661
1662 form.setMethodToCall(newMethodToCall);
1663
1664
1665 for (Method m : this.getClass().getMethods()) {
1666 RequestMapping a = m.getAnnotation(RequestMapping.class);
1667 if (a != null) {
1668 String[] annotationsParams = a.params();
1669 for (String param : annotationsParams) {
1670 if (param.contains("methodToCall=" + newMethodToCall)) {
1671 try {
1672 return (ModelAndView) m.invoke(this, form, result, request, response);
1673 } catch (IllegalAccessException iae) {
1674 LOG.error("Reflection Invocation failed", iae);
1675 throw new RuntimeException("Error using reflection in returnFromLightbox", iae);
1676 } catch (InvocationTargetException ite) {
1677 LOG.error("Reflection Invocation failed", ite);
1678 throw new RuntimeException("Error using reflection in returnFromLightbox", ite);
1679 } catch (IllegalArgumentException iae) {
1680 LOG.error("Reflection Invocation failed", iae);
1681 throw new RuntimeException("Error using reflection in returnFromLightbox", iae);
1682 }
1683 }
1684 }
1685
1686 }
1687 }
1688
1689
1690
1691 Properties props = new Properties();
1692 props.put(UifParameters.METHOD_TO_CALL, newMethodToCall);
1693 props.put(UifParameters.VIEW_ID, form.getViewId());
1694 props.put(UifParameters.FORM_KEY, form.getFormKey());
1695 props.put(UifParameters.AJAX_REQUEST, "false");
1696
1697 return performRedirect(form, form.getFormPostUrl(), props);
1698 }
1699
1700
1701
1702
1703 @Override
1704 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addLine")
1705 public ModelAndView addLine(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
1706 HttpServletRequest request, HttpServletResponse response) {
1707 ((AcademicCalendarForm)uifForm).setFieldsToSave(processDirtyFields((AcademicCalendarForm)uifForm));
1708 return super.addLine(uifForm,result,request,response);
1709 }
1710
1711 private void setExamPeriodAttr(ExamPeriodInfo examPeriodInfo, String attrKey, String attrValue) {
1712 AttributeInfo attributeInfo = getExamPeriodAttrForKey(examPeriodInfo, attrKey);
1713 if (attributeInfo != null) {
1714 attributeInfo.setValue(attrValue);
1715 } else {
1716 attributeInfo = AcalCommonUtils.createAttribute(attrKey, attrValue);
1717 examPeriodInfo.getAttributes().add(attributeInfo);
1718 }
1719 }
1720
1721 private AttributeInfo getExamPeriodAttrForKey(ExamPeriodInfo examPeriodInfo, String key) {
1722 for (AttributeInfo info : examPeriodInfo.getAttributes()) {
1723 if (info.getKey().equals(key)) {
1724 return info;
1725 }
1726 }
1727 return null;
1728 }
1729
1730
1731
1732 }