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