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.blogs.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.model.LayoutConstants;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.PortletURLFactoryUtil;
027    import com.liferay.portlet.blogs.model.BlogsEntry;
028    import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
029    import com.liferay.portlet.blogs.service.BlogsEntryServiceUtil;
030    import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission;
031    
032    import javax.portlet.PortletRequest;
033    import javax.portlet.PortletURL;
034    
035    /**
036     * Implements trash handling for the blogs entry entity.
037     *
038     * @author Zsolt Berentey
039     */
040    public class BlogsEntryTrashHandler extends BaseTrashHandler {
041    
042            public static final String CLASS_NAME = BlogsEntry.class.getName();
043    
044            public void deleteTrashEntries(long[] classPKs, boolean checkPermission)
045                    throws PortalException, SystemException {
046    
047                    for (long classPK : classPKs) {
048                            if (checkPermission) {
049                                    BlogsEntryServiceUtil.deleteEntry(classPK);
050                            }
051                            else {
052                                    BlogsEntryLocalServiceUtil.deleteEntry(classPK);
053                            }
054                    }
055            }
056    
057            public String getClassName() {
058                    return CLASS_NAME;
059            }
060    
061            @Override
062            public String getRestoreLink(PortletRequest portletRequest, long classPK)
063                    throws PortalException, SystemException {
064    
065                    String portletId = PortletKeys.BLOGS;
066    
067                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
068    
069                    long plid = PortalUtil.getPlidFromPortletId(
070                            entry.getGroupId(), PortletKeys.BLOGS);
071    
072                    if (plid == LayoutConstants.DEFAULT_PLID) {
073                            portletId = PortletKeys.BLOGS_ADMIN;
074    
075                            plid = PortalUtil.getControlPanelPlid(portletRequest);
076                    }
077    
078                    PortletURL portletURL = PortletURLFactoryUtil.create(
079                            portletRequest, portletId, plid, PortletRequest.RENDER_PHASE);
080    
081                    return portletURL.toString();
082            }
083    
084            @Override
085            public String getRestoreMessage(
086                    PortletRequest portletRequest, long classPK) {
087    
088                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
089                            WebKeys.THEME_DISPLAY);
090    
091                    return themeDisplay.translate("blogs");
092            }
093    
094            public boolean isInTrash(long classPK)
095                    throws PortalException, SystemException {
096    
097                    BlogsEntry entry = BlogsEntryServiceUtil.getEntry(classPK);
098    
099                    return entry.isInTrash();
100            }
101    
102            public void restoreTrashEntries(long[] classPKs)
103                    throws PortalException, SystemException {
104    
105                    for (long classPK : classPKs) {
106                            BlogsEntryServiceUtil.restoreEntryFromTrash(classPK);
107                    }
108            }
109    
110            @Override
111            protected boolean hasPermission(
112                            PermissionChecker permissionChecker, long classPK, String actionId)
113                    throws PortalException, SystemException {
114    
115                    return BlogsEntryPermission.contains(
116                            permissionChecker, classPK, actionId);
117            }
118    
119    }