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