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