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.documentlibrary.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.BaseTrashHandler;
020    import com.liferay.portal.kernel.trash.TrashActionKeys;
021    import com.liferay.portal.kernel.trash.TrashRenderer;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
025    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
026    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
027    import com.liferay.portlet.documentlibrary.service.DLFileShortcutLocalServiceUtil;
028    import com.liferay.portlet.documentlibrary.service.permission.DLFileShortcutPermission;
029    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
030    import com.liferay.portlet.documentlibrary.util.DLUtil;
031    
032    import javax.portlet.PortletRequest;
033    
034    /**
035     * Implements trash handling for the file shortcut entity.
036     *
037     * @author Zsolt Berentey
038     */
039    public class DLFileShortcutTrashHandler extends BaseTrashHandler {
040    
041            public static final String CLASS_NAME = DLFileShortcut.class.getName();
042    
043            public void deleteTrashEntries(long[] classPKs, boolean checkPermission)
044                    throws PortalException, SystemException {
045    
046                    for (long classPK : classPKs) {
047                            if (checkPermission) {
048                                    DLAppServiceUtil.deleteFileShortcut(classPK);
049                            }
050                            else {
051                                    DLAppLocalServiceUtil.deleteFileShortcut(classPK);
052                            }
053                    }
054            }
055    
056            public String getClassName() {
057                    return CLASS_NAME;
058            }
059    
060            @Override
061            public String getRestoreLink(PortletRequest portletRequest, long classPK)
062                    throws PortalException, SystemException {
063    
064                    DLFileShortcut fileShortcut =
065                            DLFileShortcutLocalServiceUtil.getDLFileShortcut(classPK);
066    
067                    return DLUtil.getDLControlPanelLink(
068                            portletRequest, fileShortcut.getFolderId());
069            }
070    
071            @Override
072            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
073                    throws PortalException, SystemException {
074    
075                    DLFileShortcut fileShortcut =
076                            DLFileShortcutLocalServiceUtil.getDLFileShortcut(classPK);
077    
078                    return DLUtil.getAbsolutePath(
079                            portletRequest, fileShortcut.getFolderId());
080            }
081    
082            @Override
083            public TrashRenderer getTrashRenderer(long classPK)
084                    throws PortalException, SystemException {
085    
086                    DLFileShortcut fileShortcut =
087                            DLFileShortcutLocalServiceUtil.getDLFileShortcut(classPK);
088    
089                    return new DLFileShortcutTrashRenderer(fileShortcut);
090            }
091    
092            @Override
093            public boolean hasTrashPermission(
094                            PermissionChecker permissionChecker, long groupId, long classPK,
095                            String trashActionId)
096                    throws PortalException, SystemException {
097    
098                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
099                            return DLFolderPermission.contains(
100                                    permissionChecker, groupId, classPK, ActionKeys.ADD_SHORTCUT);
101                    }
102    
103                    return super.hasTrashPermission(
104                            permissionChecker, groupId, classPK, trashActionId);
105            }
106    
107            public boolean isInTrash(long classPK)
108                    throws PortalException, SystemException {
109    
110                    DLFileShortcut fileShortcut =
111                            DLFileShortcutLocalServiceUtil.getDLFileShortcut(classPK);
112    
113                    if (fileShortcut.isInTrash() || fileShortcut.isInTrashFolder()) {
114                            return true;
115                    }
116    
117                    return false;
118            }
119    
120            public void restoreTrashEntries(long[] classPKs)
121                    throws PortalException, SystemException {
122    
123                    for (long classPK : classPKs) {
124                            DLAppServiceUtil.restoreFileShortcutFromTrash(classPK);
125                    }
126            }
127    
128            @Override
129            protected boolean hasPermission(
130                            PermissionChecker permissionChecker, long classPK, String actionId)
131                    throws PortalException, SystemException {
132    
133                    return DLFileShortcutPermission.contains(
134                            permissionChecker, classPK, actionId);
135            }
136    
137    }