/
BlogsEntryTrashHandler.java
175 lines (135 loc) · 5.22 KB
/
BlogsEntryTrashHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.blogs.internal.trash;
import com.liferay.blogs.model.BlogsEntry;
import com.liferay.blogs.service.BlogsEntryLocalService;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.model.LayoutConstants;
import com.liferay.portal.kernel.portlet.PortletProvider;
import com.liferay.portal.kernel.portlet.PortletProviderUtil;
import com.liferay.portal.kernel.portlet.PortletURLFactoryUtil;
import com.liferay.portal.kernel.security.permission.PermissionChecker;
import com.liferay.portal.kernel.security.permission.PermissionThreadLocal;
import com.liferay.portal.kernel.security.permission.resource.ModelResourcePermission;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.trash.BaseTrashHandler;
import com.liferay.portal.kernel.trash.TrashHandler;
import com.liferay.portal.kernel.util.Portal;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.trash.constants.TrashActionKeys;
import javax.portlet.PortletRequest;
import javax.portlet.PortletURL;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* Implements trash handling for the blogs entry entity.
*
* @author Zsolt Berentey
*/
@Component(
property = "model.class.name=com.liferay.blogs.model.BlogsEntry",
service = TrashHandler.class
)
public class BlogsEntryTrashHandler extends BaseTrashHandler {
@Override
public void deleteTrashEntry(long classPK) throws PortalException {
_blogsEntryLocalService.deleteEntry(classPK);
}
@Override
public String getClassName() {
return BlogsEntry.class.getName();
}
@Override
public String getRestoreContainedModelLink(
PortletRequest portletRequest, long classPK)
throws PortalException {
PortletURL portletURL = getRestoreURL(portletRequest, classPK, false);
BlogsEntry entry = _blogsEntryLocalService.getEntry(classPK);
portletURL.setParameter("entryId", String.valueOf(entry.getEntryId()));
portletURL.setParameter("urlTitle", entry.getUrlTitle());
return portletURL.toString();
}
@Override
public String getRestoreContainerModelLink(
PortletRequest portletRequest, long classPK)
throws PortalException {
PortletURL portletURL = getRestoreURL(portletRequest, classPK, true);
return portletURL.toString();
}
@Override
public String getRestoreMessage(
PortletRequest portletRequest, long classPK) {
ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
WebKeys.THEME_DISPLAY);
return themeDisplay.translate("blogs");
}
@Override
public boolean isInTrash(long classPK) throws PortalException {
BlogsEntry entry = _blogsEntryLocalService.getEntry(classPK);
return entry.isInTrash();
}
@Override
public boolean isRestorable(long classPK) throws PortalException {
BlogsEntry entry = _blogsEntryLocalService.getEntry(classPK);
if (!hasTrashPermission(
PermissionThreadLocal.getPermissionChecker(),
entry.getGroupId(), classPK, TrashActionKeys.RESTORE)) {
return false;
}
return !entry.isInTrashContainer();
}
@Override
public void restoreTrashEntry(long userId, long classPK)
throws PortalException {
_blogsEntryLocalService.restoreEntryFromTrash(userId, classPK);
}
protected PortletURL getRestoreURL(
PortletRequest portletRequest, long classPK, boolean containerModel)
throws PortalException {
PortletURL portletURL = null;
BlogsEntry entry = _blogsEntryLocalService.getEntry(classPK);
String portletId = PortletProviderUtil.getPortletId(
BlogsEntry.class.getName(), PortletProvider.Action.VIEW);
long plid = _portal.getPlidFromPortletId(entry.getGroupId(), portletId);
if (plid == LayoutConstants.DEFAULT_PLID) {
portletId = PortletProviderUtil.getPortletId(
BlogsEntry.class.getName(), PortletProvider.Action.MANAGE);
portletURL = _portal.getControlPanelPortletURL(
portletRequest, portletId, PortletRequest.RENDER_PHASE);
}
else {
portletURL = PortletURLFactoryUtil.create(
portletRequest, portletId, plid, PortletRequest.RENDER_PHASE);
}
if (!containerModel) {
portletURL.setParameter(
"mvcRenderCommandName", "/blogs/view_entry");
}
return portletURL;
}
@Override
protected boolean hasPermission(
PermissionChecker permissionChecker, long classPK, String actionId)
throws PortalException {
return _blogsEntryModelResourcePermission.contains(
permissionChecker, classPK, actionId);
}
@Reference
private BlogsEntryLocalService _blogsEntryLocalService;
@Reference(target = "(model.class.name=com.liferay.blogs.model.BlogsEntry)")
private ModelResourcePermission<BlogsEntry>
_blogsEntryModelResourcePermission;
@Reference
private Portal _portal;
}