001
014
015 package com.liferay.portlet.journal.lar;
016
017 import com.liferay.portal.NoSuchImageException;
018 import com.liferay.portal.kernel.dao.orm.QueryUtil;
019 import com.liferay.portal.kernel.lar.BasePortletDataHandler;
020 import com.liferay.portal.kernel.lar.PortletDataContext;
021 import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
022 import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
023 import com.liferay.portal.kernel.log.Log;
024 import com.liferay.portal.kernel.log.LogFactoryUtil;
025 import com.liferay.portal.kernel.util.CalendarFactoryUtil;
026 import com.liferay.portal.kernel.util.CharPool;
027 import com.liferay.portal.kernel.util.FileUtil;
028 import com.liferay.portal.kernel.util.GetterUtil;
029 import com.liferay.portal.kernel.util.HtmlUtil;
030 import com.liferay.portal.kernel.util.HttpUtil;
031 import com.liferay.portal.kernel.util.MapUtil;
032 import com.liferay.portal.kernel.util.StringBundler;
033 import com.liferay.portal.kernel.util.StringPool;
034 import com.liferay.portal.kernel.util.StringUtil;
035 import com.liferay.portal.kernel.util.Validator;
036 import com.liferay.portal.kernel.workflow.WorkflowConstants;
037 import com.liferay.portal.kernel.xml.Document;
038 import com.liferay.portal.kernel.xml.Element;
039 import com.liferay.portal.kernel.xml.SAXReaderUtil;
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.ServiceContext;
045 import com.liferay.portal.service.UserLocalServiceUtil;
046 import com.liferay.portal.service.persistence.ImageUtil;
047 import com.liferay.portal.util.PortletKeys;
048 import com.liferay.portal.util.PropsValues;
049 import com.liferay.portlet.documentlibrary.lar.DLPortletDataHandlerImpl;
050 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
051 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
052 import com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryUtil;
053 import com.liferay.portlet.imagegallery.lar.IGPortletDataHandlerImpl;
054 import com.liferay.portlet.imagegallery.model.IGImage;
055 import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
056 import com.liferay.portlet.imagegallery.service.persistence.IGImageUtil;
057 import com.liferay.portlet.journal.NoSuchArticleException;
058 import com.liferay.portlet.journal.NoSuchStructureException;
059 import com.liferay.portlet.journal.NoSuchTemplateException;
060 import com.liferay.portlet.journal.model.JournalArticle;
061 import com.liferay.portlet.journal.model.JournalArticleConstants;
062 import com.liferay.portlet.journal.model.JournalArticleImage;
063 import com.liferay.portlet.journal.model.JournalArticleResource;
064 import com.liferay.portlet.journal.model.JournalFeed;
065 import com.liferay.portlet.journal.model.JournalStructure;
066 import com.liferay.portlet.journal.model.JournalTemplate;
067 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
068 import com.liferay.portlet.journal.service.JournalFeedLocalServiceUtil;
069 import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
070 import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
071 import com.liferay.portlet.journal.service.persistence.JournalArticleImageUtil;
072 import com.liferay.portlet.journal.service.persistence.JournalArticleResourceUtil;
073 import com.liferay.portlet.journal.service.persistence.JournalArticleUtil;
074 import com.liferay.portlet.journal.service.persistence.JournalFeedUtil;
075 import com.liferay.portlet.journal.service.persistence.JournalStructureUtil;
076 import com.liferay.portlet.journal.service.persistence.JournalTemplateUtil;
077 import com.liferay.portlet.journal.util.comparator.ArticleIDComparator;
078
079 import java.io.File;
080
081 import java.util.Calendar;
082 import java.util.Date;
083 import java.util.HashMap;
084 import java.util.List;
085 import java.util.Map;
086
087 import javax.portlet.PortletPreferences;
088
089
118 public class JournalPortletDataHandlerImpl extends BasePortletDataHandler {
119
120 protected static void exportArticle(
121 PortletDataContext context, Element articlesElement,
122 Element structuresElement, Element templatesElement,
123 Element dlFoldersElement, Element dlFileEntriesElement,
124 Element dlFileRanksElement, Element igFoldersElement,
125 Element igImagesElement, JournalArticle article,
126 boolean checkDateRange)
127 throws Exception {
128
129 if (checkDateRange &&
130 !context.isWithinDateRange(article.getModifiedDate())) {
131
132 return;
133 }
134
135 if (article.getStatus() != WorkflowConstants.STATUS_APPROVED) {
136 return;
137 }
138
139 String path = getArticlePath(context, article);
140
141 if (!context.isPathNotProcessed(path)) {
142 return;
143 }
144
145
146
147
148 article = (JournalArticle)article.clone();
149
150 Element articleElement = (Element)articlesElement.selectSingleNode(
151 "
152
153 if (articleElement == null) {
154 articleElement = articlesElement.addElement("article");
155 }
156
157 articleElement.addAttribute("path", path);
158 articleElement.addAttribute(
159 "article-resource-uuid", article.getArticleResourceUuid());
160
161 article.setUserUuid(article.getUserUuid());
162
163 Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
164 article.getCompanyId());
165
166 if (Validator.isNotNull(article.getStructureId())) {
167 JournalStructure structure = null;
168
169 try {
170 structure = JournalStructureLocalServiceUtil.getStructure(
171 article.getGroupId(), article.getStructureId());
172 }
173 catch (NoSuchStructureException nsse) {
174 structure = JournalStructureLocalServiceUtil.getStructure(
175 companyGroup.getGroupId(), article.getStructureId());
176 }
177
178 articleElement.addAttribute("structure-uuid", structure.getUuid());
179
180 if (structure.getGroupId() == companyGroup.getGroupId()) {
181 exportStructure(context, structuresElement, structure);
182 }
183 }
184
185 if (Validator.isNotNull(article.getTemplateId())) {
186 JournalTemplate template = null;
187
188 try {
189 template = JournalTemplateLocalServiceUtil.getTemplate(
190 context.getScopeGroupId(), article.getTemplateId());
191 }
192 catch (NoSuchTemplateException nste) {
193 template = JournalTemplateLocalServiceUtil.getTemplate(
194 companyGroup.getGroupId(), article.getTemplateId());
195 }
196
197 articleElement.addAttribute("template-uuid", template.getUuid());
198
199 if (template.getGroupId() == companyGroup.getGroupId()) {
200 exportTemplate(
201 context, templatesElement, dlFoldersElement,
202 dlFileEntriesElement, dlFileRanksElement, igFoldersElement,
203 igImagesElement, template, checkDateRange);
204 }
205 }
206
207 Image smallImage = ImageUtil.fetchByPrimaryKey(
208 article.getSmallImageId());
209
210 if (article.isSmallImage() && (smallImage != null)) {
211 String smallImagePath = getArticleSmallImagePath(context, article);
212
213 articleElement.addAttribute("small-image-path", smallImagePath);
214
215 article.setSmallImageType(smallImage.getType());
216
217 context.addZipEntry(smallImagePath, smallImage.getTextObj());
218 }
219
220 if (context.getBooleanParameter(_NAMESPACE, "images")) {
221 String imagePath = getArticleImagePath(context, article);
222
223 articleElement.addAttribute("image-path", imagePath);
224
225 List<JournalArticleImage> articleImages =
226 JournalArticleImageUtil.findByG_A_V(
227 article.getGroupId(), article.getArticleId(),
228 article.getVersion());
229
230 for (JournalArticleImage articleImage : articleImages) {
231 Image image = null;
232
233 try {
234 image = ImageUtil.findByPrimaryKey(
235 articleImage.getArticleImageId());
236 }
237 catch (NoSuchImageException nsie) {
238 continue;
239 }
240
241 String articleImagePath = getArticleImagePath(
242 context, article, articleImage, image);
243
244 if (!context.isPathNotProcessed(articleImagePath)) {
245 continue;
246 }
247
248 context.addZipEntry(articleImagePath, image.getTextObj());
249 }
250 }
251
252 article.setStatusByUserUuid(article.getStatusByUserUuid());
253
254 context.addPermissions(
255 JournalArticle.class, article.getResourcePrimKey());
256
257 if (context.getBooleanParameter(_NAMESPACE, "categories")) {
258 context.addAssetCategories(
259 JournalArticle.class, article.getResourcePrimKey());
260 }
261
262 if (context.getBooleanParameter(_NAMESPACE, "comments")) {
263 context.addComments(
264 JournalArticle.class, article.getResourcePrimKey());
265 }
266
267 if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
268 context.addRatingsEntries(
269 JournalArticle.class, article.getResourcePrimKey());
270 }
271
272 if (context.getBooleanParameter(_NAMESPACE, "tags")) {
273 context.addAssetTags(
274 JournalArticle.class, article.getResourcePrimKey());
275 }
276
277 if (context.getBooleanParameter(_NAMESPACE, "embedded-assets")) {
278 String content = article.getContent();
279
280 content = exportDLFileEntries(
281 context, dlFoldersElement, dlFileEntriesElement,
282 dlFileRanksElement, articleElement, content, checkDateRange);
283 content = exportIGImages(
284 context, igFoldersElement, igImagesElement, articleElement,
285 content, checkDateRange);
286 content = exportLayoutFriendlyURLs(context, content);
287
288 article.setContent(content);
289 }
290
291 context.addZipEntry(path, article);
292 }
293
294 protected static String exportDLFileEntries(
295 PortletDataContext context, Element foldersElement,
296 Element fileEntriesElement, Element fileRanksElement,
297 Element entityElement, String content, boolean checkDateRange) {
298
299 StringBuilder sb = new StringBuilder(content);
300
301 int beginPos = content.length();
302 int currentLocation = -1;
303
304 while (true) {
305 currentLocation = content.lastIndexOf(
306 "/c/document_library/get_file?", beginPos);
307
308 if (currentLocation == -1) {
309 currentLocation = content.lastIndexOf("/documents/", beginPos);
310 }
311
312 if (currentLocation == -1) {
313 return sb.toString();
314 }
315
316 beginPos = currentLocation;
317
318 int endPos1 = content.indexOf(CharPool.APOSTROPHE, beginPos);
319 int endPos2 = content.indexOf(CharPool.CLOSE_BRACKET, beginPos);
320 int endPos3 = content.indexOf(
321 CharPool.CLOSE_PARENTHESIS, beginPos);
322 int endPos4 = content.indexOf(CharPool.LESS_THAN, beginPos);
323 int endPos5 = content.indexOf(CharPool.QUOTE, beginPos);
324 int endPos6 = content.indexOf(CharPool.SPACE, beginPos);
325
326 int endPos = endPos1;
327
328 if ((endPos == -1) || ((endPos2 != -1) && (endPos2 < endPos))) {
329 endPos = endPos2;
330 }
331
332 if ((endPos == -1) || ((endPos3 != -1) && (endPos3 < endPos))) {
333 endPos = endPos3;
334 }
335
336 if ((endPos == -1) || ((endPos4 != -1) && (endPos4 < endPos))) {
337 endPos = endPos4;
338 }
339
340 if ((endPos == -1) || ((endPos5 != -1) && (endPos5 < endPos))) {
341 endPos = endPos5;
342 }
343
344 if ((endPos == -1) || ((endPos6 != -1) && (endPos6 < endPos))) {
345 endPos = endPos6;
346 }
347
348 if ((beginPos == -1) || (endPos == -1)) {
349 break;
350 }
351
352 try {
353 String oldParameters = content.substring(beginPos, endPos);
354
355 while (oldParameters.contains(StringPool.AMPERSAND_ENCODED)) {
356 oldParameters = oldParameters.replace(
357 StringPool.AMPERSAND_ENCODED, StringPool.AMPERSAND);
358 }
359
360 Map<String, String> map = new HashMap<String, String>();
361
362 if (oldParameters.startsWith("/documents/")) {
363 String[] pathArray = oldParameters.split(StringPool.SLASH);
364
365 map.put("groupId", pathArray[2]);
366
367 if (pathArray.length == 4) {
368 map.put("uuid", pathArray[3]);
369 }
370 else if (pathArray.length > 4) {
371 map.put("folderId", pathArray[3]);
372 map.put("name", pathArray[4]);
373 }
374 }
375 else {
376 oldParameters = oldParameters.substring(
377 oldParameters.indexOf(CharPool.QUESTION) + 1);
378
379 map = MapUtil.toLinkedHashMap(
380 oldParameters.split(StringPool.AMPERSAND),
381 StringPool.EQUAL);
382 }
383
384 DLFileEntry fileEntry = null;
385
386 String uuid = map.get("uuid");
387
388 if (uuid != null) {
389 String groupIdString = map.get("groupId");
390
391 long groupId = GetterUtil.getLong(groupIdString);
392
393 if (groupIdString.equals("@group_id@")) {
394 groupId = context.getScopeGroupId();
395 }
396
397 fileEntry = DLFileEntryLocalServiceUtil.
398 getFileEntryByUuidAndGroupId(uuid, groupId);
399 }
400 else {
401 String folderIdString = map.get("folderId");
402
403 if (folderIdString != null) {
404 long folderId = GetterUtil.getLong(folderIdString);
405 String name = map.get("name");
406
407 String groupIdString = map.get("groupId");
408
409 long groupId = GetterUtil.getLong(groupIdString);
410
411 if (groupIdString.equals("@group_id@")) {
412 groupId = context.getScopeGroupId();
413 }
414
415 fileEntry =
416 DLFileEntryLocalServiceUtil.getFileEntryByTitle(
417 groupId, folderId, name);
418 }
419 }
420
421 if (fileEntry == null) {
422 beginPos--;
423
424 continue;
425 }
426
427 String path = DLPortletDataHandlerImpl.getFileEntryPath(
428 context, fileEntry);
429
430 Element dlReferenceElement = entityElement.addElement(
431 "dl-reference");
432
433 dlReferenceElement.addAttribute("path", path);
434
435 DLPortletDataHandlerImpl.exportFileEntry(
436 context, foldersElement, fileEntriesElement,
437 fileRanksElement, fileEntry, checkDateRange);
438
439 String dlReference = "[$dl-reference=" + path + "$]";
440
441 sb.replace(beginPos, endPos, dlReference);
442 }
443 catch (Exception e) {
444 if (_log.isWarnEnabled()) {
445 _log.warn(e);
446 }
447 }
448
449 beginPos--;
450 }
451
452 return sb.toString();
453 }
454
455 protected static void exportFeed(
456 PortletDataContext context, Element feedsElement, JournalFeed feed)
457 throws Exception {
458
459 if (!context.isWithinDateRange(feed.getModifiedDate())) {
460 return;
461 }
462
463 String path = getFeedPath(context, feed);
464
465 if (!context.isPathNotProcessed(path)) {
466 return;
467 }
468
469 Element feedElement = feedsElement.addElement("feed");
470
471 feedElement.addAttribute("path", path);
472
473 feed.setUserUuid(feed.getUserUuid());
474
475 context.addPermissions(JournalFeed.class, feed.getId());
476
477 context.addZipEntry(path, feed);
478 }
479
480 protected static String exportIGImages(
481 PortletDataContext context, Element foldersElement,
482 Element imagesElement, Element entityElement, String content,
483 boolean checkDateRange) {
484
485 StringBuilder sb = new StringBuilder(content);
486
487 int beginPos = content.length();
488
489 while (true) {
490 beginPos = content.lastIndexOf("/image/image_gallery?", beginPos);
491
492 if (beginPos == -1) {
493 return sb.toString();
494 }
495
496 int endPos1 = content.indexOf(CharPool.APOSTROPHE, beginPos);
497 int endPos2 = content.indexOf(CharPool.CLOSE_BRACKET, beginPos);
498 int endPos3 = content.indexOf(
499 CharPool.CLOSE_PARENTHESIS, beginPos);
500 int endPos4 = content.indexOf(CharPool.LESS_THAN, beginPos);
501 int endPos5 = content.indexOf(CharPool.QUOTE, beginPos);
502 int endPos6 = content.indexOf(CharPool.SPACE, beginPos);
503
504 int endPos = endPos1;
505
506 if ((endPos == -1) || ((endPos2 != -1) && (endPos2 < endPos))) {
507 endPos = endPos2;
508 }
509
510 if ((endPos == -1) || ((endPos3 != -1) && (endPos3 < endPos))) {
511 endPos = endPos3;
512 }
513
514 if ((endPos == -1) || ((endPos4 != -1) && (endPos4 < endPos))) {
515 endPos = endPos4;
516 }
517
518 if ((endPos == -1) || ((endPos5 != -1) && (endPos5 < endPos))) {
519 endPos = endPos5;
520 }
521
522 if ((endPos == -1) || ((endPos6 != -1) && (endPos6 < endPos))) {
523 endPos = endPos6;
524 }
525
526 if ((beginPos == -1) || (endPos == -1)) {
527 break;
528 }
529
530 try {
531 String oldParameters = content.substring(beginPos, endPos);
532
533 oldParameters = oldParameters.substring(
534 oldParameters.indexOf(StringPool.QUESTION) + 1);
535
536 while (oldParameters.contains(StringPool.AMPERSAND_ENCODED)) {
537 oldParameters = oldParameters.replace(
538 StringPool.AMPERSAND_ENCODED, StringPool.AMPERSAND);
539 }
540
541 Map<String, String> map = MapUtil.toLinkedHashMap(
542 oldParameters.split(StringPool.AMPERSAND),
543 StringPool.EQUAL);
544
545 IGImage image = null;
546
547 if (map.containsKey("uuid")) {
548 String uuid = map.get("uuid");
549
550 String groupIdString = map.get("groupId");
551
552 long groupId = GetterUtil.getLong(groupIdString);
553
554 if (groupIdString.equals("@group_id@")) {
555 groupId = context.getScopeGroupId();
556 }
557
558 image = IGImageLocalServiceUtil.getImageByUuidAndGroupId(
559 uuid, groupId);
560 }
561 else if (map.containsKey("image_id") ||
562 map.containsKey("img_id") ||
563 map.containsKey("i_id")) {
564
565 long imageId = GetterUtil.getLong(map.get("image_id"));
566
567 if (imageId <= 0) {
568 imageId = GetterUtil.getLong(map.get("img_id"));
569
570 if (imageId <= 0) {
571 imageId = GetterUtil.getLong(map.get("i_id"));
572 }
573 }
574
575 try {
576 image = IGImageLocalServiceUtil.getImageByLargeImageId(
577 imageId);
578 }
579 catch (Exception e) {
580 image = IGImageLocalServiceUtil.getImageBySmallImageId(
581 imageId);
582 }
583 }
584
585 if (image == null) {
586 beginPos--;
587
588 continue;
589 }
590
591 String path = IGPortletDataHandlerImpl.getImagePath(
592 context, image);
593
594 Element igReferenceElement = entityElement.addElement(
595 "ig-reference");
596
597 igReferenceElement.addAttribute("path", path);
598
599 IGPortletDataHandlerImpl.exportImage(
600 context, foldersElement, imagesElement, image,
601 checkDateRange);
602
603 String igReference = "[$ig-reference=" + path + "$]";
604
605 sb.replace(beginPos, endPos, igReference);
606 }
607 catch (Exception e) {
608 if (_log.isWarnEnabled()) {
609 _log.warn(e);
610 }
611 }
612
613 beginPos--;
614 }
615
616 return sb.toString();
617 }
618
619 protected static String exportLayoutFriendlyURLs(
620 PortletDataContext context, String content) {
621
622 Group group = null;
623
624 try {
625 group = GroupLocalServiceUtil.getGroup(context.getScopeGroupId());
626 }
627 catch (Exception e) {
628 if (_log.isWarnEnabled()) {
629 _log.warn(e);
630 }
631
632 return content;
633 }
634
635 StringBuilder sb = new StringBuilder(content);
636
637 String friendlyURLPrivateGroupPath =
638 PropsValues.LAYOUT_FRIENDLY_URL_PRIVATE_GROUP_SERVLET_MAPPING;
639 String friendlyURLPrivateUserPath =
640 PropsValues.LAYOUT_FRIENDLY_URL_PRIVATE_USER_SERVLET_MAPPING;
641 String friendlyURLPublicPath =
642 PropsValues.LAYOUT_FRIENDLY_URL_PUBLIC_SERVLET_MAPPING;
643
644 String href = "href=";
645
646 int beginPos = content.length();
647
648 while (true) {
649 int hrefLength = href.length();
650
651 beginPos = content.lastIndexOf(href, beginPos);
652
653 if (beginPos == -1) {
654 break;
655 }
656
657 char c = content.charAt(beginPos + hrefLength);
658
659 if ((c == CharPool.APOSTROPHE) || (c == CharPool.QUOTE)) {
660 hrefLength++;
661 }
662
663 int endPos1 = content.indexOf(
664 CharPool.APOSTROPHE, beginPos + hrefLength);
665 int endPos2 = content.indexOf(
666 CharPool.CLOSE_BRACKET, beginPos + hrefLength);
667 int endPos3 = content.indexOf(
668 CharPool.CLOSE_PARENTHESIS, beginPos + hrefLength);
669 int endPos4 = content.indexOf(
670 CharPool.LESS_THAN, beginPos + hrefLength);
671 int endPos5 = content.indexOf(
672 CharPool.QUOTE, beginPos + hrefLength);
673 int endPos6 = content.indexOf(
674 CharPool.SPACE, beginPos + hrefLength);
675
676 int endPos = endPos1;
677
678 if ((endPos == -1) || ((endPos2 != -1) && (endPos2 < endPos))) {
679 endPos = endPos2;
680 }
681
682 if ((endPos == -1) || ((endPos3 != -1) && (endPos3 < endPos))) {
683 endPos = endPos3;
684 }
685
686 if ((endPos == -1) || ((endPos4 != -1) && (endPos4 < endPos))) {
687 endPos = endPos4;
688 }
689
690 if ((endPos == -1) || ((endPos5 != -1) && (endPos5 < endPos))) {
691 endPos = endPos5;
692 }
693
694 if ((endPos == -1) || ((endPos6 != -1) && (endPos6 < endPos))) {
695 endPos = endPos6;
696 }
697
698 if (endPos == -1) {
699 beginPos--;
700
701 continue;
702 }
703
704 String url = content.substring(beginPos + hrefLength, endPos);
705
706 if (!url.startsWith(friendlyURLPrivateGroupPath) &&
707 !url.startsWith(friendlyURLPrivateUserPath) &&
708 !url.startsWith(friendlyURLPublicPath)) {
709
710 beginPos--;
711
712 continue;
713 }
714
715 int beginGroupPos = content.indexOf(
716 CharPool.SLASH, beginPos + hrefLength + 1);
717
718 if (beginGroupPos == -1) {
719 beginPos--;
720
721 continue;
722 }
723
724 int endGroupPos = content.indexOf(
725 CharPool.SLASH, beginGroupPos + 1);
726
727 if (endGroupPos == -1) {
728 beginPos--;
729
730 continue;
731 }
732
733 String groupFriendlyURL = content.substring(
734 beginGroupPos, endGroupPos);
735
736 if (groupFriendlyURL.equals(group.getFriendlyURL())) {
737 sb.replace(
738 beginGroupPos, endGroupPos,
739 "@data_handler_group_friendly_url@");
740 }
741
742 beginPos--;
743 }
744
745 return sb.toString();
746 }
747
748 protected static void exportStructure(
749 PortletDataContext context, Element structuresElement,
750 JournalStructure structure)
751 throws Exception {
752
753 String path = getStructurePath(context, structure);
754
755 if (!context.isPathNotProcessed(path)) {
756 return;
757 }
758
759 Element structureElement = structuresElement.addElement("structure");
760
761 structureElement.addAttribute("path", path);
762
763 structure.setUserUuid(structure.getUserUuid());
764
765 context.addPermissions(JournalStructure.class, structure.getId());
766
767 context.addZipEntry(path, structure);
768 }
769
770 protected static void exportTemplate(
771 PortletDataContext context, Element templatesElement,
772 Element dlFoldersElement, Element dlFileEntriesElement,
773 Element dlFileRanksElement, Element igFoldersElement,
774 Element igImagesElement, JournalTemplate template,
775 boolean checkDateRange)
776 throws Exception {
777
778 String path = getTemplatePath(context, template);
779
780 if (!context.isPathNotProcessed(path)) {
781 return;
782 }
783
784
785
786
787 template = (JournalTemplate)template.clone();
788
789 Element templateElement = templatesElement.addElement("template");
790
791 templateElement.addAttribute("path", path);
792
793 if (template.isSmallImage()) {
794 String smallImagePath = getTemplateSmallImagePath(
795 context, template);
796
797 templateElement.addAttribute("small-image-path", smallImagePath);
798
799 Image smallImage = ImageUtil.fetchByPrimaryKey(
800 template.getSmallImageId());
801
802 template.setSmallImageType(smallImage.getType());
803
804 context.addZipEntry(smallImagePath, smallImage.getTextObj());
805 }
806
807 if (context.getBooleanParameter(_NAMESPACE, "embedded-assets")) {
808 String content = template.getXsl();
809
810 content = exportDLFileEntries(
811 context, dlFoldersElement, dlFileEntriesElement,
812 dlFileRanksElement, templateElement, content, checkDateRange);
813 content = exportIGImages(
814 context, igFoldersElement, igImagesElement, templateElement,
815 content, checkDateRange);
816 content = exportLayoutFriendlyURLs(context, content);
817
818 content = StringUtil.replace(
819 content, StringPool.AMPERSAND_ENCODED, StringPool.AMPERSAND);
820
821 template.setXsl(content);
822 }
823
824 template.setUserUuid(template.getUserUuid());
825
826 context.addPermissions(JournalTemplate.class, template.getId());
827
828 context.addZipEntry(path, template);
829 }
830
831 protected static String getArticleImagePath(
832 PortletDataContext context, JournalArticle article)
833 throws Exception {
834
835 StringBundler sb = new StringBundler(6);
836
837 sb.append(context.getPortletPath(PortletKeys.JOURNAL));
838 sb.append("/articles/");
839 sb.append(article.getArticleResourceUuid());
840 sb.append(StringPool.SLASH);
841 sb.append(article.getVersion());
842 sb.append(StringPool.SLASH);
843
844 return sb.toString();
845 }
846
847 protected static String getArticleImagePath(
848 PortletDataContext context, JournalArticle article,
849 JournalArticleImage articleImage, Image image)
850 throws Exception {
851
852 StringBundler sb = new StringBundler(13);
853
854 sb.append(context.getPortletPath(PortletKeys.JOURNAL));
855 sb.append("/articles/");
856 sb.append(article.getArticleResourceUuid());
857 sb.append(StringPool.SLASH);
858 sb.append(article.getVersion());
859 sb.append(StringPool.SLASH);
860 sb.append(articleImage.getElInstanceId());
861 sb.append(StringPool.UNDERLINE);
862 sb.append(articleImage.getElName());
863
864 if (Validator.isNotNull(articleImage.getLanguageId())) {
865 sb.append(StringPool.UNDERLINE);
866 sb.append(articleImage.getLanguageId());
867 }
868
869 sb.append(StringPool.PERIOD);
870 sb.append(image.getType());
871
872 return sb.toString();
873 }
874
875 protected static String getArticlePath(
876 PortletDataContext context, JournalArticle article)
877 throws Exception {
878
879 StringBundler sb = new StringBundler(8);
880
881 sb.append(context.getPortletPath(PortletKeys.JOURNAL));
882 sb.append("/articles/");
883 sb.append(article.getArticleResourceUuid());
884 sb.append(StringPool.SLASH);
885 sb.append(article.getVersion());
886 sb.append(StringPool.SLASH);
887 sb.append("article.xml");
888
889 return sb.toString();
890 }
891
892 protected static String getArticleSmallImagePath(
893 PortletDataContext context, JournalArticle article)
894 throws Exception {
895
896 StringBundler sb = new StringBundler(6);
897
898 sb.append(context.getPortletPath(PortletKeys.JOURNAL));
899 sb.append("/articles/");
900 sb.append(article.getArticleResourceUuid());
901 sb.append("/thumbnail");
902 sb.append(StringPool.PERIOD);
903 sb.append(article.getSmallImageType());
904
905 return sb.toString();
906 }
907
908 protected static String getFeedPath(
909 PortletDataContext context, JournalFeed feed) {
910
911 StringBundler sb = new StringBundler(4);
912
913 sb.append(context.getPortletPath(PortletKeys.JOURNAL));
914 sb.append("/feeds/");
915 sb.append(feed.getUuid());
916 sb.append(".xml");
917
918 return sb.toString();
919 }
920
921 protected static String getStructurePath(
922 PortletDataContext context, JournalStructure structure) {
923
924 StringBundler sb = new StringBundler(4);
925
926 sb.append(context.getPortletPath(PortletKeys.JOURNAL));
927 sb.append("/structures/");
928 sb.append(structure.getUuid());
929 sb.append(".xml");
930
931 return sb.toString();
932 }
933
934 protected static String getTemplatePath(
935 PortletDataContext context, JournalTemplate template) {
936
937 StringBundler sb = new StringBundler(4);
938
939 sb.append(context.getPortletPath(PortletKeys.JOURNAL));
940 sb.append("/templates/");
941 sb.append(template.getUuid());
942 sb.append(".xml");
943
944 return sb.toString();
945 }
946
947 protected static String getTemplateSmallImagePath(
948 PortletDataContext context, JournalTemplate template)
949 throws Exception {
950
951 StringBundler sb = new StringBundler(5);
952
953 sb.append(context.getPortletPath(PortletKeys.JOURNAL));
954 sb.append("/templates/thumbnail-");
955 sb.append(template.getUuid());
956 sb.append(StringPool.PERIOD);
957 sb.append(template.getSmallImageType());
958
959 return sb.toString();
960 }
961
962 protected static void importArticle(
963 PortletDataContext context, Element articleElement)
964 throws Exception {
965
966 String path = articleElement.attributeValue("path");
967
968 if (!context.isPathNotProcessed(path)) {
969 return;
970 }
971
972 JournalArticle article = (JournalArticle)context.getZipEntryAsObject(
973 path);
974
975 long userId = context.getUserId(article.getUserUuid());
976
977 User user = UserLocalServiceUtil.getUser(userId);
978
979 String articleId = article.getArticleId();
980 boolean autoArticleId = false;
981
982 if ((Validator.isNumber(articleId)) ||
983 (JournalArticleUtil.fetchByG_A_V(
984 context.getScopeGroupId(), articleId,
985 JournalArticleConstants.DEFAULT_VERSION) != null)) {
986
987 autoArticleId = true;
988 }
989
990 Map<String, String> articleIds =
991 (Map<String, String>)context.getNewPrimaryKeysMap(
992 JournalArticle.class);
993
994 String newArticleId = articleIds.get(articleId);
995
996 if (Validator.isNotNull(newArticleId)) {
997
998
999
1000
1001 articleId = newArticleId;
1002 autoArticleId = false;
1003 }
1004
1005 String content = article.getContent();
1006
1007 content = importDLFileEntries(context, articleElement, content);
1008 content = importIGImages(context, articleElement, content);
1009
1010 Group group = GroupLocalServiceUtil.getGroup(context.getScopeGroupId());
1011
1012 content = StringUtil.replace(
1013 content, "@data_handler_group_friendly_url@",
1014 group.getFriendlyURL());
1015
1016 article.setContent(content);
1017
1018 Map<String, String> structureIds =
1019 (Map<String, String>)context.getNewPrimaryKeysMap(
1020 JournalStructure.class);
1021
1022 String parentStructureId = MapUtil.getString(
1023 structureIds, article.getStructureId(), article.getStructureId());
1024
1025 Map<String, String> templateIds =
1026 (Map<String, String>)context.getNewPrimaryKeysMap(
1027 JournalTemplate.class);
1028
1029 String parentTemplateId = MapUtil.getString(
1030 templateIds, article.getTemplateId(), article.getTemplateId());
1031
1032 Date displayDate = article.getDisplayDate();
1033
1034 int displayDateMonth = 0;
1035 int displayDateDay = 0;
1036 int displayDateYear = 0;
1037 int displayDateHour = 0;
1038 int displayDateMinute = 0;
1039
1040 if (displayDate != null) {
1041 Calendar displayCal = CalendarFactoryUtil.getCalendar(
1042 user.getTimeZone());
1043
1044 displayCal.setTime(displayDate);
1045
1046 displayDateMonth = displayCal.get(Calendar.MONTH);
1047 displayDateDay = displayCal.get(Calendar.DATE);
1048 displayDateYear = displayCal.get(Calendar.YEAR);
1049 displayDateHour = displayCal.get(Calendar.HOUR);
1050 displayDateMinute = displayCal.get(Calendar.MINUTE);
1051
1052 if (displayCal.get(Calendar.AM_PM) == Calendar.PM) {
1053 displayDateHour += 12;
1054 }
1055 }
1056
1057 Date expirationDate = article.getExpirationDate();
1058
1059 int expirationDateMonth = 0;
1060 int expirationDateDay = 0;
1061 int expirationDateYear = 0;
1062 int expirationDateHour = 0;
1063 int expirationDateMinute = 0;
1064 boolean neverExpire = true;
1065
1066 if (expirationDate != null) {
1067 Calendar expirationCal = CalendarFactoryUtil.getCalendar(
1068 user.getTimeZone());
1069
1070 expirationCal.setTime(expirationDate);
1071
1072 expirationDateMonth = expirationCal.get(Calendar.MONTH);
1073 expirationDateDay = expirationCal.get(Calendar.DATE);
1074 expirationDateYear = expirationCal.get(Calendar.YEAR);
1075 expirationDateHour = expirationCal.get(Calendar.HOUR);
1076 expirationDateMinute = expirationCal.get(Calendar.MINUTE);
1077 neverExpire = false;
1078
1079 if (expirationCal.get(Calendar.AM_PM) == Calendar.PM) {
1080 expirationDateHour += 12;
1081 }
1082 }
1083
1084 Date reviewDate = article.getReviewDate();
1085
1086 int reviewDateMonth = 0;
1087 int reviewDateDay = 0;
1088 int reviewDateYear = 0;
1089 int reviewDateHour = 0;
1090 int reviewDateMinute = 0;
1091 boolean neverReview = true;
1092
1093 if (reviewDate != null) {
1094 Calendar reviewCal = CalendarFactoryUtil.getCalendar(
1095 user.getTimeZone());
1096
1097 reviewCal.setTime(reviewDate);
1098
1099 reviewDateMonth = reviewCal.get(Calendar.MONTH);
1100 reviewDateDay = reviewCal.get(Calendar.DATE);
1101 reviewDateYear = reviewCal.get(Calendar.YEAR);
1102 reviewDateHour = reviewCal.get(Calendar.HOUR);
1103 reviewDateMinute = reviewCal.get(Calendar.MINUTE);
1104 neverReview = false;
1105
1106 if (reviewCal.get(Calendar.AM_PM) == Calendar.PM) {
1107 reviewDateHour += 12;
1108 }
1109 }
1110
1111 if (Validator.isNotNull(article.getStructureId())) {
1112 String structureUuid = articleElement.attributeValue(
1113 "structure-uuid");
1114
1115 JournalStructure existingStructure =
1116 JournalStructureUtil.fetchByUUID_G(
1117 structureUuid, context.getScopeGroupId());
1118
1119 if (existingStructure == null) {
1120 String newStructureId = structureIds.get(
1121 article.getStructureId());
1122
1123 if (Validator.isNotNull(newStructureId)) {
1124 existingStructure = JournalStructureUtil.fetchByG_S(
1125 context.getScopeGroupId(),
1126 String.valueOf(newStructureId));
1127 }
1128
1129 if (existingStructure == null) {
1130 if (_log.isWarnEnabled()) {
1131 StringBundler sb = new StringBundler();
1132
1133 sb.append("Structure ");
1134 sb.append(article.getStructureId());
1135 sb.append(" is missing for article ");
1136 sb.append(article.getArticleId());
1137 sb.append(", skipping this article.");
1138
1139 _log.warn(sb.toString());
1140 }
1141
1142 return;
1143 }
1144 }
1145
1146 parentStructureId = existingStructure.getStructureId();
1147 }
1148
1149 if (Validator.isNotNull(article.getTemplateId())) {
1150 String templateUuid = articleElement.attributeValue(
1151 "template-uuid");
1152
1153 JournalTemplate existingTemplate =
1154 JournalTemplateUtil.fetchByUUID_G(
1155 templateUuid, context.getScopeGroupId());
1156
1157 if (existingTemplate == null) {
1158 String newTemplateId = templateIds.get(article.getTemplateId());
1159
1160 if (Validator.isNotNull(newTemplateId)) {
1161 existingTemplate = JournalTemplateUtil.fetchByG_T(
1162 context.getScopeGroupId(), newTemplateId);
1163 }
1164
1165 if (existingTemplate == null) {
1166 if (_log.isWarnEnabled()) {
1167 StringBundler sb = new StringBundler();
1168
1169 sb.append("Template ");
1170 sb.append(article.getTemplateId());
1171 sb.append(" is missing for article ");
1172 sb.append(article.getArticleId());
1173 sb.append(", skipping this article.");
1174
1175 _log.warn(sb.toString());
1176 }
1177
1178 return;
1179 }
1180 }
1181
1182 parentTemplateId = existingTemplate.getTemplateId();
1183 }
1184
1185 File smallFile = null;
1186
1187 String smallImagePath = articleElement.attributeValue(
1188 "small-image-path");
1189
1190 if (article.isSmallImage() && Validator.isNotNull(smallImagePath)) {
1191 byte[] bytes = context.getZipEntryAsByteArray(smallImagePath);
1192
1193 smallFile = File.createTempFile(
1194 String.valueOf(article.getSmallImageId()),
1195 StringPool.PERIOD + article.getSmallImageType());
1196
1197 FileUtil.write(smallFile, bytes);
1198 }
1199
1200 Map<String, byte[]> images = new HashMap<String, byte[]>();
1201
1202 String imagePath = articleElement.attributeValue("image-path");
1203
1204 if (context.getBooleanParameter(_NAMESPACE, "images") &&
1205 Validator.isNotNull(imagePath)) {
1206
1207 List<String> imageFiles = context.getZipFolderEntries(imagePath);
1208
1209 for (String imageFile : imageFiles) {
1210 String fileName = imageFile;
1211
1212 if (fileName.contains(StringPool.SLASH)) {
1213 fileName = fileName.substring(
1214 fileName.lastIndexOf(CharPool.SLASH) + 1);
1215 }
1216
1217 if (fileName.endsWith(".xml")) {
1218 continue;
1219 }
1220
1221 int pos = fileName.lastIndexOf(CharPool.PERIOD);
1222
1223 if (pos != -1) {
1224 fileName = fileName.substring(0, pos);
1225 }
1226
1227 images.put(fileName, context.getZipEntryAsByteArray(imageFile));
1228 }
1229 }
1230
1231 String articleURL = null;
1232
1233 long[] assetCategoryIds = null;
1234 String[] assetTagNames = null;
1235
1236 if (context.getBooleanParameter(_NAMESPACE, "categories")) {
1237 assetCategoryIds = context.getAssetCategoryIds(
1238 JournalArticle.class, article.getResourcePrimKey());
1239 }
1240
1241 if (context.getBooleanParameter(_NAMESPACE, "tags")) {
1242 assetTagNames = context.getAssetTagNames(
1243 JournalArticle.class, article.getResourcePrimKey());
1244 }
1245
1246 JournalCreationStrategy creationStrategy =
1247 JournalCreationStrategyFactory.getInstance();
1248
1249 long authorId = creationStrategy.getAuthorUserId(context, article);
1250
1251 if (authorId != JournalCreationStrategy.USE_DEFAULT_USER_ID_STRATEGY) {
1252 userId = authorId;
1253 }
1254
1255 String newContent = creationStrategy.getTransformedContent(
1256 context, article);
1257
1258 if (newContent != JournalCreationStrategy.ARTICLE_CONTENT_UNCHANGED) {
1259 article.setContent(newContent);
1260 }
1261
1262 boolean addCommunityPermissions =
1263 creationStrategy.addCommunityPermissions(context, article);
1264 boolean addGuestPermissions = creationStrategy.addGuestPermissions(
1265 context, article);
1266
1267 ServiceContext serviceContext = new ServiceContext();
1268
1269 serviceContext.setAddCommunityPermissions(addCommunityPermissions);
1270 serviceContext.setAddGuestPermissions(addGuestPermissions);
1271 serviceContext.setAssetCategoryIds(assetCategoryIds);
1272 serviceContext.setAssetTagNames(assetTagNames);
1273 serviceContext.setAttribute("imported", Boolean.TRUE.toString());
1274 serviceContext.setCreateDate(article.getCreateDate());
1275 serviceContext.setModifiedDate(article.getModifiedDate());
1276 serviceContext.setScopeGroupId(context.getScopeGroupId());
1277
1278 if (article.getStatus() != WorkflowConstants.STATUS_APPROVED) {
1279 serviceContext.setWorkflowAction(
1280 WorkflowConstants.ACTION_SAVE_DRAFT);
1281 }
1282
1283 JournalArticle importedArticle = null;
1284
1285 String articleResourceUuid = articleElement.attributeValue(
1286 "article-resource-uuid");
1287
1288 if (context.isDataStrategyMirror()) {
1289 JournalArticleResource articleResource =
1290 JournalArticleResourceUtil.fetchByUUID_G(
1291 articleResourceUuid, context.getScopeGroupId());
1292
1293 serviceContext.setUuid(articleResourceUuid);
1294
1295 JournalArticle existingArticle = null;
1296
1297 if (articleResource != null) {
1298 try {
1299 existingArticle =
1300 JournalArticleLocalServiceUtil.getLatestArticle(
1301 articleResource.getResourcePrimKey(),
1302 WorkflowConstants.STATUS_ANY, false);
1303 }
1304 catch (NoSuchArticleException nsae) {
1305 }
1306 }
1307
1308 if (existingArticle == null) {
1309 existingArticle = JournalArticleUtil.fetchByG_A_V(
1310 context.getScopeGroupId(), newArticleId,
1311 article.getVersion());
1312 }
1313
1314 if (existingArticle == null) {
1315 importedArticle = JournalArticleLocalServiceUtil.addArticle(
1316 userId, context.getScopeGroupId(), articleId, autoArticleId,
1317 article.getVersion(), article.getTitle(),
1318 article.getDescription(), article.getContent(),
1319 article.getType(), parentStructureId, parentTemplateId,
1320 displayDateMonth, displayDateDay, displayDateYear,
1321 displayDateHour, displayDateMinute, expirationDateMonth,
1322 expirationDateDay, expirationDateYear, expirationDateHour,
1323 expirationDateMinute, neverExpire, reviewDateMonth,
1324 reviewDateDay, reviewDateYear, reviewDateHour,
1325 reviewDateMinute, neverReview, article.isIndexable(),
1326 article.isSmallImage(), article.getSmallImageURL(),
1327 smallFile, images, articleURL, serviceContext);
1328 }
1329 else {
1330 importedArticle = JournalArticleLocalServiceUtil.updateArticle(
1331 userId, existingArticle.getGroupId(),
1332 existingArticle.getArticleId(),
1333 existingArticle.getVersion(), article.getTitle(),
1334 article.getDescription(), article.getContent(),
1335 article.getType(), parentStructureId, parentTemplateId,
1336 displayDateMonth, displayDateDay, displayDateYear,
1337 displayDateHour, displayDateMinute, expirationDateMonth,
1338 expirationDateDay, expirationDateYear, expirationDateHour,
1339 expirationDateMinute, neverExpire, reviewDateMonth,
1340 reviewDateDay, reviewDateYear, reviewDateHour,
1341 reviewDateMinute, neverReview, article.isIndexable(),
1342 article.isSmallImage(), article.getSmallImageURL(),
1343 smallFile, images, articleURL, serviceContext);
1344 }
1345 }
1346 else {
1347 importedArticle = JournalArticleLocalServiceUtil.addArticle(
1348 userId, context.getScopeGroupId(), articleId, autoArticleId,
1349 article.getVersion(), article.getTitle(),
1350 article.getDescription(), article.getContent(),
1351 article.getType(), parentStructureId, parentTemplateId,
1352 displayDateMonth, displayDateDay, displayDateYear,
1353 displayDateHour, displayDateMinute, expirationDateMonth,
1354 expirationDateDay, expirationDateYear, expirationDateHour,
1355 expirationDateMinute, neverExpire, reviewDateMonth,
1356 reviewDateDay, reviewDateYear, reviewDateHour, reviewDateMinute,
1357 neverReview, article.isIndexable(), article.isSmallImage(),
1358 article.getSmallImageURL(), smallFile, images, articleURL,
1359 serviceContext);
1360 }
1361
1362 context.importPermissions(
1363 JournalArticle.class, article.getResourcePrimKey(),
1364 importedArticle.getResourcePrimKey());
1365
1366 if (context.getBooleanParameter(_NAMESPACE, "comments")) {
1367 context.importComments(
1368 JournalArticle.class, article.getResourcePrimKey(),
1369 importedArticle.getResourcePrimKey(),
1370 context.getScopeGroupId());
1371 }
1372
1373 if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
1374 context.importRatingsEntries(
1375 JournalArticle.class, article.getResourcePrimKey(),
1376 importedArticle.getResourcePrimKey());
1377 }
1378
1379 articleIds.put(articleId, importedArticle.getArticleId());
1380
1381 if (!articleId.equals(importedArticle.getArticleId())) {
1382 if (_log.isWarnEnabled()) {
1383 _log.warn(
1384 "An article with the ID " + articleId + " already " +
1385 "exists. The new generated ID is " +
1386 importedArticle.getArticleId());
1387 }
1388 }
1389 }
1390
1391 protected static String importDLFileEntries(
1392 PortletDataContext context, Element parentElement, String content)
1393 throws Exception {
1394
1395 Map<Long, Long> fileEntryPKs =
1396 (Map<Long, Long>)context.getNewPrimaryKeysMap(DLFileEntry.class);
1397
1398 List<Element> dlReferenceElements = parentElement.elements(
1399 "dl-reference");
1400
1401 for (Element dlReferenceElement : dlReferenceElements) {
1402 String dlReferencePath = dlReferenceElement.attributeValue("path");
1403
1404 DLFileEntry fileEntry = (DLFileEntry)context.getZipEntryAsObject(
1405 dlReferencePath);
1406
1407 if (fileEntry == null) {
1408 continue;
1409 }
1410
1411 long fileEntryId = MapUtil.getLong(
1412 fileEntryPKs, fileEntry.getFileEntryId(),
1413 fileEntry.getFileEntryId());
1414
1415 fileEntry = DLFileEntryUtil.fetchByPrimaryKey(fileEntryId);
1416
1417 if (fileEntry == null) {
1418 continue;
1419 }
1420
1421 String dlReference = "[$dl-reference=" + dlReferencePath + "$]";
1422
1423 StringBundler sb = new StringBundler(6);
1424
1425 sb.append("/documents/");
1426 sb.append(context.getScopeGroupId());
1427 sb.append(StringPool.SLASH);
1428 sb.append(fileEntry.getFolderId());
1429 sb.append(StringPool.SLASH);
1430 sb.append(
1431 HttpUtil.encodeURL(HtmlUtil.unescape(fileEntry.getTitle())));
1432
1433 content = StringUtil.replace(content, dlReference, sb.toString());
1434 }
1435
1436 return content;
1437 }
1438
1439 protected static void importFeed(
1440 PortletDataContext context, Element feedElement)
1441 throws Exception {
1442
1443 String path = feedElement.attributeValue("path");
1444
1445 if (!context.isPathNotProcessed(path)) {
1446 return;
1447 }
1448
1449 JournalFeed feed = (JournalFeed)context.getZipEntryAsObject(path);
1450
1451 long userId = context.getUserId(feed.getUserUuid());
1452
1453 String feedId = feed.getFeedId();
1454 boolean autoFeedId = false;
1455
1456 if ((Validator.isNumber(feedId)) ||
1457 (JournalFeedUtil.fetchByG_F(
1458 context.getScopeGroupId(), feedId) != null)) {
1459
1460 autoFeedId = true;
1461 }
1462
1463 Map<String, String> structureIds =
1464 (Map<String, String>)context.getNewPrimaryKeysMap(
1465 JournalStructure.class);
1466
1467 String parentStructureId = MapUtil.getString(
1468 structureIds, feed.getStructureId(), feed.getStructureId());
1469
1470 Map<String, String> templateIds =
1471 (Map<String, String>)context.getNewPrimaryKeysMap(
1472 JournalTemplate.class);
1473
1474 String parentTemplateId = MapUtil.getString(
1475 templateIds, feed.getTemplateId(), feed.getTemplateId());
1476 String parentRenderTemplateId = MapUtil.getString(
1477 templateIds, feed.getRendererTemplateId(),
1478 feed.getRendererTemplateId());
1479
1480 JournalCreationStrategy creationStrategy =
1481 JournalCreationStrategyFactory.getInstance();
1482
1483 long authorId = creationStrategy.getAuthorUserId(context, feed);
1484
1485 if (authorId != JournalCreationStrategy.USE_DEFAULT_USER_ID_STRATEGY) {
1486 userId = authorId;
1487 }
1488
1489 boolean addCommunityPermissions =
1490 creationStrategy.addCommunityPermissions(context, feed);
1491 boolean addGuestPermissions = creationStrategy.addGuestPermissions(
1492 context, feed);
1493
1494 ServiceContext serviceContext = new ServiceContext();
1495
1496 serviceContext.setAddCommunityPermissions(addCommunityPermissions);
1497 serviceContext.setAddGuestPermissions(addGuestPermissions);
1498 serviceContext.setCreateDate(feed.getCreateDate());
1499 serviceContext.setModifiedDate(feed.getModifiedDate());
1500
1501 JournalFeed existingFeed = null;
1502
1503 if (context.isDataStrategyMirror()) {
1504 existingFeed = JournalFeedUtil.fetchByUUID_G(
1505 feed.getUuid(), context.getScopeGroupId());
1506
1507 if (existingFeed == null) {
1508 serviceContext.setUuid(feed.getUuid());
1509
1510 existingFeed = JournalFeedLocalServiceUtil.addFeed(
1511 userId, context.getScopeGroupId(), feedId, autoFeedId,
1512 feed.getName(), feed.getDescription(), feed.getType(),
1513 parentStructureId, parentTemplateId, parentRenderTemplateId,
1514 feed.getDelta(), feed.getOrderByCol(),
1515 feed.getOrderByType(), feed.getTargetLayoutFriendlyUrl(),
1516 feed.getTargetPortletId(), feed.getContentField(),
1517 feed.getFeedType(), feed.getFeedVersion(),
1518 serviceContext);
1519 }
1520 else {
1521 existingFeed = JournalFeedLocalServiceUtil.updateFeed(
1522 existingFeed.getGroupId(), existingFeed.getFeedId(),
1523 feed.getName(), feed.getDescription(), feed.getType(),
1524 parentStructureId, parentTemplateId, parentRenderTemplateId,
1525 feed.getDelta(), feed.getOrderByCol(),
1526 feed.getOrderByType(), feed.getTargetLayoutFriendlyUrl(),
1527 feed.getTargetPortletId(), feed.getContentField(),
1528 feed.getFeedType(), feed.getFeedVersion(), serviceContext);
1529 }
1530 }
1531 else {
1532 existingFeed = JournalFeedLocalServiceUtil.addFeed(
1533 userId, context.getScopeGroupId(), feedId, autoFeedId,
1534 feed.getName(), feed.getDescription(), feed.getType(),
1535 parentStructureId, parentTemplateId, parentRenderTemplateId,
1536 feed.getDelta(), feed.getOrderByCol(), feed.getOrderByType(),
1537 feed.getTargetLayoutFriendlyUrl(), feed.getTargetPortletId(),
1538 feed.getContentField(), feed.getFeedType(),
1539 feed.getFeedVersion(), serviceContext);
1540 }
1541
1542 Map<String, String> feedIds =
1543 (Map<String, String>)context.getNewPrimaryKeysMap(
1544 JournalFeed.class);
1545
1546 feedIds.put(feedId, existingFeed.getFeedId());
1547
1548 context.importPermissions(
1549 JournalFeed.class, feed.getId(), existingFeed.getId());
1550
1551 if (!feedId.equals(existingFeed.getFeedId())) {
1552 if (_log.isWarnEnabled()) {
1553 _log.warn(
1554 "A feed with the ID " + feedId + " already " +
1555 "exists. The new generated ID is " +
1556 existingFeed.getFeedId());
1557 }
1558 }
1559 }
1560
1561 protected static String importIGImages(
1562 PortletDataContext context, Element parentElement, String content)
1563 throws Exception {
1564
1565 Map<Long, Long> imagePKs =
1566 (Map<Long, Long>)context.getNewPrimaryKeysMap(IGImage.class);
1567
1568 List<Element> igReferenceElements = parentElement.elements(
1569 "ig-reference");
1570
1571 for (Element igReferenceElement : igReferenceElements) {
1572 String igReferencePath = igReferenceElement.attributeValue("path");
1573
1574 IGImage image = (IGImage)context.getZipEntryAsObject(
1575 igReferencePath);
1576
1577 if (image == null) {
1578 continue;
1579 }
1580
1581 long imageId = MapUtil.getLong(
1582 imagePKs, image.getImageId(), image.getImageId());
1583
1584 image = IGImageUtil.fetchByPrimaryKey(imageId);
1585
1586 if (image == null) {
1587 continue;
1588 }
1589
1590 String igReference = "[$ig-reference=" + igReferencePath + "$]";
1591
1592 StringBundler sb = new StringBundler(6);
1593
1594 sb.append("/image/image_gallery?uuid=");
1595 sb.append(image.getUuid());
1596 sb.append("&groupId=");
1597 sb.append(context.getScopeGroupId());
1598 sb.append("&t=");
1599 sb.append(System.currentTimeMillis());
1600
1601 content = StringUtil.replace(content, igReference, sb.toString());
1602 }
1603
1604 return content;
1605 }
1606
1607 protected static void importStructure(
1608 PortletDataContext context, Element structureElement)
1609 throws Exception {
1610
1611 String path = structureElement.attributeValue("path");
1612
1613 if (!context.isPathNotProcessed(path)) {
1614 return;
1615 }
1616
1617 JournalStructure structure =
1618 (JournalStructure)context.getZipEntryAsObject(path);
1619
1620 long userId = context.getUserId(structure.getUserUuid());
1621
1622 String structureId = structure.getStructureId();
1623 boolean autoStructureId = false;
1624
1625 if ((Validator.isNumber(structureId)) ||
1626 (JournalStructureUtil.fetchByG_S(
1627 context.getScopeGroupId(), structureId) != null)) {
1628
1629 autoStructureId = true;
1630 }
1631
1632 JournalCreationStrategy creationStrategy =
1633 JournalCreationStrategyFactory.getInstance();
1634
1635 long authorId = creationStrategy.getAuthorUserId(context, structure);
1636
1637 if (authorId != JournalCreationStrategy.USE_DEFAULT_USER_ID_STRATEGY) {
1638 userId = authorId;
1639 }
1640
1641 boolean addCommunityPermissions =
1642 creationStrategy.addCommunityPermissions(context, structure);
1643 boolean addGuestPermissions = creationStrategy.addGuestPermissions(
1644 context, structure);
1645
1646 ServiceContext serviceContext = new ServiceContext();
1647
1648 serviceContext.setAddCommunityPermissions(addCommunityPermissions);
1649 serviceContext.setAddGuestPermissions(addGuestPermissions);
1650 serviceContext.setCreateDate(structure.getCreateDate());
1651 serviceContext.setModifiedDate(structure.getModifiedDate());
1652
1653 JournalStructure existingStructure = null;
1654
1655 if (context.isDataStrategyMirror()) {
1656 existingStructure = JournalStructureUtil.fetchByUUID_G(
1657 structure.getUuid(), context.getScopeGroupId());
1658
1659 if (existingStructure == null) {
1660 serviceContext.setUuid(structure.getUuid());
1661
1662 existingStructure =
1663 JournalStructureLocalServiceUtil.addStructure(
1664 userId, context.getScopeGroupId(), structureId,
1665 autoStructureId, structure.getParentStructureId(),
1666 structure.getName(), structure.getDescription(),
1667 structure.getXsd(), serviceContext);
1668 }
1669 else {
1670 existingStructure =
1671 JournalStructureLocalServiceUtil.updateStructure(
1672 existingStructure.getGroupId(),
1673 existingStructure.getStructureId(),
1674 structure.getParentStructureId(), structure.getName(),
1675 structure.getDescription(), structure.getXsd(),
1676 serviceContext);
1677 }
1678 }
1679 else {
1680 existingStructure = JournalStructureLocalServiceUtil.addStructure(
1681 userId, context.getScopeGroupId(), structureId, autoStructureId,
1682 structure.getParentStructureId(), structure.getName(),
1683 structure.getDescription(), structure.getXsd(), serviceContext);
1684 }
1685
1686 Map<String, String> structureIds =
1687 (Map<String, String>)context.getNewPrimaryKeysMap(
1688 JournalStructure.class);
1689
1690 structureIds.put(structureId, existingStructure.getStructureId());
1691
1692 context.importPermissions(
1693 JournalStructure.class, structure.getId(),
1694 existingStructure.getId());
1695
1696 if (!structureId.equals(existingStructure.getStructureId())) {
1697 if (_log.isWarnEnabled()) {
1698 _log.warn(
1699 "A structure with the ID " + structureId + " already " +
1700 "exists. The new generated ID is " +
1701 existingStructure.getStructureId());
1702 }
1703 }
1704 }
1705
1706 protected static void importTemplate(
1707 PortletDataContext context, Element templateElement)
1708 throws Exception {
1709
1710 String path = templateElement.attributeValue("path");
1711
1712 if (!context.isPathNotProcessed(path)) {
1713 return;
1714 }
1715
1716 JournalTemplate template = (JournalTemplate)context.getZipEntryAsObject(
1717 path);
1718
1719 long userId = context.getUserId(template.getUserUuid());
1720
1721 String templateId = template.getTemplateId();
1722 boolean autoTemplateId = false;
1723
1724 if ((Validator.isNumber(templateId)) ||
1725 (JournalTemplateUtil.fetchByG_T(
1726 context.getScopeGroupId(), templateId) != null)) {
1727
1728 autoTemplateId = true;
1729 }
1730
1731 Map<String, String> structureIds =
1732 (Map<String, String>)context.getNewPrimaryKeysMap(
1733 JournalStructure.class);
1734
1735 String parentStructureId = MapUtil.getString(
1736 structureIds, template.getStructureId(), template.getStructureId());
1737
1738 String xsl = template.getXsl();
1739
1740 xsl = importDLFileEntries(context, templateElement, xsl);
1741 xsl = importIGImages(context, templateElement, xsl);
1742
1743 Group group = GroupLocalServiceUtil.getGroup(context.getScopeGroupId());
1744
1745 xsl = StringUtil.replace(
1746 xsl, "@data_handler_group_friendly_url@", group.getFriendlyURL());
1747
1748 template.setXsl(xsl);
1749
1750 boolean formatXsl = false;
1751
1752 JournalCreationStrategy creationStrategy =
1753 JournalCreationStrategyFactory.getInstance();
1754
1755 long authorId = creationStrategy.getAuthorUserId(context, template);
1756
1757 if (authorId != JournalCreationStrategy.USE_DEFAULT_USER_ID_STRATEGY) {
1758 userId = authorId;
1759 }
1760
1761 boolean addCommunityPermissions =
1762 creationStrategy.addCommunityPermissions(context, template);
1763 boolean addGuestPermissions = creationStrategy.addGuestPermissions(
1764 context, template);
1765
1766 ServiceContext serviceContext = new ServiceContext();
1767
1768 serviceContext.setAddCommunityPermissions(addCommunityPermissions);
1769 serviceContext.setAddGuestPermissions(addGuestPermissions);
1770 serviceContext.setCreateDate(template.getCreateDate());
1771 serviceContext.setModifiedDate(template.getModifiedDate());
1772
1773 File smallFile = null;
1774
1775 String smallImagePath = templateElement.attributeValue(
1776 "small-image-path");
1777
1778 if (template.isSmallImage() && Validator.isNotNull(smallImagePath)) {
1779 if (smallImagePath.endsWith(StringPool.PERIOD)) {
1780 smallImagePath += template.getSmallImageType();
1781 }
1782
1783 byte[] bytes = context.getZipEntryAsByteArray(smallImagePath);
1784
1785 if (bytes != null) {
1786 smallFile = File.createTempFile(
1787 String.valueOf(template.getSmallImageId()),
1788 StringPool.PERIOD + template.getSmallImageType());
1789
1790 FileUtil.write(smallFile, bytes);
1791 }
1792 }
1793
1794 JournalTemplate existingTemplate = null;
1795
1796 if (context.isDataStrategyMirror()) {
1797 existingTemplate = JournalTemplateUtil.fetchByUUID_G(
1798 template.getUuid(), context.getScopeGroupId());
1799
1800 if (existingTemplate == null) {
1801 serviceContext.setUuid(template.getUuid());
1802
1803 existingTemplate = JournalTemplateLocalServiceUtil.addTemplate(
1804 userId, context.getScopeGroupId(), templateId,
1805 autoTemplateId, parentStructureId, template.getName(),
1806 template.getDescription(), template.getXsl(), formatXsl,
1807 template.getLangType(), template.getCacheable(),
1808 template.isSmallImage(), template.getSmallImageURL(),
1809 smallFile, serviceContext);
1810 }
1811 else {
1812 existingTemplate =
1813 JournalTemplateLocalServiceUtil.updateTemplate(
1814 existingTemplate.getGroupId(),
1815 existingTemplate.getTemplateId(),
1816 existingTemplate.getStructureId(), template.getName(),
1817 template.getDescription(), template.getXsl(), formatXsl,
1818 template.getLangType(), template.getCacheable(),
1819 template.isSmallImage(), template.getSmallImageURL(),
1820 smallFile, serviceContext);
1821 }
1822 }
1823 else {
1824 existingTemplate = JournalTemplateLocalServiceUtil.addTemplate(
1825 userId, context.getScopeGroupId(), templateId, autoTemplateId,
1826 parentStructureId, template.getName(),
1827 template.getDescription(), template.getXsl(), formatXsl,
1828 template.getLangType(), template.getCacheable(),
1829 template.isSmallImage(), template.getSmallImageURL(), smallFile,
1830 serviceContext);
1831 }
1832
1833 Map<String, String> templateIds =
1834 (Map<String, String>)context.getNewPrimaryKeysMap(
1835 JournalTemplate.class);
1836
1837 templateIds.put(templateId, existingTemplate.getTemplateId());
1838
1839 context.importPermissions(
1840 JournalTemplate.class, template.getId(),
1841 existingTemplate.getId());
1842
1843 if (!templateId.equals(existingTemplate.getTemplateId())) {
1844 if (_log.isWarnEnabled()) {
1845 _log.warn(
1846 "A template with the ID " + templateId + " already " +
1847 "exists. The new generated ID is " +
1848 existingTemplate.getTemplateId());
1849 }
1850 }
1851 }
1852
1853 public PortletDataHandlerControl[] getExportControls() {
1854 return new PortletDataHandlerControl[] {
1855 _articles, _structuresTemplatesAndFeeds, _embeddedAssets, _images,
1856 _categories, _comments, _ratings, _tags
1857 };
1858 }
1859
1860 public PortletDataHandlerControl[] getImportControls() {
1861 return new PortletDataHandlerControl[] {
1862 _articles, _structuresTemplatesAndFeeds, _images, _categories,
1863 _comments, _ratings, _tags
1864 };
1865 }
1866
1867 public boolean isAlwaysExportable() {
1868 return _ALWAYS_EXPORTABLE;
1869 }
1870
1871 public boolean isPublishToLiveByDefault() {
1872 return PropsValues.JOURNAL_PUBLISH_TO_LIVE_BY_DEFAULT;
1873 }
1874
1875 protected PortletPreferences doDeleteData(
1876 PortletDataContext context, String portletId,
1877 PortletPreferences preferences)
1878 throws Exception {
1879
1880 if (!context.addPrimaryKey(
1881 JournalPortletDataHandlerImpl.class, "deleteData")) {
1882
1883 JournalArticleLocalServiceUtil.deleteArticles(
1884 context.getScopeGroupId());
1885
1886 JournalTemplateLocalServiceUtil.deleteTemplates(
1887 context.getScopeGroupId());
1888
1889 JournalStructureLocalServiceUtil.deleteStructures(
1890 context.getScopeGroupId());
1891 }
1892
1893 return preferences;
1894 }
1895
1896 protected String doExportData(
1897 PortletDataContext context, String portletId,
1898 PortletPreferences preferences)
1899 throws Exception {
1900
1901 context.addPermissions(
1902 "com.liferay.portlet.journal", context.getScopeGroupId());
1903
1904 Document document = SAXReaderUtil.createDocument();
1905
1906 Element rootElement = document.addElement("journal-data");
1907
1908 rootElement.addAttribute(
1909 "group-id", String.valueOf(context.getScopeGroupId()));
1910
1911 Element structuresElement = rootElement.addElement("structures");
1912
1913 List<JournalStructure> structures =
1914 JournalStructureUtil.findByGroupId(context.getScopeGroupId());
1915
1916 for (JournalStructure structure : structures) {
1917 if (context.isWithinDateRange(structure.getModifiedDate())) {
1918 exportStructure(context, structuresElement, structure);
1919 }
1920 }
1921
1922 Element templatesElement = rootElement.addElement("templates");
1923 Element dlFoldersElement = rootElement.addElement("dl-folders");
1924 Element dlFilesElement = rootElement.addElement("dl-file-entries");
1925 Element dlFileRanksElement = rootElement.addElement("dl-file-ranks");
1926 Element igFoldersElement = rootElement.addElement("ig-folders");
1927 Element igImagesElement = rootElement.addElement("ig-images");
1928
1929 List<JournalTemplate> templates = JournalTemplateUtil.findByGroupId(
1930 context.getScopeGroupId());
1931
1932 for (JournalTemplate template : templates) {
1933 if (context.isWithinDateRange(template.getModifiedDate())) {
1934 exportTemplate(
1935 context, templatesElement, dlFoldersElement,
1936 dlFilesElement, dlFileRanksElement, igFoldersElement,
1937 igImagesElement, template, true);
1938 }
1939 }
1940
1941 Element feedsElement = rootElement.addElement("feeds");
1942
1943 List<JournalFeed> feeds = JournalFeedUtil.findByGroupId(
1944 context.getScopeGroupId());
1945
1946 for (JournalFeed feed : feeds) {
1947 if (context.isWithinDateRange(feed.getModifiedDate())) {
1948 exportFeed(context, feedsElement, feed);
1949 }
1950 }
1951
1952 Element articlesElement = rootElement.addElement("articles");
1953
1954 if (context.getBooleanParameter(_NAMESPACE, "articles")) {
1955 List<JournalArticle> articles = JournalArticleUtil.findByG_ST(
1956 context.getScopeGroupId(), WorkflowConstants.STATUS_APPROVED,
1957 QueryUtil.ALL_POS, QueryUtil.ALL_POS,
1958 new ArticleIDComparator(true));
1959
1960 for (JournalArticle article : articles) {
1961 exportArticle(
1962 context, articlesElement, structuresElement,
1963 templatesElement, dlFoldersElement, dlFilesElement,
1964 dlFileRanksElement, igFoldersElement, igImagesElement,
1965 article, true);
1966 }
1967 }
1968
1969 return document.formattedString();
1970 }
1971
1972 protected PortletPreferences doImportData(
1973 PortletDataContext context, String portletId,
1974 PortletPreferences preferences, String data)
1975 throws Exception {
1976
1977 context.importPermissions(
1978 "com.liferay.portlet.journal", context.getSourceGroupId(),
1979 context.getScopeGroupId());
1980
1981 Document document = SAXReaderUtil.read(data);
1982
1983 Element rootElement = document.getRootElement();
1984
1985 Element dlFoldersElement = rootElement.element("dl-folders");
1986
1987 List<Element> dlFolderElements = dlFoldersElement.elements("folder");
1988
1989 for (Element dlFolderElement : dlFolderElements) {
1990 DLPortletDataHandlerImpl.importFolder(context, dlFolderElement);
1991 }
1992
1993 Element dlFileEntriesElement = rootElement.element("dl-file-entries");
1994
1995 List<Element> dlFileEntryElements = dlFileEntriesElement.elements(
1996 "file-entry");
1997
1998 for (Element dlFileEntryElement : dlFileEntryElements) {
1999 DLPortletDataHandlerImpl.importFileEntry(
2000 context, dlFileEntryElement);
2001 }
2002
2003 Element dlFileRanksElement = rootElement.element("dl-file-ranks");
2004
2005 List<Element> dlFileRankElements = dlFileRanksElement.elements(
2006 "file-rank");
2007
2008 for (Element dlFileRankElement : dlFileRankElements) {
2009 DLPortletDataHandlerImpl.importFileRank(context, dlFileRankElement);
2010 }
2011
2012 Element igFoldersElement = rootElement.element("ig-folders");
2013
2014 List<Element> igFolderElements = igFoldersElement.elements("folder");
2015
2016 for (Element igFolderElement : igFolderElements) {
2017 IGPortletDataHandlerImpl.importFolder(context, igFolderElement);
2018 }
2019
2020 Element igImagesElement = rootElement.element("ig-images");
2021
2022 List<Element> igImageElements = igImagesElement.elements("image");
2023
2024 for (Element igImageElement : igImageElements) {
2025 IGPortletDataHandlerImpl.importImage(context, igImageElement);
2026 }
2027
2028 Element structuresElement = rootElement.element("structures");
2029
2030 List<Element> structureElements = structuresElement.elements(
2031 "structure");
2032
2033 for (Element structureElement : structureElements) {
2034 importStructure(context, structureElement);
2035 }
2036
2037 Element templatesElement = rootElement.element("templates");
2038
2039 List<Element> templateElements = templatesElement.elements("template");
2040
2041 for (Element templateElement : templateElements) {
2042 importTemplate(context, templateElement);
2043 }
2044
2045 Element feedsElement = rootElement.element("feeds");
2046
2047 List<Element> feedElements = feedsElement.elements("feed");
2048
2049 for (Element feedElement : feedElements) {
2050 importFeed(context, feedElement);
2051 }
2052
2053 if (context.getBooleanParameter(_NAMESPACE, "articles")) {
2054 Element articlesElement = rootElement.element("articles");
2055
2056 List<Element> articleElements = articlesElement.elements("article");
2057
2058 for (Element articleElement : articleElements) {
2059 importArticle(context, articleElement);
2060 }
2061 }
2062
2063 return preferences;
2064 }
2065
2066 private static final boolean _ALWAYS_EXPORTABLE = true;
2067
2068 private static final String _NAMESPACE = "journal";
2069
2070 private static Log _log = LogFactoryUtil.getLog(
2071 JournalPortletDataHandlerImpl.class);
2072
2073 private static PortletDataHandlerBoolean _categories =
2074 new PortletDataHandlerBoolean(_NAMESPACE, "categories");
2075
2076 private static PortletDataHandlerBoolean _comments =
2077 new PortletDataHandlerBoolean(_NAMESPACE, "comments");
2078
2079 private static PortletDataHandlerBoolean _embeddedAssets =
2080 new PortletDataHandlerBoolean(_NAMESPACE, "embedded-assets");
2081
2082 private static PortletDataHandlerBoolean _images =
2083 new PortletDataHandlerBoolean(_NAMESPACE, "images");
2084
2085 private static PortletDataHandlerBoolean _ratings =
2086 new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
2087
2088 private static PortletDataHandlerBoolean
2089 _structuresTemplatesAndFeeds = new PortletDataHandlerBoolean(
2090 _NAMESPACE, "structures-templates-and-feeds", true, true);
2091
2092 private static PortletDataHandlerBoolean _tags =
2093 new PortletDataHandlerBoolean(_NAMESPACE, "tags");
2094
2095 private static PortletDataHandlerBoolean _articles =
2096 new PortletDataHandlerBoolean(_NAMESPACE, "articles", true, false,
2097 new PortletDataHandlerControl[] {_images, _comments, _ratings, _tags});
2098
2099 }