Coverage Report - org.kuali.student.enrollment.class2.appointment.controller.RegistrationWindowsController
 
Classes in this File Line Coverage Branch Coverage Complexity
RegistrationWindowsController
0%
0/166
0%
0/82
3.895
 
 1  
 package org.kuali.student.enrollment.class2.appointment.controller;
 2  
 
 3  
 import org.apache.commons.lang.StringUtils;
 4  
 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
 5  
 import org.kuali.rice.krad.uif.UifParameters;
 6  
 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
 7  
 import org.kuali.rice.krad.util.GlobalVariables;
 8  
 import org.kuali.rice.krad.util.KRADConstants;
 9  
 import org.kuali.rice.krad.web.controller.UifControllerBase;
 10  
 import org.kuali.rice.krad.web.form.UifFormBase;
 11  
 import org.kuali.student.enrollment.acal.constants.AcademicCalendarServiceConstants;
 12  
 import org.kuali.student.enrollment.acal.dto.KeyDateInfo;
 13  
 import org.kuali.student.enrollment.acal.dto.TermInfo;
 14  
 import org.kuali.student.enrollment.acal.service.AcademicCalendarService;
 15  
 import org.kuali.student.enrollment.class2.appointment.dto.AppointmentWindowWrapper;
 16  
 import org.kuali.student.enrollment.class2.appointment.form.RegistrationWindowsManagementForm;
 17  
 import org.kuali.student.enrollment.class2.appointment.service.AppointmentViewHelperService;
 18  
 import org.kuali.student.enrollment.class2.appointment.util.AppointmentConstants;
 19  
 import org.kuali.student.enrollment.class2.appointment.util.AppointmentSlotRuleTypeConversion;
 20  
 import org.kuali.student.mock.utilities.TestHelper;
 21  
 import org.kuali.student.r2.common.dto.ContextInfo;
 22  
 import org.kuali.student.r2.common.dto.StatusInfo;
 23  
 import org.kuali.student.r2.common.exceptions.*;
 24  
 import org.kuali.student.r2.common.util.constants.PopulationServiceConstants;
 25  
 import org.kuali.student.r2.core.appointment.constants.AppointmentServiceConstants;
 26  
 import org.kuali.student.r2.core.appointment.dto.AppointmentSlotInfo;
 27  
 import org.kuali.student.r2.core.appointment.dto.AppointmentWindowInfo;
 28  
 import org.kuali.student.r2.core.appointment.service.AppointmentService;
 29  
 import org.kuali.student.r2.core.population.dto.PopulationInfo;
 30  
 import org.kuali.student.r2.core.population.service.PopulationService;
 31  
 import org.springframework.stereotype.Controller;
 32  
 import org.springframework.validation.BindingResult;
 33  
 import org.springframework.web.bind.annotation.ModelAttribute;
 34  
 import org.springframework.web.bind.annotation.RequestMapping;
 35  
 import org.springframework.web.bind.annotation.RequestMethod;
 36  
 import org.springframework.web.servlet.ModelAndView;
 37  
 
 38  
 import javax.servlet.http.HttpServletRequest;
 39  
 import javax.servlet.http.HttpServletResponse;
 40  
 import javax.xml.namespace.QName;
 41  
 import java.text.DateFormat;
 42  
 import java.text.SimpleDateFormat;
 43  
 import java.util.*;
 44  
 
 45  
 /**
 46  
  * This class //TODO ...
 47  
  *
 48  
  * @author Kuali Student Team
 49  
  */
 50  
 
 51  
 @Controller
 52  
 @RequestMapping(value = "/registrationWindows")
 53  0
 public class RegistrationWindowsController extends UifControllerBase {
 54  
 
 55  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RegistrationWindowsController.class);
 56  
 
 57  
     private AppointmentViewHelperService viewHelperService;
 58  
     
 59  
     private AcademicCalendarService acalService;
 60  
 
 61  
     private AppointmentService appointmentService;
 62  
 
 63  
     private PopulationService populationService;
 64  
 
 65  
     private ContextInfo contextInfo;
 66  
 
 67  
     @Override
 68  
     protected UifFormBase createInitialForm(HttpServletRequest request) {
 69  0
         return new RegistrationWindowsManagementForm();
 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
         RegistrationWindowsManagementForm theForm = (RegistrationWindowsManagementForm)form;
 77  0
         String termId = request.getParameter("termId");
 78  
 
 79  0
         if (StringUtils.isNotBlank(termId)){
 80  
             try {
 81  0
                 getViewHelperService(theForm).loadTermAndPeriods(termId, theForm);
 82  0
                 return getUIFModelAndView(theForm);
 83  0
             } catch (Exception ex) {
 84  0
                 throw new RuntimeException(ex);
 85  
             }
 86  
         }
 87  
 
 88  0
         return super.start(theForm, result, request, response);
 89  
     }
 90  
 
 91  
     /**
 92  
      * Method used to search term
 93  
      */
 94  
     @RequestMapping(params = "methodToCall=searchForTerm")
 95  
     public ModelAndView searchForTerm(@ModelAttribute("KualiForm") RegistrationWindowsManagementForm searchForm, BindingResult result,
 96  
                                       HttpServletRequest request, HttpServletResponse response) throws Exception {
 97  0
         String termType = searchForm.getTermType();
 98  0
         String termYear = searchForm.getTermYear();
 99  
 
 100  
         // resetForm(searchForm);
 101  0
         getViewHelperService(searchForm).searchForTerm(termType, termYear, searchForm);
 102  
 
 103  0
         if (GlobalVariables.getMessageMap().hasErrors()){
 104  0
             return getUIFModelAndView(searchForm, AppointmentConstants.SELECT_TERM_PAGE);
 105  
         }
 106  
 
 107  0
         return getUIFModelAndView(searchForm, AppointmentConstants.REGISTRATION_WINDOWS_EDIT_PAGE);
 108  
     }
 109  
 
 110  
     @RequestMapping(params = "methodToCall=show")
 111  
     public ModelAndView show(@ModelAttribute("KualiForm") RegistrationWindowsManagementForm form, BindingResult result,
 112  
                              HttpServletRequest request, HttpServletResponse response) throws Exception {
 113  
 
 114  0
         if (!form.isShowAddWindows()){
 115  0
             form.setShowAddWindows(true);
 116  
         }
 117  0
         String periodId = form.getPeriodId();
 118  0
         String periodInfoDetails = "";
 119  
 
 120  
         //Clear all the windows
 121  0
         form.getAppointmentWindows().clear();
 122  
 
 123  0
         if(periodId == null || periodId.isEmpty()) {
 124  0
             form.setPeriodInfoDetails(periodInfoDetails);
 125  0
             form.setShowAddWindows(false);
 126  0
             form.getAppointmentWindows().clear();
 127  
         }
 128  0
         else if (!periodId.isEmpty() && !periodId.equals("all")) {
 129  
 
 130  
             //Lookup the period information
 131  0
             KeyDateInfo period = getAcalService().getKeyDate(periodId,getContextInfo());
 132  
 
 133  
             //pull in the windows for this period
 134  0
             List<KeyDateInfo> periods = new ArrayList<KeyDateInfo>();
 135  0
             periods.add(period);
 136  0
             _loadWindowsInfoForm(periods, form);
 137  
 
 138  
             // display the period start/end time in details and the period name in the AddLine
 139  0
             AppointmentWindowWrapper addLine= (AppointmentWindowWrapper)form.getNewCollectionLines().get("appointmentWindows");
 140  0
             if (addLine == null){
 141  0
                 addLine = new AppointmentWindowWrapper();
 142  0
                 form.getNewCollectionLines().put("appointmentWindows", addLine);
 143  
             }
 144  
 
 145  0
             if (period.getName() != null) {
 146  0
                 periodInfoDetails = period.getName()+" Start Date: "+_getSimpleDate(period.getStartDate())+ "<br>"
 147  
                         + period.getName()+" End Date: "+_getSimpleDate(period.getEndDate());
 148  0
                 form.setPeriodName(period.getName());
 149  0
                 form.setPeriodId(period.getId());
 150  0
                 addLine.setPeriodName(period.getName());
 151  0
                 addLine.setPeriodKey(period.getId());
 152  
             } else {
 153  0
                 periodInfoDetails = period.getId()+" Start Date: "+_getSimpleDate(period.getStartDate())+ "<br>"
 154  
                         + period.getId()+" End Date: "+_getSimpleDate(period.getEndDate());
 155  0
                 form.setPeriodName(period.getId());
 156  0
                 form.setPeriodId(period.getId());
 157  0
                 addLine.setPeriodName(period.getId());
 158  0
                 addLine.setPeriodKey(period.getId());
 159  
             }
 160  0
             form.setPeriodInfoDetails(periodInfoDetails);
 161  
 
 162  
             //TODO: pull out all windows for that period and add to the collection
 163  0
         }
 164  0
         else if (periodId.equals("all")) {
 165  0
             List<KeyDateInfo> periodMilestones = form.getPeriodMilestones();
 166  0
             if(periodMilestones.isEmpty()) {
 167  0
                 TermInfo term = form.getTermInfo();
 168  0
                 if (term.getId() != null && !term.getId().isEmpty()) {
 169  0
                     getViewHelperService(form).loadPeriods(term.getId(), form);
 170  0
                     periodMilestones = form.getPeriodMilestones();
 171  
                 }
 172  
             }
 173  0
             for (KeyDateInfo period : periodMilestones){
 174  
 
 175  0
                 if (period.getName() != null) {
 176  0
                     periodInfoDetails = periodInfoDetails.concat(period.getName()+" Start Date: "+_getSimpleDate(period.getStartDate())+ "<br>"
 177  
                             + period.getName()+" End Date: "+_getSimpleDate(period.getEndDate())+"<br>");
 178  
                 } else {
 179  0
                     periodInfoDetails = periodInfoDetails.concat(period.getId()+" Start Date: "+period.getStartDate()+ "<br>"
 180  
                             + period.getId()+" End Date: "+_getSimpleDate(period.getEndDate())+"<br>");
 181  
                 }
 182  
             }
 183  0
             form.setPeriodInfoDetails(periodInfoDetails);
 184  0
             _loadWindowsInfoForm(periodMilestones, form);
 185  
         }
 186  0
         return getUIFModelAndView(form);
 187  
     }
 188  
 
 189  
     @RequestMapping(params = "methodToCall=save")
 190  
     public ModelAndView save(@ModelAttribute("KualiForm") RegistrationWindowsManagementForm form, BindingResult result,
 191  
                              HttpServletRequest request, HttpServletResponse response) throws Exception {
 192  
 
 193  
         //Loop through the form's appointment windows and create/update them using the appointmentService
 194  0
         getViewHelperService(form).saveWindows(form);
 195  
 
 196  0
         return getUIFModelAndView(form);
 197  
     }
 198  
 
 199  
     @RequestMapping(params = "methodToCall=assignStudents")
 200  
     public ModelAndView assignStudents(@ModelAttribute("KualiForm") RegistrationWindowsManagementForm uifForm, BindingResult result,
 201  
                              HttpServletRequest request, HttpServletResponse response) throws Exception {
 202  
 
 203  
         ///First save selected window
 204  0
         AppointmentWindowWrapper window = _getSelectedWindow(uifForm, "Assign Students");
 205  0
         boolean isValid = getViewHelperService(uifForm).validateApptWidnow(window);
 206  0
         if (isValid) {
 207  0
             boolean isSaved = getViewHelperService(uifForm).saveApptWindow(window);
 208  
 
 209  
             //Now do the assignments of slots and students
 210  0
             if(window!=null && isSaved){
 211  
                 //Create the appointment slots and assign students
 212  0
                 List<AppointmentSlotInfo> slots = getAppointmentService().generateAppointmentSlotsByWindow(window.getAppointmentWindowInfo().getId(), new ContextInfo());
 213  0
                 StatusInfo status = getAppointmentService().generateAppointmentsByWindow(window.getAppointmentWindowInfo().getId(), window.getAppointmentWindowInfo().getTypeKey(), new ContextInfo());
 214  
 
 215  
                 //Get feedback to the user6
 216  0
                 if(status.getIsSuccess()){
 217  0
                     GlobalVariables.getMessageMap().putInfo( KRADConstants.GLOBAL_MESSAGES,
 218  
                             AppointmentConstants.APPOINTMENT_MSG_INFO_ASSIGNED,window.getAppointmentWindowInfo().getName(), status.getMessage(), String.valueOf(slots.size()));
 219  
                     //Update window state
 220  0
                     window.getAppointmentWindowInfo().setStateKey(AppointmentServiceConstants.APPOINTMENT_WINDOW_STATE_ASSIGNED_KEY);
 221  
                 }else{
 222  
                     //There was an error
 223  0
                     GlobalVariables.getMessageMap().putInfo( KRADConstants.GLOBAL_MESSAGES,
 224  
                             AppointmentConstants.APPOINTMENT_MSG_ERROR_TOO_MANY_STUDENTS, status.getMessage());
 225  
                 }
 226  
             }
 227  
         }
 228  0
         return getUIFModelAndView(uifForm);
 229  
     }
 230  
 
 231  
     @RequestMapping(params = "methodToCall=breakAppointments")
 232  
     public ModelAndView breakAppointments(@ModelAttribute("KualiForm") RegistrationWindowsManagementForm uifForm, BindingResult result,
 233  
                                        HttpServletRequest request, HttpServletResponse response) throws Exception {
 234  
 
 235  0
         AppointmentWindowWrapper window = _getSelectedWindow(uifForm, "Break Appointments");
 236  0
         if(window!=null){
 237  
             //Delete the appointment slots and appointments for this window
 238  0
             StatusInfo status = getAppointmentService().deleteAppointmentSlotsByWindowCascading(window.getAppointmentWindowInfo().getId(), new ContextInfo());
 239  0
             if(status.getIsSuccess()){
 240  0
                 GlobalVariables.getMessageMap().putInfo( KRADConstants.GLOBAL_MESSAGES,
 241  
                         AppointmentConstants.APPOINTMENT_MSG_INFO_BREAK_APPOINTMENTS_SUCCESS);
 242  
 
 243  
                 //Update window state back to draft
 244  0
                 window.getAppointmentWindowInfo().setStateKey(AppointmentServiceConstants.APPOINTMENT_WINDOW_STATE_DRAFT_KEY);
 245  
             }else{
 246  
                 //There was an error
 247  0
                 GlobalVariables.getMessageMap().putInfo( KRADConstants.GLOBAL_MESSAGES,
 248  
                         AppointmentConstants.APPOINTMENT_MSG_ERROR_BREAK_APPOINTMENTS_FAILURE, status.getMessage());
 249  
             }
 250  
         }
 251  
 
 252  0
         return getUIFModelAndView(uifForm);
 253  
     }
 254  
 
 255  
     @Override
 256  
     public ModelAndView deleteLine(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
 257  
 
 258  0
         RegistrationWindowsManagementForm theForm = (RegistrationWindowsManagementForm)uifForm;
 259  
         try{
 260  0
             AppointmentWindowWrapper window = _getSelectedWindow(theForm, "Delete a Window");
 261  0
             if (window != null){
 262  0
                 if (AppointmentServiceConstants.APPOINTMENT_WINDOW_STATE_ASSIGNED_KEY.equals(window.getAppointmentWindowInfo().getStateKey())){
 263  
                     //need to break Assignment first
 264  
                     //Delete the appointment slots and appointments for this window
 265  0
                     StatusInfo status = getAppointmentService().deleteAppointmentSlotsByWindowCascading(window.getAppointmentWindowInfo().getId(), new ContextInfo());
 266  0
                     if(status.getIsSuccess()){
 267  0
                         getAppointmentService().deleteAppointmentWindowCascading(window.getId(), new ContextInfo());
 268  0
                         return super.deleteLine(uifForm, result, request, response);
 269  
                     }else{
 270  
                         //There was an error
 271  0
                         GlobalVariables.getMessageMap().putInfo( KRADConstants.GLOBAL_MESSAGES,
 272  
                                 AppointmentConstants.APPOINTMENT_MSG_ERROR_BREAK_APPOINTMENTS_FAILURE, status.getMessage());
 273  0
                         return getUIFModelAndView(uifForm);
 274  
                     }
 275  
                 }else {
 276  0
                     getAppointmentService().deleteAppointmentWindowCascading(window.getId(), new ContextInfo());
 277  0
                     return super.deleteLine(uifForm, result, request, response);
 278  
                 }
 279  
             }else {
 280  
                 //TODO: log window == null message
 281  0
                 return getUIFModelAndView(uifForm);
 282  
             }
 283  0
         }catch (Exception e){
 284  
             //TODO: log exception
 285  0
             return getUIFModelAndView(uifForm);
 286  
 
 287  
         }
 288  
     }
 289  
 
 290  
     // this is a more generic way to get a selected window when the form contains more than one collection
 291  
     private AppointmentWindowWrapper _getSelectedWindow(RegistrationWindowsManagementForm theForm, String actionLink){
 292  0
         String selectedCollectionPath = theForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
 293  0
         if (StringUtils.isBlank(selectedCollectionPath)) {
 294  0
             throw new RuntimeException("Selected collection was not set for " + actionLink);
 295  
         }
 296  
 
 297  0
         int selectedLineIndex = -1;
 298  0
         String selectedLine = theForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
 299  0
         if (StringUtils.isNotBlank(selectedLine)) {
 300  0
             selectedLineIndex = Integer.parseInt(selectedLine);
 301  
         }
 302  
 
 303  0
         if (selectedLineIndex == -1) {
 304  0
             throw new RuntimeException("Selected line index was not set");
 305  
         }
 306  
 
 307  0
         Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(theForm, selectedCollectionPath);
 308  0
         Object window = ((List<Object>) collection).get(selectedLineIndex);
 309  
 
 310  0
         return (AppointmentWindowWrapper)window;
 311  
     }
 312  
 
 313  
 
 314  
     private String _getSimpleDate(Date date) {
 315  0
         if (date == null)
 316  0
             return new String();
 317  
 
 318  0
         DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
 319  0
         return df.format(date);
 320  
     }
 321  
 
 322  
     private void _loadWindowsInfoForm(List<KeyDateInfo> periods, RegistrationWindowsManagementForm form) throws InvalidParameterException, MissingParameterException, DoesNotExistException, PermissionDeniedException, OperationFailedException {
 323  0
         for(KeyDateInfo period:periods){
 324  0
             List<AppointmentWindowInfo> windows = getAppointmentService().getAppointmentWindowsByPeriod(period.getId(), new ContextInfo());
 325  0
             if(windows!=null && windows.size()>0){
 326  0
                 for(AppointmentWindowInfo window:windows){
 327  
 
 328  
                     //Look up the population
 329  0
                     PopulationInfo population = getPopulationService().getPopulation(window.getAssignedPopulationId(), new ContextInfo());
 330  
 
 331  0
                     AppointmentWindowWrapper windowWrapper = new AppointmentWindowWrapper();
 332  
 
 333  0
                     windowWrapper.setAppointmentWindowInfo(window);
 334  0
                     windowWrapper.setId(window.getId());
 335  0
                     windowWrapper.setWindowName(window.getName());
 336  0
                     windowWrapper.setPeriodKey(window.getPeriodMilestoneId());
 337  0
                     windowWrapper.setPeriodName(period.getName());
 338  0
                     windowWrapper.setAssignedPopulationName(population.getName());
 339  0
                     windowWrapper.setWindowTypeKey(window.getTypeKey());
 340  0
                     if(!AppointmentServiceConstants.APPOINTMENT_WINDOW_TYPE_ONE_SLOT_KEY.equals(window.getTypeKey())) {
 341  0
                         windowWrapper.setSlotRuleEnumType(AppointmentSlotRuleTypeConversion.convTotAppointmentSlotRuleCode(window.getSlotRule()));
 342  
                     }
 343  0
                     windowWrapper.setStartDate(_parseDate(window.getStartDate()));
 344  0
                     windowWrapper.setStartTime(_parseTime(window.getStartDate()));
 345  0
                     windowWrapper.setStartTimeAmPm(_parseAmPm(window.getStartDate()));
 346  
 
 347  0
                     windowWrapper.setEndDate(_parseDate(window.getEndDate()));
 348  0
                     windowWrapper.setEndTime(_parseTime(window.getEndDate()));
 349  0
                     windowWrapper.setEndTimeAmPm(_parseAmPm(window.getEndDate()));
 350  
 
 351  0
                     form.getAppointmentWindows().add(windowWrapper);
 352  0
                 }
 353  
             }
 354  0
         }
 355  0
     }
 356  
 
 357  
     private String _parseAmPm(Date date) {
 358  0
         if(date==null){
 359  0
             return null;
 360  
         }
 361  0
         DateFormat df = new SimpleDateFormat("a");
 362  0
         return df.format(date);
 363  
     }
 364  
 
 365  
     private String _parseTime(Date date) {
 366  0
         if(date==null){
 367  0
             return null;
 368  
         }
 369  0
         DateFormat df = new SimpleDateFormat("hh:mm");
 370  0
         return df.format(date);
 371  
     }
 372  
 
 373  
     private Date _parseDate(Date date) {
 374  0
         return date;
 375  
     }
 376  
 
 377  
     public AppointmentViewHelperService getViewHelperService(RegistrationWindowsManagementForm appointmentForm){
 378  0
         if (viewHelperService == null) {
 379  0
             if (appointmentForm.getView().getViewHelperServiceClass() != null){
 380  0
                 viewHelperService = (AppointmentViewHelperService)appointmentForm.getView().getViewHelperService();
 381  
             }else{
 382  0
                 viewHelperService= (AppointmentViewHelperService)appointmentForm.getPostedView().getViewHelperService();
 383  
             }
 384  
         }
 385  0
         return viewHelperService;
 386  
     }
 387  
     public AcademicCalendarService getAcalService() {
 388  0
         if(acalService == null) {
 389  0
             acalService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
 390  
         }
 391  0
         return this.acalService;
 392  
     }
 393  
 
 394  
 
 395  
     public AppointmentService getAppointmentService() {
 396  0
         if(appointmentService == null) {
 397  0
             appointmentService = (AppointmentService) GlobalResourceLoader.getService(new QName(AppointmentServiceConstants.NAMESPACE, AppointmentServiceConstants.SERVICE_NAME_LOCAL_PART));
 398  
         }
 399  0
         return appointmentService;
 400  
     }
 401  
 
 402  
     public PopulationService getPopulationService() {
 403  0
         if(populationService == null) {
 404  0
             populationService = (PopulationService) GlobalResourceLoader.getService(new QName(PopulationServiceConstants.NAMESPACE, PopulationService.class.getSimpleName()));
 405  
         }
 406  0
         return populationService;
 407  
     }
 408  
 
 409  
     public ContextInfo getContextInfo() {
 410  0
         if (null == contextInfo) {
 411  
             //TODO - get real ContextInfo
 412  0
             contextInfo = TestHelper.getContext1();
 413  
         }
 414  0
         return contextInfo;
 415  
     }
 416  
 
 417  
 }