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.portlet.bookmarks.trash;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.trash.TrashActionKeys;
020    import com.liferay.portal.model.ContainerModel;
021    import com.liferay.portal.model.TrashedModel;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
026    import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
027    import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
028    import com.liferay.portlet.bookmarks.service.permission.BookmarksEntryPermission;
029    import com.liferay.portlet.bookmarks.service.permission.BookmarksFolderPermission;
030    import com.liferay.portlet.bookmarks.util.BookmarksUtil;
031    import com.liferay.portlet.trash.model.TrashEntry;
032    
033    import javax.portlet.PortletRequest;
034    
035    /**
036     * Represents the trash handler for bookmarks entries entity.
037     *
038     * @author Levente Hud??k
039     * @author Zsolt Berentey
040     */
041    public class BookmarksEntryTrashHandler extends BookmarksBaseTrashHandler {
042    
043            @Override
044            public void deleteTrashEntry(long classPK)
045                    throws PortalException, SystemException {
046    
047                    BookmarksEntryLocalServiceUtil.deleteEntry(classPK);
048            }
049    
050            @Override
051            public String getClassName() {
052                    return BookmarksEntry.class.getName();
053            }
054    
055            @Override
056            public ContainerModel getParentContainerModel(long classPK)
057                    throws PortalException, SystemException {
058    
059                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);
060    
061                    long parentFolderId = entry.getFolderId();
062    
063                    if (parentFolderId <= 0) {
064                            return null;
065                    }
066    
067                    return getContainerModel(parentFolderId);
068            }
069    
070            @Override
071            public ContainerModel getParentContainerModel(TrashedModel trashedModel)
072                    throws PortalException, SystemException {
073    
074                    BookmarksEntry entry = (BookmarksEntry)trashedModel;
075    
076                    return getContainerModel(entry.getFolderId());
077            }
078    
079            @Override
080            public String getRestoreContainerModelLink(
081                            PortletRequest portletRequest, long classPK)
082                    throws PortalException, SystemException {
083    
084                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);
085    
086                    return BookmarksUtil.getControlPanelLink(
087                            portletRequest, entry.getFolderId());
088            }
089    
090            @Override
091            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
092                    throws PortalException, SystemException {
093    
094                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);
095    
096                    return BookmarksUtil.getAbsolutePath(
097                            portletRequest, entry.getFolderId());
098            }
099    
100            @Override
101            public TrashEntry getTrashEntry(long classPK)
102                    throws PortalException, SystemException {
103    
104                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);
105    
106                    return entry.getTrashEntry();
107            }
108    
109            @Override
110            public boolean hasTrashPermission(
111                            PermissionChecker permissionChecker, long groupId, long classPK,
112                            String trashActionId)
113                    throws PortalException, SystemException {
114    
115                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
116                            return BookmarksFolderPermission.contains(
117                                    permissionChecker, groupId, classPK, ActionKeys.ADD_ENTRY);
118                    }
119    
120                    return super.hasTrashPermission(
121                            permissionChecker, groupId, classPK, trashActionId);
122            }
123    
124            @Override
125            public boolean isInTrash(long classPK)
126                    throws PortalException, SystemException {
127    
128                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);
129    
130                    return entry.isInTrash();
131            }
132    
133            @Override
134            public boolean isInTrashContainer(long classPK)
135                    throws PortalException, SystemException {
136    
137                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);
138    
139                    return entry.isInTrashContainer();
140            }
141    
142            @Override
143            public boolean isRestorable(long classPK)
144                    throws PortalException, SystemException {
145    
146                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);
147    
148                    if ((entry.getFolderId() > 0) &&
149                            (BookmarksFolderLocalServiceUtil.fetchBookmarksFolder(
150                                    entry.getFolderId()) == null)) {
151    
152                            return false;
153                    }
154    
155                    return !entry.isInTrashContainer();
156            }
157    
158            @Override
159            public void moveEntry(
160                            long userId, long classPK, long containerModelId,
161                            ServiceContext serviceContext)
162                    throws PortalException, SystemException {
163    
164                    BookmarksEntryLocalServiceUtil.moveEntry(classPK, containerModelId);
165            }
166    
167            @Override
168            public void moveTrashEntry(
169                            long userId, long classPK, long containerId,
170                            ServiceContext serviceContext)
171                    throws PortalException, SystemException {
172    
173                    BookmarksEntryLocalServiceUtil.moveEntryFromTrash(
174                            userId, classPK, containerId);
175            }
176    
177            @Override
178            public void restoreTrashEntry(long userId, long classPK)
179                    throws PortalException, SystemException {
180    
181                    BookmarksEntryLocalServiceUtil.restoreEntryFromTrash(userId, classPK);
182            }
183    
184            @Override
185            protected long getGroupId(long classPK)
186                    throws PortalException, SystemException {
187    
188                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);
189    
190                    return entry.getGroupId();
191            }
192    
193            @Override
194            protected boolean hasPermission(
195                            PermissionChecker permissionChecker, long classPK, String actionId)
196                    throws PortalException, SystemException {
197    
198                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);
199    
200                    return BookmarksEntryPermission.contains(
201                            permissionChecker, entry, actionId);
202            }
203    
204    }