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.portlet.layoutconfiguration.util.velocity;
016    
017    import com.liferay.portal.kernel.util.Validator;
018    import com.liferay.portal.model.PortletConstants;
019    import com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil;
020    
021    import java.util.Map;
022    
023    import javax.portlet.RenderRequest;
024    import javax.portlet.RenderResponse;
025    
026    import javax.servlet.ServletContext;
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    /**
031     * @author Ivica Cardic
032     * @author Brian Wing Shun Chan
033     */
034    public class PortletLogic extends RuntimeLogic {
035    
036            public PortletLogic(
037                    ServletContext servletContext, HttpServletRequest request,
038                    HttpServletResponse response, String portletId) {
039    
040                    this(servletContext, request, response, null, null);
041    
042                    _portletId = portletId;
043            }
044    
045            public PortletLogic(
046                    ServletContext servletContext, HttpServletRequest request,
047                    HttpServletResponse response, RenderRequest renderRequest,
048                    RenderResponse renderResponse) {
049    
050                    _servletContext = servletContext;
051                    _request = request;
052                    _response = response;
053                    _renderRequest = renderRequest;
054                    _renderResponse = renderResponse;
055            }
056    
057            @Override
058            public String processContent(Map<String, String> attributes)
059                    throws Exception {
060    
061                    String rootPortletId = attributes.get("name");
062                    String instanceId = attributes.get("instance");
063                    String queryString = attributes.get("queryString");
064    
065                    String portletId = _portletId;
066    
067                    if (portletId == null) {
068                            portletId = rootPortletId;
069    
070                            if (Validator.isNotNull(instanceId)) {
071                                    portletId += PortletConstants.INSTANCE_SEPARATOR + instanceId;
072                            }
073                    }
074    
075                    return RuntimePortletUtil.processPortlet(
076                            _servletContext, _request, _response, _renderRequest,
077                            _renderResponse, portletId, queryString, false);
078            }
079    
080            private String _portletId;
081            private RenderRequest _renderRequest;
082            private RenderResponse _renderResponse;
083            private HttpServletRequest _request;
084            private HttpServletResponse _response;
085            private ServletContext _servletContext;
086    
087    }