001    /**
002     * Copyright (c) 2000-present 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.InvalidRepositoryException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.repository.Repository;
020    import com.liferay.portal.kernel.repository.capabilities.TrashCapability;
021    import com.liferay.portal.kernel.trash.TrashActionKeys;
022    import com.liferay.portal.kernel.trash.TrashRenderer;
023    import com.liferay.portal.model.ContainerModel;
024    import com.liferay.portal.model.TrashedModel;
025    import com.liferay.portal.security.permission.ActionKeys;
026    import com.liferay.portal.security.permission.PermissionChecker;
027    import com.liferay.portal.service.RepositoryServiceUtil;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
030    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
031    import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalServiceUtil;
032    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
033    import com.liferay.portlet.documentlibrary.service.DLFileShortcutLocalServiceUtil;
034    import com.liferay.portlet.documentlibrary.service.permission.DLFileShortcutPermission;
035    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
036    import com.liferay.portlet.documentlibrary.util.DLUtil;
037    import com.liferay.portlet.trash.model.TrashEntry;
038    
039    import javax.portlet.PortletRequest;
040    
041    /**
042     * Implements trash handling for the file shortcut entity.
043     *
044     * @author Zsolt Berentey
045     */
046    public class DLFileShortcutTrashHandler extends DLBaseTrashHandler {
047    
048            @Override
049            public void deleteTrashEntry(long classPK) throws PortalException {
050                    DLAppLocalServiceUtil.deleteFileShortcut(classPK);
051            }
052    
053            @Override
054            public String getClassName() {
055                    return DLFileShortcut.class.getName();
056            }
057    
058            @Override
059            public ContainerModel getParentContainerModel(long classPK)
060                    throws PortalException {
061    
062                    DLFileShortcut dlFileShortcut = getDLFileShortcut(classPK);
063    
064                    long parentFolderId = dlFileShortcut.getFolderId();
065    
066                    if (parentFolderId <= 0) {
067                            return null;
068                    }
069    
070                    return getContainerModel(parentFolderId);
071            }
072    
073            @Override
074            public ContainerModel getParentContainerModel(TrashedModel trashedModel)
075                    throws PortalException {
076    
077                    DLFileShortcut fileShortcut = (DLFileShortcut)trashedModel;
078    
079                    return getContainerModel(fileShortcut.getFolderId());
080            }
081    
082            @Override
083            public String getRestoreContainedModelLink(
084                            PortletRequest portletRequest, long classPK)
085                    throws PortalException {
086    
087                    DLFileShortcut dlFileShortcut = getDLFileShortcut(classPK);
088    
089                    return DLUtil.getDLFileEntryControlPanelLink(
090                            portletRequest, dlFileShortcut.getToFileEntryId());
091            }
092    
093            @Override
094            public String getRestoreContainerModelLink(
095                            PortletRequest portletRequest, long classPK)
096                    throws PortalException {
097    
098                    DLFileShortcut fileShortcut = getDLFileShortcut(classPK);
099    
100                    return DLUtil.getDLFolderControlPanelLink(
101                            portletRequest, fileShortcut.getFolderId());
102            }
103    
104            @Override
105            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
106                    throws PortalException {
107    
108                    DLFileShortcut fileShortcut = getDLFileShortcut(classPK);
109    
110                    return DLUtil.getAbsolutePath(
111                            portletRequest, fileShortcut.getFolderId());
112            }
113    
114            @Override
115            public TrashEntry getTrashEntry(long classPK) throws PortalException {
116                    DLFileShortcut fileShortcut = getDLFileShortcut(classPK);
117    
118                    return fileShortcut.getTrashEntry();
119            }
120    
121            @Override
122            public TrashRenderer getTrashRenderer(long classPK) throws PortalException {
123                    DLFileShortcut fileShortcut = getDLFileShortcut(classPK);
124    
125                    return new DLFileShortcutTrashRenderer(fileShortcut);
126            }
127    
128            @Override
129            public boolean hasTrashPermission(
130                            PermissionChecker permissionChecker, long groupId, long classPK,
131                            String trashActionId)
132                    throws PortalException {
133    
134                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
135                            return DLFolderPermission.contains(
136                                    permissionChecker, groupId, classPK, ActionKeys.ADD_SHORTCUT);
137                    }
138    
139                    return super.hasTrashPermission(
140                            permissionChecker, groupId, classPK, trashActionId);
141            }
142    
143            @Override
144            public boolean isInTrash(long classPK) throws PortalException {
145                    DLFileShortcut fileShortcut = getDLFileShortcut(classPK);
146    
147                    return fileShortcut.isInTrash();
148            }
149    
150            @Override
151            public boolean isInTrashContainer(long classPK) throws PortalException {
152                    DLFileShortcut fileShortcut = getDLFileShortcut(classPK);
153    
154                    return fileShortcut.isInTrashContainer();
155            }
156    
157            @Override
158            public boolean isRestorable(long classPK) throws PortalException {
159                    DLFileShortcut dlFileShortcut = getDLFileShortcut(classPK);
160    
161                    try {
162                            dlFileShortcut.getFolder();
163                    }
164                    catch (NoSuchFolderException nsfe) {
165                            return false;
166                    }
167    
168                    return !dlFileShortcut.isInTrashContainer();
169            }
170    
171            @Override
172            public void moveEntry(
173                            long userId, long classPK, long containerModelId,
174                            ServiceContext serviceContext)
175                    throws PortalException {
176    
177                    DLFileShortcut dlFileShortcut = getDLFileShortcut(classPK);
178    
179                    DLAppLocalServiceUtil.updateFileShortcut(
180                            userId, classPK, containerModelId,
181                            dlFileShortcut.getToFileEntryId(), serviceContext);
182            }
183    
184            @Override
185            public void moveTrashEntry(
186                            long userId, long classPK, long containerModelId,
187                            ServiceContext serviceContext)
188                    throws PortalException {
189    
190                    DLAppHelperLocalServiceUtil.moveFileShortcutFromTrash(
191                            userId, getDLFileShortcut(classPK), containerModelId,
192                            serviceContext);
193            }
194    
195            @Override
196            public void restoreTrashEntry(long userId, long classPK)
197                    throws PortalException {
198    
199                    DLAppHelperLocalServiceUtil.restoreFileShortcutFromTrash(
200                            userId, getDLFileShortcut(classPK));
201            }
202    
203            protected DLFileShortcut getDLFileShortcut(long classPK)
204                    throws PortalException {
205    
206                    return DLFileShortcutLocalServiceUtil.getDLFileShortcut(classPK);
207            }
208    
209            @Override
210            protected Repository getRepository(long classPK) throws PortalException {
211                    DLFileShortcut dlFileShortcut = getDLFileShortcut(classPK);
212    
213                    Repository repository = RepositoryServiceUtil.getRepositoryImpl(
214                            0, dlFileShortcut.getToFileEntryId(), 0);
215    
216                    if (!repository.isCapabilityProvided(TrashCapability.class)) {
217                            throw new InvalidRepositoryException(
218                                    "Repository " + repository.getRepositoryId() +
219                                            " does not support trash operations");
220                    }
221    
222                    return repository;
223            }
224    
225            @Override
226            protected boolean hasPermission(
227                            PermissionChecker permissionChecker, long classPK, String actionId)
228                    throws PortalException {
229    
230                    DLFileShortcut dlFileShortcut = getDLFileShortcut(classPK);
231    
232                    if (dlFileShortcut.isInHiddenFolder() &&
233                            actionId.equals(ActionKeys.VIEW)) {
234    
235                            return false;
236                    }
237    
238                    return DLFileShortcutPermission.contains(
239                            permissionChecker, classPK, actionId);
240            }
241    
242    }