Coverage Report - org.kuali.student.enrollment.class2.acal.controller.AcademicCalendarController
 
Classes in this File Line Coverage Branch Coverage Complexity
AcademicCalendarController
0%
0/291
0%
0/114
4.214
 
 1  
 /*
 2  
  * Copyright 2011 The Kuali Foundation
 3  
  * 
 4  
  * Licensed under the Educational Community License, Version 1.0 (the
 5  
  * "License"); you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  * http://www.opensource.org/licenses/ecl1.php
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12  
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13  
  * License for the specific language governing permissions and limitations under
 14  
  * the License.
 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.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
 21  
 import org.kuali.rice.krad.uif.UifParameters;
 22  
 import org.kuali.rice.krad.util.GlobalVariables;
 23  
 import org.kuali.rice.krad.util.KRADConstants;
 24  
 import org.kuali.rice.krad.util.UrlFactory;
 25  
 import org.kuali.rice.krad.web.controller.UifControllerBase;
 26  
 import org.kuali.rice.krad.web.form.UifFormBase;
 27  
 import org.kuali.student.enrollment.acal.constants.AcademicCalendarServiceConstants;
 28  
 import org.kuali.student.enrollment.acal.dto.AcademicCalendarInfo;
 29  
 import org.kuali.student.enrollment.acal.dto.TermInfo;
 30  
 import org.kuali.student.enrollment.acal.service.AcademicCalendarService;
 31  
 import org.kuali.student.enrollment.class2.acal.dto.*;
 32  
 import org.kuali.student.enrollment.class2.acal.form.AcademicCalendarForm;
 33  
 import org.kuali.student.enrollment.class2.acal.util.CalendarConstants;
 34  
 import org.kuali.student.r2.common.util.constants.AtpServiceConstants;
 35  
 import org.springframework.stereotype.Controller;
 36  
 import org.springframework.validation.BindingResult;
 37  
 import org.springframework.web.bind.annotation.ModelAttribute;
 38  
 import org.springframework.web.bind.annotation.RequestMapping;
 39  
 import org.springframework.web.bind.annotation.RequestMethod;
 40  
 import org.springframework.web.servlet.ModelAndView;
 41  
 
 42  
 import javax.servlet.http.HttpServletRequest;
 43  
 import javax.servlet.http.HttpServletResponse;
 44  
 import javax.xml.namespace.QName;
 45  
 import java.util.*;
 46  
 
 47  
 /**
 48  
  * This controller handles all the request from Academic calendar UI.
 49  
  *
 50  
  * @author Kuali Student Team
 51  
  */
 52  
 
 53  
 @Controller
 54  
 @RequestMapping(value = "/academicCalendar")
 55  0
 public class AcademicCalendarController extends UifControllerBase {
 56  
 
 57  
     private AcademicCalendarService acalService;
 58  
 
 59  
     @Override
 60  
     protected UifFormBase createInitialForm(HttpServletRequest request) {
 61  0
         return new AcademicCalendarForm();
 62  
     }
 63  
 
 64  
     /**
 65  
      * This GET method loads an academic calendar based on the parameters passed into the request.
 66  
      *
 67  
      * These are the supported request parameters
 68  
      * 1. id - Academic Calendar Id to load in to UI
 69  
      * 2. readOnlyView - If true, sets the view as read only
 70  
      * 3. selectTab - can be 'info' or 'term'
 71  
      *
 72  
      */
 73  
     @Override
 74  
     @RequestMapping(method = RequestMethod.GET, params = "methodToCall=start")
 75  
     public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
 76  
                               HttpServletRequest request, HttpServletResponse response) {
 77  0
         AcademicCalendarForm acalForm = (AcademicCalendarForm) form;
 78  
 
 79  0
         String acalId = request.getParameter(CalendarConstants.CALENDAR_ID);
 80  
 
 81  0
         if (StringUtils.isNotBlank(acalId)){
 82  
             try {
 83  0
                 loadAcademicCalendar(acalId, acalForm);
 84  0
             } catch (Exception ex) {
 85  0
                 throw new RuntimeException(ex);
 86  0
             }
 87  
         }
 88  
 
 89  0
         String readOnlyView = request.getParameter(CalendarConstants.READ_ONLY_VIEW);
 90  0
         acalForm.getView().setReadOnly(BooleanUtils.toBoolean(readOnlyView));
 91  
 
 92  0
         if (StringUtils.isNotBlank(request.getParameter(CalendarConstants.SELECT_TAB))) {
 93  0
             acalForm.setDefaultTabToShow(request.getParameter(CalendarConstants.SELECT_TAB));
 94  
         }
 95  
 
 96  0
         return super.start(form, result, request, response);
 97  
     }
 98  
 
 99  
     /**
 100  
      * This method creates a blank academic calendar.
 101  
      */
 102  
     @RequestMapping(params = "methodToCall=createBlankCalendar")
 103  
     public ModelAndView createBlankCalendar(@ModelAttribute("KualiForm") AcademicCalendarForm acalForm, BindingResult result,
 104  
                                             HttpServletRequest request, HttpServletResponse response){
 105  
 
 106  0
         acalForm.setAcademicCalendarInfo(new AcademicCalendarInfo());
 107  0
         acalForm.setEvents(new ArrayList<AcalEventWrapper>());
 108  0
         acalForm.setHolidayCalendarList(new ArrayList<HolidayCalendarWrapper>());
 109  0
         acalForm.setTermWrapperList(new ArrayList<AcademicTermWrapper>());
 110  
 
 111  0
         return getUIFModelAndView(acalForm, CalendarConstants.ACADEMIC_CALENDAR_EDIT_PAGE);
 112  
     }
 113  
 
 114  
     /**
 115  
      * This GET method is called before the Create New Academic Calendar page is displayed
 116  
      *
 117  
      * It fills in the original Acal for the form with the latest calendar found, by default
 118  
      */
 119  
     @RequestMapping(method = RequestMethod.GET, params = "methodToCall=startNew")
 120  
     public ModelAndView startNew( @ModelAttribute("KualiForm") AcademicCalendarForm acalForm, BindingResult result,
 121  
                                   HttpServletRequest request, HttpServletResponse response) {
 122  
 
 123  
         try {
 124  0
             AcademicCalendarInfo acalInfo = acalForm.getViewHelperService().getLatestAcademicCalendar();
 125  0
             acalForm.setOrgAcalInfo(acalInfo);
 126  
         }
 127  0
         catch (Exception x) {
 128  0
             throw new RuntimeException(x);
 129  0
         }
 130  
 
 131  
 
 132  0
         return getUIFModelAndView(acalForm);
 133  
     }
 134  
 
 135  
     @RequestMapping(params = "methodToCall=toEdit")
 136  
     public ModelAndView toEdit(@ModelAttribute("KualiForm") AcademicCalendarForm acalForm, BindingResult result,
 137  
                                               HttpServletRequest request, HttpServletResponse response){
 138  0
         AcademicCalendarInfo acalInfo = acalForm.getAcademicCalendarInfo();
 139  0
         AcademicCalendarInfo orgAcalInfo = acalForm.getOrgAcalInfo();
 140  
 
 141  0
         if (StringUtils.isBlank(acalInfo.getId()) && StringUtils.isNotBlank(orgAcalInfo.getId())){
 142  
             try{
 143  0
                 loadAcademicCalendar(orgAcalInfo.getId(), acalForm);
 144  0
              } catch (Exception ex) {
 145  0
                  throw new RuntimeException("unable to getAcademicCalendar");
 146  0
             }
 147  0
             acalForm.setOrgAcalInfo(new AcademicCalendarInfo());
 148  
         }
 149  
 
 150  0
         acalForm.getView().setReadOnly(false);
 151  
 
 152  0
         return getUIFModelAndView(acalForm, CalendarConstants.ACADEMIC_CALENDAR_EDIT_PAGE);
 153  
     }
 154  
 
 155  
     @RequestMapping(method = RequestMethod.GET, params = "methodToCall=copyForNew")
 156  
     public ModelAndView copyForNew( @ModelAttribute("KualiForm") AcademicCalendarForm acalForm, BindingResult result,
 157  
                                   HttpServletRequest request, HttpServletResponse response) {
 158  
 
 159  0
         AcademicCalendarInfo acalInfo = null;
 160  
 
 161  0
         String acalId = request.getParameter(CalendarConstants.CALENDAR_ID);
 162  0
         if (acalId != null && !acalId.trim().isEmpty()) {
 163  0
             String pageId = request.getParameter(CalendarConstants.PAGE_ID);
 164  0
             if (CalendarConstants.ACADEMIC_CALENDAR_COPY_PAGE.equals(pageId)) {
 165  
 
 166  
                 try {
 167  0
                     acalInfo= getAcalService().getAcademicCalendar(acalId, acalForm.getContextInfo());
 168  0
                     acalForm.setOrgAcalInfo(acalInfo);
 169  
 
 170  0
                 } catch (Exception ex) {
 171  0
                     throw new RuntimeException(ex);
 172  0
                 }
 173  
             }
 174  0
         }
 175  
         else {
 176  
             // try to get the AC from the form (in case of copy from the Acal view page) and set it as the Original Acal
 177  
             try {
 178  0
                 acalInfo = acalForm.getAcademicCalendarInfo();
 179  0
                 acalForm.reset();
 180  0
                 acalForm.setNewCalendar(true);
 181  0
                 acalForm.setOrgAcalInfo(acalInfo);
 182  
             }
 183  0
             catch (Exception x) {
 184  0
                 throw new RuntimeException(x);
 185  0
             }
 186  
 
 187  
         }
 188  0
         return copy(acalForm, result, request, response);
 189  
     }
 190  
 
 191  
     /**
 192  
      * Passes on request from Acal view to copy the Acal to the copyForNew method.  Needed a forwarder here because the
 193  
      * call is a GET rather than a POST
 194  
      *
 195  
      * @param acalForm
 196  
      * @param result
 197  
      * @param request
 198  
      * @param response
 199  
      * @return
 200  
      */
 201  
     @RequestMapping(params = "methodToCall=toCopy")
 202  
     public ModelAndView toCopy(@ModelAttribute("KualiForm") AcademicCalendarForm acalForm, BindingResult result,
 203  
                                               HttpServletRequest request, HttpServletResponse response){
 204  
 
 205  0
         return copyForNew(acalForm, result, request, response);
 206  
     }
 207  
 
 208  
     //copy over from the existing AcalInfo to create a new
 209  
     @RequestMapping(method = RequestMethod.POST, params="methodToCall=copy")
 210  
     public ModelAndView copy( @ModelAttribute("KualiForm") AcademicCalendarForm acalForm, BindingResult result,
 211  
                               HttpServletRequest request, HttpServletResponse response) {
 212  
         try {
 213  0
            acalForm.getViewHelperService().copyToCreateAcademicCalendar(acalForm);
 214  0
         }catch (Exception ex) {
 215  0
             throw new RuntimeException(ex);
 216  0
         }
 217  
 
 218  0
         return getUIFModelAndView(acalForm, CalendarConstants.ACADEMIC_CALENDAR_EDIT_PAGE);
 219  
 
 220  
     }
 221  
 
 222  
     //Editing Hcal is implemented fully and it's working.. but we're having some issues with KRAD form management
 223  
     //This will be implemented in future milestones
 224  
     @RequestMapping(method = RequestMethod.POST, params="methodToCall=editHolidayCalendar")
 225  
     public ModelAndView editHolidayCalendar( @ModelAttribute("KualiForm") AcademicCalendarForm acalForm, BindingResult result,
 226  
                                             HttpServletRequest request, HttpServletResponse response) {
 227  
 
 228  0
         String selectedCollectionPath = acalForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
 229  0
         if (StringUtils.isBlank(selectedCollectionPath)) {
 230  0
             throw new RuntimeException("unable to determine the selected collection path");
 231  
         }
 232  
 
 233  0
         int selectedLineIndex = -1;
 234  0
         String selectedLine = acalForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
 235  0
         if (StringUtils.isNotBlank(selectedLine)) {
 236  0
             selectedLineIndex = Integer.parseInt(selectedLine);
 237  
         }
 238  
 
 239  0
         if (selectedLineIndex == -1) {
 240  0
             throw new RuntimeException("unable to determine the selected line index");
 241  
         }
 242  
 
 243  0
         Properties hcalURLParam = new Properties();
 244  0
         hcalURLParam.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, CalendarConstants.HC_EDIT_METHOD);
 245  0
         hcalURLParam.put(CalendarConstants.CALENDAR_ID, acalForm.getHolidayCalendarList().get(selectedLineIndex).getId());
 246  0
         hcalURLParam.put(UifParameters.VIEW_ID, CalendarConstants.HOLIDAYCALENDAR_FLOWVIEW);
 247  0
         hcalURLParam.put(UifParameters.PAGE_ID, CalendarConstants.HOLIDAYCALENDAR_EDITPAGE);
 248  0
         hcalURLParam.put(UifParameters.RETURN_FORM_KEY, acalForm.getFormKey());
 249  
 
 250  0
         Properties acalReturnURLParams = new Properties();
 251  0
         acalReturnURLParams.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, CalendarConstants.AC_EDIT_METHOD);
 252  0
         acalReturnURLParams.put(UifParameters.VIEW_ID, CalendarConstants.ACAL_VIEW);
 253  0
         acalReturnURLParams.put(UifParameters.PAGE_ID, CalendarConstants.ACADEMIC_CALENDAR_EDIT_PAGE);
 254  0
         String returnUrl = UrlFactory.parameterizeUrl(CalendarConstants.ACAL_CONTROLLER_PATH, acalReturnURLParams);
 255  0
         hcalURLParam.put(UifParameters.RETURN_LOCATION, returnUrl);
 256  
 
 257  0
         return super.performRedirect(acalForm,CalendarConstants.HCAL_CONTROLLER_PATH, hcalURLParam);
 258  
 
 259  
     }
 260  
 
 261  
 
 262  
     /**
 263  
      * redirect to search Calendar page
 264  
      */
 265  
     @RequestMapping(params = "methodToCall=search")
 266  
     public ModelAndView search(@ModelAttribute("KualiForm") AcademicCalendarForm acalForm, BindingResult result,
 267  
                              HttpServletRequest request, HttpServletResponse response) throws Exception {
 268  
 
 269  0
         String controllerPath = CalendarConstants.CALENDAR_SEARCH_CONTROLLER_PATH;
 270  0
         Properties urlParameters = new Properties();
 271  0
         urlParameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.START_METHOD);
 272  0
         urlParameters.put(UifParameters.VIEW_ID, CalendarConstants.CALENDAR_SEARCH_VIEW);
 273  0
         return super.performRedirect(acalForm,controllerPath, urlParameters);
 274  
     }
 275  
 
 276  
 
 277  
     /**
 278  
      * Method used to save AcademicCalendar
 279  
      */
 280  
     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=save")
 281  
     public ModelAndView save(@ModelAttribute("KualiForm") AcademicCalendarForm academicCalendarForm, BindingResult result,
 282  
                              HttpServletRequest request, HttpServletResponse response) throws Exception{
 283  0
         return saveAcademicCalendar(academicCalendarForm, CalendarConstants.MSG_INFO_ACADEMIC_CALENDAR_SAVED,false);
 284  
     }
 285  
 
 286  
     /**
 287  
      * Method used to delete AcademicCalendar
 288  
      */
 289  
     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=delete")
 290  
     public ModelAndView delete(@ModelAttribute("KualiForm") AcademicCalendarForm acalForm, BindingResult result,
 291  
                                    HttpServletRequest request, HttpServletResponse response) throws Exception {
 292  
 
 293  0
         acalForm.getViewHelperService().deleteAcademicCalendar(acalForm.getAcademicCalendarInfo().getId());
 294  
 
 295  0
         GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_MESSAGES, CalendarConstants.MSG_INFO_SEARCH_DELETE_SUCCESS, acalForm.getAcademicCalendarInfo().getName());
 296  
 
 297  0
         Properties urlParameters = new  Properties();
 298  0
         urlParameters.put("viewId", CalendarConstants.ENROLLMENT_HOME_VIEW);
 299  0
         urlParameters.put("methodToCall", KRADConstants.START_METHOD);
 300  0
         return performRedirect(acalForm, request.getRequestURL().toString(), urlParameters);
 301  
     }
 302  
 
 303  
     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=cancelTerm")
 304  
     public ModelAndView cancelTerm(@ModelAttribute("KualiForm") AcademicCalendarForm academicCalendarForm, BindingResult result,
 305  
                                         HttpServletRequest request, HttpServletResponse response) {
 306  
 
 307  0
         String selectedCollectionPath = academicCalendarForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
 308  0
         if (StringUtils.isBlank(selectedCollectionPath)) {
 309  0
             throw new RuntimeException("unable to determine the selected collection path");
 310  
         }
 311  
 
 312  0
         int selectedLineIndex = -1;
 313  0
         String selectedLine = academicCalendarForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
 314  0
         if (StringUtils.isNotBlank(selectedLine)) {
 315  0
             selectedLineIndex = Integer.parseInt(selectedLine);
 316  
         }
 317  
 
 318  0
         if (selectedLineIndex == -1) {
 319  0
             throw new RuntimeException("unable to determine the selected line index");
 320  
         }
 321  
 
 322  0
         AcademicTermWrapper termWrapper = academicCalendarForm.getTermWrapperList().get(selectedLineIndex);
 323  
 
 324  0
         if (termWrapper.isNew()){
 325  0
            academicCalendarForm.getTermWrapperList().remove(selectedLineIndex);
 326  
         }else{
 327  
             try {
 328  0
                 TermInfo termInfo = getAcalService().getTerm(termWrapper.getTermInfo().getId(), academicCalendarForm.getContextInfo());
 329  0
                 AcademicTermWrapper termWrapperFromDB = academicCalendarForm.getViewHelperService().populateTermWrapper(termInfo, false);
 330  0
                 academicCalendarForm.getTermWrapperList().set(selectedLineIndex,termWrapperFromDB);
 331  
 
 332  
                try{
 333  0
                     academicCalendarForm.getViewHelperService().populateInstructionalDays(termWrapperFromDB);
 334  0
                 }catch(Exception e){
 335  
                     //TODO: FIXME: Have to handle the error.. but for now, as it's causing issue, just skipping calculation when there are errors
 336  0
                     e.printStackTrace();
 337  0
                 }
 338  0
             } catch (Exception e) {
 339  0
                 throw new RuntimeException(e);
 340  0
             }
 341  
         }
 342  
 
 343  0
         return getUIFModelAndView(academicCalendarForm);
 344  
     }
 345  
 
 346  
     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=makeTermOfficial")
 347  
     public ModelAndView makeTermOfficial(@ModelAttribute("KualiForm") AcademicCalendarForm academicCalendarForm, BindingResult result,
 348  
                                         HttpServletRequest request, HttpServletResponse response) {
 349  
 
 350  0
         String selectedCollectionPath = academicCalendarForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
 351  0
         if (StringUtils.isBlank(selectedCollectionPath)) {
 352  0
             throw new RuntimeException("unable to determine the selected collection path");
 353  
         }
 354  
 
 355  0
         int selectedLineIndex = -1;
 356  0
         String selectedLine = academicCalendarForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
 357  0
         if (StringUtils.isNotBlank(selectedLine)) {
 358  0
             selectedLineIndex = Integer.parseInt(selectedLine);
 359  
         }
 360  
 
 361  0
         if (selectedLineIndex == -1) {
 362  0
             throw new RuntimeException("unable to determine the selected line index");
 363  
         }
 364  
 
 365  0
         AcademicTermWrapper termWrapper = academicCalendarForm.getTermWrapperList().get(selectedLineIndex);
 366  
 
 367  0
         academicCalendarForm.getViewHelperService().validateTerm(academicCalendarForm.getTermWrapperList(),selectedLineIndex,academicCalendarForm.getAcademicCalendarInfo());
 368  
 
 369  0
         if (GlobalVariables.getMessageMap().getErrorCount() > 0){
 370  0
            return getUIFModelAndView(academicCalendarForm);
 371  
         }
 372  
 
 373  
         try{
 374  0
             academicCalendarForm.getViewHelperService().saveTerm(termWrapper, academicCalendarForm.getAcademicCalendarInfo().getId(),true);
 375  0
             GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_ERRORS,"info.enroll.term.official",termWrapper.getTermNameForUI());
 376  0
         }catch (Exception e){
 377  0
            throw new RuntimeException(e);
 378  0
         }
 379  
 
 380  
        try{
 381  0
             academicCalendarForm.getViewHelperService().populateInstructionalDays(termWrapper);
 382  0
         }catch(Exception e){
 383  
             //TODO: FIXME: Have to handle the error.. but for now, as it's causing issue, just skipping calculation when there are errors
 384  0
             e.printStackTrace();
 385  0
         }
 386  
 
 387  0
         return getUIFModelAndView(academicCalendarForm);
 388  
     }
 389  
 
 390  
     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=cancelAddingTerm")
 391  
     public ModelAndView cancelAddingTerm(@ModelAttribute("KualiForm") AcademicCalendarForm academicCalendarForm, BindingResult result,
 392  
                                         HttpServletRequest request, HttpServletResponse response) {
 393  
 
 394  0
         ((AcademicTermWrapper)academicCalendarForm.getNewCollectionLines().get("termWrapperList")).clear();
 395  
 
 396  0
         return getUIFModelAndView(academicCalendarForm);
 397  
     }
 398  
 
 399  
     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=cancelAddingHoliday")
 400  
     public ModelAndView cancelAddingHoliday(@ModelAttribute("KualiForm") AcademicCalendarForm academicCalendarForm, BindingResult result,
 401  
                                         HttpServletRequest request, HttpServletResponse response) {
 402  
 
 403  0
         ((HolidayCalendarWrapper)academicCalendarForm.getNewCollectionLines().get("holidayCalendarList")).setId(StringUtils.EMPTY);
 404  
 
 405  0
         return getUIFModelAndView(academicCalendarForm);
 406  
     }
 407  
 
 408  
     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=deleteTerm")
 409  
     public ModelAndView deleteTerm(@ModelAttribute("KualiForm") AcademicCalendarForm academicCalendarForm, BindingResult result,
 410  
                                         HttpServletRequest request, HttpServletResponse response) {
 411  
 
 412  0
         String selectedCollectionPath = academicCalendarForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
 413  0
         if (StringUtils.isBlank(selectedCollectionPath)) {
 414  0
             throw new RuntimeException("unable to determine the selected collection path");
 415  
         }
 416  
 
 417  0
         int selectedLineIndex = -1;
 418  0
         String selectedLine = academicCalendarForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
 419  0
         if (StringUtils.isNotBlank(selectedLine)) {
 420  0
             selectedLineIndex = Integer.parseInt(selectedLine);
 421  
         }
 422  
 
 423  0
         if (selectedLineIndex == -1) {
 424  0
             throw new RuntimeException("unable to determine the selected line index");
 425  
         }
 426  
 
 427  0
         AcademicTermWrapper termWrapper = academicCalendarForm.getTermWrapperList().get(selectedLineIndex);
 428  
 
 429  0
         if (!termWrapper.isNew()){
 430  0
             academicCalendarForm.getTermsToDeleteOnSave().add(termWrapper);
 431  
         }
 432  
 
 433  0
         academicCalendarForm.getTermWrapperList().remove(selectedLineIndex);
 434  
 
 435  0
         return getUIFModelAndView(academicCalendarForm);
 436  
 
 437  
     }
 438  
 
 439  
     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=deleteKeyDate")
 440  
     public ModelAndView deleteKeyDate(@ModelAttribute("KualiForm") AcademicCalendarForm academicCalendarForm, BindingResult result,
 441  
                                         HttpServletRequest request, HttpServletResponse response) {
 442  
 
 443  0
         String selectedCollectionPath = academicCalendarForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
 444  0
         if (StringUtils.isBlank(selectedCollectionPath)) {
 445  0
             throw new RuntimeException("unable to determine the selected collection path");
 446  
         }
 447  
 
 448  0
         int selectedLineIndex = -1;
 449  0
         String selectedLine = academicCalendarForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
 450  0
         if (StringUtils.isNotBlank(selectedLine)) {
 451  0
             selectedLineIndex = Integer.parseInt(selectedLine);
 452  
         }
 453  
 
 454  0
         if (selectedLineIndex == -1) {
 455  0
             throw new RuntimeException("unable to determine the selected line index");
 456  
         }
 457  
 
 458  0
         String selectedTermIndex = StringUtils.substringBetween(selectedCollectionPath,"termWrapperList[","]");
 459  0
         String selectedKeyDateGroup = StringUtils.substringBetween(selectedCollectionPath,"keyDatesGroupWrappers[","]");
 460  
 
 461  0
         AcademicTermWrapper termWrapper = academicCalendarForm.getTermWrapperList().get(Integer.parseInt(selectedTermIndex));
 462  0
         KeyDatesGroupWrapper keydateGroup = termWrapper.getKeyDatesGroupWrappers().get(Integer.parseInt(selectedKeyDateGroup));
 463  0
         KeyDateWrapper keyDateWrapper = keydateGroup.getKeydates().get(selectedLineIndex);
 464  
 
 465  0
         if (StringUtils.isNotBlank(keyDateWrapper.getKeyDateInfo().getId())){
 466  0
             termWrapper.getKeyDatesToDeleteOnSave().add(keyDateWrapper);
 467  
         }
 468  
 
 469  0
         keydateGroup.getKeydates().remove(selectedLineIndex);
 470  
 
 471  0
         return getUIFModelAndView(academicCalendarForm);
 472  
 
 473  
     }
 474  
 
 475  
     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=deleteKeyDateGroup")
 476  
     public ModelAndView deleteKeyDateGroup(@ModelAttribute("KualiForm") AcademicCalendarForm academicCalendarForm, BindingResult result,
 477  
                                            HttpServletRequest request, HttpServletResponse response) {
 478  
 
 479  0
         String selectedCollectionPath = academicCalendarForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
 480  0
         if (StringUtils.isBlank(selectedCollectionPath)) {
 481  0
             throw new RuntimeException("unable to determine the selected collection path");
 482  
         }
 483  
 
 484  0
         int selectedLineIndex = -1;
 485  0
         String selectedLine = academicCalendarForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
 486  0
         if (StringUtils.isNotBlank(selectedLine)) {
 487  0
             selectedLineIndex = Integer.parseInt(selectedLine);
 488  
         }
 489  
 
 490  0
         if (selectedLineIndex == -1) {
 491  0
             throw new RuntimeException("unable to determine the selected line index");
 492  
         }
 493  
 
 494  0
         String selectedTermIndex = StringUtils.substringBetween(selectedCollectionPath,"termWrapperList[","]");
 495  
 
 496  0
         AcademicTermWrapper termWrapper = academicCalendarForm.getTermWrapperList().get(Integer.parseInt(selectedTermIndex));
 497  0
         KeyDatesGroupWrapper keydateGroup = termWrapper.getKeyDatesGroupWrappers().get(selectedLineIndex);
 498  0
         for (KeyDateWrapper keyDateWrapper : keydateGroup.getKeydates()) {
 499  0
             if (StringUtils.isNotBlank(keyDateWrapper.getKeyDateInfo().getId())){
 500  0
                 termWrapper.getKeyDatesToDeleteOnSave().add(keyDateWrapper);
 501  
             }
 502  
         }
 503  
 
 504  0
         termWrapper.getKeyDatesGroupWrappers().remove(keydateGroup);
 505  
 
 506  0
         return getUIFModelAndView(academicCalendarForm);
 507  
 
 508  
     }
 509  
 
 510  
     /**
 511  
      * Method used to set Acal as official
 512  
      */
 513  
     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=makeAcalOfficial")
 514  
     public ModelAndView makeAcalOfficial(@ModelAttribute("KualiForm") AcademicCalendarForm acalForm, BindingResult result,
 515  
                                     HttpServletRequest request, HttpServletResponse response) throws Exception {
 516  0
         return saveAcademicCalendar(acalForm, CalendarConstants.MSG_INFO_ACADEMIC_CALENDAR_OFFICIAL, true);
 517  
     }
 518  
 
 519  
 
 520  
     private void loadAcademicCalendar(String acalId, AcademicCalendarForm acalForm) throws Exception {
 521  
 
 522  0
         AcademicCalendarInfo acalInfo = getAcalService().getAcademicCalendar(acalId,acalForm.getContextInfo());
 523  
 
 524  0
         acalForm.setAcademicCalendarInfo(acalInfo);
 525  0
         acalForm.setAdminOrgName(getAdminOrgNameById(acalInfo.getAdminOrgId()));
 526  0
         acalForm.setNewCalendar(false);
 527  0
         acalForm.setOfficialCalendar(StringUtils.equals(acalInfo.getStateKey(),AtpServiceConstants.ATP_OFFICIAL_STATE_KEY));
 528  
 
 529  0
         List<AcalEventWrapper> events = acalForm.getViewHelperService().populateEventWrappers(acalForm);
 530  0
         acalForm.setEvents(events);
 531  
 
 532  0
         List<AcademicTermWrapper> termWrappers = acalForm.getViewHelperService().populateTermWrappers(acalId, false);
 533  0
         acalForm.setTermWrapperList(termWrappers);
 534  
         
 535  0
         List<HolidayCalendarWrapper> holidayCalendarWrapperList = acalForm.getViewHelperService().loadHolidayCalendars(acalInfo);
 536  0
         acalForm.setHolidayCalendarList(holidayCalendarWrapperList);
 537  
 
 538  
         //Calculate instructional days (if HC exists)
 539  0
         if (acalForm.getHolidayCalendarList() != null && !acalForm.getHolidayCalendarList().isEmpty()) {
 540  0
             acalForm.getViewHelperService().populateInstructionalDays(acalForm.getTermWrapperList());
 541  
         }
 542  
 
 543  0
     }
 544  
 
 545  
     private AcademicCalendarInfo processHolidayCalendars(AcademicCalendarForm academicCalendarForm)    {
 546  0
         AcademicCalendarInfo acalInfo = academicCalendarForm.getAcademicCalendarInfo();
 547  0
         List<HolidayCalendarWrapper> holidayCalendarList = academicCalendarForm.getHolidayCalendarList();
 548  0
         List<String> holidayCalendarIds = new ArrayList<String>();
 549  0
         if (holidayCalendarList != null && !holidayCalendarList.isEmpty()) {
 550  0
             for (HolidayCalendarWrapper hcWrapper : holidayCalendarList){
 551  0
                 holidayCalendarIds.add(hcWrapper.getHolidayCalendarInfo().getId());
 552  
             }
 553  
         }
 554  
 
 555  
         // if the list from the form is empty, then all holiday calendars have been removed (or none have been assigned)
 556  
         // so an empty list will be assigned to the AcademicCalendarInfo
 557  0
         acalInfo.setHolidayCalendarIds(holidayCalendarIds);
 558  0
         academicCalendarForm.setAcademicCalendarInfo(acalInfo);
 559  
 
 560  0
         return acalInfo;
 561  
     }
 562  
 
 563  
     private ModelAndView saveAcademicCalendar(AcademicCalendarForm academicCalendarForm, String keyToDisplayOnSave, boolean isOfficial) throws Exception {
 564  
 
 565  0
         AcademicCalendarInfo academicCalendarInfo = academicCalendarForm.getAcademicCalendarInfo();
 566  
 
 567  
         //Populate default times
 568  0
         academicCalendarForm.getViewHelperService().populateAcademicCalendarDefaults(academicCalendarForm);
 569  
 
 570  
         //Validate Acal
 571  0
         academicCalendarForm.getViewHelperService().validateAcademicCalendar(academicCalendarForm);
 572  
 
 573  0
         if (GlobalVariables.getMessageMap().getErrorCount() > 0){
 574  0
             return getUIFModelAndView(academicCalendarForm);
 575  
         }
 576  
 
 577  
         //If validation succeeds, continue save
 578  0
         if(StringUtils.isNotBlank(academicCalendarInfo.getId())){
 579  
             // 1. update acal and AC-HC relationships
 580  0
             academicCalendarInfo = processHolidayCalendars(academicCalendarForm);
 581  0
             if (isOfficial) {
 582  0
                 academicCalendarInfo.setStateKey(AcademicCalendarServiceConstants.ACADEMIC_CALENDAR_OFFICIAL_STATE_KEY);
 583  
             }
 584  0
             AcademicCalendarInfo acalInfo = getAcalService().updateAcademicCalendar(academicCalendarInfo.getId(), academicCalendarInfo, academicCalendarForm.getContextInfo() );
 585  0
             academicCalendarForm.setAcademicCalendarInfo(getAcalService().getAcademicCalendar(acalInfo.getId(), academicCalendarForm.getContextInfo()));
 586  
 
 587  
             // 2. update acalEvents if any
 588  0
             List<AcalEventWrapper> events = academicCalendarForm.getEvents();
 589  0
             processEvents(academicCalendarForm, events, acalInfo.getId());
 590  0
         }
 591  
         else {
 592  0
             AcademicCalendarInfo acalInfo = null;
 593  
             // 1. create  a new acalInfo with a list of HC Ids
 594  0
             processHolidayCalendars(academicCalendarForm);
 595  0
             acalInfo = academicCalendarForm.getViewHelperService().createAcademicCalendar(academicCalendarForm);
 596  0
             academicCalendarForm.setAcademicCalendarInfo(getAcalService().getAcademicCalendar(acalInfo.getId(), academicCalendarForm.getContextInfo()));
 597  
             // 2. create new events if any
 598  0
             createEvents(acalInfo.getId(), academicCalendarForm);
 599  
         }
 600  
 
 601  
         //Delete terms which are deleted by the user in the ui
 602  0
         for (AcademicTermWrapper termWrapper : academicCalendarForm.getTermsToDeleteOnSave()){
 603  0
             getAcalService().deleteTerm(termWrapper.getTermInfo().getId(),academicCalendarForm.getContextInfo());
 604  
         }
 605  
 
 606  0
         academicCalendarForm.getTermsToDeleteOnSave().clear();
 607  
 
 608  
         //Save Term and keydates
 609  0
         for(AcademicTermWrapper termWrapper : academicCalendarForm.getTermWrapperList()){
 610  0
             academicCalendarForm.getViewHelperService().saveTerm(termWrapper, academicCalendarForm.getAcademicCalendarInfo().getId(),false);
 611  
         }
 612  
 
 613  
         //Calculate instructional days (if HC exists)
 614  
         try{
 615  0
             academicCalendarForm.getViewHelperService().populateInstructionalDays(academicCalendarForm.getTermWrapperList());
 616  0
         }catch(Exception e){
 617  
             //FIXME: Have to handle the error.. but for now, as it's causing issue, just skipping calculation when there are errors
 618  0
             e.printStackTrace();
 619  0
         }
 620  
 
 621  0
         academicCalendarForm.setNewCalendar(false);
 622  
 
 623  0
         if (isOfficial) {
 624  0
             academicCalendarForm.setOfficialCalendar(true);
 625  0
             academicCalendarForm.getView().setReadOnly(true);
 626  
         }
 627  
 
 628  0
         GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_MESSAGES, keyToDisplayOnSave, academicCalendarForm.getAcademicCalendarInfo().getName());
 629  
 
 630  0
         return getUIFModelAndView(academicCalendarForm);
 631  
     }
 632  
 
 633  
     private String getAdminOrgNameById(String id){
 634  
         //TODO: hard-coded for now, going to call OrgService
 635  0
         String adminOrgName = null;
 636  0
         Map<String, String> allAcalOrgs = new HashMap<String, String>();
 637  0
         allAcalOrgs.put("102", "Registrar's Office");
 638  0
         allAcalOrgs.put("34", "Medical School");
 639  
 
 640  0
         if(allAcalOrgs.containsKey(id)){
 641  0
             adminOrgName = allAcalOrgs.get(id);
 642  
         }
 643  
 
 644  0
         return adminOrgName;
 645  
     }
 646  
 
 647  
     private void createEvents(String acalId, AcademicCalendarForm acalForm) throws Exception {
 648  0
         List<AcalEventWrapper> events = acalForm.getEvents();
 649  
 
 650  0
         if(events != null && !events.isEmpty()){
 651  0
             List<AcalEventWrapper> createdEvents = new ArrayList<AcalEventWrapper>();
 652  0
             for (AcalEventWrapper event : events){
 653  0
                 createdEvents.add(acalForm.getViewHelperService().createEvent(acalId, event));
 654  
             }
 655  0
             acalForm.setEvents(createdEvents);
 656  
         }
 657  
 
 658  0
     }
 659  
 
 660  
     /**
 661  
      * Update existing events, create new events, and delete events that do not exist any more when a user modifies and saves an Academic Calendar
 662  
      */
 663  
     private void processEvents(AcademicCalendarForm acalForm, List<AcalEventWrapper> events, String acalId)throws Exception{
 664  0
         List<AcalEventWrapper> updatedEvents = new ArrayList<AcalEventWrapper>();
 665  0
         List<String> currentEventIds = getExistingEventIds(acalForm);
 666  0
         if(events != null && !events.isEmpty()){
 667  0
             for(AcalEventWrapper event : events){
 668  0
                 if(currentEventIds.contains(event.getAcalEventInfo().getId())){
 669  
                     //update event
 670  0
                     AcalEventWrapper updatedEvent = acalForm.getViewHelperService().updateEvent(event.getAcalEventInfo().getId(), event);
 671  0
                     updatedEvents.add(updatedEvent);
 672  0
                     currentEventIds.remove(event.getAcalEventInfo().getId());
 673  0
                 }
 674  
                 else {
 675  
                     //create a new event
 676  0
                     AcalEventWrapper createdEvent = acalForm.getViewHelperService().createEvent(acalId, event);
 677  0
                     updatedEvents.add(createdEvent);
 678  0
                 }
 679  
             }
 680  
         }
 681  0
         acalForm.setEvents(updatedEvents);
 682  
 
 683  
         //delete events that have been removed by the user
 684  0
         if (currentEventIds != null && currentEventIds.size() > 0){
 685  0
             for(String eventId: currentEventIds){
 686  
                 //TODO: delete completely from db, when "deleted" state is available, update the event with state ="deleted"
 687  0
                 acalForm.getViewHelperService().deleteEvent(eventId);
 688  
             }
 689  
         }
 690  
 
 691  0
     }
 692  
 
 693  
     private List<String> getExistingEventIds(AcademicCalendarForm acalForm) throws Exception{
 694  0
         List<AcalEventWrapper> events = acalForm.getViewHelperService().populateEventWrappers(acalForm);
 695  0
         List<String> eventIds = new ArrayList<String>();
 696  
 
 697  0
         if(events != null && !events.isEmpty()){
 698  0
             for(AcalEventWrapper event : events){
 699  0
                 eventIds.add(event.getAcalEventInfo().getId());
 700  
             }
 701  
         }
 702  0
         return eventIds;
 703  
     }
 704  
 
 705  
 
 706  
     public AcademicCalendarService getAcalService() {
 707  0
         if(acalService == null) {
 708  0
             acalService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
 709  
         }
 710  0
         return this.acalService;
 711  
     }
 712  
 
 713  
 
 714  
 }