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