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 getRestoreContainedModelLink(
107                            PortletRequest portletRequest, long classPK)
108                    throws PortalException, SystemException {
109    
110                    DLFolder dlFolder = getDLFolder(classPK);
111    
112                    return DLUtil.getDLFolderControlPanelLink(
113                            portletRequest, dlFolder.getFolderId());
114            }
115    
116            @Override
117            public String getRestoreContainerModelLink(
118                            PortletRequest portletRequest, long classPK)
119                    throws PortalException, SystemException {
120    
121                    DLFolder dlFolder = getDLFolder(classPK);
122    
123                    return DLUtil.getDLFolderControlPanelLink(
124                            portletRequest, dlFolder.getParentFolderId());
125            }
126    
127            @Override
128            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
129                    throws PortalException, SystemException {
130    
131                    DLFolder dlFolder = getDLFolder(classPK);
132    
133                    return DLUtil.getAbsolutePath(
134                            portletRequest, dlFolder.getParentFolderId());
135            }
136    
137            @Override
138            public String getSystemEventClassName() {
139                    return Folder.class.getName();
140            }
141    
142            @Override
143            public ContainerModel getTrashContainer(long classPK)
144                    throws PortalException, SystemException {
145    
146                    try {
147                            DLFolder dlFolder = getDLFolder(classPK);
148    
149                            return dlFolder.getTrashContainer();
150                    }
151                    catch (InvalidRepositoryException ire) {
152                            return null;
153                    }
154            }
155    
156            @Override
157            public TrashEntry getTrashEntry(long classPK)
158                    throws PortalException, SystemException {
159    
160                    DLFolder dlFolder = getDLFolder(classPK);
161    
162                    return dlFolder.getTrashEntry();
163            }
164    
165            @Override
166            public TrashRenderer getTrashRenderer(long classPK)
167                    throws PortalException, SystemException {
168    
169                    Folder folder = DLAppLocalServiceUtil.getFolder(classPK);
170    
171                    return new DLFolderAssetRenderer(folder);
172            }
173    
174            @Override
175            public boolean hasTrashPermission(
176                            PermissionChecker permissionChecker, long groupId, long classPK,
177                            String trashActionId)
178                    throws PortalException, SystemException {
179    
180                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
181                            return DLFolderPermission.contains(
182                                    permissionChecker, groupId, classPK, ActionKeys.ADD_FOLDER);
183                    }
184    
185                    return super.hasTrashPermission(
186                            permissionChecker, groupId, classPK, trashActionId);
187            }
188    
189            @Override
190            public boolean isContainerModel() {
191                    return true;
192            }
193    
194            @Override
195            public boolean isInTrash(long classPK)
196                    throws PortalException, SystemException {
197    
198                    try {
199                            DLFolder dlFolder = getDLFolder(classPK);
200    
201                            return dlFolder.isInTrash();
202                    }
203                    catch (InvalidRepositoryException ire) {
204                            return false;
205                    }
206            }
207    
208            @Override
209            public boolean isInTrashContainer(long classPK)
210                    throws PortalException, SystemException {
211    
212                    try {
213                            DLFolder dlFolder = getDLFolder(classPK);
214    
215                            return dlFolder.isInTrashContainer();
216                    }
217                    catch (InvalidRepositoryException ire) {
218                            return false;
219                    }
220            }
221    
222            @Override
223            public boolean isRestorable(long classPK)
224                    throws PortalException, SystemException {
225    
226                    DLFolder dlFolder = fetchDLFolder(classPK);
227    
228                    if ((dlFolder == null) ||
229                            ((dlFolder.getParentFolderId() > 0) &&
230                             (DLFolderLocalServiceUtil.fetchFolder(
231                                    dlFolder.getParentFolderId()) == null))) {
232    
233                            return false;
234                    }
235    
236                    return !dlFolder.isInTrashContainer();
237            }
238    
239            @Override
240            public void moveEntry(
241                            long userId, long classPK, long containerModelId,
242                            ServiceContext serviceContext)
243                    throws PortalException, SystemException {
244    
245                    DLFolderLocalServiceUtil.moveFolder(
246                            userId, classPK, containerModelId, serviceContext);
247            }
248    
249            @Override
250            public void moveTrashEntry(
251                            long userId, long classPK, long containerModelId,
252                            ServiceContext serviceContext)
253                    throws PortalException, SystemException {
254    
255                    Repository repository = getRepository(classPK);
256    
257                    DLAppHelperLocalServiceUtil.moveFolderFromTrash(
258                            userId, repository.getFolder(classPK), containerModelId,
259                            serviceContext);
260            }
261    
262            @Override
263            public void restoreTrashEntry(long userId, long classPK)
264                    throws PortalException, SystemException {
265    
266                    Repository repository = getRepository(classPK);
267    
268                    DLAppHelperLocalServiceUtil.restoreFolderFromTrash(
269                            userId, repository.getFolder(classPK));
270            }
271    
272            @Override
273            public void updateTitle(long classPK, String name)
274                    throws PortalException, SystemException {
275    
276                    DLFolder dlFolder = getDLFolder(classPK);
277    
278                    dlFolder.setName(name);
279    
280                    DLFolderLocalServiceUtil.updateDLFolder(dlFolder);
281            }
282    
283            protected void checkDuplicateEntry(
284                            long classPK, long trashEntryId, long containerModelId,
285                            String originalTitle, String newName)
286                    throws PortalException, SystemException {
287    
288                    DLFolder dlFolder = getDLFolder(classPK);
289    
290                    if (containerModelId == TrashEntryConstants.DEFAULT_CONTAINER_ID) {
291                            containerModelId = dlFolder.getParentFolderId();
292                    }
293    
294                    if (Validator.isNotNull(newName)) {
295                            originalTitle = newName;
296                    }
297    
298                    DLFolder duplicateDLFolder = DLFolderLocalServiceUtil.fetchFolder(
299                            dlFolder.getGroupId(), containerModelId, originalTitle);
300    
301                    if (duplicateDLFolder != null) {
302                            DuplicateEntryException dee = new DuplicateEntryException();
303    
304                            dee.setDuplicateEntryId(duplicateDLFolder.getFolderId());
305                            dee.setOldName(duplicateDLFolder.getName());
306                            dee.setTrashEntryId(trashEntryId);
307    
308                            throw dee;
309                    }
310            }
311    
312            @Override
313            protected Repository getRepository(long classPK)
314                    throws PortalException, SystemException {
315    
316                    Repository repository = RepositoryServiceUtil.getRepositoryImpl(
317                            classPK, 0, 0);
318    
319                    if (!(repository instanceof LiferayRepository)) {
320                            throw new InvalidRepositoryException(
321                                    "Repository " + repository.getRepositoryId() +
322                                            " does not support trash operations");
323                    }
324    
325                    return repository;
326            }
327    
328            @Override
329            protected boolean hasPermission(
330                            PermissionChecker permissionChecker, long classPK, String actionId)
331                    throws PortalException, SystemException {
332    
333                    DLFolder dlFolder = getDLFolder(classPK);
334    
335                    if (dlFolder.isInHiddenFolder() && actionId.equals(ActionKeys.VIEW)) {
336                            return false;
337                    }
338    
339                    return DLFolderPermission.contains(
340                            permissionChecker, dlFolder, actionId);
341            }
342    
343    }