1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.layoutconfiguration.util.velocity;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.model.LayoutTypePortlet;
25  import com.liferay.portal.model.Portlet;
26  import com.liferay.portal.theme.ThemeDisplay;
27  import com.liferay.portal.util.PropsValues;
28  import com.liferay.portal.util.WebKeys;
29  import com.liferay.portal.util.comparator.PortletRenderWeightComparator;
30  import com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil;
31  
32  import java.util.List;
33  import java.util.Map;
34  import java.util.TreeMap;
35  
36  import javax.servlet.ServletContext;
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  
40  /**
41   * <a href="PortletColumnLogic.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Ivica Cardic
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class PortletColumnLogic extends RuntimeLogic {
48  
49      public PortletColumnLogic(
50          ServletContext servletContext, HttpServletRequest request,
51          HttpServletResponse response) {
52  
53          _servletContext = servletContext;
54          _request = request;
55          _response = response;
56          _themeDisplay = (ThemeDisplay)_request.getAttribute(
57              WebKeys.THEME_DISPLAY);
58          _portletsMap = new TreeMap<Portlet, Object[]>(
59              new PortletRenderWeightComparator());
60  
61          _parallelRenderEnable = PropsValues.LAYOUT_PARALLEL_RENDER_ENABLE;
62  
63          if (_parallelRenderEnable) {
64              if (PropsValues.SESSION_DISABLED) {
65                  if (_log.isWarnEnabled()) {
66                      _log.warn(
67                          "Parallel rendering should be disabled if sessions " +
68                              "are disabled");
69                  }
70              }
71          }
72  
73          if (_parallelRenderEnable) {
74              Boolean portletParallelRender =
75                  (Boolean)request.getAttribute(WebKeys.PORTLET_PARALLEL_RENDER);
76  
77              if ((portletParallelRender != null) &&
78                  (portletParallelRender.booleanValue() == false)) {
79  
80                  _parallelRenderEnable = false;
81              }
82          }
83          else {
84              request.removeAttribute(WebKeys.PORTLET_PARALLEL_RENDER);
85          }
86      }
87  
88      public void processContent(StringBuilder sb, Map<String, String> attributes)
89          throws Exception {
90  
91          LayoutTypePortlet layoutTypePortlet =
92              _themeDisplay.getLayoutTypePortlet();
93  
94          String columnId = attributes.get("id");
95  
96          List<Portlet> portlets = layoutTypePortlet.getAllPortlets(columnId);
97  
98          String columnCssClass = "lfr-portlet-column";
99  
100         if (portlets.size() == 0) {
101             columnCssClass += " empty";
102         }
103 
104         sb.append("<div class=\"");
105         sb.append(columnCssClass);
106         sb.append("\" id=\"layout-column_");
107         sb.append(columnId);
108         sb.append("\">");
109 
110         for (int i = 0; i < portlets.size(); i++) {
111             Portlet portlet = portlets.get(i);
112 
113             String queryString = null;
114             Integer columnPos = new Integer(i);
115             Integer columnCount = new Integer(portlets.size());
116             String path = null;
117 
118             if (_parallelRenderEnable) {
119                 path = "/html/portal/load_render_portlet.jsp";
120 
121                 if (portlet.getRenderWeight() >= 1) {
122                     _portletsMap.put(
123                         portlet,
124                         new Object[] {
125                             queryString, columnId, columnPos, columnCount
126                         });
127                 }
128             }
129 
130             RuntimePortletUtil.processPortlet(
131                 sb, _servletContext, _request, _response, portlet, queryString,
132                 columnId, columnPos, columnCount, path);
133         }
134 
135         sb.append("</div>");
136     }
137 
138     public Map<Portlet, Object[]> getPortletsMap() {
139         return _portletsMap;
140     }
141 
142     private static Log _log = LogFactoryUtil.getLog(PortletColumnLogic.class);
143 
144     private ServletContext _servletContext;
145     private HttpServletRequest _request;
146     private HttpServletResponse _response;
147     private ThemeDisplay _themeDisplay;
148     private Map<Portlet, Object[]> _portletsMap;
149     private boolean _parallelRenderEnable;
150 
151 }