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.StringPool;
018    import com.liferay.portal.kernel.util.Validator;
019    import com.liferay.portal.kernel.util.WebKeys;
020    import com.liferay.portal.theme.ThemeDisplay;
021    import com.liferay.portal.util.PortalUtil;
022    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
023    import com.liferay.portlet.portletdisplaytemplate.util.PortletDisplayTemplateUtil;
024    import com.liferay.taglib.util.IncludeTag;
025    
026    import java.util.List;
027    
028    import javax.servlet.http.HttpServletRequest;
029    
030    /**
031     * @author Juan Fern??ndez
032     */
033    public class DDMTemplateSelectorTag extends IncludeTag {
034    
035            public void setClassName(String className) {
036                    _className = className;
037            }
038    
039            public void setDefaultDisplayStyle(String defaultDisplayStyle) {
040                    _defaultDisplayStyle = defaultDisplayStyle;
041            }
042    
043            public void setDisplayStyle(String displayStyle) {
044                    _displayStyle = displayStyle;
045            }
046    
047            public void setDisplayStyleGroupId(long displayStyleGroupId) {
048                    _displayStyleGroupId = displayStyleGroupId;
049            }
050    
051            public void setDisplayStyles(List<String> displayStyles) {
052                    _displayStyles = displayStyles;
053            }
054    
055            public void setIcon(String icon) {
056                    _icon = icon;
057            }
058    
059            public void setLabel(String label) {
060                    _label = label;
061            }
062    
063            public void setRefreshURL(String refreshURL) {
064                    _refreshURL = refreshURL;
065            }
066    
067            public void setShowEmptyOption(boolean showEmptyOption) {
068                    _showEmptyOption = showEmptyOption;
069            }
070    
071            @Override
072            protected void cleanUp() {
073                    _className = null;
074                    _defaultDisplayStyle = StringPool.BLANK;
075                    _displayStyle = null;
076                    _displayStyleGroupId = 0;
077                    _displayStyles = null;
078                    _icon = null;
079                    _label = "display-template";
080                    _refreshURL = null;
081                    _showEmptyOption = false;
082            }
083    
084            protected String getDisplayStyle() {
085                    String displayStyle = _displayStyle;
086    
087                    if (Validator.isNull(displayStyle)) {
088                            displayStyle = _defaultDisplayStyle;
089                    }
090    
091                    DDMTemplate portletDisplayDDMTemplate = getPortletDisplayDDMTemplate();
092    
093                    if (Validator.isNull(displayStyle) &&
094                            (portletDisplayDDMTemplate != null)) {
095    
096                            displayStyle = PortletDisplayTemplateUtil.getDisplayStyle(
097                                    portletDisplayDDMTemplate.getTemplateKey());
098                    }
099    
100                    return displayStyle;
101            }
102    
103            protected long getDisplayStyleGroupId() {
104                    if (_displayStyleGroupId > 0) {
105                            return _displayStyleGroupId;
106                    }
107    
108                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
109                            WebKeys.THEME_DISPLAY);
110    
111                    return themeDisplay.getScopeGroupId();
112            }
113    
114            @Override
115            protected String getPage() {
116                    return _PAGE;
117            }
118    
119            protected DDMTemplate getPortletDisplayDDMTemplate() {
120                    String displayStyle = _displayStyle;
121    
122                    if (Validator.isNull(displayStyle)) {
123                            displayStyle = _defaultDisplayStyle;
124                    }
125    
126                    DDMTemplate portletDisplayDDMTemplate =
127                            PortletDisplayTemplateUtil.getPortletDisplayTemplateDDMTemplate(
128                                    getDisplayStyleGroupId(), PortalUtil.getClassNameId(_className),
129                                    displayStyle, true);
130    
131                    return portletDisplayDDMTemplate;
132            }
133    
134            @Override
135            protected void setAttributes(HttpServletRequest request) {
136                    request.setAttribute(
137                            "liferay-ui:ddm-template-select:classNameId",
138                            String.valueOf(PortalUtil.getClassNameId(_className)));
139                    request.setAttribute(
140                            "liferay-ui:ddm-template-select:displayStyle", getDisplayStyle());
141                    request.setAttribute(
142                            "liferay-ui:ddm-template-select:displayStyleGroupId",
143                            String.valueOf(getDisplayStyleGroupId()));
144                    request.setAttribute(
145                            "liferay-ui:ddm-template-select:displayStyles", _displayStyles);
146                    request.setAttribute("liferay-ui:ddm-template-select:icon", _icon);
147                    request.setAttribute("liferay-ui:ddm-template-select:label", _label);
148                    request.setAttribute(
149                            "liferay-ui:ddm-template-select:portletDisplayDDMTemplate",
150                            getPortletDisplayDDMTemplate());
151                    request.setAttribute(
152                            "liferay-ui:ddm-template-select:refreshURL", _refreshURL);
153                    request.setAttribute(
154                            "liferay-ui:ddm-template-select:showEmptyOption",
155                            String.valueOf(_showEmptyOption));
156            }
157    
158            private static final String _PAGE =
159                    "/html/taglib/ui/ddm_template_selector/page.jsp";
160    
161            private String _className;
162            private String _defaultDisplayStyle = StringPool.BLANK;
163            private String _displayStyle;
164            private long _displayStyleGroupId;
165            private List<String> _displayStyles;
166            private String _icon;
167            private String _label = "display-template";
168            private String _refreshURL;
169            private boolean _showEmptyOption;
170    
171    }