001    /**
002     * Copyright (c) 2000-2012 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.model.ContainerModel;
024    import com.liferay.portal.repository.liferayrepository.LiferayRepository;
025    import com.liferay.portal.security.permission.ActionKeys;
026    import com.liferay.portal.security.permission.PermissionChecker;
027    import com.liferay.portal.service.RepositoryServiceUtil;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
030    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
031    import com.liferay.portlet.documentlibrary.model.DLFolder;
032    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
033    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
034    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
035    import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
036    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
037    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
038    import com.liferay.portlet.documentlibrary.util.DLAppHelperThreadLocal;
039    import com.liferay.portlet.documentlibrary.util.DLUtil;
040    import com.liferay.portlet.trash.DuplicateEntryException;
041    import com.liferay.portlet.trash.TrashEntryConstants;
042    import com.liferay.portlet.trash.model.TrashEntry;
043    
044    import javax.portlet.PortletRequest;
045    
046    /**
047     * Implements trash handling for the file entry entity.
048     *
049     * @author Alexander Chow
050     * @author Manuel de la Peña
051     * @author Zsolt Berentey
052     */
053    public class DLFileEntryTrashHandler extends DLBaseTrashHandler {
054    
055            public static final String CLASS_NAME = DLFileEntry.class.getName();
056    
057            @Override
058            public void checkDuplicateTrashEntry(
059                            TrashEntry trashEntry, long containerModelId, String newName)
060                    throws PortalException, SystemException {
061    
062                    DLFileEntry dlFileEntry = getDLFileEntry(trashEntry.getClassPK());
063    
064                    if (containerModelId == TrashEntryConstants.DEFAULT_CONTAINER_ID) {
065                            containerModelId = dlFileEntry.getFolderId();
066                    }
067    
068                    String originalTitle = trashEntry.getTypeSettingsProperty("title");
069    
070                    DLFileEntry duplicateDLFileEntry =
071                            DLFileEntryLocalServiceUtil.fetchFileEntry(
072                                    dlFileEntry.getGroupId(), containerModelId, originalTitle);
073    
074                    if (duplicateDLFileEntry != null) {
075                            DuplicateEntryException dee = new DuplicateEntryException();
076    
077                            dee.setDuplicateEntryId(duplicateDLFileEntry.getFileEntryId());
078                            dee.setOldName(duplicateDLFileEntry.getTitle());
079                            dee.setTrashEntryId(trashEntry.getEntryId());
080    
081                            throw dee;
082                    }
083            }
084    
085            public void deleteTrashEntries(long[] classPKs, boolean checkPermission)
086                    throws PortalException, SystemException {
087    
088                    for (long classPK : classPKs) {
089                            if (checkPermission) {
090                                    DLAppServiceUtil.deleteFileEntry(classPK);
091                            }
092                            else {
093                                    DLAppLocalServiceUtil.deleteFileEntry(classPK);
094                            }
095                    }
096            }
097    
098            public String getClassName() {
099                    return CLASS_NAME;
100            }
101    
102            @Override
103            public ContainerModel getParentContainerModel(long classPK)
104                    throws PortalException, SystemException {
105    
106                    DLFileEntry dlFileEntry = getDLFileEntry(classPK);
107    
108                    long parentFolderId = dlFileEntry.getFolderId();
109    
110                    if (parentFolderId <= 0) {
111                            return null;
112                    }
113    
114                    return getContainerModel(parentFolderId);
115            }
116    
117            @Override
118            public String getRestoreLink(PortletRequest portletRequest, long classPK)
119                    throws PortalException, SystemException {
120    
121                    DLFileEntry dlFileEntry = getDLFileEntry(classPK);
122    
123                    return DLUtil.getDLControlPanelLink(
124                            portletRequest, dlFileEntry.getFolderId());
125            }
126    
127            @Override
128            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
129                    throws PortalException, SystemException {
130    
131                    DLFileEntry dlFileEntry = getDLFileEntry(classPK);
132    
133                    DLFolder dlFolder = dlFileEntry.getFolder();
134    
135                    return DLUtil.getAbsolutePath(portletRequest, dlFolder.getFolderId());
136            }
137    
138            @Override
139            public boolean hasTrashPermission(
140                            PermissionChecker permissionChecker, long groupId, long classPK,
141                            String trashActionId)
142                    throws PortalException, SystemException {
143    
144                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
145                            return DLFolderPermission.contains(
146                                    permissionChecker, groupId, classPK, ActionKeys.ADD_DOCUMENT);
147                    }
148    
149                    return super.hasTrashPermission(
150                            permissionChecker, groupId, classPK, trashActionId);
151            }
152    
153            public boolean isInTrash(long classPK)
154                    throws PortalException, SystemException {
155    
156                    try {
157                            DLFileEntry dlFileEntry = getDLFileEntry(classPK);
158    
159                            DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();
160    
161                            if (dlFileEntry.isInTrashContainer() || dlFileVersion.isInTrash()) {
162                                    return true;
163                            }
164    
165                            return false;
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                    DLFileEntry dlFileEntry = getDLFileEntry(classPK);
177    
178                    return dlFileEntry.isInTrashContainer();
179            }
180    
181            @Override
182            public boolean isRestorable(long classPK)
183                    throws PortalException, SystemException {
184    
185                    try {
186                            DLFileEntry dlFileEntry = getDLFileEntry(classPK);
187    
188                            return !dlFileEntry.isInTrashContainer();
189                    }
190                    catch (InvalidRepositoryException ire) {
191                            return false;
192                    }
193            }
194    
195            @Override
196            public void moveEntry(
197                            long classPK, long containerModelId, ServiceContext serviceContext)
198                    throws PortalException, SystemException {
199    
200                    DLAppServiceUtil.moveFileEntry(
201                            classPK, containerModelId, serviceContext);
202            }
203    
204            @Override
205            public void moveTrashEntry(
206                            long classPK, long containerModelId, ServiceContext serviceContext)
207                    throws PortalException, SystemException {
208    
209                    DLAppServiceUtil.moveFileEntryFromTrash(
210                            classPK, containerModelId, serviceContext);
211            }
212    
213            public void restoreTrashEntries(long[] classPKs)
214                    throws PortalException, SystemException {
215    
216                    for (long classPK : classPKs) {
217                            boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
218    
219                            try {
220                                    DLFileEntry dlFileEntry = getDLFileEntry(classPK);
221    
222                                    if (dlFileEntry.isInHiddenFolder()) {
223                                            DLAppHelperThreadLocal.setEnabled(false);
224                                    }
225    
226                                    DLAppServiceUtil.restoreFileEntryFromTrash(classPK);
227                            }
228                            finally {
229                                    DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
230                            }
231                    }
232            }
233    
234            @Override
235            public void updateTitle(long classPK, String name)
236                    throws PortalException, SystemException {
237    
238                    DLFileEntry dlFileEntry = getDLFileEntry(classPK);
239    
240                    dlFileEntry.setTitle(name);
241    
242                    DLFileEntryLocalServiceUtil.updateDLFileEntry(dlFileEntry);
243    
244                    DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();
245    
246                    dlFileVersion.setTitle(name);
247    
248                    DLFileVersionLocalServiceUtil.updateDLFileVersion(dlFileVersion);
249            }
250    
251            protected DLFileEntry getDLFileEntry(long classPK)
252                    throws PortalException, SystemException {
253    
254                    Repository repository = RepositoryServiceUtil.getRepositoryImpl(
255                            0, classPK, 0);
256    
257                    if (!(repository instanceof LiferayRepository)) {
258                            throw new InvalidRepositoryException(
259                                    "Repository " + repository.getRepositoryId() +
260                                            " does not support trash operations");
261                    }
262    
263                    FileEntry fileEntry = repository.getFileEntry(classPK);
264    
265                    return (DLFileEntry)fileEntry.getModel();
266            }
267    
268            @Override
269            protected Repository getRepository(long classPK)
270                    throws PortalException, SystemException {
271    
272                    Repository repository = RepositoryServiceUtil.getRepositoryImpl(
273                            0, classPK, 0);
274    
275                    if (!(repository instanceof LiferayRepository)) {
276                            throw new InvalidRepositoryException(
277                                    "Repository " + repository.getRepositoryId() +
278                                            " does not support trash operations");
279                    }
280    
281                    return repository;
282            }
283    
284            @Override
285            protected boolean hasPermission(
286                            PermissionChecker permissionChecker, long classPK, String actionId)
287                    throws PortalException, SystemException {
288    
289                    DLFileEntry dlFileEntry = getDLFileEntry(classPK);
290    
291                    if (dlFileEntry.isInHiddenFolder()) {
292                            return false;
293                    }
294    
295                    return DLFileEntryPermission.contains(
296                            permissionChecker, classPK, actionId);
297            }
298    
299    }