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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ServerDetector;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.util.WebKeys;
022    import com.liferay.portlet.asset.model.AssetEntry;
023    import com.liferay.portlet.asset.model.AssetRenderer;
024    import com.liferay.portlet.asset.model.AssetRendererFactory;
025    import com.liferay.portlet.asset.model.Renderer;
026    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
027    import com.liferay.taglib.util.IncludeTag;
028    
029    import java.io.IOException;
030    
031    import javax.servlet.ServletException;
032    import javax.servlet.http.HttpServletRequest;
033    import javax.servlet.http.HttpServletResponse;
034    import javax.servlet.jsp.JspException;
035    
036    /**
037     * @author Julio Camarero
038     */
039    public class AssetDisplayTag extends IncludeTag {
040    
041            @Override
042            public int doEndTag() throws JspException {
043                    try {
044                            callSetAttributes();
045    
046                            doInclude(null, false);
047    
048                            return EVAL_PAGE;
049                    }
050                    catch (Exception e) {
051                            throw new JspException(e);
052                    }
053                    finally {
054                            clearDynamicAttributes();
055                            clearParams();
056                            clearProperties();
057    
058                            cleanUpSetAttributes();
059    
060                            if (!ServerDetector.isResin()) {
061                                    setPage(null);
062                                    setUseCustomPage(true);
063    
064                                    cleanUp();
065                            }
066                    }
067            }
068    
069            public int getAbstractLength() {
070                    return _abstractLength;
071            }
072    
073            public AssetEntry getAssetEntry() {
074                    return _assetEntry;
075            }
076    
077            public AssetRendererFactory<?> getAssetRendererFactory() {
078                    return _assetRendererFactory;
079            }
080    
081            public String getClassName() {
082                    return _className;
083            }
084    
085            public long getClassPK() {
086                    return _classPK;
087            }
088    
089            public Renderer getRenderer() {
090                    return _renderer;
091            }
092    
093            public String getTemplate() {
094                    return _template;
095            }
096    
097            public String getViewURL() {
098                    return _viewURL;
099            }
100    
101            public boolean isShowComments() {
102                    return _showComments;
103            }
104    
105            public boolean isShowExtraInfo() {
106                    return _showExtraInfo;
107            }
108    
109            public boolean isShowHeader() {
110                    return _showHeader;
111            }
112    
113            public void setAbstractLength(int abstractLength) {
114                    _abstractLength = abstractLength;
115            }
116    
117            public void setAssetEntry(AssetEntry assetEntry) {
118                    _assetEntry = assetEntry;
119            }
120    
121            public void setAssetRenderer(AssetRenderer<?> assetRenderer) {
122                    _renderer = assetRenderer;
123            }
124    
125            public void setAssetRendererFactory(
126                    AssetRendererFactory<?> assetRendererFactory) {
127    
128                    _assetRendererFactory = assetRendererFactory;
129            }
130    
131            public void setClassName(String className) {
132                    _className = className;
133            }
134    
135            public void setClassPK(long classPK) {
136                    _classPK = classPK;
137            }
138    
139            public void setRenderer(Renderer renderer) {
140                    _renderer = renderer;
141            }
142    
143            public void setShowComments(boolean showComments) {
144                    _showComments = showComments;
145            }
146    
147            public void setShowExtraInfo(boolean showExtraInfo) {
148                    _showExtraInfo = showExtraInfo;
149            }
150    
151            public void setShowHeader(boolean showHeader) {
152                    _showHeader = showHeader;
153            }
154    
155            public void setTemplate(String template) {
156                    _template = template;
157            }
158    
159            public void setViewURL(String viewURL) {
160                    _viewURL = viewURL;
161            }
162    
163            @Override
164            protected void cleanUp() {
165                    _abstractLength = 200;
166                    _assetEntry = null;
167                    _className = null;
168                    _classPK = 0;
169                    _page = null;
170                    _renderer = null;
171                    _showComments = false;
172                    _showExtraInfo = false;
173                    _showHeader = false;
174                    _template = AssetRenderer.TEMPLATE_FULL_CONTENT;
175                    _viewURL = null;
176            }
177    
178            @Override
179            protected String getPage() {
180                    return _page;
181            }
182    
183            @Override
184            protected void includePage(String page, HttpServletResponse response)
185                    throws IOException, ServletException {
186    
187                    try {
188                            boolean include = _renderer.include(request, response, _template);
189    
190                            if (include) {
191                                    return;
192                            }
193                    }
194                    catch (Exception e) {
195                            _log.error(e);
196                    }
197    
198                    super.includePage(
199                            "/html/taglib/ui/asset_display/" + _template + ".jsp", response);
200            }
201    
202            @Override
203            protected void setAttributes(HttpServletRequest request) {
204                    request.setAttribute(
205                            "liferay-ui:asset-display:abstractLength", _abstractLength);
206    
207                    AssetEntry assetEntry = _assetEntry;
208    
209                    if (assetEntry == null) {
210                            if (Validator.isNotNull(_className) && (_classPK > 0)) {
211                                    assetEntry = AssetEntryLocalServiceUtil.fetchEntry(
212                                            _className, _classPK);
213                            }
214                            else if (_renderer != null) {
215                                    assetEntry = AssetEntryLocalServiceUtil.fetchEntry(
216                                            _renderer.getClassName(), _renderer.getClassPK());
217                            }
218                    }
219    
220                    request.setAttribute("liferay-ui:asset-display:assetEntry", assetEntry);
221    
222                    if ((_renderer == null) && (assetEntry != null)) {
223                            _renderer = assetEntry.getAssetRenderer();
224                    }
225    
226                    if (_renderer instanceof AssetRenderer) {
227                            AssetRenderer<?> assetRenderer = (AssetRenderer<?>)_renderer;
228    
229                            request.setAttribute(WebKeys.ASSET_RENDERER, assetRenderer);
230                    }
231                    else {
232                            request.setAttribute(
233                                    "liferay-ui:asset-display:renderer", _renderer);
234                    }
235    
236                    AssetRendererFactory<?> assetRendererFactory = _assetRendererFactory;
237    
238                    if ((assetRendererFactory == null) && (assetEntry != null)) {
239                            assetRendererFactory = assetEntry.getAssetRendererFactory();
240                    }
241    
242                    if (assetRendererFactory != null) {
243                            request.setAttribute(
244                                    WebKeys.ASSET_RENDERER_FACTORY, assetRendererFactory);
245                    }
246    
247                    request.setAttribute(WebKeys.ASSET_ENTRY_VIEW_URL, _viewURL);
248    
249                    addParam("showComments", String.valueOf(_showComments));
250                    addParam("showExtraInfo", String.valueOf(_showExtraInfo));
251                    addParam("showHeader", String.valueOf(_showHeader));
252            }
253    
254            private static final Log _log = LogFactoryUtil.getLog(
255                    AssetDisplayTag.class);
256    
257            private int _abstractLength = 200;
258            private AssetEntry _assetEntry;
259            private AssetRendererFactory<?> _assetRendererFactory;
260            private String _className;
261            private long _classPK;
262            private String _page;
263            private Renderer _renderer;
264            private boolean _showComments;
265            private boolean _showExtraInfo;
266            private boolean _showHeader;
267            private String _template = AssetRenderer.TEMPLATE_FULL_CONTENT;
268            private String _viewURL;
269    
270    }