View Javadoc

1   /**
2    * Copyright 2013 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   * Created by vgadiyak on 8/20/13
16   */
17  package org.kuali.student.enrollment.class2.courseoffering.controller;
18  
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.krad.util.GlobalVariables;
21  import org.kuali.rice.krad.util.KRADConstants;
22  import org.kuali.rice.krad.web.form.DocumentFormBase;
23  import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
24  import org.kuali.student.common.uif.form.KSUifMaintenanceDocumentForm;
25  import org.kuali.student.enrollment.class2.courseoffering.dto.CourseOfferingEditWrapper;
26  import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingConstants;
27  import org.kuali.student.enrollment.common.util.EnrollConstants;
28  import org.kuali.student.r2.core.class1.search.CourseOfferingManagementSearchImpl;
29  import org.springframework.stereotype.Controller;
30  import org.springframework.validation.BindingResult;
31  import org.springframework.web.bind.annotation.ModelAttribute;
32  import org.springframework.web.bind.annotation.RequestMapping;
33  import org.springframework.web.servlet.ModelAndView;
34  
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  import java.util.Properties;
38  
39  /**
40   * This class provides controller methods for Course Offering Edit ui
41   *
42   * @author Kuali Student Team
43   */
44  
45  @Controller
46  @RequestMapping(value = "/courseOfferingEdit")
47  public class CourseOfferingEditController extends CourseOfferingBaseController {
48  
49      @Override
50      protected MaintenanceDocumentForm createInitialForm(HttpServletRequest request) {
51          return new KSUifMaintenanceDocumentForm();
52      }
53  
54      @Override
55      public ModelAndView maintenanceEdit(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
56                                          HttpServletRequest request, HttpServletResponse response) throws Exception {
57  
58          setupMaintenance(form, request, KRADConstants.MAINTENANCE_EDIT_ACTION);
59  
60          // check view authorization
61          // TODO: this needs to be invoked for each request
62          if (form.getView() != null) {
63              String methodToCall = request.getParameter(KRADConstants.DISPATCH_REQUEST_PARAMETER);
64              checkViewAuthorization(form, methodToCall);
65              String crossListedAlias = request.getParameter("editCrossListedCoAlias");
66              if(StringUtils.equals(crossListedAlias, "true")) {
67                  Object selectedObject =  form.getDocument().getNewMaintainableObject().getDataObject();
68                  if(selectedObject instanceof CourseOfferingEditWrapper) {
69                      ((CourseOfferingEditWrapper) selectedObject).setEditCrossListedCoAlias(true);
70                  }
71              }
72          }
73  
74          return getUIFModelAndView(form);
75      }
76  
77      @Override
78      @RequestMapping(params = "methodToCall=route")
79      public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
80                                HttpServletRequest request, HttpServletResponse response) {
81  
82          super.route(form, result, request, response);
83  
84          if( GlobalVariables.getMessageMap().hasErrors() ) {
85              return handleRouteForErrors( form );
86          }
87  
88          return handleRouteForCoEdit(form);
89      }
90  
91      /* Returns a ModelAndView for the route()-method to return a new view if we are editing a CO */
92      private ModelAndView handleRouteForCoEdit( DocumentFormBase form ) {
93  
94          String urlToRedirectTo;
95          Properties urlParameters = new Properties();
96  
97          GlobalVariables.getUifFormManager().removeSessionForm(form);    // clear current form from session
98  
99          // create a Growl-message
100         CourseOfferingEditWrapper dataObject = (CourseOfferingEditWrapper)((MaintenanceDocumentForm)form).getDocument().getNewMaintainableObject().getDataObject();
101 
102         //display the correct growl message based on the availability of exam period and final exam type
103         if (StringUtils.isEmpty(dataObject.getExamPeriodId())) {
104             urlParameters.put(EnrollConstants.GROWL_MESSAGE, CourseOfferingConstants.COURSE_OFFERING_EDIT_SUCCESS_WITH_MISSING_EXAMPERIOD);
105         } else if (!StringUtils.equals(dataObject.getCourseOfferingInfo().getFinalExamType(), CourseOfferingConstants.COURSEOFFERING_FINAL_EXAM_TYPE_STANDARD)) {
106             urlParameters.put(EnrollConstants.GROWL_MESSAGE, CourseOfferingConstants.COURSE_OFFERING_EDIT_SUCCESS);
107         }
108         else {
109             urlParameters.put(EnrollConstants.GROWL_MESSAGE, CourseOfferingConstants.COURSE_OFFERING_EDIT_SUCCESS_WITH_EXAMOFFERING_GENERATED);
110         }
111 
112         urlParameters.put(EnrollConstants.GROWL_MESSAGE_PARAMS, dataObject.getCourseOfferingCode());
113 
114         // determine which url to redirect to
115         String returnLocationFromForm = form.getReturnLocation();
116         if ( StringUtils.contains(returnLocationFromForm,"viewId=courseOfferingManagementView")
117                 || StringUtils.contains(returnLocationFromForm,"pageId=manageTheCourseOfferingPage")) {
118             if (!returnLocationFromForm.contains("methodToCall=")) {
119                 // This happens when we display a list of COs and then user click on Manage action
120                 form.getViewRequestParameters().put(CourseOfferingManagementSearchImpl.SearchParameters.IS_EXACT_MATCH_CO_CODE_SEARCH, Boolean.TRUE.toString());
121             }
122             else {
123                 form.getViewRequestParameters().put(CourseOfferingManagementSearchImpl.SearchParameters.IS_EXACT_MATCH_CO_CODE_SEARCH, Boolean.FALSE.toString());
124             }
125             urlToRedirectTo = returnLocationFromForm.replaceFirst("methodToCall=[a-zA-Z0-9]+","methodToCall=show");
126         } else if (StringUtils.contains(returnLocationFromForm,"pageId=manageCourseOfferingsPage")
127                 && !returnLocationFromForm.contains("methodToCall=")) {
128             urlToRedirectTo = returnLocationFromForm + "&methodToCall=show";
129         } else {
130             urlToRedirectTo = returnLocationFromForm;
131         }
132 
133         //remove growl message param from request url
134         urlToRedirectTo = urlToRedirectTo.replaceAll("growl.[^&]+&","");
135 
136         // special handling if navigating to a specific CO
137         String loadNewCO = form.getActionParameters().get( "coId" );
138         if( StringUtils.isNotBlank( loadNewCO ) ) {
139 
140             urlParameters.put( KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.Maintenance.METHOD_TO_CALL_EDIT );
141             urlParameters.put( "courseOfferingInfo.id", loadNewCO );
142             urlParameters.put( KRADConstants.DATA_OBJECT_CLASS_ATTRIBUTE, CourseOfferingEditWrapper.class.getName() );
143             urlParameters.put( "returnLocation", urlToRedirectTo );
144 
145             urlToRedirectTo = CourseOfferingConstants.CONTROLLER_PATH_COURSEOFFERING_EDIT_MAINTENANCE;
146         }
147 
148         return performRedirect(form, urlToRedirectTo, urlParameters);
149     }
150 
151 }