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.model.Portlet;
18  import com.liferay.portal.util.comparator.PortletRenderWeightComparator;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.TreeMap;
23  
24  import javax.servlet.ServletContext;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  /**
29   * <a href="TemplateProcessor.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Ivica Cardic
32   * @author Brian Wing Shun Chan
33   */
34  public class TemplateProcessor {
35  
36      public TemplateProcessor(
37          ServletContext servletContext, HttpServletRequest request,
38          HttpServletResponse response, String portletId) {
39  
40          _servletContext = servletContext;
41          _request = request;
42          _response = response;
43          _portletId = portletId;
44          _columnsMap = new HashMap<String, String>();
45          _portletsMap = new TreeMap<Portlet, Object[]>(
46              new PortletRenderWeightComparator());
47      }
48  
49      public String processColumn(String columnId) throws Exception {
50          Map<String, String> attributes = new HashMap<String, String>();
51  
52          attributes.put("id", columnId);
53  
54          PortletColumnLogic logic = new PortletColumnLogic(
55              _servletContext, _request, _response);
56  
57          StringBuilder sb = new StringBuilder();
58  
59          logic.processContent(sb, attributes);
60  
61          _portletsMap.putAll(logic.getPortletsMap());
62  
63          String columnIdPlaceHolder = "[$TEMPLATE_COLUMN_" + columnId + "$]";
64  
65          _columnsMap.put(columnIdPlaceHolder, sb.toString());
66  
67          return columnIdPlaceHolder;
68      }
69  
70      public String processMax() throws Exception {
71          RuntimeLogic logic = new PortletLogic(
72              _servletContext, _request, _response, _portletId);
73  
74          StringBuilder sb = new StringBuilder();
75  
76          logic.processContent(sb, new HashMap<String, String>());
77  
78          return sb.toString();
79      }
80  
81      public String processPortlet(String portletId) throws Exception {
82          RuntimeLogic logic = new PortletLogic(
83              _servletContext, _request, _response, portletId);
84  
85          StringBuilder sb = new StringBuilder();
86  
87          logic.processContent(sb, new HashMap<String, String>());
88  
89          return sb.toString();
90      }
91  
92      public Map<String, String> getColumnsMap() {
93          return _columnsMap;
94      }
95  
96      public Map<Portlet, Object[]> getPortletsMap() {
97          return _portletsMap;
98      }
99  
100     private ServletContext _servletContext;
101     private HttpServletRequest _request;
102     private HttpServletResponse _response;
103     private String _portletId;
104     private Map<String, String> _columnsMap;
105     private Map<Portlet, Object[]> _portletsMap;
106 
107 }