1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journal.lar;
24  
25  import com.liferay.portal.NoSuchImageException;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
29  import com.liferay.portal.kernel.util.FileUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.ObjectValuePair;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.kernel.xml.Document;
35  import com.liferay.portal.kernel.xml.Element;
36  import com.liferay.portal.kernel.xml.SAXReaderUtil;
37  import com.liferay.portal.lar.PortletDataContext;
38  import com.liferay.portal.lar.PortletDataException;
39  import com.liferay.portal.lar.PortletDataHandler;
40  import com.liferay.portal.lar.PortletDataHandlerBoolean;
41  import com.liferay.portal.lar.PortletDataHandlerControl;
42  import com.liferay.portal.lar.PortletDataHandlerKeys;
43  import com.liferay.portal.model.Image;
44  import com.liferay.portal.service.persistence.ImageUtil;
45  import com.liferay.portal.util.PortletKeys;
46  import com.liferay.portlet.documentlibrary.lar.DLPortletDataHandlerImpl;
47  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
48  import com.liferay.portlet.documentlibrary.model.DLFileRank;
49  import com.liferay.portlet.documentlibrary.model.DLFolder;
50  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
51  import com.liferay.portlet.imagegallery.lar.IGPortletDataHandlerImpl;
52  import com.liferay.portlet.imagegallery.model.IGFolder;
53  import com.liferay.portlet.imagegallery.model.IGImage;
54  import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
55  import com.liferay.portlet.journal.model.JournalArticle;
56  import com.liferay.portlet.journal.model.JournalArticleImage;
57  import com.liferay.portlet.journal.model.JournalFeed;
58  import com.liferay.portlet.journal.model.JournalStructure;
59  import com.liferay.portlet.journal.model.JournalTemplate;
60  import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
61  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
62  import com.liferay.portlet.journal.service.JournalFeedLocalServiceUtil;
63  import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
64  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
65  import com.liferay.portlet.journal.service.persistence.JournalArticleImageUtil;
66  import com.liferay.portlet.journal.service.persistence.JournalArticleUtil;
67  import com.liferay.portlet.journal.service.persistence.JournalFeedUtil;
68  import com.liferay.portlet.journal.service.persistence.JournalStructureUtil;
69  import com.liferay.portlet.journal.service.persistence.JournalTemplateUtil;
70  import com.liferay.util.MapUtil;
71  
72  import java.io.File;
73  
74  import java.util.Calendar;
75  import java.util.Date;
76  import java.util.HashMap;
77  import java.util.List;
78  import java.util.Map;
79  
80  import javax.portlet.PortletPreferences;
81  
82  import org.apache.commons.logging.Log;
83  import org.apache.commons.logging.LogFactory;
84  
85  /**
86   * <a href="JournalPortletDataHandlerImpl.java.html"><b><i>View Source</i></b>
87   * </a>
88   *
89   * <p>
90   * Provides the Journal portlet export and import functionality, which is to
91   * clone all articles, structures, and templates associated with the layout's
92   * group. Upon import, new instances of the corresponding articles, structures,
93   * and templates are created or updated according to the DATA_MIRROW strategy
94   * The author of the newly created objects are determined by the
95   * JournalCreationStrategy class defined in <i>portal.properties</i>. That
96   * strategy also allows the text of the journal article to be modified prior
97   * to import.
98   * </p>
99   *
100  * <p>
101  * This <code>PortletDataHandler</code> differs from
102  * <code>JournalContentPortletDataHandlerImpl</code> in that it exports all
103  * articles owned by the group whether or not they are actually displayed in a
104  * portlet in the layout set.
105  * </p>
106  *
107  * @author Raymond Augé
108  * @author Joel Kozikowski
109  * @author Brian Wing Shun Chan
110  * @author Bruno Farache
111  *
112  * @see com.liferay.portal.lar.PortletDataHandler
113  * @see com.liferay.portlet.journal.lar.JournalContentPortletDataHandlerImpl
114  * @see com.liferay.portlet.journal.lar.JournalCreationStrategy
115  *
116  */
117 public class JournalPortletDataHandlerImpl implements PortletDataHandler {
118 
119     public static void exportArticle(
120             PortletDataContext context, Element articlesEl, Element dlFoldersEl,
121             Element dlFileEntriesEl, Element dlFileRanks, Element igFoldersEl,
122             Element igImagesEl, JournalArticle article)
123         throws PortalException, SystemException {
124 
125         if (!context.isWithinDateRange(article.getModifiedDate())) {
126             return;
127         }
128 
129         String path = getArticlePath(context, article);
130 
131         if (!context.isPathNotProcessed(path)) {
132             return;
133         }
134 
135         // Clone this article to make sure changes to its content are never
136         // persisted
137 
138         article = (JournalArticle)article.clone();
139 
140         Element articleEl = articlesEl.addElement("article");
141 
142         articleEl.addAttribute("path", path);
143 
144         if (article.isSmallImage()) {
145             String smallImagePath = getArticleSmallImagePath(context, article);
146 
147             articleEl.addAttribute("small-image-path", smallImagePath);
148 
149             Image smallImage = ImageUtil.fetchByPrimaryKey(
150                 article.getSmallImageId());
151 
152             article.setSmallImageType(smallImage.getType());
153 
154             context.addZipEntry(smallImagePath, smallImage.getTextObj());
155         }
156 
157         if (context.getBooleanParameter(_NAMESPACE, "images")) {
158             String imagePath = getArticleImagePath(context, article);
159 
160             articleEl.addAttribute("image-path", imagePath);
161 
162             List<JournalArticleImage> articleImages =
163                 JournalArticleImageUtil.findByG_A_V(
164                     context.getGroupId(), article.getArticleId(),
165                     article.getVersion());
166 
167             for (JournalArticleImage articleImage : articleImages) {
168                 try {
169                     Image image = ImageUtil.findByPrimaryKey(
170                         articleImage.getArticleImageId());
171 
172                     context.addZipEntry(
173                         getArticleImagePath(
174                             context, article, articleImage, image),
175                         image.getTextObj());
176                 }
177                 catch (NoSuchImageException nsie) {
178                 }
179             }
180         }
181 
182         if (context.getBooleanParameter(_NAMESPACE, "comments")) {
183             context.addComments(
184                 JournalArticle.class, article.getResourcePrimKey());
185         }
186 
187         if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
188             context.addRatingsEntries(
189                 JournalArticle.class, article.getResourcePrimKey());
190         }
191 
192         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
193             context.addTagsEntries(
194                 JournalArticle.class, article.getResourcePrimKey());
195         }
196 
197         if (context.getBooleanParameter(_NAMESPACE, "embedded-assets")) {
198             String content = article.getContent();
199 
200             content = exportDLFileEntries(
201                 context, dlFoldersEl, dlFileEntriesEl, dlFileRanks, content);
202             content = exportIGImages(context, igFoldersEl, igImagesEl, content);
203 
204             article.setContent(content);
205         }
206 
207         article.setUserUuid(article.getUserUuid());
208         article.setApprovedByUserUuid(article.getApprovedByUserUuid());
209 
210         context.addZipEntry(path, article);
211     }
212 
213     public static void exportFeed(
214             PortletDataContext context, Element feedsEl, JournalFeed feed)
215         throws SystemException {
216 
217         if (!context.isWithinDateRange(feed.getModifiedDate())) {
218             return;
219         }
220 
221         String path = getFeedPath(context, feed);
222 
223         if (!context.isPathNotProcessed(path)) {
224             return;
225         }
226 
227         Element feedEl = feedsEl.addElement("feed");
228 
229         feedEl.addAttribute("path", path);
230 
231         feed.setUserUuid(feed.getUserUuid());
232 
233         context.addZipEntry(path, feed);
234     }
235 
236     public static String exportDLFileEntries(
237         PortletDataContext context, Element foldersEl, Element fileEntriesEl,
238         Element fileRanks, String content) {
239 
240         StringBuilder sb = new StringBuilder(content);
241 
242         int beginPos = content.length();
243 
244         while (true) {
245             beginPos = content.lastIndexOf("/get_file?", beginPos);
246 
247             int endPos1 = content.indexOf(StringPool.APOSTROPHE, beginPos);
248             int endPos2 = content.indexOf(StringPool.CLOSE_BRACKET, beginPos);
249             int endPos3 = content.indexOf(StringPool.LESS_THAN, beginPos);
250             int endPos4 = content.indexOf(StringPool.QUOTE, beginPos);
251             int endPos5 = content.indexOf(StringPool.SPACE, beginPos);
252 
253             int endPos = endPos1;
254 
255             if ((endPos == -1) || ((endPos2 != -1) && (endPos2 < endPos))) {
256                 endPos = endPos2;
257             }
258 
259             if ((endPos == -1) || ((endPos3 != -1) && (endPos3 < endPos))) {
260                 endPos = endPos3;
261             }
262 
263             if ((endPos == -1) || ((endPos4 != -1) && (endPos4 < endPos))) {
264                 endPos = endPos4;
265             }
266 
267             if ((endPos == -1) || ((endPos5 != -1) && (endPos5 < endPos))) {
268                 endPos = endPos5;
269             }
270 
271             if ((beginPos == -1) || (endPos == -1)) {
272                 break;
273             }
274 
275             try {
276                 String oldParameters = content.substring(beginPos, endPos);
277 
278                 oldParameters = oldParameters.substring(
279                     oldParameters.indexOf(StringPool.QUESTION) + 1);
280                 oldParameters = oldParameters.replace(
281                     StringPool.AMPERSAND_ENCODED, StringPool.AMPERSAND);
282 
283                 Map<String, String> map = MapUtil.toLinkedHashMap(
284                     oldParameters.split(StringPool.AMPERSAND),
285                     StringPool.EQUAL);
286 
287                 DLFileEntry fileEntry = null;
288 
289                 if (map.containsKey("uuid")) {
290                     String uuid = map.get("uuid");
291                     long groupId = GetterUtil.getLong(map.get("groupId"));
292 
293                     fileEntry = DLFileEntryLocalServiceUtil.
294                         getFileEntryByUuidAndGroupId(uuid, groupId);
295                 }
296                 else if (map.containsKey("folderId")) {
297                     long folderId = GetterUtil.getLong(map.get("folderId"));
298                     String name = map.get("name");
299 
300                     fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
301                         folderId, name);
302                 }
303 
304                 if (fileEntry == null) {
305                     continue;
306                 }
307 
308                 DLPortletDataHandlerImpl.exportFileEntry(
309                     context, foldersEl, fileEntriesEl, fileRanks, fileEntry);
310 
311                 String newParameters =
312                     "/get_file?uuid=" + fileEntry.getUuid() +
313                         "&amp;groupId=@group_id@";
314 
315                 sb.replace(beginPos, endPos, newParameters);
316             }
317             catch (Exception e) {
318                 if (_log.isWarnEnabled()) {
319                     _log.warn(e, e);
320                 }
321             }
322 
323             beginPos--;
324         }
325 
326         return sb.toString();
327     }
328 
329     public static String exportIGImages(
330         PortletDataContext context, Element foldersEl, Element imagesEl,
331         String content) {
332 
333         StringBuilder sb = new StringBuilder(content);
334 
335         int beginPos = content.length();
336 
337         while (true) {
338             beginPos = content.lastIndexOf("/image/image_gallery?", beginPos);
339 
340             int endPos1 = content.indexOf(StringPool.APOSTROPHE, beginPos);
341             int endPos2 = content.indexOf(StringPool.CLOSE_BRACKET, beginPos);
342             int endPos3 = content.indexOf(StringPool.LESS_THAN, beginPos);
343             int endPos4 = content.indexOf(StringPool.QUOTE, beginPos);
344             int endPos5 = content.indexOf(StringPool.SPACE, beginPos);
345 
346             int endPos = endPos1;
347 
348             if ((endPos == -1) || ((endPos2 != -1) && (endPos2 < endPos))) {
349                 endPos = endPos2;
350             }
351 
352             if ((endPos == -1) || ((endPos3 != -1) && (endPos3 < endPos))) {
353                 endPos = endPos3;
354             }
355 
356             if ((endPos == -1) || ((endPos4 != -1) && (endPos4 < endPos))) {
357                 endPos = endPos4;
358             }
359 
360             if ((endPos == -1) || ((endPos5 != -1) && (endPos5 < endPos))) {
361                 endPos = endPos5;
362             }
363 
364             if ((beginPos == -1) || (endPos == -1)) {
365                 break;
366             }
367 
368             try {
369                 String oldParameters = content.substring(beginPos, endPos);
370 
371                 oldParameters = oldParameters.substring(
372                     oldParameters.indexOf(StringPool.QUESTION) + 1);
373                 oldParameters = oldParameters.replace(
374                     StringPool.AMPERSAND_ENCODED, StringPool.AMPERSAND);
375 
376                 Map<String, String> map = MapUtil.toLinkedHashMap(
377                     oldParameters.split(StringPool.AMPERSAND),
378                     StringPool.EQUAL);
379 
380                 IGImage image = null;
381 
382                 if (map.containsKey("uuid")) {
383                     String uuid = map.get("uuid");
384                     long groupId = GetterUtil.getLong(map.get("groupId"));
385 
386                     image = IGImageLocalServiceUtil.getImageByUuidAndGroupId(
387                         uuid, groupId);
388                 }
389                 else if (map.containsKey("image_id") ||
390                          map.containsKey("img_id") ||
391                          map.containsKey("i_id")) {
392 
393                     long imageId = GetterUtil.getLong(map.get("image_id"));
394 
395                     if (imageId <= 0) {
396                         imageId = GetterUtil.getLong(map.get("img_id"));
397 
398                         if (imageId <= 0) {
399                             imageId = GetterUtil.getLong(map.get("i_id"));
400                         }
401                     }
402 
403                     try {
404                         image = IGImageLocalServiceUtil.getImageByLargeImageId(
405                             imageId);
406                     }
407                     catch (Exception e) {
408                         image = IGImageLocalServiceUtil.getImageBySmallImageId(
409                             imageId);
410                     }
411                 }
412 
413                 if (image == null) {
414                     continue;
415                 }
416 
417                 IGPortletDataHandlerImpl.exportImage(
418                     context, foldersEl, imagesEl, image);
419 
420                 String timestamp = map.get("t");
421 
422                 if (timestamp == null) {
423                     timestamp = String.valueOf(System.currentTimeMillis());
424                 }
425 
426                 String newParameters =
427                     "/image/image_gallery?uuid=" + image.getUuid() +
428                         "&amp;groupId=@group_id@&amp;t=" + timestamp;
429 
430                 sb.replace(beginPos, endPos, newParameters);
431             }
432             catch (Exception e) {
433                 if (_log.isWarnEnabled()) {
434                     _log.warn(e, e);
435                 }
436             }
437 
438             beginPos--;
439         }
440 
441         return sb.toString();
442     }
443 
444     public static void exportStructure(
445             PortletDataContext context, Element structuresEl,
446             JournalStructure structure)
447         throws SystemException {
448 
449         if (!context.isWithinDateRange(structure.getModifiedDate())) {
450             return;
451         }
452 
453         String path = getStructurePath(context, structure);
454 
455         if (!context.isPathNotProcessed(path)) {
456             return;
457         }
458 
459         Element structureEl = structuresEl.addElement("structure");
460 
461         structureEl.addAttribute("path", path);
462 
463         structure.setUserUuid(structure.getUserUuid());
464 
465         context.addZipEntry(path, structure);
466     }
467 
468     public static void exportTemplate(
469             PortletDataContext context, Element templatesEl,
470             JournalTemplate template)
471         throws PortalException, SystemException {
472 
473         if (!context.isWithinDateRange(template.getModifiedDate())) {
474             return;
475         }
476 
477         String path = getTemplatePath(context, template);
478 
479         if (!context.isPathNotProcessed(path)) {
480             return;
481         }
482 
483         Element templateEl = templatesEl.addElement("template");
484 
485         templateEl.addAttribute("path", path);
486 
487         if (template.isSmallImage()) {
488             String smallImagePath = getTemplateSmallImagePath(
489                 context, template);
490 
491             templateEl.addAttribute("small-image-path", smallImagePath);
492 
493             Image smallImage = ImageUtil.fetchByPrimaryKey(
494                 template.getSmallImageId());
495 
496             template.setSmallImageType(smallImage.getType());
497 
498             context.addZipEntry(smallImagePath, smallImage.getTextObj());
499         }
500 
501         template.setUserUuid(template.getUserUuid());
502 
503         context.addZipEntry(path, template);
504     }
505 
506     public static void importArticle(
507             PortletDataContext context, Map<String, String> structureIds,
508             Map<String, String> templateIds, Map<String, String> articleIds,
509             Element articleEl)
510         throws Exception {
511 
512         String path = articleEl.attributeValue("path");
513 
514         if (!context.isPathNotProcessed(path)) {
515             return;
516         }
517 
518         JournalArticle article = (JournalArticle)context.getZipEntryAsObject(
519             path);
520 
521         long userId = context.getUserId(article.getUserUuid());
522         long plid = context.getPlid();
523 
524         String articleId = article.getArticleId();
525         boolean autoArticleId = false;
526 
527         if ((Validator.isNumber(articleId)) ||
528             (JournalArticleUtil.fetchByG_A_V(
529                 context.getGroupId(), articleId,
530                     JournalArticleImpl.DEFAULT_VERSION) != null)) {
531 
532             autoArticleId = true;
533         }
534 
535         String newArticleId = articleIds.get(articleId);
536 
537         if (Validator.isNotNull(newArticleId)) {
538 
539             // A sibling of a different version was already assigned a new
540             // article id
541 
542             articleId = newArticleId;
543             autoArticleId = false;
544         }
545 
546         boolean incrementVersion = false;
547 
548         String parentStructureId = MapUtil.getString(
549             structureIds, article.getStructureId(), article.getStructureId());
550         String parentTemplateId = MapUtil.getString(
551             templateIds, article.getTemplateId(), article.getTemplateId());
552 
553         Date displayDate = article.getDisplayDate();
554 
555         int displayDateMonth = 0;
556         int displayDateDay = 0;
557         int displayDateYear = 0;
558         int displayDateHour = 0;
559         int displayDateMinute = 0;
560 
561         if (displayDate != null) {
562             Calendar displayCal = CalendarFactoryUtil.getCalendar();
563 
564             displayCal.setTime(displayDate);
565 
566             displayDateMonth = displayCal.get(Calendar.MONTH);
567             displayDateDay = displayCal.get(Calendar.DATE);
568             displayDateYear = displayCal.get(Calendar.YEAR);
569             displayDateHour = displayCal.get(Calendar.HOUR);
570             displayDateMinute = displayCal.get(Calendar.MINUTE);
571 
572             if (displayCal.get(Calendar.AM_PM) == Calendar.PM) {
573                 displayDateHour += 12;
574             }
575         }
576 
577         Date expirationDate = article.getExpirationDate();
578 
579         int expirationDateMonth = 0;
580         int expirationDateDay = 0;
581         int expirationDateYear = 0;
582         int expirationDateHour = 0;
583         int expirationDateMinute = 0;
584         boolean neverExpire = true;
585 
586         if (expirationDate != null) {
587             Calendar expirationCal = CalendarFactoryUtil.getCalendar();
588 
589             expirationCal.setTime(expirationDate);
590 
591             expirationDateMonth = expirationCal.get(Calendar.MONTH);
592             expirationDateDay = expirationCal.get(Calendar.DATE);
593             expirationDateYear = expirationCal.get(Calendar.YEAR);
594             expirationDateHour = expirationCal.get(Calendar.HOUR);
595             expirationDateMinute = expirationCal.get(Calendar.MINUTE);
596             neverExpire = false;
597 
598             if (expirationCal.get(Calendar.AM_PM) == Calendar.PM) {
599                 expirationDateHour += 12;
600             }
601         }
602 
603         Date reviewDate = article.getReviewDate();
604 
605         int reviewDateMonth = 0;
606         int reviewDateDay = 0;
607         int reviewDateYear = 0;
608         int reviewDateHour = 0;
609         int reviewDateMinute = 0;
610         boolean neverReview = true;
611 
612         if (reviewDate != null) {
613             Calendar reviewCal = CalendarFactoryUtil.getCalendar();
614 
615             reviewCal.setTime(reviewDate);
616 
617             reviewDateMonth = reviewCal.get(Calendar.MONTH);
618             reviewDateDay = reviewCal.get(Calendar.DATE);
619             reviewDateYear = reviewCal.get(Calendar.YEAR);
620             reviewDateHour = reviewCal.get(Calendar.HOUR);
621             reviewDateMinute = reviewCal.get(Calendar.MINUTE);
622             neverReview = false;
623 
624             if (reviewCal.get(Calendar.AM_PM) == Calendar.PM) {
625                 reviewDateHour += 12;
626             }
627         }
628 
629         File smallFile = null;
630 
631         if (article.isSmallImage()) {
632             String smallImagePath = articleEl.attributeValue(
633                 "small-image-path");
634 
635             byte[] bytes = context.getZipEntryAsByteArray(smallImagePath);
636 
637             smallFile = File.createTempFile(
638                 String.valueOf(article.getSmallImageId()),
639                 StringPool.PERIOD + article.getSmallImageType());
640 
641             FileUtil.write(smallFile, bytes);
642         }
643 
644         Map<String, byte[]> images = new HashMap<String, byte[]>();
645 
646         if (context.getBooleanParameter(_NAMESPACE, "images")) {
647             String imagePath = articleEl.attributeValue("image-path");
648 
649             List<ObjectValuePair<String, byte[]>> imageFiles =
650                 context.getZipFolderEntries(imagePath);
651 
652             if (imageFiles != null) {
653                 for (ObjectValuePair<String, byte[]> imageFile : imageFiles) {
654                     String fileName = imageFile.getKey();
655 
656                     if (fileName.endsWith(".xml")) {
657                         continue;
658                     }
659 
660                     int pos = fileName.lastIndexOf(StringPool.PERIOD);
661 
662                     if (pos != -1) {
663                         fileName = fileName.substring(0, pos);
664                     }
665 
666                     images.put(fileName, imageFile.getValue());
667                 }
668             }
669         }
670 
671         String articleURL = null;
672 
673         PortletPreferences prefs = null;
674 
675         String[] tagsEntries = null;
676 
677         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
678             tagsEntries = context.getTagsEntries(
679                 JournalArticle.class, article.getResourcePrimKey());
680         }
681 
682         JournalCreationStrategy creationStrategy =
683             JournalCreationStrategyFactory.getInstance();
684 
685         long authorId = creationStrategy.getAuthorUserId(context, article);
686 
687         if (authorId != JournalCreationStrategy.USE_DEFAULT_USER_ID_STRATEGY) {
688             userId = authorId;
689         }
690 
691         String newContent = creationStrategy.getTransformedContent(
692             context, article);
693 
694         if (newContent != JournalCreationStrategy.ARTICLE_CONTENT_UNCHANGED) {
695             article.setContent(newContent);
696         }
697 
698         boolean addCommunityPermissions =
699             creationStrategy.addCommunityPermissions(context, article);
700         boolean addGuestPermissions = creationStrategy.addGuestPermissions(
701             context, article);
702 
703         JournalArticle existingArticle = null;
704 
705         if (context.getDataStrategy().equals(
706                 PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
707 
708             existingArticle = JournalArticleUtil.fetchByUUID_G(
709                 article.getUuid(), context.getGroupId());
710 
711             if (existingArticle == null) {
712                 existingArticle = JournalArticleLocalServiceUtil.addArticle(
713                     article.getUuid(), userId, articleId, autoArticleId,
714                     plid, article.getVersion(), article.getTitle(),
715                     article.getDescription(), article.getContent(),
716                     article.getType(), parentStructureId, parentTemplateId,
717                     displayDateMonth, displayDateDay, displayDateYear,
718                     displayDateHour, displayDateMinute, expirationDateMonth,
719                     expirationDateDay, expirationDateYear, expirationDateHour,
720                     expirationDateMinute, neverExpire, reviewDateMonth,
721                     reviewDateDay, reviewDateYear, reviewDateHour,
722                     reviewDateMinute, neverReview, article.getIndexable(),
723                     article.getSmallImage(), article.getSmallImageURL(),
724                     smallFile, images, articleURL, prefs, tagsEntries,
725                     addCommunityPermissions, addGuestPermissions);
726             }
727             else {
728                 existingArticle = JournalArticleLocalServiceUtil.updateArticle(
729                     userId, existingArticle.getGroupId(),
730                     existingArticle.getArticleId(),
731                     existingArticle.getVersion(), incrementVersion,
732                     article.getTitle(), article.getDescription(),
733                     article.getContent(), article.getType(),
734                     existingArticle.getStructureId(),
735                     existingArticle.getTemplateId(), displayDateMonth,
736                     displayDateDay, displayDateYear, displayDateHour,
737                     displayDateMinute, expirationDateMonth, expirationDateDay,
738                     expirationDateYear, expirationDateHour,
739                     expirationDateMinute, neverExpire, reviewDateMonth,
740                     reviewDateDay, reviewDateYear, reviewDateHour,
741                     reviewDateMinute, neverReview, article.getIndexable(),
742                     article.getSmallImage(), article.getSmallImageURL(),
743                     smallFile, images, articleURL, prefs, tagsEntries);
744             }
745         }
746         else {
747             existingArticle = JournalArticleLocalServiceUtil.addArticle(
748                 userId, articleId, autoArticleId, plid, article.getVersion(),
749                 article.getTitle(), article.getDescription(),
750                 article.getContent(), article.getType(), parentStructureId,
751                 parentTemplateId, displayDateMonth, displayDateDay,
752                 displayDateYear, displayDateHour, displayDateMinute,
753                 expirationDateMonth, expirationDateDay, expirationDateYear,
754                 expirationDateHour, expirationDateMinute, neverExpire,
755                 reviewDateMonth, reviewDateDay, reviewDateYear, reviewDateHour,
756                 reviewDateMinute, neverReview, article.getIndexable(),
757                 article.getSmallImage(), article.getSmallImageURL(), smallFile,
758                 images, articleURL, prefs, tagsEntries, addCommunityPermissions,
759                 addGuestPermissions);
760         }
761 
762         long strategyApprovalUserId = creationStrategy.getApprovalUserId(
763             context, article);
764 
765         if ((strategyApprovalUserId !=
766                 JournalCreationStrategy.USE_DEFAULT_USER_ID_STRATEGY) ||
767             (article.isApproved() && !existingArticle.isApproved())) {
768 
769             long approvedByUserId = strategyApprovalUserId;
770 
771             if (approvedByUserId == 0) {
772                 approvedByUserId = context.getUserId(
773                     article.getApprovedByUserUuid());
774             }
775 
776             JournalArticleLocalServiceUtil.approveArticle(
777                 approvedByUserId, context.getGroupId(),
778                 existingArticle.getArticleId(), existingArticle.getVersion(),
779                 articleURL, prefs);
780         }
781 
782         if (context.getBooleanParameter(_NAMESPACE, "comments")) {
783             context.importComments(
784                 JournalArticle.class, article.getResourcePrimKey(),
785                 existingArticle.getResourcePrimKey(), context.getGroupId());
786         }
787 
788         if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
789             context.importRatingsEntries(
790                 JournalArticle.class, article.getResourcePrimKey(),
791                 existingArticle.getResourcePrimKey());
792         }
793 
794         articleIds.put(articleId, existingArticle.getArticleId());
795 
796         if (!articleId.equals(existingArticle.getArticleId())) {
797             if (_log.isWarnEnabled()) {
798                 _log.warn(
799                     "An article with the ID " + articleId + " already " +
800                         "exists. The new generated ID is " +
801                             existingArticle.getArticleId());
802             }
803         }
804     }
805 
806     public static void importFeed(
807             PortletDataContext context, Map<String, String> structureIds,
808             Map<String, String> templateIds, Map<String, String> feedIds,
809             Element feedEl)
810         throws Exception {
811 
812         String path = feedEl.attributeValue("path");
813 
814         if (!context.isPathNotProcessed(path)) {
815             return;
816         }
817 
818         JournalFeed feed = (JournalFeed)context.getZipEntryAsObject(path);
819 
820         long userId = context.getUserId(feed.getUserUuid());
821         long plid = context.getPlid();
822 
823         String feedId = feed.getFeedId();
824         boolean autoFeedId = false;
825 
826         if ((Validator.isNumber(feedId)) ||
827             (JournalFeedUtil.fetchByG_F(
828                 context.getGroupId(), feedId) != null)) {
829 
830             autoFeedId = true;
831         }
832 
833         String parentStructureId = MapUtil.getString(
834             structureIds, feed.getStructureId(), feed.getStructureId());
835         String parentTemplateId = MapUtil.getString(
836             templateIds, feed.getTemplateId(), feed.getTemplateId());
837         String parentRenderTemplateId = MapUtil.getString(
838             templateIds, feed.getRendererTemplateId(),
839             feed.getRendererTemplateId());
840 
841         JournalCreationStrategy creationStrategy =
842             JournalCreationStrategyFactory.getInstance();
843 
844         long authorId = creationStrategy.getAuthorUserId(context, feed);
845 
846         if (authorId != JournalCreationStrategy.USE_DEFAULT_USER_ID_STRATEGY) {
847             userId = authorId;
848         }
849 
850         boolean addCommunityPermissions =
851             creationStrategy.addCommunityPermissions(context, feed);
852         boolean addGuestPermissions = creationStrategy.addGuestPermissions(
853             context, feed);
854 
855         JournalFeed existingFeed = null;
856 
857         if (context.getDataStrategy().equals(
858                 PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
859 
860             existingFeed = JournalFeedUtil.fetchByUUID_G(
861                 feed.getUuid(), context.getGroupId());
862 
863             if (existingFeed == null) {
864                 existingFeed = JournalFeedLocalServiceUtil.addFeed(
865                     feed.getUuid(), userId, plid, feedId, autoFeedId,
866                     feed.getName(), feed.getDescription(), feed.getType(),
867                     parentStructureId, parentTemplateId, parentRenderTemplateId,
868                     feed.getDelta(), feed.getOrderByCol(),
869                     feed.getOrderByType(), feed.getTargetLayoutFriendlyUrl(),
870                     feed.getTargetPortletId(), feed.getContentField(),
871                     feed.getFeedType(), feed.getFeedVersion(),
872                     addCommunityPermissions, addGuestPermissions);
873             }
874             else {
875                 existingFeed = JournalFeedLocalServiceUtil.updateFeed(
876                     existingFeed.getGroupId(), existingFeed.getFeedId(),
877                     feed.getName(), feed.getDescription(), feed.getType(),
878                     parentStructureId, parentTemplateId, parentRenderTemplateId,
879                     feed.getDelta(), feed.getOrderByCol(),
880                     feed.getOrderByType(), feed.getTargetLayoutFriendlyUrl(),
881                     feed.getTargetPortletId(), feed.getContentField(),
882                     feed.getFeedType(), feed.getFeedVersion());
883             }
884         }
885         else {
886             existingFeed = JournalFeedLocalServiceUtil.addFeed(
887                 userId, plid, feedId, autoFeedId, feed.getName(),
888                 feed.getDescription(), feed.getType(), parentStructureId,
889                 parentTemplateId, parentRenderTemplateId, feed.getDelta(),
890                 feed.getOrderByCol(), feed.getOrderByType(),
891                 feed.getTargetLayoutFriendlyUrl(), feed.getTargetPortletId(),
892                 feed.getContentField(), feed.getFeedType(),
893                 feed.getFeedVersion(), addCommunityPermissions,
894                 addGuestPermissions);
895         }
896 
897         feedIds.put(feedId, existingFeed.getFeedId());
898 
899         if (!feedId.equals(existingFeed.getStructureId())) {
900             if (_log.isWarnEnabled()) {
901                 _log.warn(
902                     "A feed with the ID " + feedId + " already " +
903                         "exists. The new generated ID is " +
904                             existingFeed.getFeedId());
905             }
906         }
907     }
908 
909     public static void importStructure(
910             PortletDataContext context, Map<String, String> structureIds,
911             Element structureEl)
912         throws Exception {
913 
914         String path = structureEl.attributeValue("path");
915 
916         if (!context.isPathNotProcessed(path)) {
917             return;
918         }
919 
920         JournalStructure structure =
921             (JournalStructure)context.getZipEntryAsObject(path);
922 
923         long userId = context.getUserId(structure.getUserUuid());
924         long plid = context.getPlid();
925 
926         String structureId = structure.getStructureId();
927         boolean autoStructureId = false;
928 
929         if ((Validator.isNumber(structureId)) ||
930             (JournalStructureUtil.fetchByG_S(
931                 context.getGroupId(), structureId) != null)) {
932 
933             autoStructureId = true;
934         }
935 
936         JournalCreationStrategy creationStrategy =
937             JournalCreationStrategyFactory.getInstance();
938 
939         long authorId = creationStrategy.getAuthorUserId(context, structure);
940 
941         if (authorId != JournalCreationStrategy.USE_DEFAULT_USER_ID_STRATEGY) {
942             userId = authorId;
943         }
944 
945         boolean addCommunityPermissions =
946             creationStrategy.addCommunityPermissions(context, structure);
947         boolean addGuestPermissions = creationStrategy.addGuestPermissions(
948             context, structure);
949 
950         JournalStructure existingStructure = null;
951 
952         if (context.getDataStrategy().equals(
953                 PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
954 
955             existingStructure = JournalStructureUtil.fetchByUUID_G(
956                 structure.getUuid(), context.getGroupId());
957 
958             if (existingStructure == null) {
959                 existingStructure =
960                     JournalStructureLocalServiceUtil.addStructure(
961                         structure.getUuid(), userId, structureId,
962                         autoStructureId, plid, structure.getName(),
963                         structure.getDescription(), structure.getXsd(),
964                         addCommunityPermissions, addGuestPermissions);
965             }
966             else {
967                 existingStructure =
968                     JournalStructureLocalServiceUtil.updateStructure(
969                         existingStructure.getGroupId(),
970                         existingStructure.getStructureId(), structure.getName(),
971                         structure.getDescription(), structure.getXsd());
972             }
973         }
974         else {
975             existingStructure = JournalStructureLocalServiceUtil.addStructure(
976                 userId, structureId, autoStructureId, plid, structure.getName(),
977                 structure.getDescription(), structure.getXsd(),
978                 addCommunityPermissions, addGuestPermissions);
979         }
980 
981         structureIds.put(structureId, existingStructure.getStructureId());
982 
983         if (!structureId.equals(existingStructure.getStructureId())) {
984             if (_log.isWarnEnabled()) {
985                 _log.warn(
986                     "A structure with the ID " + structureId + " already " +
987                         "exists. The new generated ID is " +
988                             existingStructure.getStructureId());
989             }
990         }
991     }
992 
993     public static void importTemplate(
994             PortletDataContext context, Map<String, String> structureIds,
995             Map<String, String> templateIds, Element templateEl)
996         throws Exception {
997 
998         String path = templateEl.attributeValue("path");
999 
1000        if (!context.isPathNotProcessed(path)) {
1001            return;
1002        }
1003
1004        JournalTemplate template = (JournalTemplate)context.getZipEntryAsObject(
1005            path);
1006
1007        long userId = context.getUserId(template.getUserUuid());
1008        long plid = context.getPlid();
1009
1010        String templateId = template.getTemplateId();
1011        boolean autoTemplateId = false;
1012
1013        if ((Validator.isNumber(templateId)) ||
1014            (JournalTemplateUtil.fetchByG_T(
1015                context.getGroupId(), templateId) != null)) {
1016
1017            autoTemplateId = true;
1018        }
1019
1020        String parentStructureId = MapUtil.getString(
1021            structureIds, template.getStructureId(), template.getStructureId());
1022
1023        boolean formatXsl = false;
1024
1025        JournalCreationStrategy creationStrategy =
1026            JournalCreationStrategyFactory.getInstance();
1027
1028        long authorId = creationStrategy.getAuthorUserId(context, template);
1029
1030        if (authorId != JournalCreationStrategy.USE_DEFAULT_USER_ID_STRATEGY) {
1031            userId = authorId;
1032        }
1033
1034        boolean addCommunityPermissions =
1035            creationStrategy.addCommunityPermissions(context, template);
1036        boolean addGuestPermissions = creationStrategy.addGuestPermissions(
1037            context, template);
1038
1039        File smallFile = null;
1040
1041        if (template.isSmallImage()) {
1042            String smallImagePath = templateEl.attributeValue(
1043                "small-image-path");
1044
1045            byte[] bytes = context.getZipEntryAsByteArray(smallImagePath);
1046
1047            smallFile = File.createTempFile(
1048                String.valueOf(template.getSmallImageId()),
1049                StringPool.PERIOD + template.getSmallImageType());
1050
1051            FileUtil.write(smallFile, bytes);
1052        }
1053
1054        JournalTemplate existingTemplate = null;
1055
1056        if (context.getDataStrategy().equals(
1057                PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
1058
1059            existingTemplate = JournalTemplateUtil.fetchByUUID_G(
1060                template.getUuid(), context.getGroupId());
1061
1062            if (existingTemplate == null) {
1063                existingTemplate = JournalTemplateLocalServiceUtil.addTemplate(
1064                    template.getUuid(), userId, templateId, autoTemplateId,
1065                    plid, parentStructureId, template.getName(),
1066                    template.getDescription(), template.getXsl(), formatXsl,
1067                    template.getLangType(), template.getCacheable(),
1068                    template.isSmallImage(), template.getSmallImageURL(),
1069                    smallFile, addCommunityPermissions, addGuestPermissions);
1070            }
1071            else {
1072                existingTemplate =
1073                    JournalTemplateLocalServiceUtil.updateTemplate(
1074                        existingTemplate.getGroupId(),
1075                        existingTemplate.getTemplateId(),
1076                        existingTemplate.getStructureId(), template.getName(),
1077                        template.getDescription(), template.getXsl(), formatXsl,
1078                        template.getLangType(), template.getCacheable(),
1079                        template.isSmallImage(), template.getSmallImageURL(),
1080                        smallFile);
1081            }
1082        }
1083        else {
1084            existingTemplate = JournalTemplateLocalServiceUtil.addTemplate(
1085                userId, templateId, autoTemplateId, plid, parentStructureId,
1086                template.getName(), template.getDescription(),
1087                template.getXsl(), formatXsl, template.getLangType(),
1088                template.getCacheable(), template.isSmallImage(),
1089                template.getSmallImageURL(), smallFile, addCommunityPermissions,
1090                addGuestPermissions);
1091        }
1092
1093        templateIds.put(templateId, existingTemplate.getTemplateId());
1094
1095        if (!templateId.equals(existingTemplate.getTemplateId())) {
1096            if (_log.isWarnEnabled()) {
1097                _log.warn(
1098                    "A template with the ID " + templateId + " already " +
1099                        "exists. The new generated ID is " +
1100                            existingTemplate.getTemplateId());
1101            }
1102        }
1103    }
1104
1105    public PortletPreferences deleteData(
1106            PortletDataContext context, String portletId,
1107            PortletPreferences prefs)
1108        throws PortletDataException {
1109
1110        try {
1111            if (!context.addPrimaryKey(
1112                    JournalPortletDataHandlerImpl.class, "deleteData")) {
1113
1114                JournalArticleLocalServiceUtil.deleteArticles(
1115                    context.getGroupId());
1116
1117                JournalTemplateLocalServiceUtil.deleteTemplates(
1118                    context.getGroupId());
1119
1120                JournalStructureLocalServiceUtil.deleteStructures(
1121                    context.getGroupId());
1122            }
1123
1124            return prefs;
1125        }
1126        catch (Exception e) {
1127            throw new PortletDataException(e);
1128        }
1129    }
1130
1131    public String exportData(
1132            PortletDataContext context, String portletId,
1133            PortletPreferences prefs)
1134        throws PortletDataException {
1135
1136        try {
1137            Document doc = SAXReaderUtil.createDocument();
1138
1139            Element root = doc.addElement("journal-data");
1140
1141            root.addAttribute("group-id", String.valueOf(context.getGroupId()));
1142
1143            Element structuresEl = root.addElement("structures");
1144
1145            List<JournalStructure> structures =
1146                JournalStructureUtil.findByGroupId(context.getGroupId());
1147
1148            for (JournalStructure structure : structures) {
1149                exportStructure(context, structuresEl, structure);
1150            }
1151
1152            Element templatesEl = root.addElement("templates");
1153
1154            List<JournalTemplate> templates = JournalTemplateUtil.findByGroupId(
1155                context.getGroupId());
1156
1157            for (JournalTemplate template : templates) {
1158                exportTemplate(context, templatesEl, template);
1159            }
1160
1161            Element feedsEl = root.addElement("feeds");
1162
1163            List<JournalFeed> feeds = JournalFeedUtil.findByGroupId(
1164                context.getGroupId());
1165
1166            for (JournalFeed feed : feeds) {
1167                if (context.isWithinDateRange(feed.getModifiedDate())) {
1168                    exportFeed(context, feedsEl, feed);
1169                }
1170            }
1171
1172            Element articlesEl = root.addElement("articles");
1173            Element dlFoldersEl = root.addElement("dl-folders");
1174            Element dlFilesEl = root.addElement("dl-file-entries");
1175            Element dlFileRanksEl = root.addElement("dl-file-ranks");
1176            Element igFoldersEl = root.addElement("ig-folders");
1177            Element igImagesEl = root.addElement("ig-images");
1178
1179            if (context.getBooleanParameter(_NAMESPACE, "articles")) {
1180                List<JournalArticle> articles =
1181                    JournalArticleUtil.findByGroupId(context.getGroupId());
1182
1183                for (JournalArticle article : articles) {
1184                    if (context.isWithinDateRange(article.getModifiedDate())) {
1185                        exportArticle(
1186                            context, articlesEl, dlFoldersEl, dlFilesEl,
1187                            dlFileRanksEl, igFoldersEl, igImagesEl, article);
1188                    }
1189                }
1190            }
1191
1192            return doc.formattedString();
1193        }
1194        catch (Exception e) {
1195            throw new PortletDataException(e);
1196        }
1197    }
1198
1199    public PortletDataHandlerControl[] getExportControls() {
1200        return new PortletDataHandlerControl[] {
1201            _articles, _structuresTemplatesAndFeeds, _embeddedAssets, _images,
1202            _comments, _ratings, _tags
1203        };
1204    }
1205
1206    public PortletDataHandlerControl[] getImportControls() {
1207        return new PortletDataHandlerControl[] {
1208            _articles, _structuresTemplatesAndFeeds, _images, _comments,
1209            _ratings, _tags
1210        };
1211    }
1212
1213    public PortletPreferences importData(
1214            PortletDataContext context, String portletId,
1215            PortletPreferences prefs, String data)
1216        throws PortletDataException {
1217
1218        try {
1219            Document doc = SAXReaderUtil.read(data);
1220
1221            Element root = doc.getRootElement();
1222
1223            List<Element> structureEls = root.element("structures").elements(
1224                "structure");
1225
1226            Map<String, String> structureIds =
1227                (Map<String, String>)context.getNewPrimaryKeysMap(
1228                    JournalStructure.class);
1229
1230            for (Element structureEl : structureEls) {
1231                importStructure(context, structureIds, structureEl);
1232            }
1233
1234            List<Element> templateEls = root.element("templates").elements(
1235                "template");
1236
1237            Map<String, String> templateIds =
1238                (Map<String, String>)context.getNewPrimaryKeysMap(
1239                    JournalTemplate.class);
1240
1241            for (Element templateEl : templateEls) {
1242                importTemplate(context, structureIds, templateIds, templateEl);
1243            }
1244
1245            List<Element> feedEls = root.element("feeds").elements("feed");
1246
1247            Map<String, String> feedIds =
1248                (Map<String, String>)context.getNewPrimaryKeysMap(
1249                    JournalFeed.class);
1250
1251            for (Element feedEl : feedEls) {
1252                importFeed(context, structureIds, templateIds, feedIds, feedEl);
1253            }
1254
1255            if (context.getBooleanParameter(_NAMESPACE, "articles")) {
1256                List<Element> articleEls = root.element("articles").elements(
1257                    "article");
1258
1259                Map<String, String> articleIds =
1260                    (Map<String, String>)context.getNewPrimaryKeysMap(
1261                        JournalArticle.class);
1262
1263                for (Element articleEl : articleEls) {
1264                    importArticle(
1265                        context, structureIds, templateIds, articleIds,
1266                        articleEl);
1267                }
1268            }
1269
1270            List<Element> dlFolderEls = root.element("dl-folders").elements(
1271                "folder");
1272
1273            Map<Long, Long> dlFolderPKs =
1274                (Map<Long, Long>)context.getNewPrimaryKeysMap(DLFolder.class);
1275
1276            for (Element folderEl : dlFolderEls) {
1277                String path = folderEl.attributeValue("path");
1278
1279                if (!context.isPathNotProcessed(path)) {
1280                    continue;
1281                }
1282
1283                DLFolder folder = (DLFolder)context.getZipEntryAsObject(path);
1284
1285                DLPortletDataHandlerImpl.importFolder(
1286                    context, dlFolderPKs, folder);
1287            }
1288
1289            List<Element> fileEntryEls = root.element(
1290                "dl-file-entries").elements("file-entry");
1291
1292            Map<String, String> fileEntryNames =
1293                (Map<String, String>)context.getNewPrimaryKeysMap(
1294                    DLFileEntry.class);
1295
1296            for (Element fileEntryEl : fileEntryEls) {
1297                String path = fileEntryEl.attributeValue("path");
1298
1299                if (!context.isPathNotProcessed(path)) {
1300                    continue;
1301                }
1302
1303                DLFileEntry fileEntry =
1304                    (DLFileEntry)context.getZipEntryAsObject(path);
1305
1306                String binPath = fileEntryEl.attributeValue("bin-path");
1307
1308                DLPortletDataHandlerImpl.importFileEntry(
1309                    context, dlFolderPKs, fileEntryNames, fileEntry, binPath);
1310            }
1311
1312            List<Element> fileRankEls = root.element("dl-file-ranks").elements(
1313                "file-rank");
1314
1315            for (Element fileRankEl : fileRankEls) {
1316                String path = fileRankEl.attributeValue("path");
1317
1318                if (!context.isPathNotProcessed(path)) {
1319                    continue;
1320                }
1321
1322                DLFileRank fileRank = (DLFileRank)context.getZipEntryAsObject(
1323                    path);
1324
1325                DLPortletDataHandlerImpl.importFileRank(
1326                    context, dlFolderPKs, fileEntryNames, fileRank);
1327            }
1328
1329            List<Element> igFolderEls = root.element("ig-folders").elements(
1330                "folder");
1331
1332            Map<Long, Long> igFolderPKs =
1333                (Map<Long, Long>)context.getNewPrimaryKeysMap(IGFolder.class);
1334
1335            for (Element folderEl : igFolderEls) {
1336                String path = folderEl.attributeValue("path");
1337
1338                if (!context.isPathNotProcessed(path)) {
1339                    continue;
1340                }
1341
1342                IGFolder folder = (IGFolder)context.getZipEntryAsObject(path);
1343
1344                IGPortletDataHandlerImpl.importFolder(
1345                    context, igFolderPKs, folder);
1346            }
1347
1348            List<Element> imageEls = root.element("ig-images").elements(
1349                "image");
1350
1351            for (Element imageEl : imageEls) {
1352                String path = imageEl.attributeValue("path");
1353
1354                if (!context.isPathNotProcessed(path)) {
1355                    continue;
1356                }
1357
1358                IGImage image = (IGImage)context.getZipEntryAsObject(path);
1359
1360                String binPath = imageEl.attributeValue("bin-path");
1361
1362                IGPortletDataHandlerImpl.importImage(
1363                    context, igFolderPKs, image, binPath);
1364            }
1365
1366            return prefs;
1367        }
1368        catch (Exception e) {
1369            throw new PortletDataException(e);
1370        }
1371    }
1372
1373    public boolean isPublishToLiveByDefault() {
1374        return true;
1375    }
1376
1377    protected static String getArticlePath(
1378        PortletDataContext context, JournalArticle article) {
1379
1380        StringBuilder sb = new StringBuilder();
1381
1382        sb.append(context.getPortletPath(PortletKeys.JOURNAL));
1383        sb.append("/articles/");
1384        sb.append(article.getArticleId());
1385        sb.append(StringPool.SLASH);
1386        sb.append(article.getVersion());
1387        sb.append(StringPool.SLASH);
1388        sb.append(article.getArticleId());
1389        sb.append(".xml");
1390
1391        return sb.toString();
1392    }
1393
1394    protected static String getArticleImagePath(
1395        PortletDataContext context, JournalArticle article) {
1396
1397        StringBuilder sb = new StringBuilder();
1398
1399        sb.append(context.getPortletPath(PortletKeys.JOURNAL));
1400        sb.append("/articles/");
1401        sb.append(article.getArticleId());
1402        sb.append(StringPool.SLASH);
1403        sb.append(article.getVersion());
1404        sb.append(StringPool.SLASH);
1405
1406        return sb.toString();
1407    }
1408
1409    protected static String getArticleImagePath(
1410        PortletDataContext context, JournalArticle article,
1411        JournalArticleImage articleImage, Image image) {
1412
1413        StringBuilder sb = new StringBuilder();
1414
1415        sb.append(context.getPortletPath(PortletKeys.JOURNAL));
1416        sb.append("/articles/");
1417        sb.append(article.getArticleId());
1418        sb.append(StringPool.SLASH);
1419        sb.append(article.getVersion());
1420        sb.append(StringPool.SLASH);
1421        sb.append(articleImage.getElName());
1422        sb.append(articleImage.getLanguageId());
1423        sb.append(StringPool.PERIOD);
1424        sb.append(image.getType());
1425
1426        return sb.toString();
1427    }
1428
1429    protected static String getArticleSmallImagePath(
1430            PortletDataContext context, JournalArticle article)
1431        throws PortalException, SystemException {
1432
1433        StringBuilder sb = new StringBuilder();
1434
1435        sb.append(context.getPortletPath(PortletKeys.JOURNAL));
1436        sb.append("/articles/thumbnail.");
1437        sb.append(article.getSmallImageType());
1438
1439        return sb.toString();
1440    }
1441
1442    protected static String getFeedPath(
1443        PortletDataContext context, JournalFeed feed) {
1444
1445        StringBuilder sb = new StringBuilder();
1446
1447        sb.append(context.getPortletPath(PortletKeys.JOURNAL));
1448        sb.append("/feeds/");
1449        sb.append(feed.getFeedId());
1450        sb.append(".xml");
1451
1452        return sb.toString();
1453    }
1454
1455    protected static String getTemplatePath(
1456        PortletDataContext context, JournalTemplate template) {
1457
1458        StringBuilder sb = new StringBuilder();
1459
1460        sb.append(context.getPortletPath(PortletKeys.JOURNAL));
1461        sb.append("/templates/");
1462        sb.append(template.getTemplateId());
1463        sb.append(".xml");
1464
1465        return sb.toString();
1466    }
1467
1468    protected static String getTemplateSmallImagePath(
1469            PortletDataContext context, JournalTemplate template)
1470        throws PortalException, SystemException {
1471
1472        StringBuilder sb = new StringBuilder();
1473
1474        sb.append(context.getPortletPath(PortletKeys.JOURNAL));
1475        sb.append("/templates/thumbnail-");
1476        sb.append(template.getTemplateId());
1477        sb.append(template.getSmallImageType());
1478
1479        return sb.toString();
1480    }
1481
1482    protected static String getStructurePath(
1483        PortletDataContext context, JournalStructure structure) {
1484
1485        StringBuilder sb = new StringBuilder();
1486
1487        sb.append(context.getPortletPath(PortletKeys.JOURNAL));
1488        sb.append("/structures/");
1489        sb.append(structure.getStructureId());
1490        sb.append(".xml");
1491
1492        return sb.toString();
1493    }
1494
1495    private static final String _NAMESPACE = "journal";
1496
1497    private static final PortletDataHandlerBoolean _embeddedAssets =
1498        new PortletDataHandlerBoolean(_NAMESPACE, "embedded-assets");
1499
1500    private static final PortletDataHandlerBoolean _images =
1501        new PortletDataHandlerBoolean(_NAMESPACE, "images");
1502
1503    private static final PortletDataHandlerBoolean _comments =
1504        new PortletDataHandlerBoolean(_NAMESPACE, "comments");
1505
1506    private static final PortletDataHandlerBoolean _ratings =
1507        new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
1508
1509    private static final PortletDataHandlerBoolean _tags =
1510        new PortletDataHandlerBoolean(_NAMESPACE, "tags");
1511
1512    private static final PortletDataHandlerBoolean _articles =
1513        new PortletDataHandlerBoolean(_NAMESPACE, "articles", true, false,
1514        new PortletDataHandlerControl[] {_images, _comments, _ratings, _tags});
1515
1516    private static final PortletDataHandlerBoolean
1517        _structuresTemplatesAndFeeds = new PortletDataHandlerBoolean(
1518            _NAMESPACE, "structures-templates-and-feeds", true, true);
1519
1520    private static Log _log =
1521        LogFactory.getLog(JournalPortletDataHandlerImpl.class);
1522
1523}