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.repository.model.Folder;
022    import com.liferay.portal.kernel.trash.TrashActionKeys;
023    import com.liferay.portal.kernel.trash.TrashRenderer;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.model.ContainerModel;
026    import com.liferay.portal.repository.liferayrepository.LiferayRepository;
027    import com.liferay.portal.security.permission.ActionKeys;
028    import com.liferay.portal.security.permission.PermissionChecker;
029    import com.liferay.portal.service.RepositoryServiceUtil;
030    import com.liferay.portal.service.ServiceContext;
031    import com.liferay.portlet.documentlibrary.asset.DLFolderAssetRenderer;
032    import com.liferay.portlet.documentlibrary.model.DLFolder;
033    import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalServiceUtil;
034    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
035    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
036    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
037    import com.liferay.portlet.documentlibrary.util.DLUtil;
038    import com.liferay.portlet.trash.DuplicateEntryException;
039    import com.liferay.portlet.trash.TrashEntryConstants;
040    import com.liferay.portlet.trash.model.TrashEntry;
041    
042    import javax.portlet.PortletRequest;
043    
044    /**
045     * Implements trash handling for the folder entity.
046     *
047     * @author Alexander Chow
048     * @author Zsolt Berentey
049     */
050    public class DLFolderTrashHandler extends DLBaseTrashHandler {
051    
052            @Override
053            public void checkDuplicateEntry(
054                            long classPK, long containerModelId, String newName)
055                    throws PortalException, SystemException {
056    
057                    DLFolder dlFolder = getDLFolder(classPK);
058    
059                    checkDuplicateEntry(
060                            classPK, 0, containerModelId, dlFolder.getName(), newName);
061            }
062    
063            @Override
064            public void checkDuplicateTrashEntry(
065                            TrashEntry trashEntry, long containerModelId, String newName)
066                    throws PortalException, SystemException {
067    
068                    checkDuplicateEntry(
069                            trashEntry.getClassPK(), trashEntry.getEntryId(), containerModelId,
070                            trashEntry.getTypeSettingsProperty("title"), newName);
071            }
072    
073            @Override
074            public void deleteTrashEntry(long classPK)
075                    throws PortalException, SystemException {
076    
077                    DLFolderLocalServiceUtil.deleteFolder(classPK, false);
078            }
079    
080            @Override
081            public String getClassName() {
082                    return DLFolder.class.getName();
083            }
084    
085            @Override
086            public String getDeleteMessage() {
087                    return "found-in-deleted-folder-x";
088            }
089    
090            @Override
091            public ContainerModel getParentContainerModel(long classPK)
092                    throws PortalException, SystemException {
093    
094                    DLFolder dlFolder = getDLFolder(classPK);
095    
096                    long parentFolderId = dlFolder.getParentFolderId();
097    
098                    if (parentFolderId <= 0) {
099                            return null;
100                    }
101    
102                    return getContainerModel(parentFolderId);
103            }
104    
105            @Override
106            public String getRestoreLink(PortletRequest portletRequest, long classPK)
107                    throws PortalException, SystemException {
108    
109                    DLFolder dlFolder = getDLFolder(classPK);
110    
111                    return DLUtil.getDLControlPanelLink(
112                            portletRequest, dlFolder.getParentFolderId());
113            }
114    
115            @Override
116            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
117                    throws PortalException, SystemException {
118    
119                    DLFolder dlFolder = getDLFolder(classPK);
120    
121                    return DLUtil.getAbsolutePath(
122                            portletRequest, dlFolder.getParentFolderId());
123            }
124    
125            @Override
126            public ContainerModel getTrashContainer(long classPK)
127                    throws PortalException, SystemException {
128    
129                    try {
130                            DLFolder dlFolder = getDLFolder(classPK);
131    
132                            return dlFolder.getTrashContainer();
133                    }
134                    catch (InvalidRepositoryException ire) {
135                            return null;
136                    }
137            }
138    
139            @Override
140            public TrashRenderer getTrashRenderer(long classPK)
141                    throws PortalException, SystemException {
142    
143                    Folder folder = DLAppLocalServiceUtil.getFolder(classPK);
144    
145                    return new DLFolderAssetRenderer(folder);
146            }
147    
148            @Override
149            public boolean hasTrashPermission(
150                            PermissionChecker permissionChecker, long groupId, long classPK,
151                            String trashActionId)
152                    throws PortalException, SystemException {
153    
154                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
155                            return DLFolderPermission.contains(
156                                    permissionChecker, groupId, classPK, ActionKeys.ADD_FOLDER);
157                    }
158    
159                    return super.hasTrashPermission(
160                            permissionChecker, groupId, classPK, trashActionId);
161            }
162    
163            @Override
164            public boolean isContainerModel() {
165                    return true;
166            }
167    
168            @Override
169            public boolean isInTrash(long classPK)
170                    throws PortalException, SystemException {
171    
172                    try {
173                            DLFolder dlFolder = getDLFolder(classPK);
174    
175                            return dlFolder.isInTrash();
176                    }
177                    catch (InvalidRepositoryException ire) {
178                            return false;
179                    }
180            }
181    
182            @Override
183            public boolean isInTrashContainer(long classPK)
184                    throws PortalException, SystemException {
185    
186                    try {
187                            DLFolder dlFolder = getDLFolder(classPK);
188    
189                            return dlFolder.isInTrashContainer();
190                    }
191                    catch (InvalidRepositoryException ire) {
192                            return false;
193                    }
194            }
195    
196            @Override
197            public boolean isRestorable(long classPK)
198                    throws PortalException, SystemException {
199    
200                    DLFolder dlFolder = fetchDLFolder(classPK);
201    
202                    if ((dlFolder == null) ||
203                            ((dlFolder.getParentFolderId() > 0) &&
204                             (DLFolderLocalServiceUtil.fetchFolder(
205                                    dlFolder.getParentFolderId()) == null))) {
206    
207                            return false;
208                    }
209    
210                    return !dlFolder.isInTrashContainer();
211            }
212    
213            @Override
214            public void moveEntry(
215                            long userId, long classPK, long containerModelId,
216                            ServiceContext serviceContext)
217                    throws PortalException, SystemException {
218    
219                    DLFolderLocalServiceUtil.moveFolder(
220                            userId, classPK, containerModelId, serviceContext);
221            }
222    
223            @Override
224            public void moveTrashEntry(
225                            long userId, long classPK, long containerModelId,
226                            ServiceContext serviceContext)
227                    throws PortalException, SystemException {
228    
229                    Repository repository = getRepository(classPK);
230    
231                    DLAppHelperLocalServiceUtil.moveFolderFromTrash(
232                            userId, repository.getFolder(classPK), containerModelId,
233                            serviceContext);
234            }
235    
236            @Override
237            public void restoreTrashEntry(long userId, long classPK)
238                    throws PortalException, SystemException {
239    
240                    Repository repository = getRepository(classPK);
241    
242                    DLAppHelperLocalServiceUtil.restoreFolderFromTrash(
243                            userId, repository.getFolder(classPK));
244            }
245    
246            @Override
247            public void updateTitle(long classPK, String name)
248                    throws PortalException, SystemException {
249    
250                    DLFolder dlFolder = getDLFolder(classPK);
251    
252                    dlFolder.setName(name);
253    
254                    DLFolderLocalServiceUtil.updateDLFolder(dlFolder);
255            }
256    
257            protected void checkDuplicateEntry(
258                            long classPK, long trashEntryId, long containerModelId,
259                            String originalTitle, String newName)
260                    throws PortalException, SystemException {
261    
262                    DLFolder dlFolder = getDLFolder(classPK);
263    
264                    if (containerModelId == TrashEntryConstants.DEFAULT_CONTAINER_ID) {
265                            containerModelId = dlFolder.getParentFolderId();
266                    }
267    
268                    if (Validator.isNotNull(newName)) {
269                            originalTitle = newName;
270                    }
271    
272                    DLFolder duplicateDLFolder = DLFolderLocalServiceUtil.fetchFolder(
273                            dlFolder.getGroupId(), containerModelId, originalTitle);
274    
275                    if (duplicateDLFolder != null) {
276                            DuplicateEntryException dee = new DuplicateEntryException();
277    
278                            dee.setDuplicateEntryId(duplicateDLFolder.getFolderId());
279                            dee.setOldName(duplicateDLFolder.getName());
280                            dee.setTrashEntryId(trashEntryId);
281    
282                            throw dee;
283                    }
284            }
285    
286            @Override
287            protected Repository getRepository(long classPK)
288                    throws PortalException, SystemException {
289    
290                    Repository repository = RepositoryServiceUtil.getRepositoryImpl(
291                            classPK, 0, 0);
292    
293                    if (!(repository instanceof LiferayRepository)) {
294                            throw new InvalidRepositoryException(
295                                    "Repository " + repository.getRepositoryId() +
296                                            " does not support trash operations");
297                    }
298    
299                    return repository;
300            }
301    
302            @Override
303            protected boolean hasPermission(
304                            PermissionChecker permissionChecker, long classPK, String actionId)
305                    throws PortalException, SystemException {
306    
307                    DLFolder dlFolder = getDLFolder(classPK);
308    
309                    if (dlFolder.isInHiddenFolder() && actionId.equals(ActionKeys.VIEW)) {
310                            return false;
311                    }
312    
313                    return DLFolderPermission.contains(
314                            permissionChecker, dlFolder, actionId);
315            }
316    
317    }