001    /**
002     * Copyright (c) 2000-2013 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.portal.kernel.trash;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.model.ContainerModel;
023    import com.liferay.portal.security.permission.ActionKeys;
024    import com.liferay.portal.security.permission.PermissionChecker;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
027    import com.liferay.portlet.asset.model.AssetRenderer;
028    import com.liferay.portlet.asset.model.AssetRendererFactory;
029    import com.liferay.portlet.trash.model.TrashEntry;
030    
031    import java.util.Collections;
032    import java.util.List;
033    
034    import javax.portlet.PortletRequest;
035    
036    /**
037     * Provides the base implementation of {@link TrashHandler}.
038     *
039     * @author Alexander Chow
040     * @author Zsolt Berentey
041     * @see    {@link TrashHandler}
042     */
043    public abstract class BaseTrashHandler implements TrashHandler {
044    
045            @SuppressWarnings("unused")
046            public void checkDuplicateTrashEntry(
047                            TrashEntry trashEntry, long containerModelId, String newName)
048                    throws PortalException, SystemException {
049            }
050    
051            @SuppressWarnings("unused")
052            public ContainerModel getContainerModel(long containerModelId)
053                    throws PortalException, SystemException {
054    
055                    return null;
056            }
057    
058            public String getContainerModelClassName() {
059                    return StringPool.BLANK;
060            }
061    
062            public String getContainerModelName() {
063                    return StringPool.BLANK;
064            }
065    
066            @SuppressWarnings("unused")
067            public List<ContainerModel> getContainerModels(
068                            long classPK, long containerModelId, int start, int end)
069                    throws PortalException, SystemException {
070    
071                    return Collections.emptyList();
072            }
073    
074            @SuppressWarnings("unused")
075            public int getContainerModelsCount(long classPK, long containerModelId)
076                    throws PortalException, SystemException {
077    
078                    return 0;
079            }
080    
081            public String getDeleteMessage() {
082                    return "deleted-in-x";
083            }
084    
085            @SuppressWarnings("unused")
086            public ContainerModel getParentContainerModel(long classPK)
087                    throws PortalException, SystemException {
088    
089                    return null;
090            }
091    
092            @SuppressWarnings("unused")
093            public List<ContainerModel> getParentContainerModels(long classPK)
094                    throws PortalException, SystemException {
095    
096                    return Collections.emptyList();
097            }
098    
099            @SuppressWarnings("unused")
100            public String getRestoreLink(PortletRequest portletRequest, long classPK)
101                    throws PortalException, SystemException {
102    
103                    return StringPool.BLANK;
104            }
105    
106            @SuppressWarnings("unused")
107            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
108                    throws PortalException, SystemException {
109    
110                    return StringPool.BLANK;
111            }
112    
113            public String getRootContainerModelName() {
114                    return StringPool.BLANK;
115            }
116    
117            public String getSubcontainerModelName() {
118                    return StringPool.BLANK;
119            }
120    
121            public String getTrashContainedModelName() {
122                    return StringPool.BLANK;
123            }
124    
125            @SuppressWarnings("unused")
126            public int getTrashContainedModelsCount(long classPK)
127                    throws PortalException, SystemException {
128    
129                    return 0;
130            }
131    
132            @SuppressWarnings("unused")
133            public List<TrashRenderer> getTrashContainedModelTrashRenderers(
134                            long classPK, int start, int end)
135                    throws PortalException, SystemException {
136    
137                    return Collections.emptyList();
138            }
139    
140            @SuppressWarnings("unused")
141            public ContainerModel getTrashContainer(long classPK)
142                    throws PortalException, SystemException {
143    
144                    return null;
145            }
146    
147            public String getTrashContainerModelName() {
148                    return StringPool.BLANK;
149            }
150    
151            @SuppressWarnings("unused")
152            public int getTrashContainerModelsCount(long classPK)
153                    throws PortalException, SystemException {
154    
155                    return 0;
156            }
157    
158            @SuppressWarnings("unused")
159            public List<TrashRenderer> getTrashContainerModelTrashRenderers(
160                            long classPK, int start, int end)
161                    throws PortalException, SystemException {
162    
163                    return Collections.emptyList();
164            }
165    
166            public TrashRenderer getTrashRenderer(long classPK)
167                    throws PortalException, SystemException {
168    
169                    AssetRendererFactory assetRendererFactory = getAssetRendererFactory();
170    
171                    if (assetRendererFactory != null) {
172                            AssetRenderer assetRenderer = assetRendererFactory.getAssetRenderer(
173                                    classPK);
174    
175                            if (assetRenderer instanceof TrashRenderer) {
176                                    return (TrashRenderer)assetRenderer;
177                            }
178                    }
179    
180                    return null;
181            }
182    
183            public boolean hasTrashPermission(
184                            PermissionChecker permissionChecker, long groupId, long classPK,
185                            String trashActionId)
186                    throws PortalException, SystemException {
187    
188                    String actionId = trashActionId;
189    
190                    if (trashActionId.equals(ActionKeys.DELETE)) {
191                            actionId = ActionKeys.DELETE;
192                    }
193                    else if (trashActionId.equals(TrashActionKeys.OVERWRITE)) {
194                            actionId = ActionKeys.DELETE;
195                    }
196                    else if (trashActionId.equals(TrashActionKeys.MOVE)) {
197                            return false;
198                    }
199                    else if (trashActionId.equals(TrashActionKeys.RENAME)) {
200                            actionId = ActionKeys.UPDATE;
201                    }
202                    else if (trashActionId.equals(TrashActionKeys.RESTORE)) {
203                            actionId = ActionKeys.DELETE;
204                    }
205    
206                    return hasPermission(permissionChecker, classPK, actionId);
207            }
208    
209            public boolean isContainerModel() {
210                    return false;
211            }
212    
213            public boolean isDeletable() {
214                    return true;
215            }
216    
217            @SuppressWarnings("unused")
218            public boolean isInTrashContainer(long classPK)
219                    throws PortalException, SystemException {
220    
221                    return false;
222            }
223    
224            public boolean isMovable() {
225                    return false;
226            }
227    
228            @SuppressWarnings("unused")
229            public boolean isRestorable(long classPK)
230                    throws PortalException, SystemException {
231    
232                    return true;
233            }
234    
235            @SuppressWarnings("unused")
236            public void moveEntry(
237                            long userId, long classPK, long containerModelId,
238                            ServiceContext serviceContext)
239                    throws PortalException, SystemException {
240            }
241    
242            public void moveTrashEntry(
243                            long userId, long classPK, long containerModelId,
244                            ServiceContext serviceContext)
245                    throws PortalException, SystemException {
246    
247                    if (isRestorable(classPK)) {
248                            restoreTrashEntry(userId, classPK);
249                    }
250    
251                    _log.error(
252                            "moveTrashEntry() is not implemented in " + getClass().getName());
253    
254                    throw new SystemException();
255            }
256    
257            @SuppressWarnings("unused")
258            public void restoreRelatedTrashEntry(String className, long classPK)
259                    throws PortalException, SystemException {
260            }
261    
262            @SuppressWarnings("unused")
263            public void updateTitle(long classPK, String title)
264                    throws PortalException, SystemException {
265            }
266    
267            protected AssetRendererFactory getAssetRendererFactory() {
268                    return AssetRendererFactoryRegistryUtil.
269                            getAssetRendererFactoryByClassName(getClassName());
270            }
271    
272            protected abstract boolean hasPermission(
273                            PermissionChecker permissionChecker, long classPK, String actionId)
274                    throws PortalException, SystemException;
275    
276            private static Log _log = LogFactoryUtil.getLog(BaseTrashHandler.class);
277    
278    }