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.model.Group;
024    import com.liferay.portal.security.permission.ActionKeys;
025    import com.liferay.portal.security.permission.PermissionChecker;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
028    import com.liferay.portlet.asset.model.AssetRenderer;
029    import com.liferay.portlet.asset.model.AssetRendererFactory;
030    import com.liferay.portlet.trash.model.TrashEntry;
031    
032    import java.util.Date;
033    import java.util.List;
034    
035    import javax.portlet.PortletRequest;
036    
037    /**
038     * Provides the base implementation of {@link TrashHandler}.
039     *
040     * @author Alexander Chow
041     * @author Zsolt Berentey
042     * @see    {@link TrashHandler}
043     */
044    public abstract class BaseTrashHandler implements TrashHandler {
045    
046            /**
047             * Checks if a duplicate trash entry already exists in the destination
048             * container.
049             *
050             * <p>
051             * This method is used to check for duplicates when a trash entry is being
052             * restored or moved out of the Recycle Bin.
053             * </p>
054             *
055             * @param  trashEntry the trash entry to check
056             * @param  containerModelId the primary key of the destination (e.g. folder)
057             * @param  newName the new name to be assigned to the trash entry
058             *         (optionally <code>null</code> to forego renaming the trash entry)
059             * @throws PortalException if a duplicate trash entry already existed in the
060             *         destination container
061             * @throws SystemException if a system exception occurred
062             */
063            public void checkDuplicateTrashEntry(
064                            TrashEntry trashEntry, long containerModelId, String newName)
065                    throws PortalException, SystemException {
066            }
067    
068            /**
069             * Deletes the group's attachments that were trashed before the given date.
070             *
071             * @param  group ID the primary key of the group
072             * @param  date the date from which attachments will be deleted
073             * @throws PortalException if any one of the attachment file paths were
074             *         invalid
075             * @throws SystemException if a system exception occurred
076             */
077            public void deleteTrashAttachments(Group group, Date date)
078                    throws PortalException, SystemException {
079            }
080    
081            public void deleteTrashEntries(long[] classPKs)
082                    throws PortalException, SystemException {
083    
084                    deleteTrashEntries(classPKs, true);
085            }
086    
087            public void deleteTrashEntry(long classPK)
088                    throws PortalException, SystemException {
089    
090                    deleteTrashEntries(new long[] {classPK});
091            }
092    
093            public void deleteTrashEntry(long classPK, boolean checkPermission)
094                    throws PortalException, SystemException {
095    
096                    deleteTrashEntries(new long[] {classPK}, checkPermission);
097            }
098    
099            /**
100             * Returns the container model with the primary key.
101             *
102             * @param  containerModelId the primary key of the container model
103             * @return the container model with the primary key
104             * @throws PortalException if a container model with the primary key could
105             *         not be found
106             * @throws SystemException if a system exception occurred
107             */
108            public ContainerModel getContainerModel(long containerModelId)
109                    throws PortalException, SystemException {
110    
111                    return null;
112            }
113    
114            public String getContainerModelName() {
115                    return StringPool.BLANK;
116            }
117    
118            /**
119             * Returns a range of all the container models that are children of the
120             * parent container model identified by the container model ID. These
121             * container models must be able to contain the entity identified by the
122             * primary key.
123             *
124             * <p>
125             * This method checks for the view permission when retrieving the container
126             * models.
127             * </p>
128             *
129             * <p>
130             * Useful when paginating results. Returns a maximum of <code>end -
131             * start</code> instances. The <code>start</code> and <code>end</code>
132             * values are not primary keys but, rather, indexes in the result set. Thus,
133             * <code>0</code> refers to the first result in the set. Setting both
134             * <code>start</code> and <code>end</code> to {@link
135             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
136             * result set.
137             * </p>
138             *
139             * @param  classPK the primary key of an entity the container models must be
140             *         able to contain
141             * @param  containerModelId the primary key of the parent container model
142             * @param  start the lower bound of the range of results
143             * @param  end the upper bound of the range of results (not inclusive)
144             * @return the range of matching container models
145             * @throws PortalException if a trash entry with the primary key could not
146             *         be found
147             * @throws SystemException if a system exception occurred
148             */
149            public List<ContainerModel> getContainerModels(
150                            long classPK, long containerModelId, int start, int end)
151                    throws PortalException, SystemException {
152    
153                    return null;
154            }
155    
156            /**
157             * Returns the number of container models that are children of the parent
158             * container model identified by the container model ID. These container
159             * models must be able to contain the entity identified by the primary key.
160             *
161             * <p>
162             * This method checks for the view permission when counting the container
163             * models.
164             * </p>
165             *
166             * @param  classPK the primary key of an entity the container models must be
167             *         able to contain
168             * @param  containerModelId the primary key of the parent container model
169             * @return the number of matching container models
170             * @throws PortalException if a trash entry with the primary key could not
171             *         be found
172             * @throws SystemException if a system exception occurred
173             */
174            public int getContainerModelsCount(long classPK, long containerModelId)
175                    throws PortalException, SystemException {
176    
177                    return 0;
178            }
179    
180            public String getDeleteMessage() {
181                    return "deleted-in-x";
182            }
183    
184            /**
185             * Returns the link to the location to which the trash entry was restored.
186             *
187             * @param  portletRequest the portlet request
188             * @param  classPK the primary key of the restored trash entry
189             * @return the restore link
190             * @throws PortalException if a trash entry with the primary key could not
191             *         be found
192             * @throws SystemException if a system exception occurred
193             */
194            public String getRestoreLink(PortletRequest portletRequest, long classPK)
195                    throws PortalException, SystemException {
196    
197                    return StringPool.BLANK;
198            }
199    
200            /**
201             * Returns the message describing the location to which the trash entry was
202             * restored.
203             *
204             * @param  portletRequest the portlet request
205             * @param  classPK the primary key of the restored trash entry
206             * @return the restore message
207             * @throws PortalException if a trash entry with the primary key could not
208             *         be found
209             * @throws SystemException if a system exception occurred
210             */
211            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
212                    throws PortalException, SystemException {
213    
214                    return StringPool.BLANK;
215            }
216    
217            public String getRootContainerModelName() {
218                    return StringPool.BLANK;
219            }
220    
221            public String getSubcontainerModelName() {
222                    return StringPool.BLANK;
223            }
224    
225            public TrashRenderer getTrashRenderer(long classPK)
226                    throws PortalException, SystemException {
227    
228                    AssetRendererFactory assetRendererFactory = getAssetRendererFactory();
229    
230                    if (assetRendererFactory != null) {
231                            AssetRenderer assetRenderer = assetRendererFactory.getAssetRenderer(
232                                    classPK);
233    
234                            if (assetRenderer instanceof TrashRenderer) {
235                                    return (TrashRenderer)assetRenderer;
236                            }
237                    }
238    
239                    return null;
240            }
241    
242            public boolean hasTrashPermission(
243                            PermissionChecker permissionChecker, long groupId, long classPK,
244                            String trashActionId)
245                    throws PortalException, SystemException {
246    
247                    String actionId = trashActionId;
248    
249                    if (trashActionId.equals(ActionKeys.DELETE)) {
250                            actionId = ActionKeys.DELETE;
251                    }
252                    else if (trashActionId.equals(TrashActionKeys.OVERWRITE)) {
253                            actionId = ActionKeys.DELETE;
254                    }
255                    else if (trashActionId.equals(TrashActionKeys.MOVE)) {
256                            return false;
257                    }
258                    else if (trashActionId.equals(TrashActionKeys.RENAME)) {
259                            actionId = ActionKeys.UPDATE;
260                    }
261                    else if (trashActionId.equals(TrashActionKeys.RESTORE)) {
262                            actionId = ActionKeys.DELETE;
263                    }
264    
265                    return hasPermission(permissionChecker, classPK, actionId);
266            }
267    
268            /**
269             * Returns <code>true</code> if the trash entry can be restored to its
270             * original location.
271             *
272             * <p>
273             * This method usually returns <code>false</code> if the container (e.g.
274             * folder) of the trash entry is no longer available (e.g. moved to the
275             * Recycle Bin or deleted).
276             * </p>
277             *
278             * @param  classPK the primary key of the trash entry
279             * @return <code>true</code> if the trash entry can be restored to its
280             *         original location; <code>false</code> otherwise
281             * @throws PortalException if a trash entry with the primary key could not
282             *         be found
283             * @throws SystemException if a system exception occurred
284             */
285            public boolean isRestorable(long classPK)
286                    throws PortalException, SystemException {
287    
288                    return true;
289            }
290    
291            public TrashEntry moveTrashEntry(
292                            long classPK, long containerModelId, ServiceContext serviceContext)
293                    throws PortalException, SystemException {
294    
295                    if (isRestorable(classPK)) {
296                            restoreTrashEntry(classPK);
297                    }
298    
299                    _log.error("moveTrashEntry() is not implemented in " +
300                            getClass().getName());
301    
302                    throw new SystemException();
303            }
304    
305            public void restoreTrashEntry(long classPK)
306                    throws PortalException, SystemException {
307    
308                    restoreTrashEntries(new long[] {classPK});
309            }
310    
311            /**
312             * Updates the title of the trash entry with the primary key. This method is
313             * called by {@link com.liferay.portlet.trash.action.EditEntryAction} before
314             * restoring the trash entry via its restore rename action.
315             *
316             * @param  classPK the primary key of the trash entry
317             * @param  title the title to be assigned
318             * @throws PortalException if a trash entry with the primary key could not
319             *         be found
320             * @throws SystemException if a system exception occurred
321             */
322            public void updateTitle(long classPK, String title)
323                    throws PortalException, SystemException {
324            }
325    
326            protected AssetRendererFactory getAssetRendererFactory() {
327                    return AssetRendererFactoryRegistryUtil.
328                            getAssetRendererFactoryByClassName(getClassName());
329            }
330    
331            protected abstract boolean hasPermission(
332                            PermissionChecker permissionChecker, long classPK, String actionId)
333                    throws PortalException, SystemException;
334    
335            private static Log _log = LogFactoryUtil.getLog(BaseTrashHandler.class);
336    
337    }