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