001
014
015 package com.liferay.portlet.journal.lar;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.json.JSONFactoryUtil;
020 import com.liferay.portal.kernel.json.JSONObject;
021 import com.liferay.portal.kernel.lar.BaseStagedModelDataHandler;
022 import com.liferay.portal.kernel.lar.ExportImportHelperUtil;
023 import com.liferay.portal.kernel.lar.ExportImportPathUtil;
024 import com.liferay.portal.kernel.lar.PortletDataContext;
025 import com.liferay.portal.kernel.lar.PortletDataException;
026 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
027 import com.liferay.portal.kernel.trash.TrashHandler;
028 import com.liferay.portal.kernel.util.CalendarFactoryUtil;
029 import com.liferay.portal.kernel.util.FileUtil;
030 import com.liferay.portal.kernel.util.GetterUtil;
031 import com.liferay.portal.kernel.util.LocaleUtil;
032 import com.liferay.portal.kernel.util.LocalizationUtil;
033 import com.liferay.portal.kernel.util.MapUtil;
034 import com.liferay.portal.kernel.util.StringBundler;
035 import com.liferay.portal.kernel.util.StringPool;
036 import com.liferay.portal.kernel.util.Validator;
037 import com.liferay.portal.kernel.workflow.WorkflowConstants;
038 import com.liferay.portal.kernel.xml.Element;
039 import com.liferay.portal.model.Image;
040 import com.liferay.portal.model.User;
041 import com.liferay.portal.service.ImageLocalServiceUtil;
042 import com.liferay.portal.service.ServiceContext;
043 import com.liferay.portal.service.UserLocalServiceUtil;
044 import com.liferay.portal.util.PortalUtil;
045 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
046 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
047 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
048 import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
049 import com.liferay.portlet.journal.model.JournalArticle;
050 import com.liferay.portlet.journal.model.JournalArticleConstants;
051 import com.liferay.portlet.journal.model.JournalArticleImage;
052 import com.liferay.portlet.journal.model.JournalArticleResource;
053 import com.liferay.portlet.journal.model.JournalFolder;
054 import com.liferay.portlet.journal.model.JournalFolderConstants;
055 import com.liferay.portlet.journal.service.JournalArticleImageLocalServiceUtil;
056 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
057 import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
058
059 import java.io.File;
060
061 import java.util.Calendar;
062 import java.util.Date;
063 import java.util.HashMap;
064 import java.util.List;
065 import java.util.Locale;
066 import java.util.Map;
067
068
072 public class JournalArticleStagedModelDataHandler
073 extends BaseStagedModelDataHandler<JournalArticle> {
074
075 public static final String[] CLASS_NAMES = {JournalArticle.class.getName()};
076
077 @Override
078 public void deleteStagedModel(
079 String uuid, long groupId, String className, String extraData)
080 throws PortalException, SystemException {
081
082 JournalArticleResource articleResource =
083 JournalArticleResourceLocalServiceUtil.fetchArticleResource(
084 uuid, groupId);
085
086 if (articleResource == null) {
087 return;
088 }
089
090 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject(
091 extraData);
092
093 if (Validator.isNull(extraData) ||
094 extraDataJSONObject.getBoolean("inTrash")) {
095
096 JournalArticleLocalServiceUtil.deleteArticle(
097 groupId, articleResource.getArticleId(), null);
098 }
099 else {
100 double version = extraDataJSONObject.getDouble("version");
101
102 JournalArticle article = JournalArticleLocalServiceUtil.getArticle(
103 groupId, articleResource.getArticleId(), version);
104
105 JournalArticleLocalServiceUtil.deleteArticle(article);
106 }
107 }
108
109 @Override
110 public String[] getClassNames() {
111 return CLASS_NAMES;
112 }
113
114 @Override
115 public String getDisplayName(JournalArticle article) {
116 return article.getTitleCurrentValue();
117 }
118
119 @Override
120 public int[] getExportableStatuses() {
121 return new int[] {
122 WorkflowConstants.STATUS_APPROVED, WorkflowConstants.STATUS_EXPIRED,
123 WorkflowConstants.STATUS_SCHEDULED
124 };
125 }
126
127 @Override
128 public Map<String, String> getReferenceAttributes(
129 PortletDataContext portletDataContext, JournalArticle article) {
130
131 Map<String, String> referenceAttributes = new HashMap<String, String>();
132
133 String articleResourceUuid = StringPool.BLANK;
134
135 try {
136 articleResourceUuid = article.getArticleResourceUuid();
137 }
138 catch (Exception e) {
139 }
140
141 referenceAttributes.put("article-resource-uuid", articleResourceUuid);
142
143 referenceAttributes.put("article-id", article.getArticleId());
144
145 long defaultUserId = 0;
146
147 try {
148 defaultUserId = UserLocalServiceUtil.getDefaultUserId(
149 article.getCompanyId());
150 }
151 catch (Exception e) {
152 return referenceAttributes;
153 }
154
155 boolean preloaded = false;
156
157 if (defaultUserId == article.getUserId()) {
158 preloaded = true;
159 }
160
161 referenceAttributes.put("preloaded", String.valueOf(preloaded));
162
163 return referenceAttributes;
164 }
165
166 @Override
167 public void importCompanyStagedModel(
168 PortletDataContext portletDataContext, Element element)
169 throws PortletDataException {
170
171 String articleResourceUuid = element.attributeValue(
172 "article-resource-uuid");
173 String articleArticleId = element.attributeValue("article-id");
174 boolean preloaded = GetterUtil.getBoolean(
175 element.attributeValue("preloaded"));
176
177 JournalArticle existingArticle = null;
178
179 try {
180 existingArticle = fetchExistingArticle(
181 articleResourceUuid, portletDataContext.getCompanyGroupId(),
182 articleArticleId, null, 0.0, preloaded);
183
184 if (existingArticle == null) {
185 existingArticle = fetchExistingArticle(
186 articleResourceUuid, portletDataContext.getScopeGroupId(),
187 articleArticleId, null, 0.0, preloaded);
188 }
189 }
190 catch (Exception e) {
191 if (e instanceof SystemException) {
192 throw new PortletDataException(e);
193 }
194
195 return;
196 }
197
198 Map<String, String> articleArticleIds =
199 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
200 JournalArticle.class + ".articleId");
201
202 articleArticleIds.put(articleArticleId, existingArticle.getArticleId());
203
204 Map<Long, Long> articleIds =
205 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
206 JournalArticle.class);
207
208 long articleId = GetterUtil.getLong(element.attributeValue("class-pk"));
209
210 articleIds.put(articleId, existingArticle.getId());
211 }
212
213 @Override
214 public boolean validateReference(
215 PortletDataContext portletDataContext, Element referenceElement) {
216
217 String articleResourceUuid = referenceElement.attributeValue(
218 "article-resource-uuid");
219 String articleArticleId = referenceElement.attributeValue("article-id");
220 boolean preloaded = GetterUtil.getBoolean(
221 referenceElement.attributeValue("preloaded"));
222
223 try {
224 JournalArticle existingArticle = fetchExistingArticle(
225 articleResourceUuid, portletDataContext.getScopeGroupId(),
226 articleArticleId, null, 0.0, preloaded);
227
228 if (existingArticle == null) {
229 existingArticle = fetchExistingArticle(
230 articleResourceUuid, portletDataContext.getCompanyGroupId(),
231 articleArticleId, null, 0.0, preloaded);
232 }
233
234 if (existingArticle == null) {
235 return false;
236 }
237
238 return true;
239 }
240 catch (Exception e) {
241 return false;
242 }
243 }
244
245 @Override
246 protected boolean countStagedModel(
247 PortletDataContext portletDataContext, JournalArticle article) {
248
249 if (article.getClassNameId() ==
250 PortalUtil.getClassNameId(DDMStructure.class)) {
251
252 return false;
253 }
254
255 return !portletDataContext.isModelCounted(
256 JournalArticleResource.class.getName(),
257 article.getResourcePrimKey());
258 }
259
260 @Override
261 protected void doExportStagedModel(
262 PortletDataContext portletDataContext, JournalArticle article)
263 throws Exception {
264
265 Element articleElement = portletDataContext.getExportDataElement(
266 article);
267
268 articleElement.addAttribute(
269 "article-resource-uuid", article.getArticleResourceUuid());
270
271 if (article.getFolderId() !=
272 JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
273
274 StagedModelDataHandlerUtil.exportReferenceStagedModel(
275 portletDataContext, article, article.getFolder(),
276 PortletDataContext.REFERENCE_TYPE_PARENT);
277 }
278
279 if (Validator.isNotNull(article.getStructureId())) {
280 DDMStructure ddmStructure =
281 DDMStructureLocalServiceUtil.getStructure(
282 article.getGroupId(),
283 PortalUtil.getClassNameId(JournalArticle.class),
284 article.getStructureId(), true);
285
286 StagedModelDataHandlerUtil.exportReferenceStagedModel(
287 portletDataContext, article, ddmStructure,
288 PortletDataContext.REFERENCE_TYPE_STRONG);
289 }
290
291 if (Validator.isNotNull(article.getTemplateId()) &&
292 (article.getClassNameId() !=
293 PortalUtil.getClassNameId(DDMStructure.class))) {
294
295 DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.getTemplate(
296 article.getGroupId(),
297 PortalUtil.getClassNameId(DDMStructure.class),
298 article.getTemplateId(), true);
299
300 StagedModelDataHandlerUtil.exportReferenceStagedModel(
301 portletDataContext, article, ddmTemplate,
302 PortletDataContext.REFERENCE_TYPE_STRONG);
303 }
304
305 if (article.isSmallImage()) {
306 Image smallImage = ImageLocalServiceUtil.fetchImage(
307 article.getSmallImageId());
308
309 if (Validator.isNotNull(article.getSmallImageURL())) {
310 String smallImageURL =
311 ExportImportHelperUtil.replaceExportContentReferences(
312 portletDataContext, article, articleElement,
313 article.getSmallImageURL().concat(StringPool.SPACE),
314 true);
315
316 article.setSmallImageURL(smallImageURL);
317 }
318 else if (smallImage != null) {
319 String smallImagePath = ExportImportPathUtil.getModelPath(
320 article,
321 smallImage.getImageId() + StringPool.PERIOD +
322 smallImage.getType());
323
324 articleElement.addAttribute("small-image-path", smallImagePath);
325
326 article.setSmallImageType(smallImage.getType());
327
328 portletDataContext.addZipEntry(
329 smallImagePath, smallImage.getTextObj());
330 }
331 }
332
333 List<JournalArticleImage> articleImages =
334 JournalArticleImageLocalServiceUtil.getArticleImages(
335 article.getGroupId(), article.getArticleId(),
336 article.getVersion());
337
338 for (JournalArticleImage articleImage : articleImages) {
339 exportArticleImage(
340 portletDataContext, articleImage, article, articleElement);
341 }
342
343 article.setStatusByUserUuid(article.getStatusByUserUuid());
344
345 String content = ExportImportHelperUtil.replaceExportContentReferences(
346 portletDataContext, article, articleElement, article.getContent(),
347 portletDataContext.getBooleanParameter(
348 JournalPortletDataHandler.NAMESPACE, "referenced-content"));
349
350 article.setContent(content);
351
352 long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
353 article.getCompanyId());
354
355 if (defaultUserId == article.getUserId()) {
356 articleElement.addAttribute("preloaded", "true");
357 }
358
359 portletDataContext.addClassedModel(
360 articleElement, ExportImportPathUtil.getModelPath(article),
361 article);
362 }
363
364 @Override
365 protected void doImportStagedModel(
366 PortletDataContext portletDataContext, JournalArticle article)
367 throws Exception {
368
369 prepareLanguagesForImport(article);
370
371 long userId = portletDataContext.getUserId(article.getUserUuid());
372
373 JournalCreationStrategy creationStrategy =
374 JournalCreationStrategyFactory.getInstance();
375
376 long authorId = creationStrategy.getAuthorUserId(
377 portletDataContext, article);
378
379 if (authorId != JournalCreationStrategy.USE_DEFAULT_USER_ID_STRATEGY) {
380 userId = authorId;
381 }
382
383 User user = UserLocalServiceUtil.getUser(userId);
384
385 Map<Long, Long> folderIds =
386 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
387 JournalFolder.class);
388
389 if (article.getFolderId() !=
390 JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
391
392 StagedModelDataHandlerUtil.importReferenceStagedModel(
393 portletDataContext, article, JournalFolder.class,
394 article.getFolderId());
395 }
396
397 long folderId = MapUtil.getLong(
398 folderIds, article.getFolderId(), article.getFolderId());
399
400 String articleId = article.getArticleId();
401
402 boolean autoArticleId = false;
403
404 if (Validator.isNumber(articleId) ||
405 (JournalArticleLocalServiceUtil.fetchArticle(
406 portletDataContext.getScopeGroupId(), articleId,
407 JournalArticleConstants.VERSION_DEFAULT) != null)) {
408
409 autoArticleId = true;
410 }
411
412 Map<String, String> articleIds =
413 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
414 JournalArticle.class + ".articleId");
415
416 String newArticleId = articleIds.get(articleId);
417
418 if (Validator.isNotNull(newArticleId)) {
419
420
421
422
423 articleId = newArticleId;
424 autoArticleId = false;
425 }
426
427 String content = article.getContent();
428
429 Element articleElement =
430 portletDataContext.getImportDataStagedModelElement(article);
431
432 content = ExportImportHelperUtil.replaceImportContentReferences(
433 portletDataContext, articleElement, content, true);
434
435 article.setContent(content);
436
437 String newContent = creationStrategy.getTransformedContent(
438 portletDataContext, article);
439
440 if (newContent != JournalCreationStrategy.ARTICLE_CONTENT_UNCHANGED) {
441 article.setContent(newContent);
442 }
443
444 Date displayDate = article.getDisplayDate();
445
446 int displayDateMonth = 0;
447 int displayDateDay = 0;
448 int displayDateYear = 0;
449 int displayDateHour = 0;
450 int displayDateMinute = 0;
451
452 if (displayDate != null) {
453 Calendar displayCal = CalendarFactoryUtil.getCalendar(
454 user.getTimeZone());
455
456 displayCal.setTime(displayDate);
457
458 displayDateMonth = displayCal.get(Calendar.MONTH);
459 displayDateDay = displayCal.get(Calendar.DATE);
460 displayDateYear = displayCal.get(Calendar.YEAR);
461 displayDateHour = displayCal.get(Calendar.HOUR);
462 displayDateMinute = displayCal.get(Calendar.MINUTE);
463
464 if (displayCal.get(Calendar.AM_PM) == Calendar.PM) {
465 displayDateHour += 12;
466 }
467 }
468
469 Date expirationDate = article.getExpirationDate();
470
471 int expirationDateMonth = 0;
472 int expirationDateDay = 0;
473 int expirationDateYear = 0;
474 int expirationDateHour = 0;
475 int expirationDateMinute = 0;
476 boolean neverExpire = true;
477
478 if (expirationDate != null) {
479 Calendar expirationCal = CalendarFactoryUtil.getCalendar(
480 user.getTimeZone());
481
482 expirationCal.setTime(expirationDate);
483
484 expirationDateMonth = expirationCal.get(Calendar.MONTH);
485 expirationDateDay = expirationCal.get(Calendar.DATE);
486 expirationDateYear = expirationCal.get(Calendar.YEAR);
487 expirationDateHour = expirationCal.get(Calendar.HOUR);
488 expirationDateMinute = expirationCal.get(Calendar.MINUTE);
489 neverExpire = false;
490
491 if (expirationCal.get(Calendar.AM_PM) == Calendar.PM) {
492 expirationDateHour += 12;
493 }
494 }
495
496 Date reviewDate = article.getReviewDate();
497
498 int reviewDateMonth = 0;
499 int reviewDateDay = 0;
500 int reviewDateYear = 0;
501 int reviewDateHour = 0;
502 int reviewDateMinute = 0;
503 boolean neverReview = true;
504
505 if (reviewDate != null) {
506 Calendar reviewCal = CalendarFactoryUtil.getCalendar(
507 user.getTimeZone());
508
509 reviewCal.setTime(reviewDate);
510
511 reviewDateMonth = reviewCal.get(Calendar.MONTH);
512 reviewDateDay = reviewCal.get(Calendar.DATE);
513 reviewDateYear = reviewCal.get(Calendar.YEAR);
514 reviewDateHour = reviewCal.get(Calendar.HOUR);
515 reviewDateMinute = reviewCal.get(Calendar.MINUTE);
516 neverReview = false;
517
518 if (reviewCal.get(Calendar.AM_PM) == Calendar.PM) {
519 reviewDateHour += 12;
520 }
521 }
522
523 StagedModelDataHandlerUtil.importReferenceStagedModels(
524 portletDataContext, article, DDMStructure.class);
525
526 Map<String, String> ddmStructureKeys =
527 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
528 DDMStructure.class + ".ddmStructureKey");
529
530 String parentDDMStructureKey = MapUtil.getString(
531 ddmStructureKeys, article.getStructureId(),
532 article.getStructureId());
533
534 Map<String, Long> ddmStructureIds =
535 (Map<String, Long>)portletDataContext.getNewPrimaryKeysMap(
536 DDMStructure.class);
537
538 long ddmStructureId = 0;
539
540 if (article.getClassNameId() != 0) {
541 ddmStructureId = ddmStructureIds.get(article.getClassPK());
542 }
543
544 StagedModelDataHandlerUtil.importReferenceStagedModels(
545 portletDataContext, article, DDMTemplate.class);
546
547 Map<String, String> ddmTemplateKeys =
548 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
549 DDMTemplate.class + ".ddmTemplateKey");
550
551 String parentDDMTemplateKey = MapUtil.getString(
552 ddmTemplateKeys, article.getTemplateId(), article.getTemplateId());
553
554 File smallFile = null;
555
556 try {
557 if (article.isSmallImage()) {
558 String smallImagePath = articleElement.attributeValue(
559 "small-image-path");
560
561 if (Validator.isNotNull(article.getSmallImageURL())) {
562 String smallImageURL =
563 ExportImportHelperUtil.replaceImportContentReferences(
564 portletDataContext, articleElement,
565 article.getSmallImageURL(), true);
566
567 article.setSmallImageURL(smallImageURL);
568 }
569 else if (Validator.isNotNull(smallImagePath)) {
570 byte[] bytes = portletDataContext.getZipEntryAsByteArray(
571 smallImagePath);
572
573 if (bytes != null) {
574 smallFile = FileUtil.createTempFile(
575 article.getSmallImageType());
576
577 FileUtil.write(smallFile, bytes);
578 }
579 }
580 }
581
582 Map<String, byte[]> images = new HashMap<String, byte[]>();
583
584 List<Element> imagesElements =
585 portletDataContext.getReferenceDataElements(
586 article, Image.class);
587
588 for (Element imageElement : imagesElements) {
589 String imagePath = imageElement.attributeValue("path");
590
591 String fileName = imageElement.attributeValue("file-name");
592
593 images.put(
594 fileName,
595 portletDataContext.getZipEntryAsByteArray(imagePath));
596 }
597
598 String articleURL = null;
599
600 boolean addGroupPermissions = creationStrategy.addGroupPermissions(
601 portletDataContext, article);
602 boolean addGuestPermissions = creationStrategy.addGuestPermissions(
603 portletDataContext, article);
604
605 ServiceContext serviceContext =
606 portletDataContext.createServiceContext(article);
607
608 serviceContext.setAddGroupPermissions(addGroupPermissions);
609 serviceContext.setAddGuestPermissions(addGuestPermissions);
610
611 if ((expirationDate != null) && expirationDate.before(new Date())) {
612 article.setStatus(WorkflowConstants.STATUS_EXPIRED);
613 }
614
615 if ((article.getStatus() != WorkflowConstants.STATUS_APPROVED) &&
616 (article.getStatus() != WorkflowConstants.STATUS_SCHEDULED)) {
617
618 serviceContext.setWorkflowAction(
619 WorkflowConstants.ACTION_SAVE_DRAFT);
620 }
621
622 JournalArticle importedArticle = null;
623
624 String articleResourceUuid = articleElement.attributeValue(
625 "article-resource-uuid");
626
627 if (portletDataContext.isDataStrategyMirror()) {
628 serviceContext.setUuid(articleResourceUuid);
629 serviceContext.setAttribute("urlTitle", article.getUrlTitle());
630
631 boolean preloaded = GetterUtil.getBoolean(
632 articleElement.attributeValue("preloaded"));
633
634 JournalArticle existingArticle = fetchExistingArticle(
635 articleResourceUuid, portletDataContext.getScopeGroupId(),
636 articleId, newArticleId, article.getVersion(), preloaded);
637
638 if (existingArticle == null) {
639 importedArticle =
640 JournalArticleLocalServiceUtil.addArticle(
641 userId, portletDataContext.getScopeGroupId(),
642 folderId, article.getClassNameId(), ddmStructureId,
643 articleId, autoArticleId, article.getVersion(),
644 article.getTitleMap(), article.getDescriptionMap(),
645 article.getContent(), article.getType(),
646 parentDDMStructureKey, parentDDMTemplateKey,
647 article.getLayoutUuid(), displayDateMonth,
648 displayDateDay, displayDateYear, displayDateHour,
649 displayDateMinute, expirationDateMonth,
650 expirationDateDay, expirationDateYear,
651 expirationDateHour, expirationDateMinute,
652 neverExpire, reviewDateMonth, reviewDateDay,
653 reviewDateYear, reviewDateHour, reviewDateMinute,
654 neverReview, article.isIndexable(),
655 article.isSmallImage(), article.getSmallImageURL(),
656 smallFile, images, articleURL, serviceContext);
657 }
658 else {
659 importedArticle =
660 JournalArticleLocalServiceUtil.updateArticle(
661 userId, existingArticle.getGroupId(), folderId,
662 existingArticle.getArticleId(),
663 article.getVersion(), article.getTitleMap(),
664 article.getDescriptionMap(), article.getContent(),
665 article.getType(), parentDDMStructureKey,
666 parentDDMTemplateKey, article.getLayoutUuid(),
667 displayDateMonth, displayDateDay, displayDateYear,
668 displayDateHour, displayDateMinute,
669 expirationDateMonth, expirationDateDay,
670 expirationDateYear, expirationDateHour,
671 expirationDateMinute, neverExpire, reviewDateMonth,
672 reviewDateDay, reviewDateYear, reviewDateHour,
673 reviewDateMinute, neverReview,
674 article.isIndexable(), article.isSmallImage(),
675 article.getSmallImageURL(), smallFile, images,
676 articleURL, serviceContext);
677 }
678 }
679 else {
680 importedArticle = JournalArticleLocalServiceUtil.addArticle(
681 userId, portletDataContext.getScopeGroupId(), folderId,
682 article.getClassNameId(), ddmStructureId, articleId,
683 autoArticleId, article.getVersion(), article.getTitleMap(),
684 article.getDescriptionMap(), article.getContent(),
685 article.getType(), parentDDMStructureKey,
686 parentDDMTemplateKey, article.getLayoutUuid(),
687 displayDateMonth, displayDateDay, displayDateYear,
688 displayDateHour, displayDateMinute, expirationDateMonth,
689 expirationDateDay, expirationDateYear, expirationDateHour,
690 expirationDateMinute, neverExpire, reviewDateMonth,
691 reviewDateDay, reviewDateYear, reviewDateHour,
692 reviewDateMinute, neverReview, article.isIndexable(),
693 article.isSmallImage(), article.getSmallImageURL(),
694 smallFile, images, articleURL, serviceContext);
695 }
696
697 portletDataContext.importClassedModel(article, importedArticle);
698
699 if (Validator.isNull(newArticleId)) {
700 articleIds.put(
701 article.getArticleId(), importedArticle.getArticleId());
702 }
703 }
704 finally {
705 if (smallFile != null) {
706 smallFile.delete();
707 }
708 }
709 }
710
711 @Override
712 protected void doRestoreStagedModel(
713 PortletDataContext portletDataContext, JournalArticle article)
714 throws Exception {
715
716 long userId = portletDataContext.getUserId(article.getUserUuid());
717
718 Element articleElement =
719 portletDataContext.getImportDataStagedModelElement(article);
720
721 String articleResourceUuid = articleElement.attributeValue(
722 "article-resource-uuid");
723
724 boolean preloaded = GetterUtil.getBoolean(
725 articleElement.attributeValue("preloaded"));
726
727 JournalArticle existingArticle = fetchExistingArticle(
728 articleResourceUuid, portletDataContext.getScopeGroupId(),
729 article.getArticleId(), article.getArticleId(),
730 article.getVersion(), preloaded);
731
732 if ((existingArticle == null) || !existingArticle.isInTrash()) {
733 return;
734 }
735
736 TrashHandler trashHandler = existingArticle.getTrashHandler();
737
738 if (trashHandler.isRestorable(existingArticle.getResourcePrimKey())) {
739 trashHandler.restoreTrashEntry(
740 userId, existingArticle.getResourcePrimKey());
741 }
742 }
743
744 protected void exportArticleImage(
745 PortletDataContext portletDataContext,
746 JournalArticleImage articleImage, JournalArticle article,
747 Element articleElement)
748 throws SystemException {
749
750 Image image = ImageLocalServiceUtil.fetchImage(
751 articleImage.getArticleImageId());
752
753 if ((image == null) || (image.getTextObj() == null)) {
754 return;
755 }
756
757 StringBundler sb = new StringBundler(4);
758
759 sb.append(articleImage.getElInstanceId());
760 sb.append(StringPool.UNDERLINE);
761 sb.append(articleImage.getElName());
762
763 if (Validator.isNotNull(articleImage.getLanguageId())) {
764 sb.append(articleImage.getLanguageId());
765 }
766
767 String fileName = sb.toString();
768
769 String articleImagePath = ExportImportPathUtil.getModelPath(
770 article, fileName);
771
772 if (!portletDataContext.isPathNotProcessed(articleImagePath)) {
773 return;
774 }
775
776 Element imageElement = portletDataContext.getExportDataElement(image);
777
778 imageElement.addAttribute("file-name", fileName);
779 imageElement.addAttribute("path", articleImagePath);
780
781 portletDataContext.addZipEntry(articleImagePath, image.getTextObj());
782
783 portletDataContext.addReferenceElement(
784 article, articleElement, image, articleImagePath,
785 PortletDataContext.REFERENCE_TYPE_DEPENDENCY, false);
786 }
787
788 protected JournalArticle fetchExistingArticle(
789 String articleResourceUuid, long groupId, String articleId,
790 String newArticleId, double version, boolean preloaded)
791 throws Exception {
792
793 JournalArticleResource existingArticleResource = null;
794
795 if (!preloaded) {
796 existingArticleResource =
797 JournalArticleResourceLocalServiceUtil.
798 fetchJournalArticleResourceByUuidAndGroupId(
799 articleResourceUuid, groupId);
800 }
801 else {
802 existingArticleResource =
803 JournalArticleResourceLocalServiceUtil.fetchArticleResource(
804 groupId, articleId);
805 }
806
807 JournalArticle existingArticle = null;
808
809 if (existingArticleResource != null) {
810 existingArticle =
811 JournalArticleLocalServiceUtil.fetchLatestArticle(
812 existingArticleResource.getResourcePrimKey(),
813 WorkflowConstants.STATUS_ANY, false);
814 }
815
816 if ((existingArticle == null) && Validator.isNotNull(newArticleId) &&
817 (version > 0.0)) {
818
819 existingArticle =
820 JournalArticleLocalServiceUtil.fetchArticle(
821 groupId, newArticleId, version);
822 }
823
824 return existingArticle;
825 }
826
827 protected void prepareLanguagesForImport(JournalArticle article)
828 throws PortalException {
829
830 Locale articleDefaultLocale = LocaleUtil.fromLanguageId(
831 article.getDefaultLanguageId());
832
833 Locale[] articleAvailableLocales = LocaleUtil.fromLanguageIds(
834 article.getAvailableLanguageIds());
835
836 Locale defaultImportLocale = LocalizationUtil.getDefaultImportLocale(
837 JournalArticle.class.getName(), article.getPrimaryKey(),
838 articleDefaultLocale, articleAvailableLocales);
839
840 article.prepareLocalizedFieldsForImport(defaultImportLocale);
841 }
842
843 }