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 TrashEntry getTrashEntry(long classPK)
119                    throws PortalException, SystemException {
120    
121                    DLFileShortcut fileShortcut = getDLFileShortcut(classPK);
122    
123                    return fileShortcut.getTrashEntry();
124            }
125    
126            @Override
127            public TrashRenderer getTrashRenderer(long classPK)
128                    throws PortalException, SystemException {
129    
130                    DLFileShortcut fileShortcut = getDLFileShortcut(classPK);
131    
132                    return new DLFileShortcutTrashRenderer(fileShortcut);
133            }
134    
135            @Override
136            public boolean hasTrashPermission(
137                            PermissionChecker permissionChecker, long groupId, long classPK,
138                            String trashActionId)
139                    throws PortalException, SystemException {
140    
141                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
142                            return DLFolderPermission.contains(
143                                    permissionChecker, groupId, classPK, ActionKeys.ADD_SHORTCUT);
144                    }
145    
146                    return super.hasTrashPermission(
147                            permissionChecker, groupId, classPK, trashActionId);
148            }
149    
150            @Override
151            public boolean isInTrash(long classPK)
152                    throws PortalException, SystemException {
153    
154                    DLFileShortcut fileShortcut = getDLFileShortcut(classPK);
155    
156                    return fileShortcut.isInTrash();
157            }
158    
159            @Override
160            public boolean isInTrashContainer(long classPK)
161                    throws PortalException, SystemException {
162    
163                    DLFileShortcut fileShortcut = getDLFileShortcut(classPK);
164    
165                    return fileShortcut.isInTrashContainer();
166            }
167    
168            @Override
169            public boolean isRestorable(long classPK)
170                    throws PortalException, SystemException {
171    
172                    DLFileShortcut dlFileShortcut = getDLFileShortcut(classPK);
173    
174                    try {
175                            dlFileShortcut.getFolder();
176                    }
177                    catch (NoSuchFolderException nsfe) {
178                            return false;
179                    }
180    
181                    return !dlFileShortcut.isInTrashContainer();
182            }
183    
184            @Override
185            public void moveEntry(
186                            long userId, long classPK, long containerModelId,
187                            ServiceContext serviceContext)
188                    throws PortalException, SystemException {
189    
190                    DLFileShortcut dlFileShortcut = getDLFileShortcut(classPK);
191    
192                    DLAppLocalServiceUtil.updateFileShortcut(
193                            userId, classPK, containerModelId,
194                            dlFileShortcut.getToFileEntryId(), serviceContext);
195            }
196    
197            @Override
198            public void moveTrashEntry(
199                            long userId, long classPK, long containerModelId,
200                            ServiceContext serviceContext)
201                    throws PortalException, SystemException {
202    
203                    DLAppHelperLocalServiceUtil.moveFileShortcutFromTrash(
204                            userId, getDLFileShortcut(classPK), containerModelId,
205                            serviceContext);
206            }
207    
208            @Override
209            public void restoreTrashEntry(long userId, long classPK)
210                    throws PortalException, SystemException {
211    
212                    DLAppHelperLocalServiceUtil.restoreFileShortcutFromTrash(
213                            userId, getDLFileShortcut(classPK));
214            }
215    
216            protected DLFileShortcut getDLFileShortcut(long classPK)
217                    throws PortalException, SystemException {
218    
219                    return DLFileShortcutLocalServiceUtil.getDLFileShortcut(classPK);
220            }
221    
222            @Override
223            protected Repository getRepository(long classPK)
224                    throws PortalException, SystemException {
225    
226                    DLFileShortcut dlFileShortcut = getDLFileShortcut(classPK);
227    
228                    Repository repository = RepositoryServiceUtil.getRepositoryImpl(
229                            0, dlFileShortcut.getToFileEntryId(), 0);
230    
231                    if (!(repository instanceof LiferayRepository)) {
232                            throw new InvalidRepositoryException(
233                                    "Repository " + repository.getRepositoryId() +
234                                            " does not support trash operations");
235                    }
236    
237                    return repository;
238            }
239    
240            @Override
241            protected boolean hasPermission(
242                            PermissionChecker permissionChecker, long classPK, String actionId)
243                    throws PortalException, SystemException {
244    
245                    DLFileShortcut dlFileShortcut = getDLFileShortcut(classPK);
246    
247                    if (dlFileShortcut.isInHiddenFolder() &&
248                            actionId.equals(ActionKeys.VIEW)) {
249    
250                            return false;
251                    }
252    
253                    return DLFileShortcutPermission.contains(
254                            permissionChecker, classPK, actionId);
255            }
256    
257    }