View Javadoc

1   package org.kuali.student.enrollment.class2.courseoffering.controller;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.rice.krad.uif.UifConstants;
5   import org.kuali.rice.krad.uif.UifParameters;
6   import org.kuali.rice.krad.util.GlobalVariables;
7   import org.kuali.rice.krad.util.KRADConstants;
8   import org.kuali.rice.krad.web.controller.MaintenanceDocumentController;
9   import org.kuali.rice.krad.web.form.DocumentFormBase;
10  import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
11  import org.kuali.rice.krad.web.form.UifFormBase;
12  import org.kuali.student.common.uif.form.KSUifMaintenanceDocumentForm;
13  import org.kuali.student.common.uif.util.KSControllerHelper;
14  import org.kuali.student.enrollment.class2.courseoffering.dto.CourseOfferingEditWrapper;
15  import org.kuali.student.enrollment.class2.courseoffering.dto.FormatOfferingWrapper;
16  import org.kuali.student.enrollment.class2.courseoffering.service.impl.CourseOfferingEditMaintainableImpl;
17  import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingConstants;
18  import org.kuali.student.r2.core.class1.search.CourseOfferingManagementSearchImpl;
19  import org.springframework.validation.BindingResult;
20  import org.springframework.web.bind.annotation.ModelAttribute;
21  import org.springframework.web.bind.annotation.RequestMapping;
22  import org.springframework.web.bind.annotation.RequestMethod;
23  import org.springframework.web.servlet.ModelAndView;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  import java.util.Properties;
28  
29  public class CourseOfferingBaseController extends MaintenanceDocumentController {
30  
31      @Override
32      protected MaintenanceDocumentForm createInitialForm(HttpServletRequest request) {
33          return new KSUifMaintenanceDocumentForm();
34      }
35  
36      /* Returns a ModelAndView for the route()-method to return to original view if there were errors.
37       * Should only be called if there are indeed errors.
38       */
39      protected ModelAndView handleRouteForErrors(DocumentFormBase form) {
40  
41          assert ( GlobalVariables.getMessageMap().hasErrors() );
42  
43          if (((MaintenanceDocumentForm)form).getDocument().getNewMaintainableObject().getDataObject() instanceof CourseOfferingEditWrapper){
44              CourseOfferingEditMaintainableImpl viewHelper = (CourseOfferingEditMaintainableImpl)KSControllerHelper.getViewHelperService(form);
45              //Make the format type drop down readonly.. otherwise, we run into display issue when the server returns back error
46              CourseOfferingEditWrapper dataObject = (CourseOfferingEditWrapper)((MaintenanceDocumentForm)form).getDocument().getNewMaintainableObject().getDataObject();
47              viewHelper.populateFormatNames(dataObject);
48          }
49  
50          return getUIFModelAndView(form);    // because there were errors, return a MAV to re-nav back to
51      }
52  
53      @RequestMapping(params = "methodToCall=cancel")
54      @Override
55      public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
56              HttpServletRequest request, HttpServletResponse response) {
57  
58          DocumentFormBase documentForm = (DocumentFormBase) form;
59          performWorkflowAction(documentForm, UifConstants.WorkflowAction.CANCEL, false);
60  
61          String urlToRedirectTo;
62          Properties urlParameters = new Properties();
63  
64          // determine which url to redirect to
65          String returnLocationFromForm = form.getReturnLocation();
66          if( StringUtils.contains( returnLocationFromForm,"viewId=courseOfferingManagementView" )
67                  || StringUtils.contains( returnLocationFromForm,"pageId=manageTheCourseOfferingPage" ) )
68          {
69              if ( !returnLocationFromForm.contains("methodToCall=") ) {  // This happens when we display a list of COs and then user click on Manage action
70                  form.getViewRequestParameters().put(CourseOfferingManagementSearchImpl.SearchParameters.IS_EXACT_MATCH_CO_CODE_SEARCH, Boolean.TRUE.toString());
71              }
72              else {
73                  form.getViewRequestParameters().put(CourseOfferingManagementSearchImpl.SearchParameters.IS_EXACT_MATCH_CO_CODE_SEARCH, Boolean.FALSE.toString());
74              }
75              urlToRedirectTo = returnLocationFromForm.replaceFirst("methodToCall=[a-zA-Z0-9]+","methodToCall=show");
76          }
77          else {
78              urlToRedirectTo = returnLocationFromForm;
79          }
80  
81          String loadNewCO = form.getActionParameters().get( "coId" );
82          if( StringUtils.isNotBlank( loadNewCO ) ) {
83  
84              urlParameters.put( KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.Maintenance.METHOD_TO_CALL_EDIT );
85              urlParameters.put( "courseOfferingInfo.id", loadNewCO );
86              urlParameters.put( KRADConstants.DATA_OBJECT_CLASS_ATTRIBUTE, CourseOfferingEditWrapper.class.getName() );
87              urlParameters.put( "returnLocation", urlToRedirectTo );
88  
89              urlToRedirectTo = CourseOfferingConstants.CONTROLLER_PATH_COURSEOFFERING_EDIT_MAINTENANCE;
90              GlobalVariables.getUifFormManager().removeSessionForm(form);
91              return performRedirect(form, urlToRedirectTo, urlParameters);
92          }
93  
94          String newUrl = urlToRedirectTo.replaceAll("growl[^&]*&", "");
95          form.setReturnLocation(newUrl);
96          return back(form,result,request,response);
97      }
98  
99  
100     /**
101      * This override is specifically for making sure users selected the format types before adding new lines (there is no
102      * util method available at view helper).
103      */
104     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addBlankLine")
105     public ModelAndView addBlankLine(@ModelAttribute("KualiForm") UifFormBase form) {
106 
107         boolean validAction = true;
108         if (((MaintenanceDocumentForm)form).getDocument().getNewMaintainableObject().getDataObject() instanceof CourseOfferingEditWrapper){
109             CourseOfferingEditWrapper dataObject = (CourseOfferingEditWrapper)((MaintenanceDocumentForm)form).getDocument().getNewMaintainableObject().getDataObject();
110             String selectedCollectionPath = form.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
111             if (StringUtils.endsWith(selectedCollectionPath, "formatOfferingList")) {
112                 for (FormatOfferingWrapper foWrapper : dataObject.getFormatOfferingList()){
113                     if (StringUtils.isBlank(foWrapper.getFormatOfferingInfo().getFormatId())){
114                         GlobalVariables.getMessageMap().putError("KS-CourseOfferingEdit-DeliveryFormats",CourseOfferingConstants.COURSEOFFERING_FORMAT_REQUIRED);
115                         validAction = false;
116                         break;
117                     }
118                 }
119             }
120         }
121 
122         if (validAction){
123             return super.addBlankLine(form);
124         } else {
125             return getUIFModelAndView(form);
126         }
127     }
128 }