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