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 getRestoreContainedModelLink(
076                            PortletRequest portletRequest, long classPK)
077                    throws PortalException, SystemException {
078    
079                    DLFileShortcut dlFileShortcut = getDLFileShortcut(classPK);
080    
081                    return DLUtil.getDLFileEntryControlPanelLink(
082                            portletRequest, dlFileShortcut.getToFileEntryId());
083            }
084    
085            @Override
086            public String getRestoreContainerModelLink(
087                            PortletRequest portletRequest, long classPK)
088                    throws PortalException, SystemException {
089    
090                    DLFileShortcut fileShortcut = getDLFileShortcut(classPK);
091    
092                    return DLUtil.getDLFolderControlPanelLink(
093                            portletRequest, fileShortcut.getFolderId());
094            }
095    
096            @Override
097            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
098                    throws PortalException, SystemException {
099    
100                    DLFileShortcut fileShortcut = getDLFileShortcut(classPK);
101    
102                    return DLUtil.getAbsolutePath(
103                            portletRequest, fileShortcut.getFolderId());
104            }
105    
106            @Override
107            public ContainerModel getTrashContainer(long classPK)
108                    throws PortalException, SystemException {
109    
110                    DLFileShortcut dlFileShortcut =
111                            DLFileShortcutLocalServiceUtil.getDLFileShortcut(classPK);
112    
113                    return dlFileShortcut.getTrashContainer();
114            }
115    
116            @Override
117            public TrashRenderer getTrashRenderer(long classPK)
118                    throws PortalException, SystemException {
119    
120                    DLFileShortcut fileShortcut = getDLFileShortcut(classPK);
121    
122                    return new DLFileShortcutTrashRenderer(fileShortcut);
123            }
124    
125            @Override
126            public boolean hasTrashPermission(
127                            PermissionChecker permissionChecker, long groupId, long classPK,
128                            String trashActionId)
129                    throws PortalException, SystemException {
130    
131                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
132                            return DLFolderPermission.contains(
133                                    permissionChecker, groupId, classPK, ActionKeys.ADD_SHORTCUT);
134                    }
135    
136                    return super.hasTrashPermission(
137                            permissionChecker, groupId, classPK, trashActionId);
138            }
139    
140            @Override
141            public boolean isInTrash(long classPK)
142                    throws PortalException, SystemException {
143    
144                    DLFileShortcut fileShortcut = getDLFileShortcut(classPK);
145    
146                    return fileShortcut.isInTrash();
147            }
148    
149            @Override
150            public boolean isInTrashContainer(long classPK)
151                    throws PortalException, SystemException {
152    
153                    DLFileShortcut fileShortcut = getDLFileShortcut(classPK);
154    
155                    return fileShortcut.isInTrashContainer();
156            }
157    
158            @Override
159            public boolean isRestorable(long classPK)
160                    throws PortalException, SystemException {
161    
162                    DLFileShortcut dlFileShortcut = getDLFileShortcut(classPK);
163    
164                    try {
165                            dlFileShortcut.getFolder();
166                    }
167                    catch (NoSuchFolderException nsfe) {
168                            return false;
169                    }
170    
171                    return !dlFileShortcut.isInTrashContainer();
172            }
173    
174            @Override
175            public void moveEntry(
176                            long userId, long classPK, long containerModelId,
177                            ServiceContext serviceContext)
178                    throws PortalException, SystemException {
179    
180                    DLFileShortcut dlFileShortcut = getDLFileShortcut(classPK);
181    
182                    DLAppLocalServiceUtil.updateFileShortcut(
183                            userId, classPK, containerModelId,
184                            dlFileShortcut.getToFileEntryId(), serviceContext);
185            }
186    
187            @Override
188            public void moveTrashEntry(
189                            long userId, long classPK, long containerModelId,
190                            ServiceContext serviceContext)
191                    throws PortalException, SystemException {
192    
193                    DLAppHelperLocalServiceUtil.moveFileShortcutFromTrash(
194                            userId, getDLFileShortcut(classPK), containerModelId,
195                            serviceContext);
196            }
197    
198            @Override
199            public void restoreTrashEntry(long userId, long classPK)
200                    throws PortalException, SystemException {
201    
202                    DLAppHelperLocalServiceUtil.restoreFileShortcutFromTrash(
203                            userId, getDLFileShortcut(classPK));
204            }
205    
206            protected DLFileShortcut getDLFileShortcut(long classPK)
207                    throws PortalException, SystemException {
208    
209                    return DLFileShortcutLocalServiceUtil.getDLFileShortcut(classPK);
210            }
211    
212            @Override
213            protected Repository getRepository(long classPK)
214                    throws PortalException, SystemException {
215    
216                    DLFileShortcut dlFileShortcut = getDLFileShortcut(classPK);
217    
218                    Repository repository = RepositoryServiceUtil.getRepositoryImpl(
219                            0, dlFileShortcut.getToFileEntryId(), 0);
220    
221                    if (!(repository instanceof LiferayRepository)) {
222                            throw new InvalidRepositoryException(
223                                    "Repository " + repository.getRepositoryId() +
224                                            " does not support trash operations");
225                    }
226    
227                    return repository;
228            }
229    
230            @Override
231            protected boolean hasPermission(
232                            PermissionChecker permissionChecker, long classPK, String actionId)
233                    throws PortalException, SystemException {
234    
235                    DLFileShortcut dlFileShortcut = getDLFileShortcut(classPK);
236    
237                    if (dlFileShortcut.isInHiddenFolder() &&
238                            actionId.equals(ActionKeys.VIEW)) {
239    
240                            return false;
241                    }
242    
243                    return DLFileShortcutPermission.contains(
244                            permissionChecker, classPK, actionId);
245            }
246    
247    }