001    /**
002     * Copyright (c) 2000-2013 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.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.LayoutTypePortlet;
023    import com.liferay.portal.model.Portlet;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.PropsValues;
026    import com.liferay.portal.util.WebKeys;
027    import com.liferay.portal.util.comparator.PortletRenderWeightComparator;
028    import com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil;
029    
030    import java.util.List;
031    import java.util.Map;
032    import java.util.TreeMap;
033    
034    import javax.servlet.ServletContext;
035    import javax.servlet.http.HttpServletRequest;
036    import javax.servlet.http.HttpServletResponse;
037    
038    /**
039     * @author Ivica Cardic
040     * @author Brian Wing Shun Chan
041     * @author Shuyang Zhou
042     */
043    public class TemplateProcessor implements ColumnProcessor {
044    
045            public TemplateProcessor(
046                    ServletContext servletContext, HttpServletRequest request,
047                    HttpServletResponse response, String portletId) {
048    
049                    _servletContext = servletContext;
050                    _request = request;
051                    _response = response;
052                    _portletId = portletId;
053                    _portletsMap = new TreeMap<Portlet, Object[]>(
054                            new PortletRenderWeightComparator());
055            }
056    
057            public Map<Portlet, Object[]> getPortletsMap() {
058                    return _portletsMap;
059            }
060    
061            @Override
062            public String processColumn(String columnId) throws Exception {
063                    return processColumn(columnId, StringPool.BLANK);
064            }
065    
066            @Override
067            public String processColumn(String columnId, String classNames)
068                    throws Exception {
069    
070                    boolean parallelRenderEnable =
071                            PropsValues.LAYOUT_PARALLEL_RENDER_ENABLE;
072    
073                    if (parallelRenderEnable) {
074                            if (PropsValues.SESSION_DISABLED) {
075                                    if (_log.isWarnEnabled()) {
076                                            _log.warn(
077                                                    "Parallel rendering should be disabled if sessions " +
078                                                            "are disabled");
079                                    }
080                            }
081    
082                            Boolean portletParallelRender = (Boolean)_request.getAttribute(
083                                    WebKeys.PORTLET_PARALLEL_RENDER);
084    
085                            if (Boolean.FALSE.equals(portletParallelRender)) {
086                                    parallelRenderEnable = false;
087                            }
088                    }
089                    else {
090                            _request.removeAttribute(WebKeys.PORTLET_PARALLEL_RENDER);
091                    }
092    
093                    ThemeDisplay themeDisplay = (ThemeDisplay)_request.getAttribute(
094                            WebKeys.THEME_DISPLAY);
095    
096                    LayoutTypePortlet layoutTypePortlet =
097                            themeDisplay.getLayoutTypePortlet();
098    
099                    List<Portlet> portlets = layoutTypePortlet.getAllPortlets(columnId);
100    
101                    StringBundler sb = new StringBundler(portlets.size() + 11);
102    
103                    sb.append("<div class=\"");
104                    sb.append("portlet-dropzone");
105    
106                    if (layoutTypePortlet.isCustomizable() &&
107                            layoutTypePortlet.isColumnDisabled(columnId)) {
108    
109                            sb.append(" portlet-dropzone-disabled");
110                    }
111    
112                    if (layoutTypePortlet.isColumnCustomizable(columnId)) {
113                            sb.append(" customizable");
114                    }
115    
116                    if (portlets.isEmpty()) {
117                            sb.append(" empty");
118                    }
119    
120                    if (Validator.isNotNull(classNames)) {
121                            sb.append(" ");
122                            sb.append(classNames);
123                    }
124    
125                    sb.append("\" id=\"layout-column_");
126                    sb.append(columnId);
127                    sb.append("\">");
128    
129                    for (int i = 0; i < portlets.size(); i++) {
130                            Portlet portlet = portlets.get(i);
131    
132                            String queryString = null;
133                            Integer columnPos = new Integer(i);
134                            Integer columnCount = new Integer(portlets.size());
135                            String path = null;
136    
137                            if (parallelRenderEnable) {
138                                    path = "/html/portal/load_render_portlet.jsp";
139    
140                                    if (portlet.getRenderWeight() >= 1) {
141                                            _portletsMap.put(
142                                                    portlet,
143                                                    new Object[] {
144                                                            queryString, columnId, columnPos, columnCount
145                                                    });
146                                    }
147                            }
148    
149                            String content = RuntimePortletUtil.processPortlet(
150                                    _servletContext, _request, _response, portlet, queryString,
151                                    columnId, columnPos, columnCount, path, false);
152    
153                            sb.append(content);
154                    }
155    
156                    sb.append("</div>");
157    
158                    return sb.toString();
159            }
160    
161            @Override
162            public String processMax() throws Exception {
163                    return processMax(StringPool.BLANK);
164            }
165    
166            @Override
167            public String processMax(String classNames) throws Exception {
168                    return RuntimePortletUtil.processPortlet(
169                            _servletContext, _request, _response, null, null, _portletId, null,
170                            false);
171            }
172    
173            @Override
174            public String processPortlet(String portletId) throws Exception {
175                    try {
176                            _request.setAttribute(
177                                    WebKeys.RENDER_PORTLET_RESOURCE, Boolean.TRUE);
178    
179                            return RuntimePortletUtil.processPortlet(
180                                    _servletContext, _request, _response, null, null, portletId,
181                                    null, false);
182                    }
183                    finally {
184                            _request.removeAttribute(WebKeys.RENDER_PORTLET_RESOURCE);
185                    }
186            }
187    
188            private static Log _log = LogFactoryUtil.getLog(TemplateProcessor.class);
189    
190            private String _portletId;
191            private Map<Portlet, Object[]> _portletsMap;
192            private HttpServletRequest _request;
193            private HttpServletResponse _response;
194            private ServletContext _servletContext;
195    
196    }