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.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.TrashRenderer;
020    import com.liferay.portal.model.ContainerModel;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portlet.bookmarks.asset.BookmarksFolderAssetRenderer;
024    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
025    import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
026    import com.liferay.portlet.bookmarks.service.BookmarksFolderServiceUtil;
027    import com.liferay.portlet.bookmarks.service.permission.BookmarksFolderPermission;
028    import com.liferay.portlet.bookmarks.util.BookmarksUtil;
029    
030    import javax.portlet.PortletRequest;
031    
032    /**
033     * Represents the trash handler for bookmarks folder entity.
034     *
035     * @author Eudaldo Alonso
036     */
037    public class BookmarksFolderTrashHandler extends BookmarksBaseTrashHandler {
038    
039            public static final String CLASS_NAME = BookmarksFolder.class.getName();
040    
041            public void deleteTrashEntries(long[] classPKs, boolean checkPermission)
042                    throws PortalException, SystemException {
043    
044                    for (long classPK : classPKs) {
045                            if (checkPermission) {
046                                    BookmarksFolderServiceUtil.deleteFolder(classPK, false);
047                            }
048                            else {
049                                    BookmarksFolderLocalServiceUtil.deleteFolder(classPK, false);
050                            }
051                    }
052            }
053    
054            public String getClassName() {
055                    return CLASS_NAME;
056            }
057    
058            @Override
059            public String getDeleteMessage() {
060                    return "found-in-deleted-folder-x";
061            }
062    
063            @Override
064            public ContainerModel getParentContainerModel(long classPK)
065                    throws PortalException, SystemException {
066    
067                    BookmarksFolder folder = getBookmarksFolder(classPK);
068    
069                    long parentFolderId = folder.getParentFolderId();
070    
071                    if (parentFolderId <= 0) {
072                            return null;
073                    }
074    
075                    return getContainerModel(parentFolderId);
076            }
077    
078            @Override
079            public String getRestoreLink(PortletRequest portletRequest, long classPK)
080                    throws PortalException, SystemException {
081    
082                    BookmarksFolder folder = getBookmarksFolder(classPK);
083    
084                    return BookmarksUtil.getControlPanelLink(
085                            portletRequest, folder.getParentFolderId());
086            }
087    
088            @Override
089            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
090                    throws PortalException, SystemException {
091    
092                    BookmarksFolder folder = getBookmarksFolder(classPK);
093    
094                    return BookmarksUtil.getAbsolutePath(
095                            portletRequest, folder.getParentFolderId());
096            }
097    
098            @Override
099            public TrashRenderer getTrashRenderer(long classPK)
100                    throws PortalException, SystemException {
101    
102                    BookmarksFolder folder = getBookmarksFolder(classPK);
103    
104                    return new BookmarksFolderAssetRenderer(folder);
105            }
106    
107            @Override
108            public boolean isContainerModel() {
109                    return true;
110            }
111    
112            public boolean isInTrash(long classPK)
113                    throws PortalException, SystemException {
114    
115                    BookmarksFolder folder = getBookmarksFolder(classPK);
116    
117                    return folder.isInTrash();
118            }
119    
120            @Override
121            public boolean isRestorable(long classPK)
122                    throws PortalException, SystemException {
123    
124                    BookmarksFolder folder = getBookmarksFolder(classPK);
125    
126                    return !folder.isInTrashContainer();
127            }
128    
129            @Override
130            public void moveEntry(
131                            long classPK, long containerModelId, ServiceContext serviceContext)
132                    throws PortalException, SystemException {
133    
134                    BookmarksFolderServiceUtil.moveFolder(classPK, containerModelId);
135            }
136    
137            @Override
138            public void moveTrashEntry(
139                            long classPK, long containerId, ServiceContext serviceContext)
140                    throws PortalException, SystemException {
141    
142                    BookmarksFolderServiceUtil.moveFolderFromTrash(classPK, containerId);
143            }
144    
145            public void restoreTrashEntries(long[] classPKs)
146                    throws PortalException, SystemException {
147    
148                    for (long classPK : classPKs) {
149                            BookmarksFolderServiceUtil.restoreFolderFromTrash(classPK);
150                    }
151            }
152    
153            @Override
154            protected BookmarksFolder getBookmarksFolder(long classPK)
155                    throws PortalException, SystemException {
156    
157                    return BookmarksFolderLocalServiceUtil.getFolder(classPK);
158            }
159    
160            @Override
161            protected boolean hasPermission(
162                            PermissionChecker permissionChecker, long classPK, String actionId)
163                    throws PortalException, SystemException {
164    
165                    BookmarksFolder folder = getBookmarksFolder(classPK);
166    
167                    return BookmarksFolderPermission.contains(
168                            permissionChecker, folder, actionId);
169            }
170    
171    }