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.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.permission.BlogsEntryPermission;
030    
031    import javax.portlet.PortletRequest;
032    import javax.portlet.PortletURL;
033    
034    /**
035     * Implements trash handling for the blogs entry entity.
036     *
037     * @author Zsolt Berentey
038     */
039    public class BlogsEntryTrashHandler extends BaseTrashHandler {
040    
041            @Override
042            public void deleteTrashEntry(long classPK)
043                    throws PortalException, SystemException {
044    
045                    BlogsEntryLocalServiceUtil.deleteEntry(classPK);
046            }
047    
048            @Override
049            public String getClassName() {
050                    return BlogsEntry.class.getName();
051            }
052    
053            @Override
054            public String getRestoreContainedModelLink(
055                            PortletRequest portletRequest, long classPK)
056                    throws PortalException, SystemException {
057    
058                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
059    
060                    PortletURL portletURL = getRestoreURL(portletRequest, classPK, false);
061    
062                    portletURL.setParameter("entryId", String.valueOf(entry.getEntryId()));
063                    portletURL.setParameter("urlTitle", entry.getUrlTitle());
064    
065                    return portletURL.toString();
066            }
067    
068            @Override
069            public String getRestoreContainerModelLink(
070                            PortletRequest portletRequest, long classPK)
071                    throws PortalException, SystemException {
072    
073                    PortletURL portletURL = getRestoreURL(portletRequest, classPK, true);
074    
075                    return portletURL.toString();
076            }
077    
078            @Override
079            public String getRestoreMessage(
080                    PortletRequest portletRequest, long classPK) {
081    
082                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
083                            WebKeys.THEME_DISPLAY);
084    
085                    return themeDisplay.translate("blogs");
086            }
087    
088            @Override
089            public boolean isInTrash(long classPK)
090                    throws PortalException, SystemException {
091    
092                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
093    
094                    return entry.isInTrash();
095            }
096    
097            @Override
098            public void restoreTrashEntry(long userId, long classPK)
099                    throws PortalException, SystemException {
100    
101                    BlogsEntryLocalServiceUtil.restoreEntryFromTrash(userId, classPK);
102            }
103    
104            protected PortletURL getRestoreURL(
105                            PortletRequest portletRequest, long classPK,
106                            boolean isContainerModel)
107                    throws PortalException, SystemException {
108    
109                    String portletId = PortletKeys.BLOGS;
110    
111                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
112    
113                    long plid = PortalUtil.getPlidFromPortletId(
114                            entry.getGroupId(), PortletKeys.BLOGS);
115    
116                    if (plid == LayoutConstants.DEFAULT_PLID) {
117                            portletId = PortletKeys.BLOGS_ADMIN;
118    
119                            plid = PortalUtil.getControlPanelPlid(portletRequest);
120                    }
121    
122                    PortletURL portletURL = PortletURLFactoryUtil.create(
123                            portletRequest, portletId, plid, PortletRequest.RENDER_PHASE);
124    
125                    if (!isContainerModel) {
126                            if (portletId.equals(PortletKeys.BLOGS)) {
127                                    portletURL.setParameter("struts_action", "/blogs/view_entry");
128                            }
129                            else {
130                                    portletURL.setParameter(
131                                            "struts_action", "/blogs_admin/view_entry");
132                            }
133                    }
134    
135                    return portletURL;
136            }
137    
138            @Override
139            protected boolean hasPermission(
140                            PermissionChecker permissionChecker, long classPK, String actionId)
141                    throws PortalException, SystemException {
142    
143                    return BlogsEntryPermission.contains(
144                            permissionChecker, classPK, actionId);
145            }
146    
147    }