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