001package org.kuali.student.enrollment.class2.courseoffering.controller;
002
003import org.apache.commons.lang.StringUtils;
004import org.kuali.rice.krad.uif.UifConstants;
005import org.kuali.rice.krad.uif.UifParameters;
006import org.kuali.rice.krad.util.GlobalVariables;
007import org.kuali.rice.krad.util.KRADConstants;
008import org.kuali.rice.krad.web.controller.MaintenanceDocumentController;
009import org.kuali.rice.krad.web.form.DocumentFormBase;
010import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
011import org.kuali.rice.krad.web.form.UifFormBase;
012import org.kuali.student.common.uif.form.KSUifMaintenanceDocumentForm;
013import org.kuali.student.common.uif.util.KSControllerHelper;
014import org.kuali.student.enrollment.class2.courseoffering.dto.CourseOfferingEditWrapper;
015import org.kuali.student.enrollment.class2.courseoffering.dto.CourseOfferingWrapper;
016import org.kuali.student.enrollment.class2.courseoffering.dto.FormatOfferingWrapper;
017import org.kuali.student.enrollment.class2.courseoffering.service.impl.CourseOfferingEditMaintainableImpl;
018import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingConstants;
019import org.kuali.student.r2.core.class1.search.CourseOfferingManagementSearchImpl;
020import org.springframework.validation.BindingResult;
021import org.springframework.web.bind.annotation.ModelAttribute;
022import org.springframework.web.bind.annotation.RequestMapping;
023import org.springframework.web.bind.annotation.RequestMethod;
024import org.springframework.web.servlet.ModelAndView;
025
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpServletResponse;
028import java.util.HashMap;
029import java.util.Map;
030import java.util.Properties;
031
032public class CourseOfferingBaseController extends MaintenanceDocumentController {
033
034    @Override
035    protected MaintenanceDocumentForm createInitialForm(HttpServletRequest request) {
036        return new KSUifMaintenanceDocumentForm();
037    }
038
039    /* Returns a ModelAndView for the route()-method to return to original view if there were errors.
040     * Should only be called if there are indeed errors.
041     */
042    protected ModelAndView handleRouteForErrors(DocumentFormBase form) {
043
044        assert ( GlobalVariables.getMessageMap().hasErrors() );
045
046        if (((MaintenanceDocumentForm)form).getDocument().getNewMaintainableObject().getDataObject() instanceof CourseOfferingEditWrapper){
047            CourseOfferingEditMaintainableImpl viewHelper = (CourseOfferingEditMaintainableImpl)KSControllerHelper.getViewHelperService(form);
048            //Make the format type drop down readonly.. otherwise, we run into display issue when the server returns back error
049            CourseOfferingEditWrapper dataObject = (CourseOfferingEditWrapper)((MaintenanceDocumentForm)form).getDocument().getNewMaintainableObject().getDataObject();
050            viewHelper.populateFormatNames(dataObject);
051        }
052
053        return getUIFModelAndView(form);    // because there were errors, return a MAV to re-nav back to
054    }
055
056    @RequestMapping(params = "methodToCall=cancel")
057    @Override
058    public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
059            HttpServletRequest request, HttpServletResponse response) {
060
061        DocumentFormBase documentForm = (DocumentFormBase) form;
062        performWorkflowAction(documentForm, UifConstants.WorkflowAction.CANCEL, false);
063
064        String urlToRedirectTo;
065        Properties urlParameters = new Properties();
066
067        // determine which url to redirect to
068        String returnLocationFromForm = form.getReturnLocation();
069        if( StringUtils.contains( returnLocationFromForm,"viewId=courseOfferingManagementView" )
070                || StringUtils.contains( returnLocationFromForm,"pageId=manageTheCourseOfferingPage" ) )
071        {
072            // wrap with HashMap since viewRequestParameters is set with Collections.unmodifiableMap()
073            // in org.kuali.rice.krad.uif.view.View.setViewRequestParameters()
074            Map<String, String> additionalParameters = new HashMap<String, String>(form.getViewRequestParameters());
075            if ( !returnLocationFromForm.contains("methodToCall=") ) {  // This happens when we display a list of COs and then user click on Manage action
076                additionalParameters.put(CourseOfferingManagementSearchImpl.SearchParameters.IS_EXACT_MATCH_CO_CODE_SEARCH, Boolean.TRUE.toString());
077            }
078            else {
079                additionalParameters.put(CourseOfferingManagementSearchImpl.SearchParameters.IS_EXACT_MATCH_CO_CODE_SEARCH, Boolean.FALSE.toString());
080            }
081            form.setViewRequestParameters(additionalParameters);
082            urlToRedirectTo = returnLocationFromForm.replaceFirst("methodToCall=[a-zA-Z0-9]+","methodToCall=show");
083        }
084        else {
085            urlToRedirectTo = returnLocationFromForm;
086        }
087
088        String loadNewCO = form.getActionParameters().get( "coId" );
089        if( StringUtils.isNotBlank( loadNewCO ) ) {
090
091            urlParameters.put( KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.Maintenance.METHOD_TO_CALL_EDIT );
092            urlParameters.put( "courseOfferingInfo.id", loadNewCO );
093            urlParameters.put( KRADConstants.DATA_OBJECT_CLASS_ATTRIBUTE, CourseOfferingEditWrapper.class.getName() );
094            urlParameters.put( "returnLocation", urlToRedirectTo );
095
096            urlToRedirectTo = CourseOfferingConstants.CONTROLLER_PATH_COURSEOFFERING_EDIT_MAINTENANCE;
097            GlobalVariables.getUifFormManager().removeSessionForm(form);
098            return performRedirect(form, urlToRedirectTo, urlParameters);
099        }
100
101        String newUrl = urlToRedirectTo.replaceAll("growl[^&]*&", "");
102        form.setReturnLocation(newUrl);
103        return back(form,result,request,response);
104    }
105
106
107    /**
108     * This override is specifically for making sure users selected the format types before adding new lines (there is no
109     * util method available at view helper).
110     */
111    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addBlankLine")
112    @Override
113    public ModelAndView addBlankLine(@ModelAttribute("KualiForm") final UifFormBase form,
114            HttpServletRequest request, HttpServletResponse response) {
115
116        boolean validAction = true;
117        if (((MaintenanceDocumentForm)form).getDocument().getNewMaintainableObject().getDataObject() instanceof CourseOfferingEditWrapper){
118            CourseOfferingEditWrapper dataObject = (CourseOfferingEditWrapper)((MaintenanceDocumentForm)form).getDocument().getNewMaintainableObject().getDataObject();
119            String selectedCollectionPath = form.getActionParamaterValue(UifParameters.SELECTED_COLLECTION_PATH);
120            if (StringUtils.endsWith(selectedCollectionPath, "formatOfferingList")) {
121                for (FormatOfferingWrapper foWrapper : dataObject.getFormatOfferingList()){
122                    if (StringUtils.isBlank(foWrapper.getFormatOfferingInfo().getFormatId())){
123                        GlobalVariables.getMessageMap().putError("KS-CourseOfferingEdit-DeliveryFormats",CourseOfferingConstants.COURSEOFFERING_FORMAT_REQUIRED);
124                        validAction = false;
125                        break;
126                    }
127                }
128            }
129        }
130
131        if (validAction){
132            return super.addBlankLine(form, request, response);
133        } else {
134            return getUIFModelAndView(form);
135        }
136    }
137
138    /**
139     * This method makes sure that if the examtype is standard and the system is configured to use a final exam matrix,
140     * the useFinalExamMatrix checkbox defaults to true.
141     */
142    @RequestMapping(params = "methodToCall=checkExamMatrix")
143    public ModelAndView checkExamMatrix(final @ModelAttribute("KualiForm") MaintenanceDocumentForm form,
144                                                   final HttpServletRequest request,
145                                                   final HttpServletResponse response) {
146        CourseOfferingWrapper wrapper = (CourseOfferingWrapper) form.getDocument().getNewMaintainableObject().getDataObject();
147        if (wrapper.getCourseOfferingInfo().getFinalExamType().equals("STANDARD") && wrapper.isUseFinalExamMatrixSystemDefault()) {
148            wrapper.setUseFinalExamMatrix(true);
149        } else {
150            wrapper.setUseFinalExamMatrix(false);
151        }
152        return getUIFModelAndView(form);
153    }
154}