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