View Javadoc
1   /**
2    * Copyright 2005-2015 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.web.service.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.config.property.ConfigContext;
20  import org.kuali.rice.krad.uif.UifConstants;
21  import org.kuali.rice.krad.uif.UifParameters;
22  import org.kuali.rice.krad.util.GlobalVariables;
23  import org.kuali.rice.krad.util.KRADConstants;
24  import org.kuali.rice.krad.web.form.HistoryFlow;
25  import org.kuali.rice.krad.web.form.UifFormBase;
26  import org.kuali.rice.krad.web.service.ModelAndViewService;
27  import org.kuali.rice.krad.web.service.NavigationControllerService;
28  import org.springframework.web.servlet.ModelAndView;
29  
30  import java.util.Properties;
31  
32  /**
33   * Default implementation of the navigation controller service.
34   *
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  public class NavigationControllerServiceImpl implements NavigationControllerService {
38  
39      private ModelAndViewService modelAndViewService;
40  
41      /**
42       * {@inheritDoc}
43       */
44      @Override
45      public ModelAndView back(UifFormBase form) {
46          boolean returnToFlowStart = false;
47  
48          String returnToStartActionParm = form.getActionParamaterValue(UifConstants.HistoryFlow.RETURN_TO_START);
49          if (StringUtils.isNotBlank(returnToStartActionParm)) {
50              returnToFlowStart = Boolean.parseBoolean(returnToStartActionParm);
51          }
52  
53          return returnToHistory(form, true, false, returnToFlowStart);
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      @Override
60      public ModelAndView returnToPrevious(UifFormBase form) {
61          return returnToHistory(form, true, false, false);
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public ModelAndView returnToHub(UifFormBase form) {
69          return returnToHistory(form, false, false, true);
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public ModelAndView returnToHistory(UifFormBase form, boolean returnToPrevious, boolean returnToApplicationHome,
77              boolean returnToFlowStart) {
78          Properties props = new Properties();
79          props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH);
80  
81          if (StringUtils.isNotBlank(form.getReturnFormKey())) {
82              props.put(UifParameters.FORM_KEY, form.getReturnFormKey());
83          }
84  
85          GlobalVariables.getUifFormManager().removeSessionForm(form);
86  
87          String returnUrl = getReturnUrl(form, returnToPrevious, returnToApplicationHome, returnToFlowStart);
88  
89          return getModelAndViewService().performRedirect(form, returnUrl, props);
90      }
91  
92      /**
93       * Gets the URL to return to based form data and the given flags.
94       *
95       * @param form form instance containing return location (possibly) and history manager
96       * @param returnToPrevious whether we should return to the previous view
97       * @param returnToApplicationHome whether we should return to the configured application home
98       * @param returnToFlowStart when in a flow, whether to return to the flow start point
99       * @return String URL
100      */
101     protected String getReturnUrl(UifFormBase form, boolean returnToPrevious, boolean returnToApplicationHome,
102             boolean returnToFlowStart) {
103         String returnUrl = null;
104 
105         if (returnToPrevious) {
106             returnUrl = form.getReturnLocation();
107         } else {
108             HistoryFlow historyFlow = form.getHistoryManager().getMostRecentFlowByFormKey(form.getFlowKey(),
109                     form.getRequestedFormKey());
110             if (historyFlow != null) {
111                 // we are in a flow
112                 returnUrl = historyFlow.getFlowReturnPoint();
113 
114                 if (returnToFlowStart && StringUtils.isNotBlank(historyFlow.getFlowStartPoint())) {
115                     returnUrl = historyFlow.getFlowStartPoint();
116                 }
117             }
118         }
119 
120         if (StringUtils.isBlank(returnUrl) || returnToApplicationHome) {
121             returnUrl = ConfigContext.getCurrentContextConfig().getProperty(KRADConstants.APPLICATION_URL_KEY);
122         }
123 
124         return returnUrl;
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     public ModelAndView navigate(UifFormBase form) {
132         form.setDirtyForm(false);
133 
134         String pageId = form.getActionParamaterValue(UifParameters.NAVIGATE_TO_PAGE_ID);
135 
136         return getModelAndViewService().getModelAndView(form, pageId);
137     }
138 
139     protected ModelAndViewService getModelAndViewService() {
140         return modelAndViewService;
141     }
142 
143     public void setModelAndViewService(ModelAndViewService modelAndViewService) {
144         this.modelAndViewService = modelAndViewService;
145     }
146 }