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.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.model.LayoutTypePortlet;
20  import com.liferay.portal.model.Portlet;
21  import com.liferay.portal.theme.ThemeDisplay;
22  import com.liferay.portal.util.PropsValues;
23  import com.liferay.portal.util.WebKeys;
24  import com.liferay.portal.util.comparator.PortletRenderWeightComparator;
25  import com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil;
26  
27  import java.util.List;
28  import java.util.Map;
29  import java.util.TreeMap;
30  
31  import javax.servlet.ServletContext;
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  
35  /**
36   * <a href="PortletColumnLogic.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Ivica Cardic
39   * @author Brian Wing Shun Chan
40   */
41  public class PortletColumnLogic extends RuntimeLogic {
42  
43      public PortletColumnLogic(
44          ServletContext servletContext, HttpServletRequest request,
45          HttpServletResponse response) {
46  
47          _servletContext = servletContext;
48          _request = request;
49          _response = response;
50          _themeDisplay = (ThemeDisplay)_request.getAttribute(
51              WebKeys.THEME_DISPLAY);
52          _portletsMap = new TreeMap<Portlet, Object[]>(
53              new PortletRenderWeightComparator());
54  
55          _parallelRenderEnable = PropsValues.LAYOUT_PARALLEL_RENDER_ENABLE;
56  
57          if (_parallelRenderEnable) {
58              if (PropsValues.SESSION_DISABLED) {
59                  if (_log.isWarnEnabled()) {
60                      _log.warn(
61                          "Parallel rendering should be disabled if sessions " +
62                              "are disabled");
63                  }
64              }
65          }
66  
67          if (_parallelRenderEnable) {
68              Boolean portletParallelRender =
69                  (Boolean)request.getAttribute(WebKeys.PORTLET_PARALLEL_RENDER);
70  
71              if ((portletParallelRender != null) &&
72                  (portletParallelRender.booleanValue() == false)) {
73  
74                  _parallelRenderEnable = false;
75              }
76          }
77          else {
78              request.removeAttribute(WebKeys.PORTLET_PARALLEL_RENDER);
79          }
80      }
81  
82      public void processContent(StringBuilder sb, Map<String, String> attributes)
83          throws Exception {
84  
85          LayoutTypePortlet layoutTypePortlet =
86              _themeDisplay.getLayoutTypePortlet();
87  
88          String columnId = attributes.get("id");
89  
90          List<Portlet> portlets = layoutTypePortlet.getAllPortlets(columnId);
91  
92          String columnCssClass = "lfr-portlet-column";
93  
94          if (portlets.size() == 0) {
95              columnCssClass += " empty";
96          }
97  
98          sb.append("<div class=\"");
99          sb.append(columnCssClass);
100         sb.append("\" id=\"layout-column_");
101         sb.append(columnId);
102         sb.append("\">");
103 
104         for (int i = 0; i < portlets.size(); i++) {
105             Portlet portlet = portlets.get(i);
106 
107             String queryString = null;
108             Integer columnPos = new Integer(i);
109             Integer columnCount = new Integer(portlets.size());
110             String path = null;
111 
112             if (_parallelRenderEnable) {
113                 path = "/html/portal/load_render_portlet.jsp";
114 
115                 if (portlet.getRenderWeight() >= 1) {
116                     _portletsMap.put(
117                         portlet,
118                         new Object[] {
119                             queryString, columnId, columnPos, columnCount
120                         });
121                 }
122             }
123 
124             RuntimePortletUtil.processPortlet(
125                 sb, _servletContext, _request, _response, portlet, queryString,
126                 columnId, columnPos, columnCount, path);
127         }
128 
129         sb.append("</div>");
130     }
131 
132     public Map<Portlet, Object[]> getPortletsMap() {
133         return _portletsMap;
134     }
135 
136     private static Log _log = LogFactoryUtil.getLog(PortletColumnLogic.class);
137 
138     private ServletContext _servletContext;
139     private HttpServletRequest _request;
140     private HttpServletResponse _response;
141     private ThemeDisplay _themeDisplay;
142     private Map<Portlet, Object[]> _portletsMap;
143     private boolean _parallelRenderEnable;
144 
145 }