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