1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.layoutconfiguration.util.velocity;
16  
17  import com.liferay.portal.kernel.util.StringPool;
18  import com.liferay.portal.model.Portlet;
19  import com.liferay.portal.util.comparator.PortletRenderWeightComparator;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  import java.util.TreeMap;
24  
25  import javax.servlet.ServletContext;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  /**
30   * <a href="TemplateProcessor.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Ivica Cardic
33   * @author Brian Wing Shun Chan
34   */
35  public class TemplateProcessor {
36  
37      public TemplateProcessor(
38          ServletContext servletContext, HttpServletRequest request,
39          HttpServletResponse response, String portletId) {
40  
41          _servletContext = servletContext;
42          _request = request;
43          _response = response;
44          _portletId = portletId;
45          _columnsMap = new HashMap<String, String>();
46          _portletsMap = new TreeMap<Portlet, Object[]>(
47              new PortletRenderWeightComparator());
48      }
49  
50      public String processColumn(String columnId) throws Exception {
51          return processColumn(columnId, StringPool.BLANK);
52      }
53  
54      public String processColumn(String columnId, String classNames)
55          throws Exception {
56  
57          Map<String, String> attributes = new HashMap<String, String>();
58  
59          attributes.put("id", columnId);
60          attributes.put("classNames", classNames);
61  
62          PortletColumnLogic logic = new PortletColumnLogic(
63              _servletContext, _request, _response);
64  
65          String content = logic.processContent(attributes);
66  
67          _portletsMap.putAll(logic.getPortletsMap());
68  
69          String columnIdPlaceHolder = "[$TEMPLATE_COLUMN_" + columnId + "$]";
70  
71          _columnsMap.put(columnIdPlaceHolder, content);
72  
73          return columnIdPlaceHolder;
74      }
75  
76      public String processMax() throws Exception {
77          return processMax(StringPool.BLANK);
78      }
79  
80      public String processMax(String classNames) throws Exception {
81          Map<String, String> attributes = new HashMap<String, String>();
82  
83          attributes.put("classNames", classNames);
84  
85          RuntimeLogic logic = new PortletLogic(
86              _servletContext, _request, _response, _portletId);
87  
88          return logic.processContent(attributes);
89      }
90  
91      public String processPortlet(String portletId) throws Exception {
92          RuntimeLogic logic = new PortletLogic(
93              _servletContext, _request, _response, portletId);
94  
95          return logic.processContent(new HashMap<String, String>());
96      }
97  
98      public Map<String, String> getColumnsMap() {
99          return _columnsMap;
100     }
101 
102     public Map<Portlet, Object[]> getPortletsMap() {
103         return _portletsMap;
104     }
105 
106     private ServletContext _servletContext;
107     private HttpServletRequest _request;
108     private HttpServletResponse _response;
109     private String _portletId;
110     private Map<String, String> _columnsMap;
111     private Map<Portlet, Object[]> _portletsMap;
112 
113 }