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