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