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            public String getTrashContainerModelName() {
220                    return StringPool.BLANK;
221            }
222    
223            @Override
224            @SuppressWarnings("unused")
225            public int getTrashContainerModelsCount(long classPK)
226                    throws PortalException, SystemException {
227    
228                    return 0;
229            }
230    
231            @Override
232            @SuppressWarnings("unused")
233            public List<TrashRenderer> getTrashContainerModelTrashRenderers(
234                            long classPK, int start, int end)
235                    throws PortalException, SystemException {
236    
237                    return Collections.emptyList();
238            }
239    
240            @Override
241            @SuppressWarnings("unused")
242            public TrashEntry getTrashEntry(long classPK)
243                    throws PortalException, SystemException {
244    
245                    return null;
246            }
247    
248            @Override
249            public TrashRenderer getTrashRenderer(long classPK)
250                    throws PortalException, SystemException {
251    
252                    AssetRendererFactory assetRendererFactory = getAssetRendererFactory();
253    
254                    if (assetRendererFactory != null) {
255                            AssetRenderer assetRenderer = assetRendererFactory.getAssetRenderer(
256                                    classPK);
257    
258                            if (assetRenderer instanceof TrashRenderer) {
259                                    return (TrashRenderer)assetRenderer;
260                            }
261                    }
262    
263                    return null;
264            }
265    
266            @Override
267            public boolean hasTrashPermission(
268                            PermissionChecker permissionChecker, long groupId, long classPK,
269                            String trashActionId)
270                    throws PortalException, SystemException {
271    
272                    String actionId = trashActionId;
273    
274                    if (trashActionId.equals(ActionKeys.DELETE)) {
275                            actionId = ActionKeys.DELETE;
276                    }
277                    else if (trashActionId.equals(TrashActionKeys.OVERWRITE)) {
278                            actionId = ActionKeys.DELETE;
279                    }
280                    else if (trashActionId.equals(TrashActionKeys.MOVE)) {
281                            return false;
282                    }
283                    else if (trashActionId.equals(TrashActionKeys.RENAME)) {
284                            actionId = ActionKeys.UPDATE;
285                    }
286                    else if (trashActionId.equals(TrashActionKeys.RESTORE)) {
287                            actionId = ActionKeys.DELETE;
288                    }
289    
290                    return hasPermission(permissionChecker, classPK, actionId);
291            }
292    
293            @Override
294            public boolean isContainerModel() {
295                    return false;
296            }
297    
298            @Override
299            public boolean isDeletable() {
300                    return true;
301            }
302    
303            @Override
304            @SuppressWarnings("unused")
305            public boolean isInTrashContainer(long classPK)
306                    throws PortalException, SystemException {
307    
308                    return false;
309            }
310    
311            @Override
312            public boolean isMovable() {
313                    return false;
314            }
315    
316            @Override
317            @SuppressWarnings("unused")
318            public boolean isRestorable(long classPK)
319                    throws PortalException, SystemException {
320    
321                    return true;
322            }
323    
324            @Override
325            public boolean isTrashEntry(
326                    TrashEntry trashEntry, ClassedModel classedModel) {
327    
328                    if ((trashEntry == null) || (classedModel == null)) {
329                            return false;
330                    }
331    
332                    String className = getClassName();
333    
334                    if (!className.equals(trashEntry.getClassName())) {
335                            return false;
336                    }
337    
338                    Serializable primaryKeyObj = classedModel.getPrimaryKeyObj();
339    
340                    if (!(primaryKeyObj instanceof Long)) {
341                            return false;
342                    }
343    
344                    if (trashEntry.getClassPK() == (Long)primaryKeyObj) {
345                            return true;
346                    }
347    
348                    return false;
349            }
350    
351            @Override
352            @SuppressWarnings("unused")
353            public void moveEntry(
354                            long userId, long classPK, long containerModelId,
355                            ServiceContext serviceContext)
356                    throws PortalException, SystemException {
357            }
358    
359            @Override
360            public void moveTrashEntry(
361                            long userId, long classPK, long containerModelId,
362                            ServiceContext serviceContext)
363                    throws PortalException, SystemException {
364    
365                    if (isRestorable(classPK)) {
366                            restoreTrashEntry(userId, classPK);
367                    }
368    
369                    _log.error(
370                            "moveTrashEntry() is not implemented in " + getClass().getName());
371    
372                    throw new SystemException();
373            }
374    
375            @Override
376            @SuppressWarnings("unused")
377            public void restoreRelatedTrashEntry(String className, long classPK)
378                    throws PortalException, SystemException {
379            }
380    
381            @Override
382            @SuppressWarnings("unused")
383            public void updateTitle(long classPK, String title)
384                    throws PortalException, SystemException {
385            }
386    
387            protected AssetRendererFactory getAssetRendererFactory() {
388                    return AssetRendererFactoryRegistryUtil.
389                            getAssetRendererFactoryByClassName(getClassName());
390            }
391    
392            protected abstract boolean hasPermission(
393                            PermissionChecker permissionChecker, long classPK, String actionId)
394                    throws PortalException, SystemException;
395    
396            private static Log _log = LogFactoryUtil.getLog(BaseTrashHandler.class);
397    
398    }