001    /**
002     * Copyright (c) 2000-2012 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.portlet.asset.model;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
020    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
021    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
022    import com.liferay.portal.kernel.util.HttpUtil;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.kernel.util.WebKeys;
028    import com.liferay.portal.model.Group;
029    import com.liferay.portal.model.Layout;
030    import com.liferay.portal.security.permission.PermissionChecker;
031    import com.liferay.portal.theme.PortletDisplay;
032    import com.liferay.portal.theme.ThemeDisplay;
033    import com.liferay.portal.util.PortalUtil;
034    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
035    
036    import java.util.Locale;
037    
038    import javax.portlet.PortletMode;
039    import javax.portlet.PortletRequest;
040    import javax.portlet.PortletURL;
041    import javax.portlet.RenderRequest;
042    import javax.portlet.RenderResponse;
043    import javax.portlet.WindowState;
044    
045    /**
046     * @author Jorge Ferrer
047     * @author Sergio González
048     */
049    public abstract class BaseAssetRenderer implements AssetRenderer {
050    
051            public AssetRendererFactory getAssetRendererFactory() {
052                    if (_assetRendererFactory != null) {
053                            return _assetRendererFactory;
054                    }
055    
056                    _assetRendererFactory =
057                            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
058                                    getAssetRendererFactoryClassName());
059    
060                    return _assetRendererFactory;
061            }
062    
063            public String[] getAvailableLocales() {
064                    return _AVAILABLE_LOCALES;
065            }
066    
067            public String getDiscussionPath() {
068                    return null;
069            }
070    
071            public String getIconPath(PortletRequest portletRequest) {
072                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
073                            WebKeys.THEME_DISPLAY);
074    
075                    return getIconPath(themeDisplay);
076            }
077    
078            public String getSearchSummary(Locale locale) {
079                    return getSummary(locale);
080            }
081    
082            public String getThumbnailPath(PortletRequest portletRequest)
083                    throws Exception {
084    
085                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
086                            WebKeys.THEME_DISPLAY);
087    
088                    return themeDisplay.getPathThemeImages() +
089                            "/file_system/large/default.png";
090            }
091    
092            public String getURLDownload(ThemeDisplay themeDisplay) {
093                    return null;
094            }
095    
096            public PortletURL getURLEdit(
097                            LiferayPortletRequest liferayPortletRequest,
098                            LiferayPortletResponse liferayPortletResponse)
099                    throws Exception {
100    
101                    return null;
102            }
103    
104            public PortletURL getURLEdit(
105                            LiferayPortletRequest liferayPortletRequest,
106                            LiferayPortletResponse liferayPortletResponse,
107                            WindowState windowState, PortletURL redirectURL)
108                    throws Exception {
109    
110                    LiferayPortletURL editPortletURL = (LiferayPortletURL)getURLEdit(
111                            liferayPortletRequest, liferayPortletResponse);
112    
113                    if (editPortletURL == null) {
114                            return null;
115                    }
116    
117                    ThemeDisplay themeDisplay =
118                            (ThemeDisplay)liferayPortletRequest.getAttribute(
119                                    WebKeys.THEME_DISPLAY);
120    
121                    Group group = themeDisplay.getScopeGroup();
122    
123                    if (group.isLayout()) {
124                            Layout layout = themeDisplay.getLayout();
125    
126                            group = layout.getGroup();
127                    }
128    
129                    if (group.hasStagingGroup()) {
130                            return null;
131                    }
132    
133                    editPortletURL.setDoAsGroupId(getGroupId());
134    
135                    editPortletURL.setParameter("redirect", redirectURL.toString());
136                    editPortletURL.setParameter("originalRedirect", redirectURL.toString());
137    
138                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
139    
140                    String portletResource = ParamUtil.getString(
141                            liferayPortletRequest, "portletResource", portletDisplay.getId());
142    
143                    if (Validator.isNotNull(portletResource)) {
144                            editPortletURL.setParameter(
145                                    "referringPortletResource", portletResource);
146                    }
147                    else {
148                            editPortletURL.setParameter(
149                                    "referringPortletResource", portletDisplay.getId());
150                    }
151    
152                    editPortletURL.setPortletMode(PortletMode.VIEW);
153                    editPortletURL.setRefererPlid(themeDisplay.getPlid());
154                    editPortletURL.setWindowState(windowState);
155    
156                    return editPortletURL;
157            }
158    
159            public PortletURL getURLExport(
160                            LiferayPortletRequest liferayPortletRequest,
161                            LiferayPortletResponse liferayPortletResponse)
162                    throws Exception {
163    
164                    return null;
165            }
166    
167            public String getUrlTitle() {
168                    return null;
169            }
170    
171            public PortletURL getURLView(
172                            LiferayPortletResponse liferayPortletResponse,
173                            WindowState windowState)
174                    throws Exception {
175    
176                    return null;
177            }
178    
179            public String getURLViewInContext(
180                            LiferayPortletRequest liferayPortletRequest,
181                            LiferayPortletResponse liferayPortletResponse,
182                            String noSuchEntryRedirect)
183                    throws Exception {
184    
185                    return null;
186            }
187    
188            public String getViewInContextMessage() {
189                    return "view-in-context";
190            }
191    
192            @SuppressWarnings("unused")
193            public boolean hasEditPermission(PermissionChecker permissionChecker)
194                    throws PortalException, SystemException {
195    
196                    return false;
197            }
198    
199            @SuppressWarnings("unused")
200            public boolean hasViewPermission(PermissionChecker permissionChecker)
201                    throws PortalException, SystemException {
202    
203                    return true;
204            }
205    
206            public boolean isConvertible() {
207                    return false;
208            }
209    
210            public boolean isDisplayable() {
211                    return true;
212            }
213    
214            public boolean isLocalizable() {
215                    return false;
216            }
217    
218            public boolean isPreviewInContext() {
219                    return false;
220            }
221    
222            public boolean isPrintable() {
223                    return false;
224            }
225    
226            public String renderActions(
227                            RenderRequest renderRequest, RenderResponse renderResponse)
228                    throws Exception {
229    
230                    return null;
231            }
232    
233            protected long getControlPanelPlid(
234                            LiferayPortletRequest liferayPortletRequest)
235                    throws PortalException, SystemException {
236    
237                    return PortalUtil.getControlPanelPlid(liferayPortletRequest);
238            }
239    
240            protected long getControlPanelPlid(ThemeDisplay themeDisplay)
241                    throws PortalException, SystemException {
242    
243                    return PortalUtil.getControlPanelPlid(themeDisplay.getCompanyId());
244            }
245    
246            protected String getIconPath(ThemeDisplay themeDisplay) {
247                    return themeDisplay.getPathThemeImages() + "/common/page.png";
248            }
249    
250            protected String getURLViewInContext(
251                    LiferayPortletRequest liferayPortletRequest, String noSuchEntryRedirect,
252                    String path, String primaryKeyParameterName,
253                    long primaryKeyParameterValue) {
254    
255                    ThemeDisplay themeDisplay =
256                            (ThemeDisplay)liferayPortletRequest.getAttribute(
257                                    WebKeys.THEME_DISPLAY);
258    
259                    StringBundler sb = new StringBundler(11);
260    
261                    sb.append(themeDisplay.getPortalURL());
262                    sb.append(themeDisplay.getPathMain());
263                    sb.append(path);
264                    sb.append("?p_l_id=");
265                    sb.append(themeDisplay.getPlid());
266                    sb.append("&noSuchEntryRedirect=");
267                    sb.append(HttpUtil.encodeURL(noSuchEntryRedirect));
268                    sb.append(StringPool.AMPERSAND);
269                    sb.append(primaryKeyParameterName);
270                    sb.append(StringPool.EQUAL);
271                    sb.append(primaryKeyParameterValue);
272    
273                    return sb.toString();
274            }
275    
276            private static final String[] _AVAILABLE_LOCALES = new String[0];
277    
278            private AssetRendererFactory _assetRendererFactory;
279    
280    }