001    /**
002     * Copyright (c) 2000-2012 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.PortletContainerUtil;
018    import com.liferay.portal.kernel.portlet.WindowStateFactory;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.model.Layout;
021    import com.liferay.portal.model.Portlet;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.service.PortletLocalServiceUtil;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.PortalUtil;
026    import com.liferay.portal.util.WebKeys;
027    import com.liferay.taglib.aui.AUIUtil;
028    
029    import javax.portlet.WindowState;
030    
031    import javax.servlet.http.HttpServletRequest;
032    import javax.servlet.http.HttpServletResponse;
033    
034    import org.apache.struts.action.Action;
035    import org.apache.struts.action.ActionForm;
036    import org.apache.struts.action.ActionForward;
037    import org.apache.struts.action.ActionMapping;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class RenderPortletAction extends Action {
043    
044            @Override
045            public ActionForward execute(
046                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
047                            HttpServletResponse response)
048                    throws Exception {
049    
050                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
051                            WebKeys.THEME_DISPLAY);
052    
053                    themeDisplay.setAjax(true);
054    
055                    String ajaxId = request.getParameter("ajax_id");
056    
057                    long companyId = PortalUtil.getCompanyId(request);
058                    User user = PortalUtil.getUser(request);
059                    Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
060                    String portletId = ParamUtil.getString(request, "p_p_id");
061    
062                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
063                            companyId, portletId);
064    
065                    String columnId = ParamUtil.getString(request, "p_p_col_id");
066                    int columnPos = ParamUtil.getInteger(request, "p_p_col_pos");
067                    int columnCount = ParamUtil.getInteger(request, "p_p_col_count");
068                    boolean staticPortlet = ParamUtil.getBoolean(request, "p_p_static");
069                    boolean staticStartPortlet = ParamUtil.getBoolean(
070                            request, "p_p_static_start");
071    
072                    if (staticPortlet) {
073                            portlet = (Portlet)portlet.clone();
074    
075                            portlet.setStatic(true);
076                            portlet.setStaticStart(staticStartPortlet);
077                    }
078    
079                    if (ajaxId != null) {
080                            response.setHeader("Ajax-ID", ajaxId);
081                    }
082    
083                    WindowState windowState = WindowStateFactory.getWindowState(
084                            ParamUtil.getString(request, "p_p_state"));
085    
086                    PortalUtil.updateWindowState(
087                            portletId, user, layout, windowState, request);
088    
089                    request = PortletContainerUtil.setupOptionalRenderParameters(
090                            request, null, columnId, columnPos, columnCount);
091    
092                    PortletContainerUtil.render(request, response, portlet);
093    
094                    if (themeDisplay.isIsolated() || themeDisplay.isStateExclusive()) {
095                            AUIUtil.outputScriptData(request, response.getWriter());
096                    }
097    
098                    return null;
099            }
100    
101    }