001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.action;
016    
017    import com.liferay.portal.kernel.portlet.WindowStateFactory;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.model.Layout;
020    import com.liferay.portal.model.Portlet;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.service.PortletLocalServiceUtil;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portal.util.WebKeys;
026    
027    import javax.portlet.WindowState;
028    
029    import javax.servlet.ServletContext;
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletResponse;
032    
033    import org.apache.struts.action.Action;
034    import org.apache.struts.action.ActionForm;
035    import org.apache.struts.action.ActionForward;
036    import org.apache.struts.action.ActionMapping;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     */
041    public class RenderPortletAction extends Action {
042    
043            @Override
044            public ActionForward execute(
045                            ActionMapping actionMapping, ActionForm actionForm,
046                            HttpServletRequest request, HttpServletResponse response)
047                    throws Exception {
048    
049                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
050                            WebKeys.THEME_DISPLAY);
051    
052                    themeDisplay.setAjax(true);
053    
054                    ServletContext servletContext = (ServletContext)request.getAttribute(
055                            WebKeys.CTX);
056    
057                    String ajaxId = request.getParameter("ajax_id");
058    
059                    long companyId = PortalUtil.getCompanyId(request);
060                    User user = PortalUtil.getUser(request);
061                    Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
062                    String portletId = ParamUtil.getString(request, "p_p_id");
063    
064                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
065                            companyId, portletId);
066    
067                    String queryString = null;
068                    String columnId = ParamUtil.getString(request, "p_p_col_id");
069                    int columnPos = ParamUtil.getInteger(request, "p_p_col_pos");
070                    int columnCount = ParamUtil.getInteger(request, "p_p_col_count");
071                    boolean staticPortlet = ParamUtil.getBoolean(request, "p_p_static");
072                    boolean staticStartPortlet = ParamUtil.getBoolean(
073                            request, "p_p_static_start");
074    
075                    if (staticPortlet) {
076                            portlet = (Portlet)portlet.clone();
077    
078                            portlet.setStatic(true);
079                            portlet.setStaticStart(staticStartPortlet);
080                    }
081    
082                    if (ajaxId != null) {
083                            response.setHeader("Ajax-ID", ajaxId);
084                    }
085    
086                    WindowState windowState = WindowStateFactory.getWindowState(
087                            ParamUtil.getString(request, "p_p_state"));
088    
089                    PortalUtil.updateWindowState(
090                            portletId, user, layout, windowState, request);
091    
092                    PortalUtil.renderPortlet(
093                            servletContext, request, response, portlet, queryString, columnId,
094                            new Integer(columnPos), new Integer(columnCount), true);
095    
096                    return null;
097            }
098    
099    }