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