001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public String processColumn(String columnId) throws Exception {
062                    return processColumn(columnId, StringPool.BLANK);
063            }
064    
065            public String processColumn(String columnId, String classNames)
066                    throws Exception {
067    
068                    boolean parallelRenderEnable =
069                            PropsValues.LAYOUT_PARALLEL_RENDER_ENABLE;
070    
071                    if (parallelRenderEnable) {
072                            if (PropsValues.SESSION_DISABLED) {
073                                    if (_log.isWarnEnabled()) {
074                                            _log.warn(
075                                                    "Parallel rendering should be disabled if sessions " +
076                                                            "are disabled");
077                                    }
078                            }
079    
080                            Boolean portletParallelRender =
081                                    (Boolean)_request.getAttribute(WebKeys.PORTLET_PARALLEL_RENDER);
082    
083                            if (Boolean.FALSE.equals(portletParallelRender)) {
084    
085                                    parallelRenderEnable = false;
086                            }
087                    }
088                    else {
089                            _request.removeAttribute(WebKeys.PORTLET_PARALLEL_RENDER);
090                    }
091    
092                    ThemeDisplay themeDisplay = (ThemeDisplay)_request.getAttribute(
093                            WebKeys.THEME_DISPLAY);
094    
095                    LayoutTypePortlet layoutTypePortlet =
096                            themeDisplay.getLayoutTypePortlet();
097    
098                    List<Portlet> portlets = layoutTypePortlet.getAllPortlets(columnId);
099    
100                    StringBundler sb = new StringBundler(portlets.size() + 11);
101    
102                    sb.append("<div class=\"");
103                    sb.append("portlet-dropzone");
104    
105                    if (layoutTypePortlet.isCustomizable() &&
106                            layoutTypePortlet.isColumnDisabled(columnId)) {
107    
108                            sb.append(" portlet-dropzone-disabled");
109                    }
110    
111                    if (layoutTypePortlet.isColumnCustomizable(columnId)) {
112                            sb.append(" customizable");
113                    }
114    
115                    if (portlets.isEmpty()) {
116                            sb.append(" empty");
117                    }
118    
119                    if (Validator.isNotNull(classNames)) {
120                            sb.append(" ");
121                            sb.append(classNames);
122                    }
123    
124                    sb.append("\" id=\"layout-column_");
125                    sb.append(columnId);
126                    sb.append("\">");
127    
128                    for (int i = 0; i < portlets.size(); i++) {
129                            Portlet portlet = portlets.get(i);
130    
131                            String queryString = null;
132                            Integer columnPos = new Integer(i);
133                            Integer columnCount = new Integer(portlets.size());
134                            String path = null;
135    
136                            if (parallelRenderEnable) {
137                                    path = "/html/portal/load_render_portlet.jsp";
138    
139                                    if (portlet.getRenderWeight() >= 1) {
140                                            _portletsMap.put(
141                                                    portlet,
142                                                    new Object[] {
143                                                            queryString, columnId, columnPos, columnCount
144                                                    });
145                                    }
146                            }
147    
148                            String content = RuntimePortletUtil.processPortlet(
149                                    _servletContext, _request, _response, portlet, queryString,
150                                    columnId, columnPos, columnCount, path, false);
151    
152                            sb.append(content);
153                    }
154    
155                    sb.append("</div>");
156    
157                    return sb.toString();
158            }
159    
160            public String processMax() throws Exception {
161                    return processMax(StringPool.BLANK);
162            }
163    
164            public String processMax(String classNames) throws Exception {
165                    return RuntimePortletUtil.processPortlet(
166                            _servletContext, _request, _response, null, null, _portletId, null,
167                            false);
168            }
169    
170            public String processPortlet(String portletId) throws Exception {
171                    try {
172                            _request.setAttribute(
173                                    WebKeys.RENDER_PORTLET_RESOURCE, Boolean.TRUE);
174    
175                            return RuntimePortletUtil.processPortlet(
176                                    _servletContext, _request, _response, null, null, _portletId,
177                                    null, false);
178                    }
179                    finally {
180                            _request.removeAttribute(WebKeys.RENDER_PORTLET_RESOURCE);
181                    }
182            }
183    
184            private static Log _log = LogFactoryUtil.getLog(TemplateProcessor.class);
185    
186            private String _portletId;
187            private Map<Portlet, Object[]> _portletsMap;
188            private HttpServletRequest _request;
189            private HttpServletResponse _response;
190            private ServletContext _servletContext;
191    
192    }