View Javadoc
1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.kpme.pm.controller;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.kpme.pm.position.PositionBo;
20  import org.kuali.rice.krad.kpme.controller.EffectiveDateMaintenanceController;
21  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
22  import org.kuali.rice.krad.uif.UifParameters;
23  import org.kuali.rice.krad.uif.view.DialogManager;
24  import org.kuali.rice.krad.util.KRADConstants;
25  import org.kuali.rice.krad.web.form.DocumentFormBase;
26  import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
27  import org.kuali.rice.krad.web.form.UifFormBase;
28  import org.springframework.stereotype.Controller;
29  import org.springframework.validation.BindingResult;
30  import org.springframework.web.bind.annotation.ModelAttribute;
31  import org.springframework.web.bind.annotation.RequestMapping;
32  import org.springframework.web.servlet.ModelAndView;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  import java.util.Properties;
37  import java.util.concurrent.Callable;
38  
39  
40  @Controller
41  @RequestMapping(value = "/kpme/positionMaintenance")
42  public class PositionMaintenanceController extends EffectiveDateMaintenanceController {
43  
44      private static final String KPME_PROCESS_CHANGE_WARNING_DIALOG = "ProcessChangeWarning-Dialog";
45  
46      @Override
47      @RequestMapping(params = "methodToCall=returnFromLightbox")
48      public ModelAndView returnFromLightbox(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
49                                             HttpServletRequest request, HttpServletResponse response) {
50          String newMethodToCall = "";
51  
52          // Save user responses from dialog
53          DialogManager dm = form.getDialogManager();
54          String dialogId = dm.getCurrentDialogId();
55          if (dialogId == null) {
56              // may have been invoked by client.
57              // TODO:  handle this case (scheduled for 2.2-m3)
58              // for now, log WARNING and default to start, can we add a growl?
59              newMethodToCall = "start";
60          } else {
61              dm.setDialogAnswer(dialogId, form.getDialogResponse());
62              dm.setDialogExplanation(dialogId, form.getDialogExplanation());
63              newMethodToCall = dm.getDialogReturnMethod(dialogId);
64              dm.setCurrentDialogId(null);
65          }
66  
67  
68          PositionBo aPositionBo = (PositionBo) ((MaintenanceDocumentForm) form).getDocument().getNewMaintainableObject().getDataObject();
69          // call intended controller method
70          Properties props = new Properties();
71          props.put(UifParameters.METHOD_TO_CALL, newMethodToCall);
72          props.put(UifParameters.VIEW_ID, form.getViewId());
73          props.put(UifParameters.FORM_KEY, form.getFormKey());
74          props.put(UifParameters.AJAX_REQUEST, "false");
75          props.put("hrPositionId",aPositionBo.getHrPositionId());
76          props.put("oldProcess",request.getParameter("oldProcess"));
77          props.put("newProcess",request.getParameter("newProcess"));
78          return performRedirect(form, form.getFormPostUrl(), props);
79      }
80  
81      @RequestMapping(params = "methodToCall=changeProcess")
82      public ModelAndView changeProcess(@ModelAttribute("KualiForm") final MaintenanceDocumentForm form, final BindingResult result,
83                                          final HttpServletRequest request, final HttpServletResponse response) throws Exception {
84          return showProcessChangeWarningIfNeeded(form, result, request, response, new Callable<ModelAndView>() {
85              @Override
86              public ModelAndView call() throws Exception {
87                  // register the superclass method as the handler
88                  setupMaintenance(form, request, KRADConstants.MAINTENANCE_EDIT_ACTION);
89                  String newProcess = request.getParameter("newProcess");
90  
91                  MaintenanceDocument maintenanceDocument = (MaintenanceDocument) form.getDocument();
92                  ((PositionBo)maintenanceDocument.getNewMaintainableObject().getDataObject()).setProcess(newProcess);
93                  return getUIFModelAndView(form);
94              }
95          });
96      }
97  
98  
99  
100 
101     // This method contains the common pre-handler logic to show the process change warning dialog for any particular handler action that we wish to 'intercept'.
102     // The callable parameter encapsulates the actual super class handler method to be invoked in case of either an absence of warning or user's confirmation on the warning.
103     protected ModelAndView showProcessChangeWarningIfNeeded(@ModelAttribute("KualiForm") DocumentFormBase form,
104                                                             BindingResult result,
105                                                             HttpServletRequest request,
106                                                             HttpServletResponse response,
107                                                             Callable<ModelAndView> callable) throws Exception {
108 
109         ModelAndView retVal = null;
110         String oldProcess = request.getParameter("oldProcess");
111 
112             if (!hasDialogBeenAnswered(KPME_PROCESS_CHANGE_WARNING_DIALOG, form)) {
113                 if (StringUtils.isEmpty(oldProcess)) {
114                     retVal = getUIFModelAndView(form);
115                 } else {
116                     // redirect back to client to display lightbox
117                     retVal = showDialog(KPME_PROCESS_CHANGE_WARNING_DIALOG, form, request, response);
118                 }
119             } else {
120                  boolean areYouSure = getBooleanDialogResponse(KPME_PROCESS_CHANGE_WARNING_DIALOG, form, request, response);
121                 // clear all dialogs in order to display warning again
122                 form.getDialogManager().removeAllDialogs();
123                 if (areYouSure) {
124                     // call the registered handler method; user has confirmed
125                     retVal = callable.call();
126                 }
127                 else {
128                     // revert to old process
129 
130                     MaintenanceDocument maintenanceDocument = (MaintenanceDocument) form.getDocument();
131                     ((PositionBo)maintenanceDocument.getNewMaintainableObject().getDataObject()).setProcess(oldProcess);
132                     retVal = getUIFModelAndView(form);
133                 }
134             }
135         return retVal;
136     }
137 
138 }