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.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.WebKeys;
024    import com.liferay.portal.model.LayoutConstants;
025    import com.liferay.portal.security.permission.PermissionChecker;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.PortletKeys;
029    import com.liferay.portlet.PortletURLFactoryUtil;
030    import com.liferay.portlet.trash.DuplicateEntryException;
031    import com.liferay.portlet.trash.model.TrashEntry;
032    import com.liferay.portlet.wiki.asset.WikiNodeTrashRenderer;
033    import com.liferay.portlet.wiki.model.WikiNode;
034    import com.liferay.portlet.wiki.model.WikiPage;
035    import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
036    import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
037    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
038    import com.liferay.portlet.wiki.service.permission.WikiNodePermission;
039    
040    import java.util.ArrayList;
041    import java.util.List;
042    
043    import javax.portlet.PortletRequest;
044    import javax.portlet.PortletURL;
045    
046    /**
047     * Implements trash handling for the wiki node entity.
048     *
049     * @author Eudaldo Alonso
050     */
051    public class WikiNodeTrashHandler extends BaseTrashHandler {
052    
053            public static final String CLASS_NAME = WikiNode.class.getName();
054    
055            @Override
056            public void checkDuplicateTrashEntry(
057                            TrashEntry trashEntry, long containerModelId, String newName)
058                    throws PortalException, SystemException {
059    
060                    WikiNode node = WikiNodeLocalServiceUtil.getNode(
061                            trashEntry.getClassPK());
062    
063                    String originalTitle = trashEntry.getTypeSettingsProperty("title");
064    
065                    WikiNode duplicateNode = WikiNodeLocalServiceUtil.fetchWikiNode(
066                            node.getGroupId(), originalTitle);
067    
068                    if (duplicateNode != null) {
069                            DuplicateEntryException dee = new DuplicateEntryException();
070    
071                            dee.setDuplicateEntryId(duplicateNode.getNodeId());
072                            dee.setOldName(duplicateNode.getName());
073                            dee.setTrashEntryId(trashEntry.getEntryId());
074    
075                            throw dee;
076                    }
077            }
078    
079            public void deleteTrashEntries(long[] classPKs, boolean checkPermission)
080                    throws PortalException, SystemException {
081    
082                    for (long classPK : classPKs) {
083                            if (checkPermission) {
084                                    WikiNodeServiceUtil.deleteNode(classPK);
085                            }
086                            else {
087                                    WikiNodeLocalServiceUtil.deleteNode(classPK);
088                            }
089                    }
090            }
091    
092            public String getClassName() {
093                    return CLASS_NAME;
094            }
095    
096            @Override
097            public String getRestoreLink(PortletRequest portletRequest, long classPK)
098                    throws PortalException, SystemException {
099    
100                    String portletId = PortletKeys.WIKI;
101    
102                    WikiNode node = WikiNodeLocalServiceUtil.getNode(classPK);
103    
104                    long plid = PortalUtil.getPlidFromPortletId(
105                            node.getGroupId(), PortletKeys.WIKI);
106    
107                    if (plid == LayoutConstants.DEFAULT_PLID) {
108                            plid = PortalUtil.getControlPanelPlid(portletRequest);
109    
110                            portletId = PortletKeys.WIKI_ADMIN;
111                    }
112    
113                    PortletURL portletURL = PortletURLFactoryUtil.create(
114                            portletRequest, portletId, plid, PortletRequest.RENDER_PHASE);
115    
116                    return portletURL.toString();
117            }
118    
119            @Override
120            public String getRestoreMessage(
121                    PortletRequest portletRequest, long classPK) {
122    
123                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
124                            WebKeys.THEME_DISPLAY);
125    
126                    return themeDisplay.translate("wiki");
127            }
128    
129            @Override
130            public String getTrashContainedModelName() {
131                    return "wiki-pages";
132            }
133    
134            @Override
135            public int getTrashContainedModelsCount(long classPK)
136                    throws SystemException {
137    
138                    return WikiPageLocalServiceUtil.getPagesCount(classPK);
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, 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 TrashRenderer getTrashRenderer(long classPK)
167                    throws PortalException, SystemException {
168    
169                    WikiNode node = WikiNodeLocalServiceUtil.getNode(classPK);
170    
171                    return new WikiNodeTrashRenderer(node);
172            }
173    
174            @Override
175            public boolean isContainerModel() {
176                    return true;
177            }
178    
179            public boolean isInTrash(long classPK)
180                    throws PortalException, SystemException {
181    
182                    WikiNode node = WikiNodeLocalServiceUtil.getNode(classPK);
183    
184                    return node.isInTrash();
185            }
186    
187            public void restoreTrashEntries(long[] classPKs)
188                    throws PortalException, SystemException {
189    
190                    for (long classPK : classPKs) {
191                            WikiNodeServiceUtil.restoreNodeFromTrash(classPK);
192                    }
193            }
194    
195            @Override
196            public void updateTitle(long classPK, String name)
197                    throws PortalException, SystemException {
198    
199                    WikiNode node = WikiNodeLocalServiceUtil.getNode(classPK);
200    
201                    node.setName(name);
202    
203                    WikiNodeLocalServiceUtil.updateWikiNode(node);
204            }
205    
206            @Override
207            protected boolean hasPermission(
208                            PermissionChecker permissionChecker, long classPK, String actionId)
209                    throws PortalException, SystemException {
210    
211                    return WikiNodePermission.contains(
212                            permissionChecker, classPK, actionId);
213            }
214    
215    }