001/**
002 * Copyright 2005-2016 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.uif.util;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.log4j.Logger;
020import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
021import org.kuali.rice.krad.uif.UifConstants;
022import org.kuali.rice.krad.uif.view.History;
023import org.kuali.rice.krad.uif.view.View;
024import org.kuali.rice.krad.uif.component.Component;
025import org.kuali.rice.krad.uif.service.ViewService;
026import org.kuali.rice.krad.util.KRADUtils;
027import org.kuali.rice.krad.web.controller.UifControllerBase;
028import org.kuali.rice.krad.web.form.UifFormBase;
029import org.springframework.web.servlet.ModelAndView;
030
031import javax.servlet.http.HttpServletRequest;
032import javax.servlet.http.HttpServletResponse;
033import java.util.Map;
034
035/**
036 * Provides helper methods that will be used during the request lifecycle
037 *
038 * <p>
039 * Created to avoid duplication of the methods used by the UifHandlerExceptionResolver
040 * </p>
041 *
042 * @author Kuali Rice Team (rice.collab@kuali.org)
043 */
044public class UifWebUtils {
045    private static final Logger LOG = Logger.getLogger(UifWebUtils.class);
046
047    /**
048     * Configures the <code>ModelAndView</code> instance containing the form
049     * data and pointing to the UIF generic spring view
050     *
051     * @param form - Form instance containing the model data
052     * @param pageId - Id of the page within the view that should be rendered, can
053     * be left blank in which the current or default page is rendered
054     * @return ModelAndView object with the contained form
055     */
056    public static ModelAndView getUIFModelAndView(UifFormBase form, String pageId) {
057        if (StringUtils.isNotBlank(pageId)) {
058            form.setPageId(pageId);
059        }
060
061        // create the spring return object pointing to View.jsp
062        ModelAndView modelAndView = new ModelAndView();
063        modelAndView.addObject(UifConstants.DEFAULT_MODEL_NAME, form);
064        modelAndView.setViewName(UifConstants.SPRING_VIEW_ID);
065
066        return modelAndView;
067    }
068
069    public static ModelAndView getComponentModelAndView(Component component, Object model) {
070        ModelAndView modelAndView = new ModelAndView();
071        modelAndView.addObject(UifConstants.DEFAULT_MODEL_NAME, model);
072        modelAndView.addObject("Component", component);
073        modelAndView.setViewName("ComponentUpdate");
074
075        return modelAndView;
076    }
077
078    /**
079     * After the controller logic is executed, the form is placed into session
080     * and the corresponding view is prepared for rendering
081     */
082    public static void postControllerHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
083            ModelAndView modelAndView) throws Exception {
084        if (handler instanceof UifControllerBase && (modelAndView != null)) {
085            UifControllerBase controller = (UifControllerBase) handler;
086            UifFormBase form = null;
087
088            // check to see if this is a full view request
089            if (modelAndView.getViewName().equals(UifConstants.SPRING_VIEW_ID)) {
090                Object model = modelAndView.getModelMap().get(UifConstants.DEFAULT_MODEL_NAME);
091                if (model instanceof UifFormBase) {
092                    form = (UifFormBase) model;
093
094                    // prepare view instance
095                    prepareViewForRendering(request, form);
096
097                    // update history for view
098                    prepareHistory(request, form);
099                }
100            }
101        }
102    }
103
104    /**
105     * Updates the history object (or constructs a new History) for the view we are getting ready
106     * to render
107     *
108     * @param request - Http request object containing the request parameters
109     * @param form - object containing the view data
110     */
111    public static void prepareHistory(HttpServletRequest request, UifFormBase form) {
112        View view = form.getView();
113
114        // main history/breadcrumb tracking support
115        History history = form.getFormHistory();
116        if (history == null || request.getMethod().equals("GET")) {
117            history = new History();
118            history.setHomewardPath(view.getBreadcrumbs().getHomewardPathList());
119            history.setAppendHomewardPath(view.getBreadcrumbs().isDisplayHomewardPath());
120            history.setAppendPassedHistory(view.getBreadcrumbs().isDisplayPassedHistory());
121
122            // passed settings ALWAYS override the defaults
123            if (StringUtils.isNotBlank(request.getParameter(UifConstants.UrlParams.SHOW_HOME))) {
124                history.setAppendHomewardPath(Boolean.parseBoolean(request.getParameter(
125                        UifConstants.UrlParams.SHOW_HOME)));
126            }
127
128            if (StringUtils.isNotBlank(request.getParameter(UifConstants.UrlParams.SHOW_HISTORY))) {
129                history.setAppendPassedHistory(Boolean.parseBoolean(request.getParameter(
130                        UifConstants.UrlParams.SHOW_HISTORY)));
131            }
132
133            history.setCurrent(form, request);
134            history.buildHistoryFromParameterString(request.getParameter(UifConstants.UrlParams.HISTORY));
135            form.setFormHistory(history);
136        }
137    }
138
139    /**
140     * Prepares the <code>View</code> instance contained on the form for
141     * rendering
142     *
143     * @param request - request object
144     * @param form - form instance containing the data and view instance
145     */
146    public static void prepareViewForRendering(HttpServletRequest request, UifFormBase form) {
147        View view = form.getView();
148
149        // set view page to page requested on form
150        if (StringUtils.isNotBlank(form.getPageId())) {
151            view.setCurrentPageId(form.getPageId());
152        }
153
154        Map<String, String> parameterMap = KRADUtils.translateRequestParameterMap(request.getParameterMap());
155        parameterMap.putAll(form.getViewRequestParameters());
156
157        // build view which will prepare for rendering
158        getViewService().buildView(view, form, parameterMap);
159
160        // set dirty flag
161        form.setValidateDirty(view.isValidateDirty());
162    }
163
164    protected static ViewService getViewService() {
165        return KRADServiceLocatorWeb.getViewService();
166    }
167}