001    /**
002     * Copyright (c) 2000-present 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 aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.json.JSONFactoryUtil;
022    import com.liferay.portal.kernel.json.JSONObject;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.model.ContainerModel;
027    import com.liferay.portal.model.SystemEvent;
028    import com.liferay.portal.model.SystemEventConstants;
029    import com.liferay.portal.model.TrashedModel;
030    import com.liferay.portal.security.permission.ActionKeys;
031    import com.liferay.portal.security.permission.PermissionChecker;
032    import com.liferay.portal.service.ServiceContext;
033    import com.liferay.portal.service.SystemEventLocalServiceUtil;
034    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
035    import com.liferay.portlet.asset.model.AssetRenderer;
036    import com.liferay.portlet.asset.model.AssetRendererFactory;
037    import com.liferay.portlet.trash.model.TrashEntry;
038    
039    import java.util.Collections;
040    import java.util.List;
041    import java.util.Locale;
042    
043    import javax.portlet.PortletRequest;
044    
045    /**
046     * Provides the base implementation of {@link TrashHandler}.
047     *
048     * @author Alexander Chow
049     * @author Zsolt Berentey
050     * @see    TrashHandler
051     */
052    @ProviderType
053    public abstract class BaseTrashHandler implements TrashHandler {
054    
055            @Override
056            public SystemEvent addDeletionSystemEvent(
057                            long userId, long groupId, long classPK, String classUuid,
058                            String referrerClassName)
059                    throws PortalException {
060    
061                    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
062    
063                    extraDataJSONObject.put("inTrash", true);
064    
065                    return SystemEventLocalServiceUtil.addSystemEvent(
066                            userId, groupId, getSystemEventClassName(), classPK, classUuid,
067                            referrerClassName, SystemEventConstants.TYPE_DELETE,
068                            extraDataJSONObject.toString());
069            }
070    
071            /**
072             * @deprecated As of 7.0.0, replaced by {@link #checkRestorableEntry(long,
073             *             long, String)}
074             */
075            @Deprecated
076            @Override
077            public void checkDuplicateEntry(
078                            long classPK, long containerModelId, String newName)
079                    throws PortalException {
080    
081                    checkRestorableEntry(classPK, containerModelId, newName);
082            }
083    
084            /**
085             * @deprecated As of 7.0.0, replaced by {@link
086             *             #checkRestorableEntry(TrashEntry, long, String)}
087             */
088            @Deprecated
089            @Override
090            public void checkDuplicateTrashEntry(
091                            TrashEntry trashEntry, long containerModelId, String newName)
092                    throws PortalException {
093    
094                    checkRestorableEntry(trashEntry, containerModelId, newName);
095            }
096    
097            @Override
098            @SuppressWarnings("unused")
099            public void checkRestorableEntry(
100                            long classPK, long containerModelId, String newName)
101                    throws PortalException {
102            }
103    
104            @Override
105            @SuppressWarnings("unused")
106            public void checkRestorableEntry(
107                            TrashEntry trashEntry, long containerModelId, String newName)
108                    throws PortalException {
109            }
110    
111            @Override
112            @SuppressWarnings("unused")
113            public ContainerModel getContainerModel(long containerModelId)
114                    throws PortalException {
115    
116                    return null;
117            }
118    
119            /**
120             * @deprecated As of 7.0.0, replaced by {@link #getContainerModel(long)}
121             */
122            @Deprecated
123            @Override
124            public String getContainerModelClassName() {
125                    return getContainerModelClassName(0);
126            }
127    
128            @Override
129            public String getContainerModelClassName(long classPK) {
130                    return StringPool.BLANK;
131            }
132    
133            /**
134             * @deprecated As of 7.0.0, replaced by {@link
135             *             #getContainerModelName(long)}
136             */
137            @Deprecated
138            @Override
139            public String getContainerModelName() {
140                    return StringPool.BLANK;
141            }
142    
143            /**
144             * @throws PortalException
145             */
146            @Override
147            public String getContainerModelName(long classPK) throws PortalException {
148                    return StringPool.BLANK;
149            }
150    
151            @Override
152            @SuppressWarnings("unused")
153            public List<ContainerModel> getContainerModels(
154                            long classPK, long containerModelId, int start, int end)
155                    throws PortalException {
156    
157                    return Collections.emptyList();
158            }
159    
160            @Override
161            @SuppressWarnings("unused")
162            public int getContainerModelsCount(long classPK, long containerModelId)
163                    throws PortalException {
164    
165                    return 0;
166            }
167    
168            @Override
169            public String getDeleteMessage() {
170                    return "deleted-in-x";
171            }
172    
173            @Override
174            public long getDestinationContainerModelId(
175                    long classPK, long destinationContainerModelId) {
176    
177                    return destinationContainerModelId;
178            }
179    
180            @Override
181            @SuppressWarnings("unused")
182            public ContainerModel getParentContainerModel(long classPK)
183                    throws PortalException {
184    
185                    return null;
186            }
187    
188            @Override
189            public ContainerModel getParentContainerModel(TrashedModel trashedModel)
190                    throws PortalException {
191    
192                    if ((trashedModel == null) ||
193                            !(trashedModel instanceof ContainerModel)) {
194    
195                            return null;
196                    }
197    
198                    ContainerModel containerModel = (ContainerModel)trashedModel;
199    
200                    return getContainerModel(containerModel.getParentContainerModelId());
201            }
202    
203            @Override
204            @SuppressWarnings("unused")
205            public List<ContainerModel> getParentContainerModels(long classPK)
206                    throws PortalException {
207    
208                    return Collections.emptyList();
209            }
210    
211            @Override
212            @SuppressWarnings("unused")
213            public String getRestoreContainedModelLink(
214                            PortletRequest portletRequest, long classPK)
215                    throws PortalException {
216    
217                    return StringPool.BLANK;
218            }
219    
220            @Override
221            @SuppressWarnings("unused")
222            public String getRestoreContainerModelLink(
223                            PortletRequest portletRequest, long classPK)
224                    throws PortalException {
225    
226                    return StringPool.BLANK;
227            }
228    
229            @Override
230            @SuppressWarnings("unused")
231            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
232                    throws PortalException {
233    
234                    return StringPool.BLANK;
235            }
236    
237            @Override
238            public String getRootContainerModelClassName() {
239                    return getContainerModelClassName(0);
240            }
241    
242            /**
243             * @throws PortalException
244             */
245            @Override
246            public long getRootContainerModelId(long classPK) throws PortalException {
247                    return 0;
248            }
249    
250            @Override
251            public String getRootContainerModelName() {
252                    return StringPool.BLANK;
253            }
254    
255            /**
256             * @throws PortalException
257             */
258            @Override
259            public List<ContainerModel> getRootContainerModels(long groupId)
260                    throws PortalException {
261    
262                    return Collections.emptyList();
263            }
264    
265            @Override
266            public int getRootContainerModelsCount(long groupId) {
267                    return 0;
268            }
269    
270            /**
271             * @throws PortalException
272             */
273            @Override
274            public String getRootContainerModelTitle(
275                            long containerModelId, Locale locale)
276                    throws PortalException {
277    
278                    return StringPool.BLANK;
279            }
280    
281            @Override
282            public String getSubcontainerModelName() {
283                    return StringPool.BLANK;
284            }
285    
286            @Override
287            public String getSystemEventClassName() {
288                    return getClassName();
289            }
290    
291            @Override
292            public String getTrashContainedModelName() {
293                    return StringPool.BLANK;
294            }
295    
296            @Override
297            @SuppressWarnings("unused")
298            public int getTrashContainedModelsCount(long classPK)
299                    throws PortalException {
300    
301                    return 0;
302            }
303    
304            @Override
305            @SuppressWarnings("unused")
306            public List<TrashRenderer> getTrashContainedModelTrashRenderers(
307                            long classPK, int start, int end)
308                    throws PortalException {
309    
310                    return Collections.emptyList();
311            }
312    
313            @Override
314            public String getTrashContainerModelName() {
315                    return StringPool.BLANK;
316            }
317    
318            @Override
319            @SuppressWarnings("unused")
320            public int getTrashContainerModelsCount(long classPK)
321                    throws PortalException {
322    
323                    return 0;
324            }
325    
326            @Override
327            @SuppressWarnings("unused")
328            public List<TrashRenderer> getTrashContainerModelTrashRenderers(
329                            long classPK, int start, int end)
330                    throws PortalException {
331    
332                    return Collections.emptyList();
333            }
334    
335            @Override
336            @SuppressWarnings("unused")
337            public TrashEntry getTrashEntry(long classPK) throws PortalException {
338                    return null;
339            }
340    
341            @Override
342            public TrashRenderer getTrashRenderer(long classPK) throws PortalException {
343                    AssetRendererFactory assetRendererFactory = getAssetRendererFactory();
344    
345                    if (assetRendererFactory != null) {
346                            AssetRenderer assetRenderer = assetRendererFactory.getAssetRenderer(
347                                    classPK);
348    
349                            if (assetRenderer instanceof TrashRenderer) {
350                                    return (TrashRenderer)assetRenderer;
351                            }
352                    }
353    
354                    return null;
355            }
356    
357            @Override
358            public boolean hasTrashPermission(
359                            PermissionChecker permissionChecker, long groupId, long classPK,
360                            String trashActionId)
361                    throws PortalException {
362    
363                    String actionId = trashActionId;
364    
365                    if (trashActionId.equals(ActionKeys.DELETE)) {
366                            actionId = ActionKeys.DELETE;
367                    }
368                    else if (trashActionId.equals(TrashActionKeys.OVERWRITE)) {
369                            actionId = ActionKeys.DELETE;
370                    }
371                    else if (trashActionId.equals(TrashActionKeys.MOVE)) {
372                            return false;
373                    }
374                    else if (trashActionId.equals(TrashActionKeys.RENAME)) {
375                            actionId = ActionKeys.UPDATE;
376                    }
377                    else if (trashActionId.equals(TrashActionKeys.RESTORE)) {
378                            actionId = ActionKeys.DELETE;
379                    }
380    
381                    return hasPermission(permissionChecker, classPK, actionId);
382            }
383    
384            @Override
385            public boolean isContainerModel() {
386                    return false;
387            }
388    
389            @Override
390            public boolean isDeletable() {
391                    return true;
392            }
393    
394            @Override
395            @SuppressWarnings("unused")
396            public boolean isInTrashContainer(long classPK) throws PortalException {
397                    return false;
398            }
399    
400            @Override
401            public boolean isMovable() {
402                    return false;
403            }
404    
405            @Override
406            @SuppressWarnings("unused")
407            public boolean isRestorable(long classPK) throws PortalException {
408                    return true;
409            }
410    
411            @Override
412            public boolean isRootContainerModelMovable() {
413                    return false;
414            }
415    
416            @Override
417            @SuppressWarnings("unused")
418            public void moveEntry(
419                            long userId, long classPK, long containerModelId,
420                            ServiceContext serviceContext)
421                    throws PortalException {
422            }
423    
424            @Override
425            public void moveTrashEntry(
426                            long userId, long classPK, long containerModelId,
427                            ServiceContext serviceContext)
428                    throws PortalException {
429    
430                    if (isRestorable(classPK)) {
431                            restoreTrashEntry(userId, classPK);
432                    }
433    
434                    _log.error(
435                            "moveTrashEntry() is not implemented in " + getClass().getName());
436    
437                    throw new SystemException();
438            }
439    
440            @Override
441            @SuppressWarnings("unused")
442            public void restoreRelatedTrashEntry(String className, long classPK)
443                    throws PortalException {
444            }
445    
446            @Override
447            @SuppressWarnings("unused")
448            public void updateTitle(long classPK, String title) throws PortalException {
449            }
450    
451            protected AssetRendererFactory getAssetRendererFactory() {
452                    return AssetRendererFactoryRegistryUtil.
453                            getAssetRendererFactoryByClassName(getClassName());
454            }
455    
456            protected abstract boolean hasPermission(
457                            PermissionChecker permissionChecker, long classPK, String actionId)
458                    throws PortalException;
459    
460            private static final Log _log = LogFactoryUtil.getLog(
461                    BaseTrashHandler.class);
462    
463    }