001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.web.service.impl;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.config.property.ConfigContext;
020import org.kuali.rice.krad.uif.UifConstants;
021import org.kuali.rice.krad.uif.UifParameters;
022import org.kuali.rice.krad.util.GlobalVariables;
023import org.kuali.rice.krad.util.KRADConstants;
024import org.kuali.rice.krad.web.form.HistoryFlow;
025import org.kuali.rice.krad.web.form.UifFormBase;
026import org.kuali.rice.krad.web.service.ModelAndViewService;
027import org.kuali.rice.krad.web.service.NavigationControllerService;
028import org.springframework.web.servlet.ModelAndView;
029
030import java.util.Properties;
031
032/**
033 * Default implementation of the navigation controller service.
034 *
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 */
037public class NavigationControllerServiceImpl implements NavigationControllerService {
038
039    private ModelAndViewService modelAndViewService;
040
041    /**
042     * {@inheritDoc}
043     */
044    @Override
045    public ModelAndView back(UifFormBase form) {
046        boolean returnToFlowStart = false;
047
048        String returnToStartActionParm = form.getActionParamaterValue(UifConstants.HistoryFlow.RETURN_TO_START);
049        if (StringUtils.isNotBlank(returnToStartActionParm)) {
050            returnToFlowStart = Boolean.parseBoolean(returnToStartActionParm);
051        }
052
053        return returnToHistory(form, true, false, returnToFlowStart);
054    }
055
056    /**
057     * {@inheritDoc}
058     */
059    @Override
060    public ModelAndView returnToPrevious(UifFormBase form) {
061        return returnToHistory(form, true, false, false);
062    }
063
064    /**
065     * {@inheritDoc}
066     */
067    @Override
068    public ModelAndView returnToHub(UifFormBase form) {
069        return returnToHistory(form, false, false, true);
070    }
071
072    /**
073     * {@inheritDoc}
074     */
075    @Override
076    public ModelAndView returnToHistory(UifFormBase form, boolean returnToPrevious, boolean returnToApplicationHome,
077            boolean returnToFlowStart) {
078        Properties props = new Properties();
079        props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH);
080
081        if (StringUtils.isNotBlank(form.getReturnFormKey())) {
082            props.put(UifParameters.FORM_KEY, form.getReturnFormKey());
083        }
084
085        GlobalVariables.getUifFormManager().removeSessionForm(form);
086
087        String returnUrl = getReturnUrl(form, returnToPrevious, returnToApplicationHome, returnToFlowStart);
088
089        return getModelAndViewService().performRedirect(form, returnUrl, props);
090    }
091
092    /**
093     * Gets the URL to return to based form data and the given flags.
094     *
095     * @param form form instance containing return location (possibly) and history manager
096     * @param returnToPrevious whether we should return to the previous view
097     * @param returnToApplicationHome whether we should return to the configured application home
098     * @param returnToFlowStart when in a flow, whether to return to the flow start point
099     * @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}