1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.layoutconfiguration.util.velocity;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.StringMaker;
27  import com.liferay.portal.model.LayoutTypePortlet;
28  import com.liferay.portal.model.Portlet;
29  import com.liferay.portal.theme.ThemeDisplay;
30  import com.liferay.portal.util.PropsUtil;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portal.util.comparator.PortletRenderWeightComparator;
33  import com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil;
34  
35  import java.util.List;
36  import java.util.Map;
37  import java.util.TreeMap;
38  
39  import javax.servlet.ServletContext;
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  
43  import org.apache.commons.logging.Log;
44  import org.apache.commons.logging.LogFactory;
45  
46  /**
47   * <a href="PortletColumnLogic.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Ivica Cardic
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class PortletColumnLogic extends RuntimeLogic {
54  
55      public PortletColumnLogic(ServletContext ctx, HttpServletRequest req,
56                                HttpServletResponse res) {
57  
58          _ctx = ctx;
59          _req = req;
60          _res = res;
61          _themeDisplay = (ThemeDisplay)_req.getAttribute(WebKeys.THEME_DISPLAY);
62          _portletsMap = new TreeMap(new PortletRenderWeightComparator());
63  
64          _parallelRenderEnable = GetterUtil.getBoolean(PropsUtil.get(
65              PropsUtil.LAYOUT_PARALLEL_RENDER_ENABLE));
66  
67          if (_parallelRenderEnable) {
68              boolean sessionDisabled = GetterUtil.getBoolean(
69                  PropsUtil.get(PropsUtil.SESSION_DISABLED));
70  
71              if (sessionDisabled) {
72                  if (_log.isWarnEnabled()) {
73                      _log.warn(
74                          "Parallel rendering should be disabled if sessions " +
75                              "are disabled");
76                  }
77              }
78          }
79  
80          if (_parallelRenderEnable) {
81              Boolean portletParallelRender =
82                  (Boolean)req.getAttribute(WebKeys.PORTLET_PARALLEL_RENDER);
83  
84              if ((portletParallelRender != null) &&
85                  (portletParallelRender.booleanValue() == false)) {
86  
87                  _parallelRenderEnable = false;
88              }
89          }
90          else {
91              req.removeAttribute(WebKeys.PORTLET_PARALLEL_RENDER);
92          }
93      }
94  
95      public void processContent(StringMaker sm, Map attributes)
96          throws Exception {
97  
98          LayoutTypePortlet layoutTypePortlet =
99              _themeDisplay.getLayoutTypePortlet();
100 
101         String columnId = (String)attributes.get("id");
102 
103         List portlets = layoutTypePortlet.getAllPortlets(columnId);
104 
105         String columnCssClass = "lfr-portlet-column";
106 
107         if (portlets.size() == 0) {
108             columnCssClass += " empty";
109         }
110 
111         sm.append("<div class=\"");
112         sm.append(columnCssClass);
113         sm.append("\" id=\"layout-column_");
114         sm.append(columnId);
115         sm.append("\">");
116 
117         for (int i = 0; i < portlets.size(); i++) {
118             Portlet portlet = (Portlet)portlets.get(i);
119 
120             String queryString = null;
121             Integer columnPos = new Integer(i);
122             Integer columnCount = new Integer(portlets.size());
123             String path = null;
124 
125             if (_parallelRenderEnable) {
126                 path = "/html/portal/load_render_portlet.jsp";
127 
128                 if (portlet.getRenderWeight() >= 1) {
129                     _portletsMap.put(
130                         portlet,
131                         new Object[] {
132                             queryString, columnId, columnPos, columnCount
133                         });
134                 }
135             }
136 
137             RuntimePortletUtil.processPortlet(
138                 sm, _ctx, _req, _res, portlet, queryString, columnId, columnPos,
139                 columnCount, path);
140         }
141 
142         sm.append("</div>");
143     }
144 
145     public Map getPortletsMap() {
146         return _portletsMap;
147     }
148 
149     private static Log _log = LogFactory.getLog(PortletColumnLogic.class);
150 
151     private ServletContext _ctx;
152     private HttpServletRequest _req;
153     private HttpServletResponse _res;
154     private ThemeDisplay _themeDisplay;
155     private Map _portletsMap;
156     private boolean _parallelRenderEnable;
157 
158 }