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