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.language.LanguageUtil;
020    import com.liferay.portal.kernel.repository.Repository;
021    import com.liferay.portal.kernel.repository.RepositoryProviderUtil;
022    import com.liferay.portal.kernel.repository.capabilities.TrashCapability;
023    import com.liferay.portal.kernel.repository.model.FileEntry;
024    import com.liferay.portal.kernel.repository.model.FileShortcut;
025    import com.liferay.portal.kernel.repository.model.Folder;
026    import com.liferay.portal.kernel.repository.model.RepositoryEntry;
027    import com.liferay.portal.kernel.trash.BaseTrashHandler;
028    import com.liferay.portal.kernel.trash.TrashHandler;
029    import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
030    import com.liferay.portal.kernel.trash.TrashRenderer;
031    import com.liferay.portal.kernel.util.StringPool;
032    import com.liferay.portal.kernel.workflow.WorkflowConstants;
033    import com.liferay.portal.model.ContainerModel;
034    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
035    import com.liferay.portlet.documentlibrary.model.DLFileShortcutConstants;
036    import com.liferay.portlet.documentlibrary.model.DLFolder;
037    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
038    
039    import java.util.ArrayList;
040    import java.util.List;
041    import java.util.Locale;
042    
043    /**
044     * @author Zsolt Berentey
045     */
046    public abstract class DLBaseTrashHandler extends BaseTrashHandler {
047    
048            @Override
049            public ContainerModel getContainerModel(long containerModelId)
050                    throws PortalException {
051    
052                    if (containerModelId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
053                            return null;
054                    }
055    
056                    return getDLFolder(containerModelId);
057            }
058    
059            @Override
060            public String getContainerModelClassName(long classPK) {
061                    return DLFolder.class.getName();
062            }
063    
064            @Override
065            public String getContainerModelName(long classPK) {
066                    return "folder";
067            }
068    
069            @Override
070            public List<ContainerModel> getContainerModels(
071                            long classPK, long parentContainerModelId, int start, int end)
072                    throws PortalException {
073    
074                    Repository repository = getRepository(classPK);
075    
076                    List<Folder> folders = repository.getFolders(
077                            parentContainerModelId, false, start, end, null);
078    
079                    List<ContainerModel> containerModels = new ArrayList<>(folders.size());
080    
081                    for (Folder folder : folders) {
082                            containerModels.add((ContainerModel)folder.getModel());
083                    }
084    
085                    return containerModels;
086            }
087    
088            @Override
089            public int getContainerModelsCount(
090                            long classPK, long parentContainerModelId)
091                    throws PortalException {
092    
093                    Repository repository = getRepository(classPK);
094    
095                    return repository.getFoldersCount(parentContainerModelId, false);
096            }
097    
098            @Override
099            public List<ContainerModel> getParentContainerModels(long classPK)
100                    throws PortalException {
101    
102                    List<ContainerModel> containerModels = new ArrayList<>();
103    
104                    ContainerModel containerModel = getParentContainerModel(classPK);
105    
106                    if (containerModel == null) {
107                            return containerModels;
108                    }
109    
110                    containerModels.add(containerModel);
111    
112                    while (containerModel.getParentContainerModelId() > 0) {
113                            containerModel = getContainerModel(
114                                    containerModel.getParentContainerModelId());
115    
116                            if (containerModel == null) {
117                                    break;
118                            }
119    
120                            containerModels.add(containerModel);
121                    }
122    
123                    return containerModels;
124            }
125    
126            @Override
127            public String getRootContainerModelName() {
128                    return "folder";
129            }
130    
131            @Override
132            public String getRootContainerModelTitle(
133                    long containerModelId, Locale locale) {
134    
135                    return LanguageUtil.get(locale, "home");
136            }
137    
138            @Override
139            public String getTrashContainedModelName() {
140                    return "documents";
141            }
142    
143            @Override
144            public int getTrashContainedModelsCount(long classPK)
145                    throws PortalException {
146    
147                    Repository repository = getRepository(classPK);
148    
149                    return repository.getFileEntriesAndFileShortcutsCount(
150                            classPK, WorkflowConstants.STATUS_IN_TRASH);
151            }
152    
153            @Override
154            public List<TrashRenderer> getTrashContainedModelTrashRenderers(
155                            long classPK, int start, int end)
156                    throws PortalException {
157    
158                    List<TrashRenderer> trashRenderers = new ArrayList<>();
159    
160                    Repository repository = getRepository(classPK);
161    
162                    List<RepositoryEntry> repositoryEntries =
163                            repository.getFileEntriesAndFileShortcuts(
164                                    classPK, WorkflowConstants.STATUS_IN_TRASH, start, end);
165    
166                    for (RepositoryEntry repositoryEntry : repositoryEntries) {
167                            String curClassName = StringPool.BLANK;
168                            long curClassPK = 0;
169    
170                            if (repositoryEntry instanceof FileShortcut) {
171                                    FileShortcut fileShortcut = (FileShortcut)repositoryEntry;
172    
173                                    curClassName = DLFileShortcutConstants.getClassName();
174                                    curClassPK = fileShortcut.getPrimaryKey();
175                            }
176                            else if (repositoryEntry instanceof FileEntry) {
177                                    FileEntry fileEntry = (FileEntry)repositoryEntry;
178    
179                                    curClassName = DLFileEntry.class.getName();
180                                    curClassPK = fileEntry.getPrimaryKey();
181                            }
182                            else {
183                                    continue;
184                            }
185    
186                            TrashHandler trashHandler =
187                                    TrashHandlerRegistryUtil.getTrashHandler(curClassName);
188    
189                            TrashRenderer trashRenderer = trashHandler.getTrashRenderer(
190                                    curClassPK);
191    
192                            trashRenderers.add(trashRenderer);
193                    }
194    
195                    return trashRenderers;
196            }
197    
198            @Override
199            public String getTrashContainerModelName() {
200                    return "folders";
201            }
202    
203            @Override
204            public int getTrashContainerModelsCount(long classPK)
205                    throws PortalException {
206    
207                    Repository repository = getRepository(classPK);
208    
209                    return repository.getFoldersCount(
210                            classPK, WorkflowConstants.STATUS_IN_TRASH, false);
211            }
212    
213            @Override
214            public List<TrashRenderer> getTrashContainerModelTrashRenderers(
215                            long classPK, int start, int end)
216                    throws PortalException {
217    
218                    List<TrashRenderer> trashRenderers = new ArrayList<>();
219    
220                    Repository repository = getRepository(classPK);
221    
222                    List<Folder> folders = repository.getFolders(
223                            classPK, WorkflowConstants.STATUS_IN_TRASH, false, start, end,
224                            null);
225    
226                    for (Folder folder : folders) {
227                            TrashHandler trashHandler =
228                                    TrashHandlerRegistryUtil.getTrashHandler(
229                                            DLFolder.class.getName());
230    
231                            TrashRenderer trashRenderer = trashHandler.getTrashRenderer(
232                                    folder.getPrimaryKey());
233    
234                            trashRenderers.add(trashRenderer);
235                    }
236    
237                    return trashRenderers;
238            }
239    
240            @Override
241            public boolean isMovable() {
242                    return true;
243            }
244    
245            protected DLFolder fetchDLFolder(long classPK) throws PortalException {
246                    Repository repository = RepositoryProviderUtil.getFolderRepository(
247                            classPK);
248    
249                    if (!repository.isCapabilityProvided(TrashCapability.class)) {
250                            return null;
251                    }
252    
253                    Folder folder = repository.getFolder(classPK);
254    
255                    return (DLFolder)folder.getModel();
256            }
257    
258            protected DLFolder getDLFolder(long classPK) throws PortalException {
259                    Repository repository = RepositoryProviderUtil.getFolderRepository(
260                            classPK);
261    
262                    if (!repository.isCapabilityProvided(TrashCapability.class)) {
263                            throw new InvalidRepositoryException(
264                                    "Repository " + repository.getRepositoryId() +
265                                            " does not support trash operations");
266                    }
267    
268                    Folder folder = repository.getFolder(classPK);
269    
270                    return (DLFolder)folder.getModel();
271            }
272    
273            protected abstract Repository getRepository(long classPK)
274                    throws PortalException;
275    
276    }