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