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.TrashRenderer;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portlet.trash.DuplicateEntryException;
024    import com.liferay.portlet.trash.model.TrashEntry;
025    import com.liferay.portlet.trash.util.TrashUtil;
026    import com.liferay.portlet.wiki.asset.WikiNodeTrashRenderer;
027    import com.liferay.portlet.wiki.model.WikiNode;
028    import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
029    import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
030    import com.liferay.portlet.wiki.service.permission.WikiNodePermission;
031    
032    /**
033     * Implements trash handling for the wiki node entity.
034     *
035     * @author Eudaldo Alonso
036     */
037    public class WikiNodeTrashHandler extends BaseTrashHandler {
038    
039            public static final String CLASS_NAME = WikiNode.class.getName();
040    
041            @Override
042            public void checkDuplicateTrashEntry(
043                            TrashEntry trashEntry, long containerModelId, String newName)
044                    throws PortalException, SystemException {
045    
046                    WikiNode node = WikiNodeLocalServiceUtil.getNode(
047                            trashEntry.getClassPK());
048    
049                    String restoredTitle = node.getName();
050    
051                    if (Validator.isNotNull(newName)) {
052                            restoredTitle = newName;
053                    }
054    
055                    String originalTitle = TrashUtil.stripTrashNamespace(restoredTitle);
056    
057                    WikiNode duplicateNode = WikiNodeLocalServiceUtil.fetchWikiNode(
058                            node.getGroupId(), originalTitle);
059    
060                    if (duplicateNode != null) {
061                            DuplicateEntryException dee = new DuplicateEntryException();
062    
063                            dee.setDuplicateEntryId(duplicateNode.getNodeId());
064                            dee.setOldName(duplicateNode.getName());
065                            dee.setTrashEntryId(trashEntry.getEntryId());
066    
067                            throw dee;
068                    }
069            }
070    
071            public void deleteTrashEntries(long[] classPKs, boolean checkPermission)
072                    throws PortalException, SystemException {
073    
074                    for (long classPK : classPKs) {
075                            if (checkPermission) {
076                                    WikiNodeServiceUtil.deleteNode(classPK);
077                            }
078                            else {
079                                    WikiNodeLocalServiceUtil.deleteNode(classPK);
080                            }
081                    }
082            }
083    
084            public String getClassName() {
085                    return CLASS_NAME;
086            }
087    
088            @Override
089            public TrashRenderer getTrashRenderer(long classPK)
090                    throws PortalException, SystemException {
091    
092                    WikiNode node = WikiNodeLocalServiceUtil.getNode(classPK);
093    
094                    return new WikiNodeTrashRenderer(node);
095            }
096    
097            public boolean isInTrash(long classPK)
098                    throws PortalException, SystemException {
099    
100                    WikiNode node = WikiNodeLocalServiceUtil.getNode(classPK);
101    
102                    return node.isInTrash();
103            }
104    
105            public void restoreTrashEntries(long[] classPKs)
106                    throws PortalException, SystemException {
107    
108                    for (long classPK : classPKs) {
109                            WikiNodeServiceUtil.restoreNodeFromTrash(classPK);
110                    }
111            }
112    
113            @Override
114            public void updateTitle(long classPK, String name)
115                    throws PortalException, SystemException {
116    
117                    WikiNode node = WikiNodeLocalServiceUtil.getNode(classPK);
118    
119                    node.setName(name);
120    
121                    WikiNodeLocalServiceUtil.updateWikiNode(node);
122            }
123    
124            @Override
125            protected boolean hasPermission(
126                            PermissionChecker permissionChecker, long classPK, String actionId)
127                    throws PortalException, SystemException {
128    
129                    return WikiNodePermission.contains(
130                            permissionChecker, classPK, actionId);
131            }
132    
133    }