001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.layoutconfiguration.util.velocity;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.LayoutTypePortlet;
022    import com.liferay.portal.model.Portlet;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portal.util.PropsValues;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portal.util.comparator.PortletRenderWeightComparator;
027    import com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil;
028    
029    import java.util.List;
030    import java.util.Map;
031    import java.util.TreeMap;
032    
033    import javax.servlet.ServletContext;
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.http.HttpServletResponse;
036    
037    /**
038     * @author Ivica Cardic
039     * @author Brian Wing Shun Chan
040     */
041    public class PortletColumnLogic extends RuntimeLogic {
042    
043            public PortletColumnLogic(
044                    ServletContext servletContext, HttpServletRequest request,
045                    HttpServletResponse response) {
046    
047                    _servletContext = servletContext;
048                    _request = request;
049                    _response = response;
050                    _themeDisplay = (ThemeDisplay)_request.getAttribute(
051                            WebKeys.THEME_DISPLAY);
052                    _portletsMap = new TreeMap<Portlet, Object[]>(
053                            new PortletRenderWeightComparator());
054    
055                    _parallelRenderEnable = PropsValues.LAYOUT_PARALLEL_RENDER_ENABLE;
056    
057                    if (_parallelRenderEnable) {
058                            if (PropsValues.SESSION_DISABLED) {
059                                    if (_log.isWarnEnabled()) {
060                                            _log.warn(
061                                                    "Parallel rendering should be disabled if sessions " +
062                                                            "are disabled");
063                                    }
064                            }
065                    }
066    
067                    if (_parallelRenderEnable) {
068                            Boolean portletParallelRender =
069                                    (Boolean)request.getAttribute(WebKeys.PORTLET_PARALLEL_RENDER);
070    
071                            if ((portletParallelRender != null) &&
072                                    (portletParallelRender.booleanValue() == false)) {
073    
074                                    _parallelRenderEnable = false;
075                            }
076                    }
077                    else {
078                            request.removeAttribute(WebKeys.PORTLET_PARALLEL_RENDER);
079                    }
080            }
081    
082            public String processContent(Map<String, String> attributes)
083                    throws Exception {
084    
085                    LayoutTypePortlet layoutTypePortlet =
086                            _themeDisplay.getLayoutTypePortlet();
087    
088                    String columnId = attributes.get("id");
089    
090                    List<Portlet> portlets = layoutTypePortlet.getAllPortlets(columnId);
091    
092                    String columnCssClass = "portlet-dropzone";
093    
094                    if (portlets.size() == 0) {
095                            columnCssClass += " empty";
096                    }
097    
098                    String additionalClassNames = attributes.get("classNames");
099    
100                    if (Validator.isNotNull(additionalClassNames)) {
101                            columnCssClass += " " + additionalClassNames;
102                    }
103    
104                    StringBundler sb = new StringBundler();
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                            String content = RuntimePortletUtil.processPortlet(
133                                    _servletContext, _request, _response, portlet, queryString,
134                                    columnId, columnPos, columnCount, path, false);
135    
136                            sb.append(content);
137                    }
138    
139                    sb.append("</div>");
140    
141                    return sb.toString();
142            }
143    
144            public Map<Portlet, Object[]> getPortletsMap() {
145                    return _portletsMap;
146            }
147    
148            private static Log _log = LogFactoryUtil.getLog(PortletColumnLogic.class);
149    
150            private ServletContext _servletContext;
151            private HttpServletRequest _request;
152            private HttpServletResponse _response;
153            private ThemeDisplay _themeDisplay;
154            private Map<Portlet, Object[]> _portletsMap;
155            private boolean _parallelRenderEnable;
156    
157    }