001    /**
002     * Copyright (c) 2000-2012 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.BaseTrashHandler;
023    import com.liferay.portal.kernel.trash.TrashActionKeys;
024    import com.liferay.portal.kernel.trash.TrashRenderer;
025    import com.liferay.portal.kernel.util.Validator;
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.portlet.documentlibrary.model.DLFolder;
031    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
032    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
033    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
034    import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
035    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
036    import com.liferay.portlet.documentlibrary.util.DLUtil;
037    import com.liferay.portlet.trash.DuplicateEntryException;
038    import com.liferay.portlet.trash.TrashEntryConstants;
039    import com.liferay.portlet.trash.model.TrashEntry;
040    import com.liferay.portlet.trash.util.TrashUtil;
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 BaseTrashHandler {
051    
052            public static final String CLASS_NAME = DLFolder.class.getName();
053    
054            @Override
055            public void checkDuplicateTrashEntry(
056                            TrashEntry trashEntry, long containerModelId, String newName)
057                    throws PortalException, SystemException {
058    
059                    DLFolder dlFolder = getDLFolder(trashEntry.getClassPK());
060    
061                    if (containerModelId == TrashEntryConstants.DEFAULT_CONTAINER_ID) {
062                            containerModelId = dlFolder.getParentFolderId();
063                    }
064    
065                    String restoredTitle = dlFolder.getName();
066    
067                    if (Validator.isNotNull(newName)) {
068                            restoredTitle = newName;
069                    }
070    
071                    String originalTitle = TrashUtil.stripTrashNamespace(restoredTitle);
072    
073                    DLFolder duplicateDLFolder = DLFolderLocalServiceUtil.fetchFolder(
074                            dlFolder.getGroupId(), dlFolder.getParentFolderId(), originalTitle);
075    
076                    if (duplicateDLFolder != null) {
077                            DuplicateEntryException dee = new DuplicateEntryException();
078    
079                            dee.setDuplicateEntryId(duplicateDLFolder.getFolderId());
080                            dee.setOldName(duplicateDLFolder.getName());
081                            dee.setTrashEntryId(trashEntry.getEntryId());
082    
083                            throw dee;
084                    }
085            }
086    
087            public void deleteTrashEntries(long[] classPKs, boolean checkPermission)
088                    throws PortalException, SystemException {
089    
090                    for (long classPK : classPKs) {
091                            if (checkPermission) {
092                                    DLFolderServiceUtil.deleteFolder(classPK, false);
093                            }
094                            else {
095                                    DLFolderLocalServiceUtil.deleteFolder(classPK, false);
096                            }
097                    }
098            }
099    
100            public String getClassName() {
101                    return CLASS_NAME;
102            }
103    
104            @Override
105            public String getDeleteMessage() {
106                    return "found-in-deleted-folder-x";
107            }
108    
109            @Override
110            public String getRestoreLink(PortletRequest portletRequest, long classPK)
111                    throws PortalException, SystemException {
112    
113                    DLFolder dlFolder = getDLFolder(classPK);
114    
115                    return DLUtil.getDLControlPanelLink(
116                            portletRequest, dlFolder.getParentFolderId());
117            }
118    
119            @Override
120            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
121                    throws PortalException, SystemException {
122    
123                    DLFolder dlFolder = getDLFolder(classPK);
124    
125                    return DLUtil.getAbsolutePath(
126                            portletRequest, dlFolder.getParentFolderId());
127            }
128    
129            @Override
130            public TrashRenderer getTrashRenderer(long classPK)
131                    throws PortalException, SystemException {
132    
133                    Folder folder = DLAppLocalServiceUtil.getFolder(classPK);
134    
135                    return new DLFolderTrashRenderer(folder);
136            }
137    
138            @Override
139            public boolean hasTrashPermission(
140                            PermissionChecker permissionChecker, long groupId, long classPK,
141                            String trashActionId)
142                    throws PortalException, SystemException {
143    
144                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
145                            return DLFolderPermission.contains(
146                                    permissionChecker, groupId, classPK, ActionKeys.ADD_FOLDER);
147                    }
148    
149                    return super.hasTrashPermission(
150                            permissionChecker, groupId, classPK, trashActionId);
151            }
152    
153            public boolean isInTrash(long classPK)
154                    throws PortalException, SystemException {
155    
156                    DLFolder dlFolder = getDLFolder(classPK);
157    
158                    if (dlFolder.isInTrash() || dlFolder.isInTrashFolder()) {
159                            return true;
160                    }
161    
162                    return false;
163            }
164    
165            public void restoreTrashEntries(long[] classPKs)
166                    throws PortalException, SystemException {
167    
168                    for (long classPK : classPKs) {
169                            DLAppServiceUtil.restoreFolderFromTrash(classPK);
170                    }
171            }
172    
173            @Override
174            public void updateTitle(long classPK, String name)
175                    throws PortalException, SystemException {
176    
177                    DLFolder dlFolder = getDLFolder(classPK);
178    
179                    dlFolder.setName(name);
180    
181                    DLFolderLocalServiceUtil.updateDLFolder(dlFolder);
182            }
183    
184            protected DLFolder getDLFolder(long classPK)
185                    throws PortalException, SystemException {
186    
187                    Repository repository = RepositoryServiceUtil.getRepositoryImpl(
188                            classPK, 0, 0);
189    
190                    if (!(repository instanceof LiferayRepository)) {
191                            throw new InvalidRepositoryException(
192                                    "Repository " + repository.getRepositoryId() +
193                                            " does not support trash operations");
194                    }
195    
196                    Folder folder = repository.getFolder(classPK);
197    
198                    return (DLFolder)folder.getModel();
199            }
200    
201            @Override
202            protected boolean hasPermission(
203                            PermissionChecker permissionChecker, long classPK, String actionId)
204                    throws PortalException, SystemException {
205    
206                    DLFolder dlFolder = getDLFolder(classPK);
207    
208                    return DLFolderPermission.contains(
209                            permissionChecker, dlFolder, actionId);
210            }
211    
212    }