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