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.FileEntry;
022    import com.liferay.portal.kernel.trash.TrashActionKeys;
023    import com.liferay.portal.kernel.trash.TrashHandler;
024    import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.model.ContainerModel;
027    import com.liferay.portal.model.TrashedModel;
028    import com.liferay.portal.repository.liferayrepository.LiferayRepository;
029    import com.liferay.portal.security.permission.ActionKeys;
030    import com.liferay.portal.security.permission.PermissionChecker;
031    import com.liferay.portal.service.RepositoryServiceUtil;
032    import com.liferay.portal.service.ServiceContext;
033    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
034    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
035    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
036    import com.liferay.portlet.documentlibrary.model.DLFolder;
037    import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalServiceUtil;
038    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
039    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
040    import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
041    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
042    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
043    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
044    import com.liferay.portlet.documentlibrary.util.DLUtil;
045    import com.liferay.portlet.trash.DuplicateEntryException;
046    import com.liferay.portlet.trash.TrashEntryConstants;
047    import com.liferay.portlet.trash.model.TrashEntry;
048    
049    import javax.portlet.PortletRequest;
050    
051    /**
052     * Implements trash handling for the file entry entity.
053     *
054     * @author Alexander Chow
055     * @author Manuel de la Pe??a
056     * @author Zsolt Berentey
057     */
058    public class DLFileEntryTrashHandler extends DLBaseTrashHandler {
059    
060            @Override
061            public void checkDuplicateEntry(
062                            long classPK, long containerModelId, String newName)
063                    throws PortalException, SystemException {
064    
065                    DLFileEntry dlFileEntry = getDLFileEntry(classPK);
066    
067                    checkDuplicateEntry(
068                            classPK, 0, containerModelId, dlFileEntry.getTitle(), newName);
069            }
070    
071            @Override
072            public void checkDuplicateTrashEntry(
073                            TrashEntry trashEntry, long containerModelId, String newName)
074                    throws PortalException, SystemException {
075    
076                    checkDuplicateEntry(
077                            trashEntry.getClassPK(), trashEntry.getEntryId(), containerModelId,
078                            trashEntry.getTypeSettingsProperty("title"), newName);
079            }
080    
081            @Override
082            public void deleteTrashEntry(long classPK)
083                    throws PortalException, SystemException {
084    
085                    DLAppLocalServiceUtil.deleteFileEntry(classPK);
086            }
087    
088            @Override
089            public String getClassName() {
090                    return DLFileEntry.class.getName();
091            }
092    
093            @Override
094            public ContainerModel getParentContainerModel(long classPK)
095                    throws PortalException, SystemException {
096    
097                    DLFileEntry dlFileEntry = getDLFileEntry(classPK);
098    
099                    long parentFolderId = dlFileEntry.getFolderId();
100    
101                    if (parentFolderId <= 0) {
102                            return null;
103                    }
104    
105                    return getContainerModel(parentFolderId);
106            }
107    
108            @Override
109            public ContainerModel getParentContainerModel(TrashedModel trashedModel)
110                    throws PortalException, SystemException {
111    
112                    DLFileEntry dlFileEntry = (DLFileEntry)trashedModel;
113    
114                    return getContainerModel(dlFileEntry.getFolderId());
115            }
116    
117            @Override
118            public String getRestoreContainedModelLink(
119                            PortletRequest portletRequest, long classPK)
120                    throws PortalException, SystemException {
121    
122                    DLFileEntry dlFileEntry = getDLFileEntry(classPK);
123    
124                    return DLUtil.getDLFileEntryControlPanelLink(
125                            portletRequest, dlFileEntry.getFileEntryId());
126            }
127    
128            @Override
129            public String getRestoreContainerModelLink(
130                            PortletRequest portletRequest, long classPK)
131                    throws PortalException, SystemException {
132    
133                    DLFileEntry dlFileEntry = getDLFileEntry(classPK);
134    
135                    return DLUtil.getDLFolderControlPanelLink(
136                            portletRequest, dlFileEntry.getFolderId());
137            }
138    
139            @Override
140            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
141                    throws PortalException, SystemException {
142    
143                    DLFileEntry dlFileEntry = getDLFileEntry(classPK);
144    
145                    DLFolder dlFolder = dlFileEntry.getFolder();
146    
147                    return DLUtil.getAbsolutePath(portletRequest, dlFolder.getFolderId());
148            }
149    
150            @Override
151            public String getSystemEventClassName() {
152                    return DLFileEntryConstants.getClassName();
153            }
154    
155            @Override
156            public TrashEntry getTrashEntry(long classPK)
157                    throws PortalException, SystemException {
158    
159                    DLFileEntry dlFileEntry = getDLFileEntry(classPK);
160    
161                    return dlFileEntry.getTrashEntry();
162            }
163    
164            @Override
165            public boolean hasTrashPermission(
166                            PermissionChecker permissionChecker, long groupId, long classPK,
167                            String trashActionId)
168                    throws PortalException, SystemException {
169    
170                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
171                            return DLFolderPermission.contains(
172                                    permissionChecker, groupId, classPK, ActionKeys.ADD_DOCUMENT);
173                    }
174    
175                    return super.hasTrashPermission(
176                            permissionChecker, groupId, classPK, trashActionId);
177            }
178    
179            @Override
180            public boolean isInTrash(long classPK)
181                    throws PortalException, SystemException {
182    
183                    try {
184                            DLFileEntry dlFileEntry = getDLFileEntry(classPK);
185    
186                            return dlFileEntry.isInTrash();
187                    }
188                    catch (InvalidRepositoryException ire) {
189                            return false;
190                    }
191            }
192    
193            @Override
194            public boolean isInTrashContainer(long classPK)
195                    throws PortalException, SystemException {
196    
197                    try {
198                            DLFileEntry dlFileEntry = getDLFileEntry(classPK);
199    
200                            return dlFileEntry.isInTrashContainer();
201                    }
202                    catch (InvalidRepositoryException ire) {
203                            return false;
204                    }
205            }
206    
207            @Override
208            public boolean isRestorable(long classPK)
209                    throws PortalException, SystemException {
210    
211                    DLFileEntry dlFileEntry = fetchDLFileEntry(classPK);
212    
213                    if ((dlFileEntry == null) ||
214                            ((dlFileEntry.getFolderId() > 0) &&
215                             (DLFolderLocalServiceUtil.fetchFolder(
216                                    dlFileEntry.getFolderId()) == null))) {
217    
218                            return false;
219                    }
220    
221                    return !dlFileEntry.isInTrashContainer();
222            }
223    
224            @Override
225            public void moveEntry(
226                            long userId, long classPK, long containerModelId,
227                            ServiceContext serviceContext)
228                    throws PortalException, SystemException {
229    
230                    DLAppLocalServiceUtil.moveFileEntry(
231                            userId, classPK, containerModelId, serviceContext);
232            }
233    
234            @Override
235            public void moveTrashEntry(
236                            long userId, long classPK, long containerModelId,
237                            ServiceContext serviceContext)
238                    throws PortalException, SystemException {
239    
240                    Repository repository = getRepository(classPK);
241    
242                    DLAppHelperLocalServiceUtil.moveFileEntryFromTrash(
243                            userId, repository.getFileEntry(classPK), containerModelId,
244                            serviceContext);
245            }
246    
247            @Override
248            public void restoreTrashEntry(long userId, long classPK)
249                    throws PortalException, SystemException {
250    
251                    DLFileEntry dlFileEntry = getDLFileEntry(classPK);
252    
253                    if ((dlFileEntry.getClassNameId() > 0) &&
254                            (dlFileEntry.getClassPK() > 0)) {
255    
256                            TrashHandler trashHandler =
257                                    TrashHandlerRegistryUtil.getTrashHandler(
258                                            dlFileEntry.getClassName());
259    
260                            trashHandler.restoreRelatedTrashEntry(getClassName(), classPK);
261    
262                            return;
263                    }
264    
265                    DLAppLocalServiceUtil.restoreFileEntryFromTrash(userId, classPK);
266            }
267    
268            @Override
269            public void updateTitle(long classPK, String name)
270                    throws PortalException, SystemException {
271    
272                    DLFileEntry dlFileEntry = getDLFileEntry(classPK);
273    
274                    dlFileEntry.setTitle(name);
275    
276                    DLFileEntryLocalServiceUtil.updateDLFileEntry(dlFileEntry);
277    
278                    DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();
279    
280                    dlFileVersion.setTitle(name);
281    
282                    DLFileVersionLocalServiceUtil.updateDLFileVersion(dlFileVersion);
283            }
284    
285            protected void checkDuplicateEntry(
286                            long classPK, long entryId, long containerModelId,
287                            String originalTitle, String newName)
288                    throws PortalException, SystemException {
289    
290                    DLFileEntry dlFileEntry = getDLFileEntry(classPK);
291    
292                    if (containerModelId == TrashEntryConstants.DEFAULT_CONTAINER_ID) {
293                            containerModelId = dlFileEntry.getFolderId();
294                    }
295    
296                    if (Validator.isNotNull(newName)) {
297                            originalTitle = newName;
298                    }
299    
300                    DLFolder duplicateDLFolder = DLFolderLocalServiceUtil.fetchFolder(
301                            dlFileEntry.getGroupId(), containerModelId, originalTitle);
302    
303                    if (duplicateDLFolder != null) {
304                            DuplicateEntryException dee = new DuplicateEntryException();
305    
306                            dee.setDuplicateEntryId(duplicateDLFolder.getFolderId());
307                            dee.setOldName(duplicateDLFolder.getName());
308                            dee.setTrashEntryId(entryId);
309    
310                            throw dee;
311                    }
312    
313                    DLFileEntry duplicateDLFileEntry =
314                            DLFileEntryLocalServiceUtil.fetchFileEntry(
315                                    dlFileEntry.getGroupId(), containerModelId, originalTitle);
316    
317                    if (duplicateDLFileEntry != null) {
318                            DuplicateEntryException dee = new DuplicateEntryException();
319    
320                            dee.setDuplicateEntryId(duplicateDLFileEntry.getFileEntryId());
321                            dee.setOldName(duplicateDLFileEntry.getTitle());
322                            dee.setTrashEntryId(entryId);
323    
324                            throw dee;
325                    }
326            }
327    
328            protected DLFileEntry fetchDLFileEntry(long classPK)
329                    throws PortalException, SystemException {
330    
331                    Repository repository = RepositoryServiceUtil.getRepositoryImpl(
332                            0, classPK, 0);
333    
334                    if (!(repository instanceof LiferayRepository)) {
335                            return null;
336                    }
337    
338                    FileEntry fileEntry = repository.getFileEntry(classPK);
339    
340                    return (DLFileEntry)fileEntry.getModel();
341            }
342    
343            protected DLFileEntry getDLFileEntry(long classPK)
344                    throws PortalException, SystemException {
345    
346                    Repository repository = RepositoryServiceUtil.getRepositoryImpl(
347                            0, classPK, 0);
348    
349                    if (!(repository instanceof LiferayRepository)) {
350                            throw new InvalidRepositoryException(
351                                    "Repository " + repository.getRepositoryId() +
352                                            " does not support trash operations");
353                    }
354    
355                    FileEntry fileEntry = repository.getFileEntry(classPK);
356    
357                    return (DLFileEntry)fileEntry.getModel();
358            }
359    
360            @Override
361            protected Repository getRepository(long classPK)
362                    throws PortalException, SystemException {
363    
364                    Repository repository = RepositoryServiceUtil.getRepositoryImpl(
365                            0, classPK, 0);
366    
367                    if (!(repository instanceof LiferayRepository)) {
368                            throw new InvalidRepositoryException(
369                                    "Repository " + repository.getRepositoryId() +
370                                            " does not support trash operations");
371                    }
372    
373                    return repository;
374            }
375    
376            @Override
377            protected boolean hasPermission(
378                            PermissionChecker permissionChecker, long classPK, String actionId)
379                    throws PortalException, SystemException {
380    
381                    DLFileEntry dlFileEntry = getDLFileEntry(classPK);
382    
383                    if (dlFileEntry.isInHiddenFolder() &&
384                            actionId.equals(ActionKeys.VIEW)) {
385    
386                            return false;
387                    }
388    
389                    return DLFileEntryPermission.contains(
390                            permissionChecker, classPK, actionId);
391            }
392    
393    }