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