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