001    /**
002     * Copyright (c) 2000-present 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.wiki.trash;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.trash.BaseTrashHandler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.workflow.WorkflowConstants;
021    import com.liferay.portal.model.ContainerModel;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portlet.trash.util.TrashUtil;
024    import com.liferay.portlet.wiki.model.WikiNode;
025    import com.liferay.portlet.wiki.model.WikiPage;
026    import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
027    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
028    
029    import java.util.ArrayList;
030    import java.util.List;
031    import java.util.Locale;
032    
033    /**
034     * @author Roberto D??az
035     */
036    public abstract class BaseWikiTrashHandler extends BaseTrashHandler {
037    
038            @Override
039            public ContainerModel getContainerModel(long containerModelId)
040                    throws PortalException {
041    
042                    WikiPage page = WikiPageLocalServiceUtil.fetchPage(containerModelId);
043    
044                    if (page == null) {
045                            return WikiNodeLocalServiceUtil.getNode(containerModelId);
046                    }
047    
048                    return page;
049            }
050    
051            @Override
052            public String getContainerModelClassName(long classPK) {
053                    WikiPage page = null;
054    
055                    try {
056                            page = WikiPageLocalServiceUtil.getPage(classPK);
057                    }
058                    catch (Exception e) {
059                            page = WikiPageLocalServiceUtil.fetchWikiPage(classPK);
060                    }
061    
062                    try {
063                            WikiPage parentPage = page.getParentPage();
064    
065                            while (parentPage != null) {
066                                    if (parentPage.isInTrashExplicitly()) {
067                                            return WikiPage.class.getName();
068                                    }
069    
070                                    parentPage = parentPage.getParentPage();
071                            }
072                    }
073                    catch (Exception e) {
074                    }
075    
076                    return WikiNode.class.getName();
077            }
078    
079            @Override
080            public String getContainerModelName(long classPK) throws PortalException {
081                    WikiPage page = WikiPageLocalServiceUtil.fetchPage(classPK);
082    
083                    if (page == null) {
084                            WikiNodeLocalServiceUtil.getNode(classPK);
085    
086                            return "wiki-node";
087                    }
088                    else {
089                            return "wiki-page";
090                    }
091            }
092    
093            @Override
094            public List<ContainerModel> getContainerModels(
095                            long classPK, long containerModelId, int start, int end)
096                    throws PortalException {
097    
098                    List<ContainerModel> containerModels = new ArrayList<ContainerModel>();
099    
100                    WikiPage page = null;
101    
102                    String parentTitle = StringPool.BLANK;
103    
104                    if (containerModelId > 0) {
105                            page = WikiPageLocalServiceUtil.getPage(containerModelId);
106    
107                            if (page == null) {
108                                    List<WikiPage> pages = WikiPageLocalServiceUtil.getPages(
109                                            containerModelId, start, end);
110    
111                                    for (WikiPage curPage : pages) {
112                                            containerModels.add(curPage);
113                                    }
114    
115                                    return containerModels;
116                            }
117    
118                            parentTitle = page.getTitle();
119                    }
120                    else {
121                            page = WikiPageLocalServiceUtil.getPage(classPK);
122                    }
123    
124                    List<WikiPage> pages = WikiPageLocalServiceUtil.getChildren(
125                            page.getNodeId(), true, parentTitle, start, end);
126    
127                    for (WikiPage curPage : pages) {
128                            containerModels.add(curPage);
129                    }
130    
131                    return containerModels;
132            }
133    
134            @Override
135            public int getContainerModelsCount(long classPK, long containerModelId)
136                    throws PortalException {
137    
138                    WikiPage page = null;
139    
140                    String parentTitle = StringPool.BLANK;
141    
142                    if (containerModelId > 0) {
143                            page = WikiPageLocalServiceUtil.fetchPage(containerModelId);
144    
145                            if (page == null) {
146                                    return WikiPageLocalServiceUtil.getPagesCount(containerModelId);
147                            }
148    
149                            parentTitle = page.getTitle();
150                    }
151                    else {
152                            page = WikiPageLocalServiceUtil.getPage(classPK);
153                    }
154    
155                    return WikiPageLocalServiceUtil.getChildrenCount(
156                            page.getNodeId(), true, parentTitle);
157            }
158    
159            @Override
160            public long getDestinationContainerModelId(
161                    long classPK, long destinationContainerModelId) {
162    
163                    if (destinationContainerModelId == 0) {
164                            WikiPage page = WikiPageLocalServiceUtil.fetchPage(classPK);
165    
166                            if (page != null) {
167                                    return page.getNodeId();
168                            }
169                    }
170    
171                    return destinationContainerModelId;
172            }
173    
174            @Override
175            public String getRootContainerModelClassName() {
176                    return WikiNode.class.getName();
177            }
178    
179            @Override
180            public long getRootContainerModelId(long classPK) throws PortalException {
181                    WikiPage page = WikiPageLocalServiceUtil.fetchLatestPage(
182                            classPK, WorkflowConstants.STATUS_ANY, false);
183    
184                    if (page == null) {
185                            WikiNode node = WikiNodeLocalServiceUtil.getNode(classPK);
186    
187                            return node.getNodeId();
188                    }
189    
190                    return page.getNodeId();
191            }
192    
193            @Override
194            public List<ContainerModel> getRootContainerModels(long groupId)
195                    throws PortalException {
196    
197                    List<ContainerModel> containerModels = new ArrayList<ContainerModel>();
198    
199                    List<WikiNode> nodes = WikiNodeLocalServiceUtil.getNodes(
200                            groupId, WorkflowConstants.STATUS_APPROVED);
201    
202                    for (WikiNode node : nodes) {
203                            containerModels.add(node);
204                    }
205    
206                    return containerModels;
207            }
208    
209            @Override
210            public int getRootContainerModelsCount(long groupId) {
211                    return WikiNodeLocalServiceUtil.getNodesCount(groupId);
212            }
213    
214            @Override
215            public String getRootContainerModelTitle(
216                            long containerModelId, Locale locale)
217                    throws PortalException {
218    
219                    WikiNode node = null;
220    
221                    WikiPage page = WikiPageLocalServiceUtil.fetchPage(containerModelId);
222    
223                    if (page == null) {
224                            node = WikiNodeLocalServiceUtil.getNode(containerModelId);
225                    }
226                    else {
227                            node = page.getNode();
228                    }
229    
230                    return TrashUtil.getOriginalTitle(node.getName());
231            }
232    
233            @Override
234            public String getSubcontainerModelName() {
235                    return "wiki-page";
236            }
237    
238            @Override
239            public void moveEntry(
240                            long userId, long classPK, long containerModelId,
241                            ServiceContext serviceContext)
242                    throws PortalException {
243    
244                    moveTrashEntry(userId, classPK, containerModelId, serviceContext);
245            }
246    
247            @Override
248            public void moveTrashEntry(
249                            long userId, long classPK, long containerModelId,
250                            ServiceContext serviceContext)
251                    throws PortalException {
252    
253                    WikiPage page = WikiPageLocalServiceUtil.getPage(classPK);
254    
255                    WikiPage parentPage = WikiPageLocalServiceUtil.fetchPage(
256                            containerModelId);
257    
258                    if (parentPage == null) {
259                            WikiPageLocalServiceUtil.movePageFromTrash(
260                                    userId, page.getNodeId(), page.getTitle(), containerModelId,
261                                    StringPool.BLANK);
262    
263                            return;
264                    }
265    
266                    WikiPageLocalServiceUtil.movePageFromTrash(
267                            userId, page.getNodeId(), page.getTitle(), parentPage.getNodeId(),
268                            parentPage.getTitle());
269            }
270    
271    }