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