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.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            /**
046             * Checks if a duplicate trash entry already exists in the destination
047             * container.
048             *
049             * <p>
050             * This method is used to check for duplicates when a trash entry is being
051             * restored or moved out of the Recycle Bin.
052             * </p>
053             *
054             * @param  trashEntry the trash entry to check
055             * @param  containerModelId the primary key of the destination (e.g. folder)
056             * @param  newName the new name to be assigned to the trash entry
057             *         (optionally <code>null</code> to forego renaming the trash entry)
058             * @throws PortalException if a duplicate trash entry already existed in the
059             *         destination container
060             * @throws SystemException if a system exception occurred
061             */
062            public void checkDuplicateTrashEntry(
063                            TrashEntry trashEntry, long containerModelId, String newName)
064                    throws PortalException, SystemException {
065            }
066    
067            public void deleteTrashEntries(long[] classPKs)
068                    throws PortalException, SystemException {
069    
070                    deleteTrashEntries(classPKs, true);
071            }
072    
073            public void deleteTrashEntry(long classPK)
074                    throws PortalException, SystemException {
075    
076                    deleteTrashEntries(new long[] {classPK});
077            }
078    
079            public void deleteTrashEntry(long classPK, boolean checkPermission)
080                    throws PortalException, SystemException {
081    
082                    deleteTrashEntries(new long[] {classPK}, checkPermission);
083            }
084    
085            /**
086             * Returns the container model with the primary key.
087             *
088             * @param  containerModelId the primary key of the container model
089             * @return the container model with the primary key
090             * @throws PortalException if a container model with the primary key could
091             *         not be found
092             * @throws SystemException if a system exception occurred
093             */
094            public ContainerModel getContainerModel(long containerModelId)
095                    throws PortalException, SystemException {
096    
097                    return null;
098            }
099    
100            public String getContainerModelClassName() {
101                    return StringPool.BLANK;
102            }
103    
104            public String getContainerModelName() {
105                    return StringPool.BLANK;
106            }
107    
108            /**
109             * Returns a range of all the container models that are children of the
110             * parent container model identified by the container model ID. These
111             * container models must be able to contain the entity identified by the
112             * primary key.
113             *
114             * <p>
115             * This method checks for the view permission when retrieving the container
116             * models.
117             * </p>
118             *
119             * <p>
120             * Useful when paginating results. Returns a maximum of <code>end -
121             * start</code> instances. The <code>start</code> and <code>end</code>
122             * values are not primary keys but, rather, indexes in the result set. Thus,
123             * <code>0</code> refers to the first result in the set. Setting both
124             * <code>start</code> and <code>end</code> to {@link
125             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
126             * result set.
127             * </p>
128             *
129             * @param  classPK the primary key of an entity the container models must be
130             *         able to contain
131             * @param  containerModelId the primary key of the parent container model
132             * @param  start the lower bound of the range of results
133             * @param  end the upper bound of the range of results (not inclusive)
134             * @return the range of matching container models
135             * @throws PortalException if a trash entry with the primary key could not
136             *         be found
137             * @throws SystemException if a system exception occurred
138             */
139            public List<ContainerModel> getContainerModels(
140                            long classPK, long containerModelId, int start, int end)
141                    throws PortalException, SystemException {
142    
143                    return Collections.emptyList();
144            }
145    
146            /**
147             * Returns the number of container models that are children of the parent
148             * container model identified by the container model ID. These container
149             * models must be able to contain the entity identified by the primary key.
150             *
151             * <p>
152             * This method checks for the view permission when counting the container
153             * models.
154             * </p>
155             *
156             * @param  classPK the primary key of an entity the container models must be
157             *         able to contain
158             * @param  containerModelId the primary key of the parent container model
159             * @return the number of matching container models
160             * @throws PortalException if a trash entry with the primary key could not
161             *         be found
162             * @throws SystemException if a system exception occurred
163             */
164            public int getContainerModelsCount(long classPK, long containerModelId)
165                    throws PortalException, SystemException {
166    
167                    return 0;
168            }
169    
170            public String getDeleteMessage() {
171                    return "deleted-in-x";
172            }
173    
174            /**
175             * @throws PortalException
176             * @throws SystemException if a system exception occurred
177             */
178            public ContainerModel getParentContainerModel(long classPK)
179                    throws PortalException, SystemException {
180    
181                    return null;
182            }
183    
184            /**
185             * @throws PortalException
186             * @throws SystemException if a system exception occurred
187             */
188            public List<ContainerModel> getParentContainerModels(long classPK)
189                    throws PortalException, SystemException {
190    
191                    return Collections.emptyList();
192            }
193    
194            /**
195             * Returns the link to the location to which the trash entry was restored.
196             *
197             * @param  portletRequest the portlet request
198             * @param  classPK the primary key of the restored trash entry
199             * @return the restore link
200             * @throws PortalException if a trash entry with the primary key could not
201             *         be found
202             * @throws SystemException if a system exception occurred
203             */
204            public String getRestoreLink(PortletRequest portletRequest, long classPK)
205                    throws PortalException, SystemException {
206    
207                    return StringPool.BLANK;
208            }
209    
210            /**
211             * Returns the message describing the location to which the trash entry was
212             * restored.
213             *
214             * @param  portletRequest the portlet request
215             * @param  classPK the primary key of the restored trash entry
216             * @return the restore message
217             * @throws PortalException if a trash entry with the primary key could not
218             *         be found
219             * @throws SystemException if a system exception occurred
220             */
221            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
222                    throws PortalException, SystemException {
223    
224                    return StringPool.BLANK;
225            }
226    
227            public String getRootContainerModelName() {
228                    return StringPool.BLANK;
229            }
230    
231            public String getSubcontainerModelName() {
232                    return StringPool.BLANK;
233            }
234    
235            public String getTrashContainedModelName() {
236                    return StringPool.BLANK;
237            }
238    
239            /**
240             * @throws PortalException
241             * @throws SystemException if a system exception occurred
242             */
243            public int getTrashContainedModelsCount(long classPK)
244                    throws PortalException, SystemException {
245    
246                    return 0;
247            }
248    
249            /**
250             * @throws PortalException
251             * @throws SystemException if a system exception occurred
252             */
253            public List<TrashRenderer> getTrashContainedModelTrashRenderers(
254                            long classPK, int start, int end)
255                    throws PortalException, SystemException {
256    
257                    return Collections.emptyList();
258            }
259    
260            public String getTrashContainerModelName() {
261                    return StringPool.BLANK;
262            }
263    
264            /**
265             * @throws PortalException
266             * @throws SystemException if a system exception occurred
267             */
268            public int getTrashContainerModelsCount(long classPK)
269                    throws PortalException, SystemException {
270    
271                    return 0;
272            }
273    
274            /**
275             * @throws PortalException
276             * @throws SystemException if a system exception occurred
277             */
278            public List<TrashRenderer> getTrashContainerModelTrashRenderers(
279                            long classPK, int start, int end)
280                    throws PortalException, SystemException {
281    
282                    return Collections.emptyList();
283            }
284    
285            public TrashRenderer getTrashRenderer(long classPK)
286                    throws PortalException, SystemException {
287    
288                    AssetRendererFactory assetRendererFactory = getAssetRendererFactory();
289    
290                    if (assetRendererFactory != null) {
291                            AssetRenderer assetRenderer = assetRendererFactory.getAssetRenderer(
292                                    classPK);
293    
294                            if (assetRenderer instanceof TrashRenderer) {
295                                    return (TrashRenderer)assetRenderer;
296                            }
297                    }
298    
299                    return null;
300            }
301    
302            public boolean hasTrashPermission(
303                            PermissionChecker permissionChecker, long groupId, long classPK,
304                            String trashActionId)
305                    throws PortalException, SystemException {
306    
307                    String actionId = trashActionId;
308    
309                    if (trashActionId.equals(ActionKeys.DELETE)) {
310                            actionId = ActionKeys.DELETE;
311                    }
312                    else if (trashActionId.equals(TrashActionKeys.OVERWRITE)) {
313                            actionId = ActionKeys.DELETE;
314                    }
315                    else if (trashActionId.equals(TrashActionKeys.MOVE)) {
316                            return false;
317                    }
318                    else if (trashActionId.equals(TrashActionKeys.RENAME)) {
319                            actionId = ActionKeys.UPDATE;
320                    }
321                    else if (trashActionId.equals(TrashActionKeys.RESTORE)) {
322                            actionId = ActionKeys.DELETE;
323                    }
324    
325                    return hasPermission(permissionChecker, classPK, actionId);
326            }
327    
328            public boolean isContainerModel() {
329                    return false;
330            }
331    
332            /**
333             * @throws PortalException
334             * @throws SystemException if a system exception occurred
335             */
336            public boolean isInTrashContainer(long classPK)
337                    throws PortalException, SystemException {
338    
339                    return false;
340            }
341    
342            public boolean isMovable() {
343                    return false;
344            }
345    
346            /**
347             * Returns <code>true</code> if the trash entry can be restored to its
348             * original location.
349             *
350             * <p>
351             * This method usually returns <code>false</code> if the container (e.g.
352             * folder) of the trash entry is no longer available (e.g. moved to the
353             * Recycle Bin or deleted).
354             * </p>
355             *
356             * @param  classPK the primary key of the trash entry
357             * @return <code>true</code> if the trash entry can be restored to its
358             *         original location; <code>false</code> otherwise
359             * @throws PortalException if a trash entry with the primary key could not
360             *         be found
361             * @throws SystemException if a system exception occurred
362             */
363            public boolean isRestorable(long classPK)
364                    throws PortalException, SystemException {
365    
366                    return true;
367            }
368    
369            /**
370             * @throws PortalException
371             * @throws SystemException
372             */
373            public void moveEntry(
374                            long classPK, long containerModelId, ServiceContext serviceContext)
375                    throws PortalException, SystemException {
376            }
377    
378            public void moveTrashEntry(
379                            long classPK, long containerModelId, ServiceContext serviceContext)
380                    throws PortalException, SystemException {
381    
382                    if (isRestorable(classPK)) {
383                            restoreTrashEntry(classPK);
384                    }
385    
386                    _log.error("moveTrashEntry() is not implemented in " +
387                            getClass().getName());
388    
389                    throw new SystemException();
390            }
391    
392            public void restoreTrashEntry(long classPK)
393                    throws PortalException, SystemException {
394    
395                    restoreTrashEntries(new long[] {classPK});
396            }
397    
398            /**
399             * Updates the title of the trash entry with the primary key. This method is
400             * called by {@link com.liferay.portlet.trash.action.EditEntryAction} before
401             * restoring the trash entry via its restore rename action.
402             *
403             * @param  classPK the primary key of the trash entry
404             * @param  title the title to be assigned
405             * @throws PortalException if a trash entry with the primary key could not
406             *         be found
407             * @throws SystemException if a system exception occurred
408             */
409            public void updateTitle(long classPK, String title)
410                    throws PortalException, SystemException {
411            }
412    
413            protected AssetRendererFactory getAssetRendererFactory() {
414                    return AssetRendererFactoryRegistryUtil.
415                            getAssetRendererFactoryByClassName(getClassName());
416            }
417    
418            protected abstract boolean hasPermission(
419                            PermissionChecker permissionChecker, long classPK, String actionId)
420                    throws PortalException, SystemException;
421    
422            private static Log _log = LogFactoryUtil.getLog(BaseTrashHandler.class);
423    
424    }