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.StringPool;
018    import com.liferay.portal.kernel.util.WebKeys;
019    import com.liferay.portal.model.Portlet;
020    import com.liferay.portal.util.comparator.PortletRenderWeightComparator;
021    
022    import java.util.HashMap;
023    import java.util.Map;
024    import java.util.TreeMap;
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     * @author Shuyang Zhou
034     */
035    public class TemplateProcessor {
036    
037            public TemplateProcessor(
038                    ServletContext servletContext, HttpServletRequest request,
039                    HttpServletResponse response, String portletId) {
040    
041                    _servletContext = servletContext;
042                    _request = request;
043                    _response = response;
044                    _portletId = portletId;
045                    _portletsMap = new TreeMap<Portlet, Object[]>(
046                            new PortletRenderWeightComparator());
047            }
048    
049            public String processColumn(String columnId) throws Exception {
050                    return processColumn(columnId, StringPool.BLANK);
051            }
052    
053            public String processColumn(String columnId, String classNames)
054                    throws Exception {
055    
056                    Map<String, String> attributes = new HashMap<String, String>();
057    
058                    attributes.put("id", columnId);
059                    attributes.put("classNames", classNames);
060    
061                    PortletColumnLogic logic = new PortletColumnLogic(
062                            _servletContext, _request, _response);
063    
064                    String content = logic.processContent(attributes);
065    
066                    _portletsMap.putAll(logic.getPortletsMap());
067    
068                    return content;
069            }
070    
071            public String processMax() throws Exception {
072                    return processMax(StringPool.BLANK);
073            }
074    
075            public String processMax(String classNames) throws Exception {
076                    Map<String, String> attributes = new HashMap<String, String>();
077    
078                    attributes.put("classNames", classNames);
079    
080                    RuntimeLogic logic = new PortletLogic(
081                            _servletContext, _request, _response, _portletId);
082    
083                    return logic.processContent(attributes);
084            }
085    
086            public String processPortlet(String portletId) throws Exception {
087                    try {
088                            _request.setAttribute(
089                                    WebKeys.RENDER_PORTLET_RESOURCE, Boolean.TRUE);
090    
091                            RuntimeLogic logic = new PortletLogic(
092                                    _servletContext, _request, _response, portletId);
093    
094                            return logic.processContent(new HashMap<String, String>());
095                    }
096                    finally {
097                            _request.removeAttribute(WebKeys.RENDER_PORTLET_RESOURCE);
098                    }
099            }
100    
101            public Map<Portlet, Object[]> getPortletsMap() {
102                    return _portletsMap;
103            }
104    
105            private ServletContext _servletContext;
106            private HttpServletRequest _request;
107            private HttpServletResponse _response;
108            private String _portletId;
109            private Map<Portlet, Object[]> _portletsMap;
110    
111    }