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.journal.trash;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.trash.BaseTrashHandler;
020    import com.liferay.portal.kernel.trash.TrashHandler;
021    import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
022    import com.liferay.portal.kernel.trash.TrashRenderer;
023    import com.liferay.portal.model.ContainerModel;
024    import com.liferay.portlet.journal.model.JournalArticle;
025    import com.liferay.portlet.journal.model.JournalFolder;
026    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
027    import com.liferay.portlet.journal.service.JournalFolderLocalServiceUtil;
028    
029    import java.util.ArrayList;
030    import java.util.List;
031    
032    /**
033     * @author Eudaldo Alonso
034     */
035    public abstract class JournalBaseTrashHandler extends BaseTrashHandler {
036    
037            @Override
038            public ContainerModel getContainerModel(long containerModelId)
039                    throws PortalException, SystemException {
040    
041                    return JournalFolderLocalServiceUtil.getFolder(containerModelId);
042            }
043    
044            @Override
045            public String getContainerModelClassName() {
046                    return JournalFolder.class.getName();
047            }
048    
049            @Override
050            public String getContainerModelName() {
051                    return "folder";
052            }
053    
054            @Override
055            public List<ContainerModel> getContainerModels(
056                            long classPK, long parentContainerModelId, int start, int end)
057                    throws PortalException, SystemException {
058    
059                    List<JournalFolder> folders =
060                            JournalFolderLocalServiceUtil.getFolders(
061                                    getGroupId(classPK), parentContainerModelId, start, end);
062    
063                    List<ContainerModel> containerModels = new ArrayList<ContainerModel>(
064                            folders.size());
065    
066                    for (JournalFolder curFolder : folders) {
067                            containerModels.add(curFolder);
068                    }
069    
070                    return containerModels;
071            }
072    
073            @Override
074            public int getContainerModelsCount(
075                            long classPK, long parentContainerModelId)
076                    throws PortalException, SystemException {
077    
078                    return JournalFolderLocalServiceUtil.getFoldersCount(
079                            getGroupId(classPK), parentContainerModelId);
080            }
081    
082            @Override
083            public List<ContainerModel> getParentContainerModels(long classPK)
084                    throws PortalException, SystemException {
085    
086                    List<ContainerModel> containerModels = new ArrayList<ContainerModel>();
087    
088                    ContainerModel containerModel = getParentContainerModel(classPK);
089    
090                    if (containerModel == null) {
091                            return containerModels;
092                    }
093    
094                    containerModels.add(containerModel);
095    
096                    while (containerModel.getParentContainerModelId() > 0) {
097                            containerModel = getContainerModel(
098                                    containerModel.getParentContainerModelId());
099    
100                            if (containerModel == null) {
101                                    break;
102                            }
103    
104                            containerModels.add(containerModel);
105                    }
106    
107                    return containerModels;
108            }
109    
110            @Override
111            public String getRootContainerModelName() {
112                    return "home";
113            }
114    
115            @Override
116            public String getTrashContainedModelName() {
117                    return "article";
118            }
119    
120            @Override
121            public int getTrashContainedModelsCount(long classPK)
122                    throws PortalException, SystemException {
123    
124                    JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(classPK);
125    
126                    return JournalArticleLocalServiceUtil.getArticlesCount(
127                            folder.getGroupId(), classPK);
128            }
129    
130            @Override
131            public List<TrashRenderer> getTrashContainedModelTrashRenderers(
132                            long classPK, int start, int end)
133                    throws PortalException, SystemException {
134    
135                    List<TrashRenderer> trashRenderers = new ArrayList<TrashRenderer>();
136    
137                    JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(classPK);
138    
139                    List<JournalArticle> articles =
140                            JournalArticleLocalServiceUtil.getArticles(
141                                    folder.getGroupId(), classPK, start, end);
142    
143                    for (JournalArticle article : articles) {
144                            TrashHandler trashHandler =
145                                    TrashHandlerRegistryUtil.getTrashHandler(
146                                            JournalArticle.class.getName());
147    
148                            TrashRenderer trashRenderer = trashHandler.getTrashRenderer(
149                                    article.getResourcePrimKey());
150    
151                            trashRenderers.add(trashRenderer);
152                    }
153    
154                    return trashRenderers;
155            }
156    
157            @Override
158            public String getTrashContainerModelName() {
159                    return "folders";
160            }
161    
162            @Override
163            public int getTrashContainerModelsCount(long classPK)
164                    throws PortalException, SystemException {
165    
166                    JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(classPK);
167    
168                    return JournalFolderLocalServiceUtil.getFoldersCount(
169                            folder.getGroupId(), classPK);
170            }
171    
172            @Override
173            public List<TrashRenderer> getTrashContainerModelTrashRenderers(
174                            long classPK, int start, int end)
175                    throws PortalException, SystemException {
176    
177                    List<TrashRenderer> trashRenderers = new ArrayList<TrashRenderer>();
178    
179                    JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(classPK);
180    
181                    List<JournalFolder> folders =
182                            JournalFolderLocalServiceUtil.getFolders(
183                                            folder.getGroupId(), classPK, start, end);
184    
185                    for (JournalFolder curFolder : folders) {
186                            TrashHandler trashHandler =
187                                    TrashHandlerRegistryUtil.getTrashHandler(
188                                            JournalFolder.class.getName());
189    
190                            TrashRenderer trashRenderer = trashHandler.getTrashRenderer(
191                                    curFolder.getPrimaryKey());
192    
193                            trashRenderers.add(trashRenderer);
194                    }
195    
196                    return trashRenderers;
197            }
198    
199            @Override
200            public boolean isMovable() {
201                    return true;
202            }
203    
204            protected abstract long getGroupId(long classPK)
205                    throws PortalException, SystemException;
206    
207    }