1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.kernel.util.StringBundler;
20  import com.liferay.portal.model.LayoutTypePortlet;
21  import com.liferay.portal.model.Portlet;
22  import com.liferay.portal.theme.ThemeDisplay;
23  import com.liferay.portal.util.PropsValues;
24  import com.liferay.portal.util.WebKeys;
25  import com.liferay.portal.util.comparator.PortletRenderWeightComparator;
26  import com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil;
27  
28  import java.util.List;
29  import java.util.Map;
30  import java.util.TreeMap;
31  
32  import javax.servlet.ServletContext;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  /**
37   * <a href="PortletColumnLogic.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Ivica Cardic
40   * @author Brian Wing Shun Chan
41   */
42  public class PortletColumnLogic extends RuntimeLogic {
43  
44      public PortletColumnLogic(
45          ServletContext servletContext, HttpServletRequest request,
46          HttpServletResponse response) {
47  
48          _servletContext = servletContext;
49          _request = request;
50          _response = response;
51          _themeDisplay = (ThemeDisplay)_request.getAttribute(
52              WebKeys.THEME_DISPLAY);
53          _portletsMap = new TreeMap<Portlet, Object[]>(
54              new PortletRenderWeightComparator());
55  
56          _parallelRenderEnable = PropsValues.LAYOUT_PARALLEL_RENDER_ENABLE;
57  
58          if (_parallelRenderEnable) {
59              if (PropsValues.SESSION_DISABLED) {
60                  if (_log.isWarnEnabled()) {
61                      _log.warn(
62                          "Parallel rendering should be disabled if sessions " +
63                              "are disabled");
64                  }
65              }
66          }
67  
68          if (_parallelRenderEnable) {
69              Boolean portletParallelRender =
70                  (Boolean)request.getAttribute(WebKeys.PORTLET_PARALLEL_RENDER);
71  
72              if ((portletParallelRender != null) &&
73                  (portletParallelRender.booleanValue() == false)) {
74  
75                  _parallelRenderEnable = false;
76              }
77          }
78          else {
79              request.removeAttribute(WebKeys.PORTLET_PARALLEL_RENDER);
80          }
81      }
82  
83      public String processContent(Map<String, String> attributes)
84          throws Exception {
85  
86          LayoutTypePortlet layoutTypePortlet =
87              _themeDisplay.getLayoutTypePortlet();
88  
89          String columnId = attributes.get("id");
90  
91          List<Portlet> portlets = layoutTypePortlet.getAllPortlets(columnId);
92  
93          String columnCssClass = "lfr-portlet-column";
94  
95          if (portlets.size() == 0) {
96              columnCssClass += " empty";
97          }
98  
99          StringBundler sb = new StringBundler();
100 
101         sb.append("<div class=\"");
102         sb.append(columnCssClass);
103         sb.append("\" id=\"layout-column_");
104         sb.append(columnId);
105         sb.append("\">");
106 
107         for (int i = 0; i < portlets.size(); i++) {
108             Portlet portlet = portlets.get(i);
109 
110             String queryString = null;
111             Integer columnPos = new Integer(i);
112             Integer columnCount = new Integer(portlets.size());
113             String path = null;
114 
115             if (_parallelRenderEnable) {
116                 path = "/html/portal/load_render_portlet.jsp";
117 
118                 if (portlet.getRenderWeight() >= 1) {
119                     _portletsMap.put(
120                         portlet,
121                         new Object[] {
122                             queryString, columnId, columnPos, columnCount
123                         });
124                 }
125             }
126 
127             String content = RuntimePortletUtil.processPortlet(
128                 _servletContext, _request, _response, portlet, queryString,
129                 columnId, columnPos, columnCount, path);
130 
131             sb.append(content);
132         }
133 
134         sb.append("</div>");
135 
136         return sb.toString();
137     }
138 
139     public Map<Portlet, Object[]> getPortletsMap() {
140         return _portletsMap;
141     }
142 
143     private static Log _log = LogFactoryUtil.getLog(PortletColumnLogic.class);
144 
145     private ServletContext _servletContext;
146     private HttpServletRequest _request;
147     private HttpServletResponse _response;
148     private ThemeDisplay _themeDisplay;
149     private Map<Portlet, Object[]> _portletsMap;
150     private boolean _parallelRenderEnable;
151 
152 }