001    /**
002     * Copyright (c) 2000-2010 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            public String processContent(Map<String, String> attributes)
058                    throws Exception {
059    
060                    String rootPortletId = attributes.get("name");
061                    String instanceId = attributes.get("instance");
062                    String queryString = attributes.get("queryString");
063    
064                    String portletId = _portletId;
065    
066                    if (portletId == null) {
067                            portletId = rootPortletId;
068    
069                            if (Validator.isNotNull(instanceId)) {
070                                    portletId += PortletConstants.INSTANCE_SEPARATOR + instanceId;
071                            }
072                    }
073    
074                    return RuntimePortletUtil.processPortlet(
075                            _servletContext, _request, _response, _renderRequest,
076                            _renderResponse, portletId, queryString, false);
077            }
078    
079            private ServletContext _servletContext;
080            private HttpServletRequest _request;
081            private HttpServletResponse _response;
082            private RenderRequest _renderRequest;
083            private RenderResponse _renderResponse;
084            private String _portletId;
085    
086    }