001    /**
002     * Copyright (c) 2000-present 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.taglib.ui;
016    
017    import com.liferay.portal.kernel.util.WebKeys;
018    import com.liferay.portal.theme.ThemeDisplay;
019    import com.liferay.portal.util.PortalUtil;
020    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
021    import com.liferay.portlet.portletdisplaytemplate.util.PortletDisplayTemplateUtil;
022    import com.liferay.taglib.util.IncludeTag;
023    
024    import java.util.HashMap;
025    import java.util.List;
026    import java.util.Map;
027    
028    import javax.servlet.http.HttpServletRequest;
029    
030    /**
031     * @author Eduardo Garcia
032     */
033    public class DDMTemplateRendererTag extends IncludeTag {
034    
035            @Override
036            public int processStartTag() throws Exception {
037                    if (_displayStyleGroupId == 0) {
038                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
039                                    WebKeys.THEME_DISPLAY);
040    
041                            _displayStyleGroupId = themeDisplay.getScopeGroupId();
042                    }
043    
044                    _portletDisplayDDMTemplate =
045                            PortletDisplayTemplateUtil.getPortletDisplayTemplateDDMTemplate(
046                                    _displayStyleGroupId, PortalUtil.getClassNameId(_className),
047                                    _displayStyle, true);
048    
049                    if (_portletDisplayDDMTemplate != null) {
050                            return SKIP_BODY;
051                    }
052    
053                    return EVAL_BODY_INCLUDE;
054            }
055    
056            public void setClassName(String className) {
057                    _className = className;
058            }
059    
060            public void setContextObjects(Map<String, Object> contextObjects) {
061                    _contextObjects = contextObjects;
062            }
063    
064            public void setDisplayStyle(String displayStyle) {
065                    _displayStyle = displayStyle;
066            }
067    
068            public void setDisplayStyleGroupId(long displayStyleGroupId) {
069                    _displayStyleGroupId = displayStyleGroupId;
070            }
071    
072            public void setEntries(List<?> entries) {
073                    _entries = entries;
074            }
075    
076            @Override
077            protected void cleanUp() {
078                    _className = null;
079                    _contextObjects = new HashMap<>();
080                    _displayStyle = null;
081                    _displayStyleGroupId = 0;
082                    _entries = null;
083                    _portletDisplayDDMTemplate = null;
084            }
085    
086            @Override
087            protected String getPage() {
088                    return _PAGE;
089            }
090    
091            @Override
092            protected void setAttributes(HttpServletRequest request) {
093                    request.setAttribute(
094                            "liferay-ui:ddm-template-renderer:contextObjects", _contextObjects);
095                    request.setAttribute(
096                            "liferay-ui:ddm-template-renderer:entries", _entries);
097                    request.setAttribute(
098                            "liferay-ui:ddm-template-renderer:portletDisplayDDMTemplate",
099                            _portletDisplayDDMTemplate);
100            }
101    
102            private static final String _PAGE =
103                    "/html/taglib/ui/ddm_template_renderer/page.jsp";
104    
105            private String _className;
106            private Map<String, Object> _contextObjects = new HashMap<>();
107            private String _displayStyle;
108            private long _displayStyleGroupId;
109            private List<?> _entries;
110            private DDMTemplate _portletDisplayDDMTemplate;
111    
112    }