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.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 implements ColumnProcessor {
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 Map<Portlet, Object[]> getPortletsMap() {
050                    return _portletsMap;
051            }
052    
053            public String processColumn(String columnId) throws Exception {
054                    return processColumn(columnId, StringPool.BLANK);
055            }
056    
057            public String processColumn(String columnId, String classNames)
058                    throws Exception {
059    
060                    Map<String, String> attributes = new HashMap<String, String>();
061    
062                    attributes.put("id", columnId);
063                    attributes.put("classNames", classNames);
064    
065                    PortletColumnLogic logic = new PortletColumnLogic(
066                            _servletContext, _request, _response);
067    
068                    String content = logic.processContent(attributes);
069    
070                    _portletsMap.putAll(logic.getPortletsMap());
071    
072                    return content;
073            }
074    
075            public String processMax() throws Exception {
076                    return processMax(StringPool.BLANK);
077            }
078    
079            public String processMax(String classNames) throws Exception {
080                    Map<String, String> attributes = new HashMap<String, String>();
081    
082                    attributes.put("classNames", classNames);
083    
084                    RuntimeLogic logic = new PortletLogic(
085                            _servletContext, _request, _response, _portletId);
086    
087                    return logic.processContent(attributes);
088            }
089    
090            public String processPortlet(String portletId) throws Exception {
091                    try {
092                            _request.setAttribute(
093                                    WebKeys.RENDER_PORTLET_RESOURCE, Boolean.TRUE);
094    
095                            RuntimeLogic logic = new PortletLogic(
096                                    _servletContext, _request, _response, portletId);
097    
098                            return logic.processContent(new HashMap<String, String>());
099                    }
100                    finally {
101                            _request.removeAttribute(WebKeys.RENDER_PORTLET_RESOURCE);
102                    }
103            }
104    
105            private String _portletId;
106            private Map<Portlet, Object[]> _portletsMap;
107            private HttpServletRequest _request;
108            private HttpServletResponse _response;
109            private ServletContext _servletContext;
110    
111    }