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_ANY);
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_ANY, 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(classPK, false);
202            }
203    
204            @Override
205            public List<TrashRenderer> getTrashContainerModelTrashRenderers(
206                            long classPK, int start, int end)
207                    throws PortalException, SystemException {
208    
209                    List<TrashRenderer> trashRenderers = new ArrayList<TrashRenderer>();
210    
211                    Repository repository = getRepository(classPK);
212    
213                    List<Folder> folders = repository.getFolders(
214                            classPK, false, start, end, null);
215    
216                    for (Folder folder : folders) {
217                            TrashHandler trashHandler =
218                                    TrashHandlerRegistryUtil.getTrashHandler(
219                                            DLFolder.class.getName());
220    
221                            TrashRenderer trashRenderer = trashHandler.getTrashRenderer(
222                                    folder.getPrimaryKey());
223    
224                            trashRenderers.add(trashRenderer);
225                    }
226    
227                    return trashRenderers;
228            }
229    
230            @Override
231            public boolean isMovable() {
232                    return true;
233            }
234    
235            protected DLFolder fetchDLFolder(long classPK)
236                    throws PortalException, SystemException {
237    
238                    Repository repository = RepositoryServiceUtil.getRepositoryImpl(
239                            classPK, 0, 0);
240    
241                    if (!(repository instanceof LiferayRepository)) {
242                            return null;
243                    }
244    
245                    Folder folder = repository.getFolder(classPK);
246    
247                    return (DLFolder)folder.getModel();
248            }
249    
250            protected DLFolder getDLFolder(long classPK)
251                    throws PortalException, SystemException {
252    
253                    Repository repository = RepositoryServiceUtil.getRepositoryImpl(
254                            classPK, 0, 0);
255    
256                    if (!(repository instanceof LiferayRepository)) {
257                            throw new InvalidRepositoryException(
258                                    "Repository " + repository.getRepositoryId() +
259                                            " does not support trash operations");
260                    }
261    
262                    Folder folder = repository.getFolder(classPK);
263    
264                    return (DLFolder)folder.getModel();
265            }
266    
267            protected abstract Repository getRepository(long classPK)
268                    throws PortalException, SystemException;
269    
270    }