forked from liferay/liferay-portal
-
Notifications
You must be signed in to change notification settings - Fork 28
/
BlogsEntryStagedModelDataHandler.java
465 lines (364 loc) · 14.2 KB
/
BlogsEntryStagedModelDataHandler.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/**
* 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.lar;
import com.liferay.blogs.exportimport.content.processor.BlogsEntryExportImportContentProcessor;
import com.liferay.blogs.kernel.model.BlogsEntry;
import com.liferay.blogs.kernel.service.BlogsEntryLocalService;
import com.liferay.document.library.kernel.model.DLFileEntry;
import com.liferay.exportimport.kernel.lar.ExportImportPathUtil;
import com.liferay.exportimport.kernel.lar.PortletDataContext;
import com.liferay.exportimport.kernel.lar.StagedModelDataHandler;
import com.liferay.exportimport.kernel.lar.StagedModelDataHandlerUtil;
import com.liferay.exportimport.kernel.lar.StagedModelModifiedDateComparator;
import com.liferay.exportimport.lar.BaseStagedModelDataHandler;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.portletfilerepository.PortletFileRepositoryUtil;
import com.liferay.portal.kernel.repository.model.FileEntry;
import com.liferay.portal.kernel.servlet.taglib.ui.ImageSelector;
import com.liferay.portal.kernel.trash.TrashHandler;
import com.liferay.portal.kernel.util.CalendarFactoryUtil;
import com.liferay.portal.kernel.util.FileUtil;
import com.liferay.portal.kernel.util.MimeTypesUtil;
import com.liferay.portal.kernel.util.StreamUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.xml.Attribute;
import com.liferay.portal.kernel.xml.Element;
import com.liferay.portal.model.Image;
import com.liferay.portal.service.ImageLocalService;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portlet.documentlibrary.lar.FileEntryUtil;
import java.io.InputStream;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* @author Zsolt Berentey
* @author Roberto Díaz
*/
@Component(immediate = true, service = StagedModelDataHandler.class)
public class BlogsEntryStagedModelDataHandler
extends BaseStagedModelDataHandler<BlogsEntry> {
public static final String[] CLASS_NAMES = {BlogsEntry.class.getName()};
@Override
public void deleteStagedModel(BlogsEntry entry) throws PortalException {
_blogsEntryLocalService.deleteEntry(entry);
}
@Override
public void deleteStagedModel(
String uuid, long groupId, String className, String extraData)
throws PortalException {
BlogsEntry entry = fetchStagedModelByUuidAndGroupId(uuid, groupId);
if (entry != null) {
deleteStagedModel(entry);
}
}
@Override
public BlogsEntry fetchStagedModelByUuidAndGroupId(
String uuid, long groupId) {
return _blogsEntryLocalService.fetchBlogsEntryByUuidAndGroupId(
uuid, groupId);
}
@Override
public List<BlogsEntry> fetchStagedModelsByUuidAndCompanyId(
String uuid, long companyId) {
return _blogsEntryLocalService.getBlogsEntriesByUuidAndCompanyId(
uuid, companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
new StagedModelModifiedDateComparator<BlogsEntry>());
}
@Override
public String[] getClassNames() {
return CLASS_NAMES;
}
@Override
public String getDisplayName(BlogsEntry entry) {
return entry.getTitle();
}
@Override
protected void doExportStagedModel(
PortletDataContext portletDataContext, BlogsEntry entry)
throws Exception {
Element entryElement = portletDataContext.getExportDataElement(entry);
if (entry.isSmallImage()) {
Image smallImage = _imageLocalService.fetchImage(
entry.getSmallImageId());
if (smallImage != null) {
String smallImagePath = ExportImportPathUtil.getModelPath(
entry,
smallImage.getImageId() + StringPool.PERIOD +
smallImage.getType());
entryElement.addAttribute("small-image-path", smallImagePath);
entry.setSmallImageType(smallImage.getType());
portletDataContext.addZipEntry(
smallImagePath, smallImage.getTextObj());
}
}
if (entry.getSmallImageFileEntryId() != 0) {
FileEntry fileEntry = PortletFileRepositoryUtil.getPortletFileEntry(
entry.getSmallImageFileEntryId());
StagedModelDataHandlerUtil.exportReferenceStagedModel(
portletDataContext, entry, fileEntry,
PortletDataContext.REFERENCE_TYPE_WEAK);
}
if (entry.getCoverImageFileEntryId() != 0) {
FileEntry fileEntry = PortletFileRepositoryUtil.getPortletFileEntry(
entry.getCoverImageFileEntryId());
StagedModelDataHandlerUtil.exportReferenceStagedModel(
portletDataContext, entry, fileEntry,
PortletDataContext.REFERENCE_TYPE_WEAK);
}
String content =
_blogsEntryExportImportContentProcessor.
replaceExportContentReferences(
portletDataContext, entry, entry.getContent(),
portletDataContext.getBooleanParameter(
"blogs", "referenced-content"),
true);
entry.setContent(content);
portletDataContext.addClassedModel(
entryElement, ExportImportPathUtil.getModelPath(entry), entry);
}
@Override
protected void doImportMissingReference(
PortletDataContext portletDataContext, String uuid, long groupId,
long entryId)
throws Exception {
BlogsEntry existingEntry = fetchMissingReference(uuid, groupId);
if (existingEntry == null) {
return;
}
Map<Long, Long> entryIds =
(Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
BlogsEntry.class);
entryIds.put(entryId, existingEntry.getEntryId());
}
@Override
protected void doImportStagedModel(
PortletDataContext portletDataContext, BlogsEntry entry)
throws Exception {
long userId = portletDataContext.getUserId(entry.getUserUuid());
Element entryElement =
portletDataContext.getImportDataStagedModelElement(entry);
String content =
_blogsEntryExportImportContentProcessor.
replaceImportContentReferences(
portletDataContext, entry, entry.getContent());
entry.setContent(content);
Calendar displayDateCal = CalendarFactoryUtil.getCalendar();
displayDateCal.setTime(entry.getDisplayDate());
int displayDateMonth = displayDateCal.get(Calendar.MONTH);
int displayDateDay = displayDateCal.get(Calendar.DATE);
int displayDateYear = displayDateCal.get(Calendar.YEAR);
int displayDateHour = displayDateCal.get(Calendar.HOUR);
int displayDateMinute = displayDateCal.get(Calendar.MINUTE);
if (displayDateCal.get(Calendar.AM_PM) == Calendar.PM) {
displayDateHour += 12;
}
boolean allowPingbacks = entry.isAllowPingbacks();
boolean allowTrackbacks = entry.isAllowTrackbacks();
String[] trackbacks = StringUtil.split(entry.getTrackbacks());
ServiceContext serviceContext = portletDataContext.createServiceContext(
entry);
BlogsEntry importedEntry = null;
if (portletDataContext.isDataStrategyMirror()) {
serviceContext.setAttribute("urlTitle", entry.getUrlTitle());
BlogsEntry existingEntry = fetchStagedModelByUuidAndGroupId(
entry.getUuid(), portletDataContext.getScopeGroupId());
if (existingEntry == null) {
serviceContext.setUuid(entry.getUuid());
importedEntry = _blogsEntryLocalService.addEntry(
userId, entry.getTitle(), entry.getSubtitle(),
entry.getDescription(), entry.getContent(),
displayDateMonth, displayDateDay, displayDateYear,
displayDateHour, displayDateMinute, allowPingbacks,
allowTrackbacks, trackbacks, entry.getCoverImageCaption(),
null, null, serviceContext);
}
else {
importedEntry = _blogsEntryLocalService.updateEntry(
userId, existingEntry.getEntryId(), entry.getTitle(),
entry.getSubtitle(), entry.getDescription(),
entry.getContent(), displayDateMonth, displayDateDay,
displayDateYear, displayDateHour, displayDateMinute,
allowPingbacks, allowTrackbacks, trackbacks,
entry.getCoverImageCaption(), new ImageSelector(),
new ImageSelector(), serviceContext);
}
}
else {
importedEntry = _blogsEntryLocalService.addEntry(
userId, entry.getTitle(), entry.getSubtitle(),
entry.getDescription(), entry.getContent(), displayDateMonth,
displayDateDay, displayDateYear, displayDateHour,
displayDateMinute, allowPingbacks, allowTrackbacks, trackbacks,
entry.getCoverImageCaption(), null, null, serviceContext);
}
if ((entry.getCoverImageFileEntryId() == 0) &&
Validator.isNull(entry.getCoverImageURL()) &&
(entry.getSmallImageFileEntryId() == 0) &&
Validator.isNull(entry.getSmallImageURL()) &&
!entry.isSmallImage()) {
portletDataContext.importClassedModel(entry, importedEntry);
return;
}
// Cover image
ImageSelector coverImageSelector = null;
List<Element> attachmentElements =
portletDataContext.getReferenceDataElements(
entry, DLFileEntry.class,
PortletDataContext.REFERENCE_TYPE_WEAK);
if (Validator.isNotNull(entry.getCoverImageURL())) {
coverImageSelector = new ImageSelector(entry.getCoverImageURL());
}
else if (entry.getCoverImageFileEntryId() != 0) {
coverImageSelector = _getImageSelector(
portletDataContext, entry.getCoverImageFileEntryId(),
attachmentElements);
}
if (coverImageSelector != null) {
_blogsEntryLocalService.addCoverImage(
importedEntry.getEntryId(), coverImageSelector);
}
// Small image
ImageSelector smallImageSelector = null;
if (entry.isSmallImage()) {
String smallImagePath = entryElement.attributeValue(
"small-image-path");
if (Validator.isNotNull(entry.getSmallImageURL())) {
smallImageSelector = new ImageSelector(
entry.getSmallImageURL());
}
else if (Validator.isNotNull(smallImagePath)) {
String smallImageFileName =
entry.getSmallImageId() + StringPool.PERIOD +
entry.getSmallImageType();
InputStream inputStream = null;
try {
inputStream = portletDataContext.getZipEntryAsInputStream(
smallImagePath);
smallImageSelector = new ImageSelector(
FileUtil.getBytes(inputStream), smallImageFileName,
MimeTypesUtil.getContentType(smallImageFileName), null);
}
finally {
StreamUtil.cleanUp(inputStream);
}
}
else if (entry.getSmallImageFileEntryId() != 0) {
smallImageSelector = _getImageSelector(
portletDataContext, entry.getSmallImageFileEntryId(),
attachmentElements);
}
}
if (smallImageSelector != null) {
_blogsEntryLocalService.addSmallImage(
importedEntry.getEntryId(), smallImageSelector);
}
if ((coverImageSelector != null) || (smallImageSelector != null)) {
importedEntry =_blogsEntryLocalService.getBlogsEntry(
importedEntry.getEntryId());
}
portletDataContext.importClassedModel(entry, importedEntry);
}
@Override
protected void doRestoreStagedModel(
PortletDataContext portletDataContext, BlogsEntry entry)
throws Exception {
long userId = portletDataContext.getUserId(entry.getUserUuid());
BlogsEntry existingEntry = fetchStagedModelByUuidAndGroupId(
entry.getUuid(), portletDataContext.getScopeGroupId());
if ((existingEntry == null) || !existingEntry.isInTrash()) {
return;
}
TrashHandler trashHandler = existingEntry.getTrashHandler();
if (trashHandler.isRestorable(existingEntry.getEntryId())) {
trashHandler.restoreTrashEntry(userId, existingEntry.getEntryId());
}
}
protected InputStream getSmallImageInputStream(
PortletDataContext portletDataContext, Element attachmentElement) {
InputStream inputStream = null;
String path = attachmentElement.attributeValue("path");
FileEntry fileEntry = (FileEntry)portletDataContext.getZipEntryAsObject(
path);
String binPath = attachmentElement.attributeValue("bin-path");
if (Validator.isNull(binPath) &&
portletDataContext.isPerformDirectBinaryImport()) {
try {
inputStream = FileEntryUtil.getContentStream(fileEntry);
}
catch (Exception e) {
}
}
else {
inputStream = portletDataContext.getZipEntryAsInputStream(binPath);
}
if (inputStream == null) {
if (_log.isWarnEnabled()) {
_log.warn(
"Unable to import small image file entry " +
fileEntry.getFileEntryId());
}
}
return inputStream;
}
@Reference(unbind = "-")
protected void setBlogsEntryExportImportContentProcessor(
BlogsEntryExportImportContentProcessor
blogsEntryExportImportContentProcessor) {
_blogsEntryExportImportContentProcessor =
blogsEntryExportImportContentProcessor;
}
@Reference(unbind = "-")
protected void setBlogsEntryLocalService(
BlogsEntryLocalService blogsEntryLocalService) {
_blogsEntryLocalService = blogsEntryLocalService;
}
@Reference(unbind = "-")
protected void setImageLocalService(ImageLocalService imageLocalService) {
_imageLocalService = imageLocalService;
}
private ImageSelector _getImageSelector(
PortletDataContext portletDataContext, long fileEntryId,
List<Element> attachmentElements)
throws Exception {
FileEntry oldImageFileEntry =
PortletFileRepositoryUtil.getPortletFileEntry(fileEntryId);
for (Element attachmentElement : attachmentElements) {
Attribute uuidAttribute = attachmentElement.attribute("uuid");
String uuidAttributeValue = uuidAttribute.getValue();
if (uuidAttributeValue.equals(oldImageFileEntry.getUuid())) {
String path = attachmentElement.attributeValue("path");
FileEntry fileEntry =
(FileEntry)portletDataContext.getZipEntryAsObject(path);
return new ImageSelector(
FileUtil.getBytes(fileEntry.getContentStream()),
fileEntry.getFileName(), fileEntry.getMimeType(), null);
}
}
return null;
}
private static final Log _log = LogFactoryUtil.getLog(
BlogsEntryStagedModelDataHandler.class);
private BlogsEntryExportImportContentProcessor
_blogsEntryExportImportContentProcessor;
private BlogsEntryLocalService _blogsEntryLocalService;
private ImageLocalService _imageLocalService;
}