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.TrashActionKeys;
023    import com.liferay.portal.kernel.trash.TrashRenderer;
024    import com.liferay.portal.model.ContainerModel;
025    import com.liferay.portal.repository.liferayrepository.LiferayRepository;
026    import com.liferay.portal.security.permission.ActionKeys;
027    import com.liferay.portal.security.permission.PermissionChecker;
028    import com.liferay.portal.service.RepositoryServiceUtil;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portlet.documentlibrary.asset.DLFolderAssetRenderer;
031    import com.liferay.portlet.documentlibrary.model.DLFolder;
032    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
033    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
034    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
035    import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
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            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 originalTitle = trashEntry.getTypeSettingsProperty("title");
066    
067                    DLFolder duplicateDLFolder = DLFolderLocalServiceUtil.fetchFolder(
068                            dlFolder.getGroupId(), dlFolder.getParentFolderId(), originalTitle);
069    
070                    if (duplicateDLFolder != null) {
071                            DuplicateEntryException dee = new DuplicateEntryException();
072    
073                            dee.setDuplicateEntryId(duplicateDLFolder.getFolderId());
074                            dee.setOldName(duplicateDLFolder.getName());
075                            dee.setTrashEntryId(trashEntry.getEntryId());
076    
077                            throw dee;
078                    }
079            }
080    
081            public void deleteTrashEntries(long[] classPKs, boolean checkPermission)
082                    throws PortalException, SystemException {
083    
084                    for (long classPK : classPKs) {
085                            if (checkPermission) {
086                                    DLFolderServiceUtil.deleteFolder(classPK, false);
087                            }
088                            else {
089                                    DLFolderLocalServiceUtil.deleteFolder(classPK, false);
090                            }
091                    }
092            }
093    
094            public String getClassName() {
095                    return CLASS_NAME;
096            }
097    
098            @Override
099            public String getDeleteMessage() {
100                    return "found-in-deleted-folder-x";
101            }
102    
103            @Override
104            public ContainerModel getParentContainerModel(long classPK)
105                    throws PortalException, SystemException {
106    
107                    DLFolder dlFolder = getDLFolder(classPK);
108    
109                    long parentFolderId = dlFolder.getParentFolderId();
110    
111                    if (parentFolderId <= 0) {
112                            return null;
113                    }
114    
115                    return getContainerModel(parentFolderId);
116            }
117    
118            @Override
119            public String getRestoreLink(PortletRequest portletRequest, long classPK)
120                    throws PortalException, SystemException {
121    
122                    DLFolder dlFolder = getDLFolder(classPK);
123    
124                    return DLUtil.getDLControlPanelLink(
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 TrashRenderer getTrashRenderer(long classPK)
140                    throws PortalException, SystemException {
141    
142                    Folder folder = DLAppLocalServiceUtil.getFolder(classPK);
143    
144                    return new DLFolderAssetRenderer(folder);
145            }
146    
147            @Override
148            public boolean hasTrashPermission(
149                            PermissionChecker permissionChecker, long groupId, long classPK,
150                            String trashActionId)
151                    throws PortalException, SystemException {
152    
153                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
154                            return DLFolderPermission.contains(
155                                    permissionChecker, groupId, classPK, ActionKeys.ADD_FOLDER);
156                    }
157    
158                    return super.hasTrashPermission(
159                            permissionChecker, groupId, classPK, trashActionId);
160            }
161    
162            @Override
163            public boolean isContainerModel() {
164                    return true;
165            }
166    
167            public boolean isInTrash(long classPK)
168                    throws PortalException, SystemException {
169    
170                    try {
171                            DLFolder dlFolder = getDLFolder(classPK);
172    
173                            if (dlFolder.isInTrash() || dlFolder.isInTrashContainer()) {
174                                    return true;
175                            }
176    
177                            return false;
178                    }
179                    catch (InvalidRepositoryException ire) {
180                            return false;
181                    }
182            }
183    
184            @Override
185            public boolean isInTrashContainer(long classPK)
186                    throws PortalException, SystemException {
187    
188                    DLFolder dlFolder = getDLFolder(classPK);
189    
190                    return dlFolder.isInTrashContainer();
191            }
192    
193            @Override
194            public boolean isRestorable(long classPK)
195                    throws PortalException, SystemException {
196    
197                    try {
198                            DLFolder dlFolder = getDLFolder(classPK);
199    
200                            return !dlFolder.isInTrashContainer();
201                    }
202                    catch (InvalidRepositoryException ire) {
203                            return false;
204                    }
205            }
206    
207            @Override
208            public void moveEntry(
209                            long classPK, long containerModelId, ServiceContext serviceContext)
210                    throws PortalException, SystemException {
211    
212                    DLAppServiceUtil.moveFolder(classPK, containerModelId, serviceContext);
213            }
214    
215            @Override
216            public void moveTrashEntry(
217                            long classPK, long containerModelId, ServiceContext serviceContext)
218                    throws PortalException, SystemException {
219    
220                    DLAppServiceUtil.moveFolderFromTrash(
221                            classPK, containerModelId, serviceContext);
222            }
223    
224            public void restoreTrashEntries(long[] classPKs)
225                    throws PortalException, SystemException {
226    
227                    for (long classPK : classPKs) {
228                            DLAppServiceUtil.restoreFolderFromTrash(classPK);
229                    }
230            }
231    
232            @Override
233            public void updateTitle(long classPK, String name)
234                    throws PortalException, SystemException {
235    
236                    DLFolder dlFolder = getDLFolder(classPK);
237    
238                    dlFolder.setName(name);
239    
240                    DLFolderLocalServiceUtil.updateDLFolder(dlFolder);
241            }
242    
243            @Override
244            protected Repository getRepository(long classPK)
245                    throws PortalException, SystemException {
246    
247                    Repository repository = RepositoryServiceUtil.getRepositoryImpl(
248                            classPK, 0, 0);
249    
250                    if (!(repository instanceof LiferayRepository)) {
251                            throw new InvalidRepositoryException(
252                                    "Repository " + repository.getRepositoryId() +
253                                            " does not support trash operations");
254                    }
255    
256                    return repository;
257            }
258    
259            @Override
260            protected boolean hasPermission(
261                            PermissionChecker permissionChecker, long classPK, String actionId)
262                    throws PortalException, SystemException {
263    
264                    DLFolder dlFolder = getDLFolder(classPK);
265    
266                    return DLFolderPermission.contains(
267                            permissionChecker, dlFolder, actionId);
268            }
269    
270    }