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