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