001    /**
002     * Copyright (c) 2000-2012 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            @Override
083            public String processContent(Map<String, String> attributes)
084                    throws Exception {
085    
086                    LayoutTypePortlet layoutTypePortlet =
087                            _themeDisplay.getLayoutTypePortlet();
088    
089                    String columnId = attributes.get("id");
090    
091                    List<Portlet> portlets = layoutTypePortlet.getAllPortlets(columnId);
092    
093                    String columnCssClass = "portlet-dropzone";
094    
095                    if (layoutTypePortlet.isCustomizable() &&
096                            layoutTypePortlet.isColumnDisabled(columnId)) {
097    
098                            columnCssClass += " portlet-dropzone-disabled";
099                    }
100    
101                    if (layoutTypePortlet.isTemplateCustomizable(columnId) &&
102                            layoutTypePortlet.isColumnCustomizable(columnId)) {
103    
104                            columnCssClass += " customizable";
105                    }
106    
107                    if (portlets.size() == 0) {
108                            columnCssClass += " empty";
109                    }
110    
111                    String additionalClassNames = attributes.get("classNames");
112    
113                    if (Validator.isNotNull(additionalClassNames)) {
114                            columnCssClass += " " + additionalClassNames;
115                    }
116    
117                    StringBundler sb = new StringBundler();
118    
119                    sb.append("<div class=\"");
120                    sb.append(columnCssClass);
121                    sb.append("\" id=\"layout-column_");
122                    sb.append(columnId);
123                    sb.append("\">");
124    
125                    for (int i = 0; i < portlets.size(); i++) {
126                            Portlet portlet = portlets.get(i);
127    
128                            String queryString = null;
129                            Integer columnPos = new Integer(i);
130                            Integer columnCount = new Integer(portlets.size());
131                            String path = null;
132    
133                            if (_parallelRenderEnable) {
134                                    path = "/html/portal/load_render_portlet.jsp";
135    
136                                    if (portlet.getRenderWeight() >= 1) {
137                                            _portletsMap.put(
138                                                    portlet,
139                                                    new Object[] {
140                                                            queryString, columnId, columnPos, columnCount
141                                                    });
142                                    }
143                            }
144    
145                            String content = RuntimePortletUtil.processPortlet(
146                                    _servletContext, _request, _response, portlet, queryString,
147                                    columnId, columnPos, columnCount, path, false);
148    
149                            sb.append(content);
150                    }
151    
152                    sb.append("</div>");
153    
154                    return sb.toString();
155            }
156    
157            public Map<Portlet, Object[]> getPortletsMap() {
158                    return _portletsMap;
159            }
160    
161            private static Log _log = LogFactoryUtil.getLog(PortletColumnLogic.class);
162    
163            private ServletContext _servletContext;
164            private HttpServletRequest _request;
165            private HttpServletResponse _response;
166            private ThemeDisplay _themeDisplay;
167            private Map<Portlet, Object[]> _portletsMap;
168            private boolean _parallelRenderEnable;
169    
170    }