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