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