1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.kernel.util.Validator;
21  import com.liferay.portal.model.LayoutTypePortlet;
22  import com.liferay.portal.model.Portlet;
23  import com.liferay.portal.theme.ThemeDisplay;
24  import com.liferay.portal.util.PropsValues;
25  import com.liferay.portal.util.WebKeys;
26  import com.liferay.portal.util.comparator.PortletRenderWeightComparator;
27  import com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil;
28  
29  import java.util.List;
30  import java.util.Map;
31  import java.util.TreeMap;
32  
33  import javax.servlet.ServletContext;
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  
37  /**
38   * <a href="PortletColumnLogic.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Ivica Cardic
41   * @author Brian Wing Shun Chan
42   */
43  public class PortletColumnLogic extends RuntimeLogic {
44  
45      public PortletColumnLogic(
46          ServletContext servletContext, HttpServletRequest request,
47          HttpServletResponse response) {
48  
49          _servletContext = servletContext;
50          _request = request;
51          _response = response;
52          _themeDisplay = (ThemeDisplay)_request.getAttribute(
53              WebKeys.THEME_DISPLAY);
54          _portletsMap = new TreeMap<Portlet, Object[]>(
55              new PortletRenderWeightComparator());
56  
57          _parallelRenderEnable = PropsValues.LAYOUT_PARALLEL_RENDER_ENABLE;
58  
59          if (_parallelRenderEnable) {
60              if (PropsValues.SESSION_DISABLED) {
61                  if (_log.isWarnEnabled()) {
62                      _log.warn(
63                          "Parallel rendering should be disabled if sessions " +
64                              "are disabled");
65                  }
66              }
67          }
68  
69          if (_parallelRenderEnable) {
70              Boolean portletParallelRender =
71                  (Boolean)request.getAttribute(WebKeys.PORTLET_PARALLEL_RENDER);
72  
73              if ((portletParallelRender != null) &&
74                  (portletParallelRender.booleanValue() == false)) {
75  
76                  _parallelRenderEnable = false;
77              }
78          }
79          else {
80              request.removeAttribute(WebKeys.PORTLET_PARALLEL_RENDER);
81          }
82      }
83  
84      public String processContent(Map<String, String> attributes)
85          throws Exception {
86  
87          LayoutTypePortlet layoutTypePortlet =
88              _themeDisplay.getLayoutTypePortlet();
89  
90          String columnId = attributes.get("id");
91  
92          List<Portlet> portlets = layoutTypePortlet.getAllPortlets(columnId);
93  
94          String columnCssClass = "portlet-dropzone";
95  
96          if (portlets.size() == 0) {
97              columnCssClass += " empty";
98          }
99  
100         String additionalClassNames = attributes.get("classNames");
101 
102         if (Validator.isNotNull(additionalClassNames)) {
103             columnCssClass += " " + additionalClassNames;
104         }
105 
106         StringBundler sb = new StringBundler();
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             String content = RuntimePortletUtil.processPortlet(
135                 _servletContext, _request, _response, portlet, queryString,
136                 columnId, columnPos, columnCount, path, false);
137 
138             sb.append(content);
139         }
140 
141         sb.append("</div>");
142 
143         return sb.toString();
144     }
145 
146     public Map<Portlet, Object[]> getPortletsMap() {
147         return _portletsMap;
148     }
149 
150     private static Log _log = LogFactoryUtil.getLog(PortletColumnLogic.class);
151 
152     private ServletContext _servletContext;
153     private HttpServletRequest _request;
154     private HttpServletResponse _response;
155     private ThemeDisplay _themeDisplay;
156     private Map<Portlet, Object[]> _portletsMap;
157     private boolean _parallelRenderEnable;
158 
159 }