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 checkDuplicateTrashEntry(
054                            TrashEntry trashEntry, long containerModelId, String newName)
055                    throws PortalException, SystemException {
056    
057                    DLFolder dlFolder = getDLFolder(trashEntry.getClassPK());
058    
059                    if (containerModelId == TrashEntryConstants.DEFAULT_CONTAINER_ID) {
060                            containerModelId = dlFolder.getParentFolderId();
061                    }
062    
063                    String originalTitle = trashEntry.getTypeSettingsProperty("title");
064    
065                    if (Validator.isNotNull(newName)) {
066                            originalTitle = newName;
067                    }
068    
069                    DLFolder duplicateDLFolder = DLFolderLocalServiceUtil.fetchFolder(
070                            dlFolder.getGroupId(), dlFolder.getParentFolderId(), originalTitle);
071    
072                    if (duplicateDLFolder != null) {
073                            DuplicateEntryException dee = new DuplicateEntryException();
074    
075                            dee.setDuplicateEntryId(duplicateDLFolder.getFolderId());
076                            dee.setOldName(duplicateDLFolder.getName());
077                            dee.setTrashEntryId(trashEntry.getEntryId());
078    
079                            throw dee;
080                    }
081            }
082    
083            public void deleteTrashEntry(long classPK)
084                    throws PortalException, SystemException {
085    
086                    DLFolderLocalServiceUtil.deleteFolder(classPK, false);
087            }
088    
089            public String getClassName() {
090                    return DLFolder.class.getName();
091            }
092    
093            @Override
094            public String getDeleteMessage() {
095                    return "found-in-deleted-folder-x";
096            }
097    
098            @Override
099            public ContainerModel getParentContainerModel(long classPK)
100                    throws PortalException, SystemException {
101    
102                    DLFolder dlFolder = getDLFolder(classPK);
103    
104                    long parentFolderId = dlFolder.getParentFolderId();
105    
106                    if (parentFolderId <= 0) {
107                            return null;
108                    }
109    
110                    return getContainerModel(parentFolderId);
111            }
112    
113            @Override
114            public String getRestoreLink(PortletRequest portletRequest, long classPK)
115                    throws PortalException, SystemException {
116    
117                    DLFolder dlFolder = getDLFolder(classPK);
118    
119                    return DLUtil.getDLControlPanelLink(
120                            portletRequest, dlFolder.getParentFolderId());
121            }
122    
123            @Override
124            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
125                    throws PortalException, SystemException {
126    
127                    DLFolder dlFolder = getDLFolder(classPK);
128    
129                    return DLUtil.getAbsolutePath(
130                            portletRequest, dlFolder.getParentFolderId());
131            }
132    
133            @Override
134            public ContainerModel getTrashContainer(long classPK)
135                    throws PortalException, SystemException {
136    
137                    try {
138                            DLFolder dlFolder = getDLFolder(classPK);
139    
140                            return dlFolder.getTrashContainer();
141                    }
142                    catch (InvalidRepositoryException ire) {
143                            return null;
144                    }
145            }
146    
147            @Override
148            public TrashRenderer getTrashRenderer(long classPK)
149                    throws PortalException, SystemException {
150    
151                    Folder folder = DLAppLocalServiceUtil.getFolder(classPK);
152    
153                    return new DLFolderAssetRenderer(folder);
154            }
155    
156            @Override
157            public boolean hasTrashPermission(
158                            PermissionChecker permissionChecker, long groupId, long classPK,
159                            String trashActionId)
160                    throws PortalException, SystemException {
161    
162                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
163                            return DLFolderPermission.contains(
164                                    permissionChecker, groupId, classPK, ActionKeys.ADD_FOLDER);
165                    }
166    
167                    return super.hasTrashPermission(
168                            permissionChecker, groupId, classPK, trashActionId);
169            }
170    
171            @Override
172            public boolean isContainerModel() {
173                    return true;
174            }
175    
176            public boolean isInTrash(long classPK)
177                    throws PortalException, SystemException {
178    
179                    try {
180                            DLFolder dlFolder = getDLFolder(classPK);
181    
182                            return dlFolder.isInTrash();
183                    }
184                    catch (InvalidRepositoryException ire) {
185                            return false;
186                    }
187            }
188    
189            @Override
190            public boolean isInTrashContainer(long classPK)
191                    throws PortalException, SystemException {
192    
193                    try {
194                            DLFolder dlFolder = getDLFolder(classPK);
195    
196                            return dlFolder.isInTrashContainer();
197                    }
198                    catch (InvalidRepositoryException ire) {
199                            return false;
200                    }
201            }
202    
203            @Override
204            public boolean isRestorable(long classPK)
205                    throws PortalException, SystemException {
206    
207                    try {
208                            DLFolder dlFolder = getDLFolder(classPK);
209    
210                            return !dlFolder.isInTrashContainer();
211                    }
212                    catch (InvalidRepositoryException ire) {
213                            return false;
214                    }
215            }
216    
217            @Override
218            public void moveEntry(
219                            long userId, long classPK, long containerModelId,
220                            ServiceContext serviceContext)
221                    throws PortalException, SystemException {
222    
223                    DLFolderLocalServiceUtil.moveFolder(
224                            userId, classPK, containerModelId, serviceContext);
225            }
226    
227            @Override
228            public void moveTrashEntry(
229                            long userId, long classPK, long containerModelId,
230                            ServiceContext serviceContext)
231                    throws PortalException, SystemException {
232    
233                    Repository repository = getRepository(classPK);
234    
235                    DLAppHelperLocalServiceUtil.moveFolderFromTrash(
236                            userId, repository.getFolder(classPK), containerModelId,
237                            serviceContext);
238            }
239    
240            public void restoreTrashEntry(long userId, long classPK)
241                    throws PortalException, SystemException {
242    
243                    Repository repository = getRepository(classPK);
244    
245                    DLAppHelperLocalServiceUtil.restoreFolderFromTrash(
246                            userId, repository.getFolder(classPK));
247            }
248    
249            @Override
250            public void updateTitle(long classPK, String name)
251                    throws PortalException, SystemException {
252    
253                    DLFolder dlFolder = getDLFolder(classPK);
254    
255                    dlFolder.setName(name);
256    
257                    DLFolderLocalServiceUtil.updateDLFolder(dlFolder);
258            }
259    
260            @Override
261            protected Repository getRepository(long classPK)
262                    throws PortalException, SystemException {
263    
264                    Repository repository = RepositoryServiceUtil.getRepositoryImpl(
265                            classPK, 0, 0);
266    
267                    if (!(repository instanceof LiferayRepository)) {
268                            throw new InvalidRepositoryException(
269                                    "Repository " + repository.getRepositoryId() +
270                                            " does not support trash operations");
271                    }
272    
273                    return repository;
274            }
275    
276            @Override
277            protected boolean hasPermission(
278                            PermissionChecker permissionChecker, long classPK, String actionId)
279                    throws PortalException, SystemException {
280    
281                    DLFolder dlFolder = getDLFolder(classPK);
282    
283                    if (dlFolder.isInHiddenFolder() && actionId.equals(ActionKeys.VIEW)) {
284                            return false;
285                    }
286    
287                    return DLFolderPermission.contains(
288                            permissionChecker, dlFolder, actionId);
289            }
290    
291    }