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.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    
028    import javax.portlet.WindowState;
029    
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                    String ajaxId = request.getParameter("ajax_id");
055    
056                    long companyId = PortalUtil.getCompanyId(request);
057                    User user = PortalUtil.getUser(request);
058                    Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
059                    String portletId = ParamUtil.getString(request, "p_p_id");
060    
061                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
062                            companyId, portletId);
063    
064                    String columnId = ParamUtil.getString(request, "p_p_col_id");
065                    int columnPos = ParamUtil.getInteger(request, "p_p_col_pos");
066                    int columnCount = ParamUtil.getInteger(request, "p_p_col_count");
067                    boolean staticPortlet = ParamUtil.getBoolean(request, "p_p_static");
068                    boolean staticStartPortlet = ParamUtil.getBoolean(
069                            request, "p_p_static_start");
070    
071                    if (staticPortlet) {
072                            portlet = (Portlet)portlet.clone();
073    
074                            portlet.setStatic(true);
075                            portlet.setStaticStart(staticStartPortlet);
076                    }
077    
078                    if (ajaxId != null) {
079                            response.setHeader("Ajax-ID", ajaxId);
080                    }
081    
082                    WindowState windowState = WindowStateFactory.getWindowState(
083                            ParamUtil.getString(request, "p_p_state"));
084    
085                    PortalUtil.updateWindowState(
086                            portletId, user, layout, windowState, request);
087    
088                    request = PortletContainerUtil.setupOptionalRenderParameters(
089                            request, null, columnId, columnPos, columnCount);
090    
091                    PortletContainerUtil.render(request, response, portlet);
092    
093                    return null;
094            }
095    
096    }