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 getRestorePath(RenderRequest renderRequest) {
079                    return null;
080            }
081    
082            public String getSearchSummary(Locale locale) {
083                    return getSummary(locale);
084            }
085    
086            public String getURLDownload(ThemeDisplay themeDisplay) {
087                    return null;
088            }
089    
090            public PortletURL getURLEdit(
091                            LiferayPortletRequest liferayPortletRequest,
092                            LiferayPortletResponse liferayPortletResponse)
093                    throws Exception {
094    
095                    return null;
096            }
097    
098            public PortletURL getURLEdit(
099                            LiferayPortletRequest liferayPortletRequest,
100                            LiferayPortletResponse liferayPortletResponse,
101                            WindowState windowState, PortletURL redirectURL)
102                    throws Exception {
103    
104                    LiferayPortletURL editPortletURL =
105                            (LiferayPortletURL)getURLEdit(
106                                    liferayPortletRequest, liferayPortletResponse);
107    
108                    if (editPortletURL == null) {
109                            return null;
110                    }
111    
112                    ThemeDisplay themeDisplay =
113                            (ThemeDisplay)liferayPortletRequest.getAttribute(
114                                    WebKeys.THEME_DISPLAY);
115    
116                    Group group = themeDisplay.getScopeGroup();
117    
118                    if (group.isLayout()) {
119                            Layout layout = themeDisplay.getLayout();
120    
121                            group = layout.getGroup();
122                    }
123    
124                    if (group.hasStagingGroup()) {
125                            return null;
126                    }
127    
128                    editPortletURL.setDoAsGroupId(getGroupId());
129    
130                    editPortletURL.setParameter("redirect", redirectURL.toString());
131                    editPortletURL.setParameter("originalRedirect", redirectURL.toString());
132    
133                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
134    
135                    String portletResource = ParamUtil.getString(
136                            liferayPortletRequest, "portletResource", portletDisplay.getId());
137    
138                    if (Validator.isNotNull(portletResource)) {
139                            editPortletURL.setParameter(
140                                    "referringPortletResource", portletResource);
141                    }
142                    else {
143                            editPortletURL.setParameter(
144                                    "referringPortletResource", portletDisplay.getId());
145                    }
146    
147                    editPortletURL.setPortletMode(PortletMode.VIEW);
148                    editPortletURL.setRefererPlid(themeDisplay.getPlid());
149                    editPortletURL.setWindowState(windowState);
150    
151                    return editPortletURL;
152            }
153    
154            public PortletURL getURLExport(
155                            LiferayPortletRequest liferayPortletRequest,
156                            LiferayPortletResponse liferayPortletResponse)
157                    throws Exception {
158    
159                    return null;
160            }
161    
162            public String getUrlTitle() {
163                    return null;
164            }
165    
166            public PortletURL getURLView(
167                            LiferayPortletResponse liferayPortletResponse,
168                            WindowState windowState)
169                    throws Exception {
170    
171                    return null;
172            }
173    
174            public String getURLViewInContext(
175                            LiferayPortletRequest liferayPortletRequest,
176                            LiferayPortletResponse liferayPortletResponse,
177                            String noSuchEntryRedirect)
178                    throws Exception {
179    
180                    return null;
181            }
182    
183            public String getViewInContextMessage() {
184                    return "view-in-context";
185            }
186    
187            @SuppressWarnings("unused")
188            public boolean hasEditPermission(PermissionChecker permissionChecker)
189                    throws PortalException, SystemException {
190    
191                    return false;
192            }
193    
194            @SuppressWarnings("unused")
195            public boolean hasViewPermission(PermissionChecker permissionChecker)
196                    throws PortalException, SystemException {
197    
198                    return true;
199            }
200    
201            public boolean isConvertible() {
202                    return false;
203            }
204    
205            public boolean isDisplayable() {
206                    return true;
207            }
208    
209            public boolean isLocalizable() {
210                    return false;
211            }
212    
213            public boolean isPreviewInContext() {
214                    return false;
215            }
216    
217            public boolean isPrintable() {
218                    return false;
219            }
220    
221            public String renderActions(
222                            RenderRequest renderRequest, RenderResponse renderResponse)
223                    throws Exception {
224    
225                    return null;
226            }
227    
228            protected long getControlPanelPlid(
229                            LiferayPortletRequest liferayPortletRequest)
230                    throws PortalException, SystemException {
231    
232                    return PortalUtil.getControlPanelPlid(liferayPortletRequest);
233            }
234    
235            protected long getControlPanelPlid(ThemeDisplay themeDisplay)
236                    throws PortalException, SystemException {
237    
238                    return PortalUtil.getControlPanelPlid(themeDisplay.getCompanyId());
239            }
240    
241            protected String getIconPath(ThemeDisplay themeDisplay) {
242                    return themeDisplay.getPathThemeImages() + "/common/page.png";
243            }
244    
245            protected String getURLViewInContext(
246                    LiferayPortletRequest liferayPortletRequest, String noSuchEntryRedirect,
247                    String path, String primaryKeyParameterName,
248                    long primaryKeyParameterValue) {
249    
250                    ThemeDisplay themeDisplay =
251                            (ThemeDisplay)liferayPortletRequest.getAttribute(
252                                    WebKeys.THEME_DISPLAY);
253    
254                    StringBundler sb = new StringBundler(11);
255    
256                    sb.append(themeDisplay.getPortalURL());
257                    sb.append(themeDisplay.getPathMain());
258                    sb.append(path);
259                    sb.append("?p_l_id=");
260                    sb.append(themeDisplay.getPlid());
261                    sb.append("&noSuchEntryRedirect=");
262                    sb.append(HttpUtil.encodeURL(noSuchEntryRedirect));
263                    sb.append(StringPool.AMPERSAND);
264                    sb.append(primaryKeyParameterName);
265                    sb.append(StringPool.EQUAL);
266                    sb.append(primaryKeyParameterValue);
267    
268                    return sb.toString();
269            }
270    
271            private static final String[] _AVAILABLE_LOCALES = new String[0];
272    
273            private AssetRendererFactory _assetRendererFactory;
274    
275    }