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