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