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.wiki.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.kernel.util.Validator;
024    import com.liferay.portal.kernel.util.WebKeys;
025    import com.liferay.portal.kernel.workflow.WorkflowConstants;
026    import com.liferay.portal.model.LayoutConstants;
027    import com.liferay.portal.security.permission.PermissionChecker;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.PortletKeys;
031    import com.liferay.portlet.PortletURLFactoryUtil;
032    import com.liferay.portlet.trash.DuplicateEntryException;
033    import com.liferay.portlet.trash.model.TrashEntry;
034    import com.liferay.portlet.wiki.asset.WikiNodeTrashRenderer;
035    import com.liferay.portlet.wiki.model.WikiNode;
036    import com.liferay.portlet.wiki.model.WikiPage;
037    import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
038    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
039    import com.liferay.portlet.wiki.service.permission.WikiNodePermission;
040    
041    import java.util.ArrayList;
042    import java.util.List;
043    
044    import javax.portlet.PortletRequest;
045    import javax.portlet.PortletURL;
046    
047    /**
048     * Implements trash handling for the wiki node entity.
049     *
050     * @author Eudaldo Alonso
051     */
052    public class WikiNodeTrashHandler extends BaseTrashHandler {
053    
054            @Override
055            public void checkDuplicateTrashEntry(
056                            TrashEntry trashEntry, long containerModelId, String newName)
057                    throws PortalException, SystemException {
058    
059                    WikiNode node = WikiNodeLocalServiceUtil.getNode(
060                            trashEntry.getClassPK());
061    
062                    String originalTitle = trashEntry.getTypeSettingsProperty("title");
063    
064                    if (Validator.isNotNull(newName)) {
065                            originalTitle = newName;
066                    }
067    
068                    WikiNode duplicateNode = WikiNodeLocalServiceUtil.fetchNode(
069                            node.getGroupId(), originalTitle);
070    
071                    if (duplicateNode != null) {
072                            DuplicateEntryException dee = new DuplicateEntryException();
073    
074                            dee.setDuplicateEntryId(duplicateNode.getNodeId());
075                            dee.setOldName(duplicateNode.getName());
076                            dee.setTrashEntryId(trashEntry.getEntryId());
077    
078                            throw dee;
079                    }
080            }
081    
082            @Override
083            public void deleteTrashEntry(long classPK)
084                    throws PortalException, SystemException {
085    
086                    WikiNodeLocalServiceUtil.deleteNode(classPK);
087            }
088    
089            @Override
090            public String getClassName() {
091                    return WikiNode.class.getName();
092            }
093    
094            @Override
095            public String getRestoreContainedModelLink(
096                            PortletRequest portletRequest, long classPK)
097                    throws PortalException, SystemException {
098    
099                    WikiNode node = WikiNodeLocalServiceUtil.getNode(classPK);
100    
101                    PortletURL portletURL = getRestoreURL(portletRequest, classPK, false);
102    
103                    portletURL.setParameter("nodeId", String.valueOf(node.getNodeId()));
104    
105                    return portletURL.toString();
106            }
107    
108            @Override
109            public String getRestoreContainerModelLink(
110                            PortletRequest portletRequest, long classPK)
111                    throws PortalException, SystemException {
112    
113                    PortletURL portletURL = getRestoreURL(portletRequest, classPK, true);
114    
115                    return portletURL.toString();
116            }
117    
118            @Override
119            public String getRestoreMessage(
120                    PortletRequest portletRequest, long classPK) {
121    
122                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
123                            WebKeys.THEME_DISPLAY);
124    
125                    return themeDisplay.translate("wiki");
126            }
127    
128            @Override
129            public String getTrashContainedModelName() {
130                    return "wiki-pages";
131            }
132    
133            @Override
134            public int getTrashContainedModelsCount(long classPK)
135                    throws SystemException {
136    
137                    return WikiPageLocalServiceUtil.getPagesCount(
138                            classPK, true, WorkflowConstants.STATUS_IN_TRASH);
139            }
140    
141            @Override
142            public List<TrashRenderer> getTrashContainedModelTrashRenderers(
143                            long classPK, int start, int end)
144                    throws PortalException, SystemException {
145    
146                    List<TrashRenderer> trashRenderers = new ArrayList<TrashRenderer>();
147    
148                    List<WikiPage> pages = WikiPageLocalServiceUtil.getPages(
149                            classPK, true, WorkflowConstants.STATUS_IN_TRASH, start, end);
150    
151                    for (WikiPage page : pages) {
152                            TrashHandler trashHandler =
153                                    TrashHandlerRegistryUtil.getTrashHandler(
154                                            WikiPage.class.getName());
155    
156                            TrashRenderer trashRenderer = trashHandler.getTrashRenderer(
157                                    page.getResourcePrimKey());
158    
159                            trashRenderers.add(trashRenderer);
160                    }
161    
162                    return trashRenderers;
163            }
164    
165            @Override
166            public TrashEntry getTrashEntry(long classPK)
167                    throws PortalException, SystemException {
168    
169                    WikiNode node = WikiNodeLocalServiceUtil.getNode(classPK);
170    
171                    return node.getTrashEntry();
172            }
173    
174            @Override
175            public TrashRenderer getTrashRenderer(long classPK)
176                    throws PortalException, SystemException {
177    
178                    WikiNode node = WikiNodeLocalServiceUtil.getNode(classPK);
179    
180                    return new WikiNodeTrashRenderer(node);
181            }
182    
183            @Override
184            public boolean isContainerModel() {
185                    return true;
186            }
187    
188            @Override
189            public boolean isInTrash(long classPK)
190                    throws PortalException, SystemException {
191    
192                    WikiNode node = WikiNodeLocalServiceUtil.getNode(classPK);
193    
194                    return node.isInTrash();
195            }
196    
197            @Override
198            public void restoreTrashEntry(long userId, long classPK)
199                    throws PortalException, SystemException {
200    
201                    WikiNode node = WikiNodeLocalServiceUtil.getNode(classPK);
202    
203                    WikiNodeLocalServiceUtil.restoreNodeFromTrash(userId, node);
204            }
205    
206            @Override
207            public void updateTitle(long classPK, String name)
208                    throws PortalException, SystemException {
209    
210                    WikiNode node = WikiNodeLocalServiceUtil.getNode(classPK);
211    
212                    node.setName(name);
213    
214                    WikiNodeLocalServiceUtil.updateWikiNode(node);
215            }
216    
217            protected PortletURL getRestoreURL(
218                            PortletRequest portletRequest, long classPK,
219                            boolean isContainerModel)
220                    throws PortalException, SystemException {
221    
222                    String portletId = PortletKeys.WIKI;
223    
224                    WikiNode node = WikiNodeLocalServiceUtil.getNode(classPK);
225    
226                    long plid = PortalUtil.getPlidFromPortletId(
227                            node.getGroupId(), PortletKeys.WIKI);
228    
229                    if (plid == LayoutConstants.DEFAULT_PLID) {
230                            portletId = PortletKeys.WIKI_ADMIN;
231    
232                            plid = PortalUtil.getControlPanelPlid(portletRequest);
233                    }
234    
235                    PortletURL portletURL = PortletURLFactoryUtil.create(
236                            portletRequest, portletId, plid, PortletRequest.RENDER_PHASE);
237    
238                    if (!isContainerModel) {
239                            if (portletId.equals(PortletKeys.WIKI)) {
240                                    portletURL.setParameter(
241                                            "struts_action", "/wiki/view_all_pages");
242                            }
243                            else {
244                                    portletURL.setParameter(
245                                            "struts_action", "/wiki_admin/view_all_pages");
246                            }
247                    }
248    
249                    return portletURL;
250            }
251    
252            @Override
253            protected boolean hasPermission(
254                            PermissionChecker permissionChecker, long classPK, String actionId)
255                    throws PortalException, SystemException {
256    
257                    return WikiNodePermission.contains(
258                            permissionChecker, classPK, actionId);
259            }
260    
261    }