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