/
JournalFolderTrashHandler.java
345 lines (267 loc) · 9.85 KB
/
JournalFolderTrashHandler.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
* 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.journal.internal.trash;
import com.liferay.asset.kernel.AssetRendererFactoryRegistryUtil;
import com.liferay.asset.kernel.model.AssetRendererFactory;
import com.liferay.journal.exception.InvalidDDMStructureException;
import com.liferay.journal.model.JournalFolder;
import com.liferay.journal.model.JournalFolderConstants;
import com.liferay.journal.service.JournalFolderLocalService;
import com.liferay.journal.util.JournalHelper;
import com.liferay.journal.util.impl.JournalUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.model.ContainerModel;
import com.liferay.portal.kernel.model.TrashedModel;
import com.liferay.portal.kernel.security.permission.ActionKeys;
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.security.permission.resource.ModelResourcePermissionHelper;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.trash.TrashHandler;
import com.liferay.portal.kernel.trash.TrashRenderer;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.trash.constants.TrashActionKeys;
import com.liferay.trash.kernel.exception.RestoreEntryException;
import com.liferay.trash.kernel.model.TrashEntry;
import com.liferay.trash.kernel.model.TrashEntryConstants;
import javax.portlet.PortletRequest;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* Implements trash handling for the journal folder entity.
*
* @author Eudaldo Alonso
*/
@Component(
property = "model.class.name=com.liferay.journal.model.JournalFolder",
service = TrashHandler.class
)
public class JournalFolderTrashHandler extends JournalBaseTrashHandler {
@Override
public void checkRestorableEntry(
long classPK, long containerModelId, String newName)
throws PortalException {
JournalFolder folder = _journalFolderLocalService.getFolder(classPK);
checkRestorableEntry(
classPK, 0, containerModelId, folder.getName(), newName);
}
@Override
public void checkRestorableEntry(
TrashEntry trashEntry, long containerModelId, String newName)
throws PortalException {
checkRestorableEntry(
trashEntry.getClassPK(), trashEntry.getEntryId(), containerModelId,
trashEntry.getTypeSettingsProperty("title"), newName);
}
@Override
public void deleteTrashEntry(long classPK) throws PortalException {
_journalFolderLocalService.deleteFolder(classPK, false);
}
@Override
public String getClassName() {
return JournalFolder.class.getName();
}
@Override
public String getDeleteMessage() {
return "found-in-deleted-folder-x";
}
@Override
public ContainerModel getParentContainerModel(long classPK)
throws PortalException {
JournalFolder folder = getJournalFolder(classPK);
long parentFolderId = folder.getParentFolderId();
if (parentFolderId <= 0) {
return null;
}
return getContainerModel(parentFolderId);
}
@Override
public String getRestoreContainedModelLink(
PortletRequest portletRequest, long classPK)
throws PortalException {
JournalFolder folder = _journalFolderLocalService.getFolder(classPK);
return JournalUtil.getJournalControlPanelLink(
portletRequest, folder.getFolderId());
}
@Override
public String getRestoreContainerModelLink(
PortletRequest portletRequest, long classPK)
throws PortalException {
JournalFolder folder = _journalFolderLocalService.getFolder(classPK);
return JournalUtil.getJournalControlPanelLink(
portletRequest, folder.getParentFolderId());
}
@Override
public String getRestoreMessage(PortletRequest portletRequest, long classPK)
throws PortalException {
JournalFolder folder = _journalFolderLocalService.getFolder(classPK);
return _journalHelper.getAbsolutePath(
portletRequest, folder.getParentFolderId());
}
@Override
public TrashedModel getTrashedModel(long classPK) {
return _journalFolderLocalService.fetchFolder(classPK);
}
@Override
public TrashRenderer getTrashRenderer(long classPK) throws PortalException {
AssetRendererFactory<JournalFolder> assetRendererFactory =
AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClass(
JournalFolder.class);
return (TrashRenderer)assetRendererFactory.getAssetRenderer(classPK);
}
@Override
public boolean hasTrashPermission(
PermissionChecker permissionChecker, long groupId, long classPK,
String trashActionId)
throws PortalException {
if (trashActionId.equals(TrashActionKeys.MOVE)) {
return ModelResourcePermissionHelper.contains(
_journalFolderModelResourcePermission, permissionChecker,
groupId, classPK, ActionKeys.ADD_FOLDER);
}
return super.hasTrashPermission(
permissionChecker, groupId, classPK, trashActionId);
}
@Override
public boolean isContainerModel() {
return true;
}
@Override
public boolean isMovable(long classPK) throws PortalException {
JournalFolder folder = getJournalFolder(classPK);
if (folder.getParentFolderId() > 0) {
JournalFolder parentFolder = _journalFolderLocalService.fetchFolder(
folder.getParentFolderId());
if ((parentFolder == null) || parentFolder.isInTrash()) {
return true;
}
}
return false;
}
@Override
public boolean isRestorable(long classPK) throws PortalException {
JournalFolder folder = getJournalFolder(classPK);
if (folder.getParentFolderId() > 0) {
JournalFolder parentFolder = _journalFolderLocalService.fetchFolder(
folder.getParentFolderId());
if (parentFolder == null) {
return false;
}
}
if (!hasTrashPermission(
PermissionThreadLocal.getPermissionChecker(),
folder.getGroupId(), classPK, TrashActionKeys.RESTORE)) {
return false;
}
return !folder.isInTrashContainer();
}
@Override
public void moveEntry(
long userId, long classPK, long containerModelId,
ServiceContext serviceContext)
throws PortalException {
_journalFolderLocalService.moveFolder(
classPK, containerModelId, serviceContext);
}
@Override
public void moveTrashEntry(
long userId, long classPK, long containerModelId,
ServiceContext serviceContext)
throws PortalException {
_journalFolderLocalService.moveFolderFromTrash(
userId, classPK, containerModelId, serviceContext);
}
@Override
public void restoreTrashEntry(long userId, long classPK)
throws PortalException {
_journalFolderLocalService.restoreFolderFromTrash(userId, classPK);
}
@Override
public void updateTitle(long classPK, String name) throws PortalException {
JournalFolder folder = _journalFolderLocalService.getFolder(classPK);
folder.setName(name);
_journalFolderLocalService.updateJournalFolder(folder);
}
protected void checkDuplicateEntry(
long classPK, long trashEntryId, long containerModelId,
String originalTitle, String newName)
throws PortalException {
JournalFolder folder = _journalFolderLocalService.getFolder(classPK);
if (containerModelId == TrashEntryConstants.DEFAULT_CONTAINER_ID) {
containerModelId = JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID;
}
if (Validator.isNotNull(newName)) {
originalTitle = newName;
}
JournalFolder duplicateFolder = _journalFolderLocalService.fetchFolder(
folder.getGroupId(), containerModelId, originalTitle);
if (duplicateFolder != null) {
RestoreEntryException ree = new RestoreEntryException(
RestoreEntryException.DUPLICATE);
ree.setDuplicateEntryId(duplicateFolder.getFolderId());
ree.setOldName(duplicateFolder.getName());
ree.setTrashEntryId(trashEntryId);
throw ree;
}
}
protected void checkRestorableEntry(
long classPK, long trashEntryId, long containerModelId,
String originalTitle, String newName)
throws PortalException {
checkValidContainer(classPK, containerModelId);
checkDuplicateEntry(
classPK, trashEntryId, containerModelId, originalTitle, newName);
}
protected void checkValidContainer(long classPK, long containerModelId)
throws PortalException {
try {
_journalFolderLocalService.validateFolderDDMStructures(
classPK, containerModelId);
}
catch (InvalidDDMStructureException iddmse) {
throw new RestoreEntryException(
RestoreEntryException.INVALID_CONTAINER, iddmse);
}
}
@Override
protected long getGroupId(long classPK) throws PortalException {
JournalFolder folder = _journalFolderLocalService.getFolder(classPK);
return folder.getGroupId();
}
protected JournalFolder getJournalFolder(long classPK)
throws PortalException {
return _journalFolderLocalService.getFolder(classPK);
}
@Override
protected boolean hasPermission(
PermissionChecker permissionChecker, long classPK, String actionId)
throws PortalException {
return _journalFolderModelResourcePermission.contains(
permissionChecker, classPK, actionId);
}
@Reference(unbind = "-")
protected void setJournalFolderLocalService(
JournalFolderLocalService journalFolderLocalService) {
_journalFolderLocalService = journalFolderLocalService;
}
private JournalFolderLocalService _journalFolderLocalService;
@Reference(
target = "(model.class.name=com.liferay.journal.model.JournalFolder)"
)
private ModelResourcePermission<JournalFolder>
_journalFolderModelResourcePermission;
@Reference
private JournalHelper _journalHelper;
}