1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PortletColumnLogic extends RuntimeLogic {
50  
51      public PortletColumnLogic(
52          ServletContext servletContext, HttpServletRequest request,
53          HttpServletResponse response) {
54  
55          _servletContext = servletContext;
56          _request = request;
57          _response = response;
58          _themeDisplay = (ThemeDisplay)_request.getAttribute(
59              WebKeys.THEME_DISPLAY);
60          _portletsMap = new TreeMap<Portlet, Object[]>(
61              new PortletRenderWeightComparator());
62  
63          _parallelRenderEnable = PropsValues.LAYOUT_PARALLEL_RENDER_ENABLE;
64  
65          if (_parallelRenderEnable) {
66              if (PropsValues.SESSION_DISABLED) {
67                  if (_log.isWarnEnabled()) {
68                      _log.warn(
69                          "Parallel rendering should be disabled if sessions " +
70                              "are disabled");
71                  }
72              }
73          }
74  
75          if (_parallelRenderEnable) {
76              Boolean portletParallelRender =
77                  (Boolean)request.getAttribute(WebKeys.PORTLET_PARALLEL_RENDER);
78  
79              if ((portletParallelRender != null) &&
80                  (portletParallelRender.booleanValue() == false)) {
81  
82                  _parallelRenderEnable = false;
83              }
84          }
85          else {
86              request.removeAttribute(WebKeys.PORTLET_PARALLEL_RENDER);
87          }
88      }
89  
90      public void processContent(StringBuilder sb, Map<String, String> attributes)
91          throws Exception {
92  
93          LayoutTypePortlet layoutTypePortlet =
94              _themeDisplay.getLayoutTypePortlet();
95  
96          String columnId = attributes.get("id");
97  
98          List<Portlet> portlets = layoutTypePortlet.getAllPortlets(columnId);
99  
100         String columnCssClass = "lfr-portlet-column";
101 
102         if (portlets.size() == 0) {
103             columnCssClass += " empty";
104         }
105 
106         sb.append("<div class=\"");
107         sb.append(columnCssClass);
108         sb.append("\" id=\"layout-column_");
109         sb.append(columnId);
110         sb.append("\">");
111 
112         for (int i = 0; i < portlets.size(); i++) {
113             Portlet portlet = portlets.get(i);
114 
115             String queryString = null;
116             Integer columnPos = new Integer(i);
117             Integer columnCount = new Integer(portlets.size());
118             String path = null;
119 
120             if (_parallelRenderEnable) {
121                 path = "/html/portal/load_render_portlet.jsp";
122 
123                 if (portlet.getRenderWeight() >= 1) {
124                     _portletsMap.put(
125                         portlet,
126                         new Object[] {
127                             queryString, columnId, columnPos, columnCount
128                         });
129                 }
130             }
131 
132             RuntimePortletUtil.processPortlet(
133                 sb, _servletContext, _request, _response, portlet, queryString,
134                 columnId, columnPos, columnCount, path);
135         }
136 
137         sb.append("</div>");
138     }
139 
140     public Map<Portlet, Object[]> getPortletsMap() {
141         return _portletsMap;
142     }
143 
144     private static Log _log = LogFactoryUtil.getLog(PortletColumnLogic.class);
145 
146     private ServletContext _servletContext;
147     private HttpServletRequest _request;
148     private HttpServletResponse _response;
149     private ThemeDisplay _themeDisplay;
150     private Map<Portlet, Object[]> _portletsMap;
151     private boolean _parallelRenderEnable;
152 
153 }