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.capabilities.TrashCapability;
022    import com.liferay.portal.kernel.repository.model.FileEntry;
023    import com.liferay.portal.kernel.repository.model.Folder;
024    import com.liferay.portal.kernel.trash.BaseTrashHandler;
025    import com.liferay.portal.kernel.trash.TrashHandler;
026    import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
027    import com.liferay.portal.kernel.trash.TrashRenderer;
028    import com.liferay.portal.kernel.util.StringPool;
029    import com.liferay.portal.kernel.workflow.WorkflowConstants;
030    import com.liferay.portal.model.ContainerModel;
031    import com.liferay.portal.service.RepositoryServiceUtil;
032    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
033    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
034    import com.liferay.portlet.documentlibrary.model.DLFolder;
035    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
036    
037    import java.util.ArrayList;
038    import java.util.List;
039    import java.util.Locale;
040    
041    /**
042     * @author Zsolt Berentey
043     */
044    public abstract class DLBaseTrashHandler extends BaseTrashHandler {
045    
046            @Override
047            public ContainerModel getContainerModel(long containerModelId)
048                    throws PortalException {
049    
050                    if (containerModelId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
051                            return null;
052                    }
053    
054                    return getDLFolder(containerModelId);
055            }
056    
057            @Override
058            public String getContainerModelClassName(long classPK) {
059                    return DLFolder.class.getName();
060            }
061    
062            @Override
063            public String getContainerModelName(long classPK) {
064                    return "folder";
065            }
066    
067            @Override
068            public List<ContainerModel> getContainerModels(
069                            long classPK, long parentContainerModelId, int start, int end)
070                    throws PortalException {
071    
072                    Repository repository = getRepository(classPK);
073    
074                    List<Folder> folders = repository.getFolders(
075                            parentContainerModelId, false, start, end, null);
076    
077                    List<ContainerModel> containerModels = new ArrayList<ContainerModel>(
078                            folders.size());
079    
080                    for (Folder folder : folders) {
081                            containerModels.add((ContainerModel)folder.getModel());
082                    }
083    
084                    return containerModels;
085            }
086    
087            @Override
088            public int getContainerModelsCount(
089                            long classPK, long parentContainerModelId)
090                    throws PortalException {
091    
092                    Repository repository = getRepository(classPK);
093    
094                    return repository.getFoldersCount(parentContainerModelId, false);
095            }
096    
097            @Override
098            public List<ContainerModel> getParentContainerModels(long classPK)
099                    throws PortalException {
100    
101                    List<ContainerModel> containerModels = new ArrayList<ContainerModel>();
102    
103                    ContainerModel containerModel = getParentContainerModel(classPK);
104    
105                    if (containerModel == null) {
106                            return containerModels;
107                    }
108    
109                    containerModels.add(containerModel);
110    
111                    while (containerModel.getParentContainerModelId() > 0) {
112                            containerModel = getContainerModel(
113                                    containerModel.getParentContainerModelId());
114    
115                            if (containerModel == null) {
116                                    break;
117                            }
118    
119                            containerModels.add(containerModel);
120                    }
121    
122                    return containerModels;
123            }
124    
125            @Override
126            public String getRootContainerModelName() {
127                    return "folder";
128            }
129    
130            @Override
131            public String getRootContainerModelTitle(
132                    long containerModelId, Locale locale) {
133    
134                    return LanguageUtil.get(locale, "home");
135            }
136    
137            @Override
138            public String getTrashContainedModelName() {
139                    return "documents";
140            }
141    
142            @Override
143            public int getTrashContainedModelsCount(long classPK)
144                    throws PortalException {
145    
146                    Repository repository = getRepository(classPK);
147    
148                    return repository.getFileEntriesAndFileShortcutsCount(
149                            classPK, WorkflowConstants.STATUS_IN_TRASH);
150            }
151    
152            @Override
153            public List<TrashRenderer> getTrashContainedModelTrashRenderers(
154                            long classPK, int start, int end)
155                    throws PortalException {
156    
157                    List<TrashRenderer> trashRenderers = new ArrayList<TrashRenderer>();
158    
159                    Repository repository = getRepository(classPK);
160    
161                    List<Object> fileEntriesAndFileShortcuts =
162                            repository.getFileEntriesAndFileShortcuts(
163                                    classPK, WorkflowConstants.STATUS_IN_TRASH, start, end);
164    
165                    for (Object fileEntryOrFileShortcut : fileEntriesAndFileShortcuts) {
166                            String curClassName = StringPool.BLANK;
167                            long curClassPK = 0;
168    
169                            if (fileEntryOrFileShortcut instanceof DLFileShortcut) {
170                                    DLFileShortcut dlFileShortcut =
171                                            (DLFileShortcut)fileEntryOrFileShortcut;
172    
173                                    curClassName = DLFileShortcut.class.getName();
174                                    curClassPK = dlFileShortcut.getPrimaryKey();
175                            }
176                            else if (fileEntryOrFileShortcut instanceof FileEntry) {
177                                    FileEntry fileEntry = (FileEntry)fileEntryOrFileShortcut;
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<TrashRenderer>();
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 = RepositoryServiceUtil.getRepositoryImpl(
247                            classPK, 0, 0);
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 = RepositoryServiceUtil.getRepositoryImpl(
260                            classPK, 0, 0);
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    }