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.lar.PortletDataContext;
29  import com.liferay.portal.kernel.lar.PortletDataException;
30  import com.liferay.portal.kernel.lar.PortletDataHandler;
31  import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
32  import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
33  import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
34  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
35  import com.liferay.portal.kernel.util.ObjectValuePair;
36  import com.liferay.portal.kernel.util.StringPool;
37  import com.liferay.portal.kernel.util.Validator;
38  import com.liferay.portal.model.Image;
39  import com.liferay.portal.service.persistence.ImageUtil;
40  import com.liferay.portal.util.PortalUtil;
41  import com.liferay.portlet.journal.model.JournalArticle;
42  import com.liferay.portlet.journal.model.JournalArticleImage;
43  import com.liferay.portlet.journal.model.JournalStructure;
44  import com.liferay.portlet.journal.model.JournalTemplate;
45  import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
46  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
47  import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
48  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
49  import com.liferay.portlet.journal.service.persistence.JournalArticleImageUtil;
50  import com.liferay.portlet.journal.service.persistence.JournalArticleUtil;
51  import com.liferay.portlet.journal.service.persistence.JournalStructureUtil;
52  import com.liferay.portlet.journal.service.persistence.JournalTemplateUtil;
53  import com.liferay.util.CollectionFactory;
54  import com.liferay.util.FileUtil;
55  import com.liferay.util.MapUtil;
56  
57  import com.thoughtworks.xstream.XStream;
58  
59  import java.io.File;
60  import java.io.IOException;
61  
62  import java.util.Calendar;
63  import java.util.Date;
64  import java.util.Iterator;
65  import java.util.List;
66  import java.util.Map;
67  
68  import javax.portlet.PortletPreferences;
69  
70  import org.apache.commons.logging.Log;
71  import org.apache.commons.logging.LogFactory;
72  
73  import org.dom4j.Document;
74  import org.dom4j.DocumentHelper;
75  import org.dom4j.Element;
76  
77  /**
78   * <a href="JournalPortletDataHandlerImpl.java.html"><b><i>View Source</i></b>
79   * </a>
80   *
81   * @author Raymond Augé
82   * @author Joel Kozikowski
83   * @author Brian Wing Shun Chan
84   * @author Bruno Farache
85   *
86   */
87  public class JournalPortletDataHandlerImpl implements PortletDataHandler {
88  
89      public PortletPreferences deleteData(
90              PortletDataContext context, String portletId,
91              PortletPreferences prefs)
92          throws PortletDataException {
93  
94          try {
95              if (!context.addPrimaryKey(
96                      JournalPortletDataHandlerImpl.class, "deleteData")) {
97  
98                  List articles = JournalArticleUtil.findByGroupId(
99                      context.getGroupId());
100 
101                 Iterator itr = articles.iterator();
102 
103                 while (itr.hasNext()) {
104                     JournalArticle article = (JournalArticle)itr.next();
105 
106                     // Templates
107 
108                     JournalTemplateLocalServiceUtil.deleteTemplate(
109                         context.getGroupId(), article.getTemplateId());
110 
111                     // Structures
112 
113                     JournalStructureLocalServiceUtil.deleteStructure(
114                         context.getGroupId(), article.getStructureId());
115                 }
116 
117                 // Articles
118 
119                 JournalArticleLocalServiceUtil.deleteArticles(
120                     context.getGroupId());
121             }
122 
123             return null;
124         }
125         catch (Exception e) {
126             throw new PortletDataException(e);
127         }
128     }
129 
130     public String exportData(
131             PortletDataContext context, String portletId,
132             PortletPreferences prefs)
133         throws PortletDataException {
134 
135         try {
136             XStream xStream = new XStream();
137 
138             Document doc = DocumentHelper.createDocument();
139 
140             Element root = doc.addElement("journal-data");
141 
142             root.addAttribute("group-id", String.valueOf(context.getGroupId()));
143 
144             // Structures
145 
146             List obj = JournalStructureUtil.findByGroupId(context.getGroupId());
147 
148             Iterator itr = obj.iterator();
149 
150             while (itr.hasNext()) {
151                 JournalStructure structure = (JournalStructure)itr.next();
152 
153                 if (context.addPrimaryKey(
154                         JournalStructure.class, structure.getPrimaryKeyObj())) {
155 
156                     itr.remove();
157                 }
158                 else {
159                     exportStructure(structure);
160                 }
161             }
162 
163             String xml = xStream.toXML(obj);
164 
165             Document tempDoc = PortalUtil.readDocumentFromXML(xml);
166 
167             Element el = root.addElement("journal-structures");
168 
169             el.content().add(tempDoc.getRootElement().createCopy());
170 
171             // Templates
172 
173             obj = JournalTemplateUtil.findByGroupId(context.getGroupId());
174 
175             itr = obj.iterator();
176 
177             while (itr.hasNext()) {
178                 JournalTemplate template = (JournalTemplate)itr.next();
179 
180                 if (context.addPrimaryKey(
181                         JournalTemplate.class, template.getPrimaryKeyObj())) {
182 
183                     itr.remove();
184                 }
185                 else {
186                     exportTemplate(context, template);
187                 }
188             }
189 
190             xml = xStream.toXML(obj);
191 
192             el = root.addElement("journal-templates");
193 
194             tempDoc = PortalUtil.readDocumentFromXML(xml);
195 
196             el.content().add(tempDoc.getRootElement().createCopy());
197 
198             // Articles
199 
200             obj = JournalArticleUtil.findByGroupId(context.getGroupId());
201 
202             itr = obj.iterator();
203 
204             while (itr.hasNext()) {
205                 JournalArticle article = (JournalArticle)itr.next();
206 
207                 if (context.addPrimaryKey(
208                         JournalArticle.class, article.getPrimaryKeyObj())) {
209 
210                     itr.remove();
211                 }
212                 else {
213                     exportArticle(context, article);
214                 }
215             }
216 
217             xml = xStream.toXML(obj);
218 
219             el = root.addElement("journal-articles");
220 
221             tempDoc = PortalUtil.readDocumentFromXML(xml);
222 
223             el.content().add(tempDoc.getRootElement().createCopy());
224 
225             return doc.asXML();
226         }
227         catch (Exception e) {
228             throw new PortletDataException(e);
229         }
230     }
231 
232     public PortletDataHandlerControl[] getExportControls()
233         throws PortletDataException {
234 
235         return new PortletDataHandlerControl[] {
236             _articlesStructuresAndTemplates, _images, _comments, _ratings, _tags
237         };
238     }
239 
240     public PortletDataHandlerControl[] getImportControls()
241         throws PortletDataException {
242 
243         return new PortletDataHandlerControl[] {
244             _articlesStructuresAndTemplates, _images, _comments, _ratings, _tags
245         };
246     }
247 
248     public PortletPreferences importData(
249             PortletDataContext context, String portletId,
250             PortletPreferences prefs, String data)
251         throws PortletDataException {
252 
253         try {
254             XStream xStream = new XStream();
255 
256             Document doc = PortalUtil.readDocumentFromXML(data);
257 
258             Element root = doc.getRootElement();
259 
260             // Structures
261 
262             Element el = root.element("journal-structures").element("list");
263 
264             Document tempDoc = DocumentHelper.createDocument();
265 
266             tempDoc.content().add(el.createCopy());
267 
268             Map structurePKs = CollectionFactory.getHashMap();
269 
270             List structures = (List)xStream.fromXML(tempDoc.asXML());
271 
272             Iterator itr = structures.iterator();
273 
274             while (itr.hasNext()) {
275                 JournalStructure structure = (JournalStructure)itr.next();
276 
277                 importStructure(context, structurePKs,  structure);
278             }
279 
280             // Templates
281 
282             el = root.element("journal-templates").element("list");
283 
284             tempDoc = DocumentHelper.createDocument();
285 
286             tempDoc.content().add(el.createCopy());
287 
288             Map templatePKs = CollectionFactory.getHashMap();
289 
290             List templates = (List)xStream.fromXML(tempDoc.asXML());
291 
292             itr = templates.iterator();
293 
294             while (itr.hasNext()) {
295                 JournalTemplate template = (JournalTemplate)itr.next();
296 
297                 importTemplate(context, structurePKs, templatePKs, template);
298             }
299 
300             // Articles
301 
302             el = root.element("journal-articles").element("list");
303 
304             tempDoc = DocumentHelper.createDocument();
305 
306             tempDoc.content().add(el.createCopy());
307 
308             List articles = (List)xStream.fromXML(tempDoc.asXML());
309 
310             itr = articles.iterator();
311 
312             while (itr.hasNext()) {
313                 JournalArticle article = (JournalArticle)itr.next();
314 
315                 importArticle(context, structurePKs, templatePKs, article);
316             }
317 
318             return null;
319         }
320         catch (Exception e) {
321             throw new PortletDataException(e);
322         }
323     }
324 
325     protected static void exportArticle(
326             PortletDataContext context, JournalArticle article)
327         throws IOException, PortalException, SystemException {
328 
329         article.setUserUuid(article.getUserUuid());
330         article.setApprovedByUserUuid(article.getApprovedByUserUuid());
331 
332         if (article.isSmallImage()) {
333             Image smallImage = ImageUtil.fetchByPrimaryKey(
334                 article.getSmallImageId());
335 
336             article.setSmallImageType(smallImage.getType());
337 
338             context.getZipWriter().addEntry(
339                 getSmallImageDir(article), smallImage.getTextObj());
340         }
341 
342         if (context.getBooleanParameter(_NAMESPACE, "images")) {
343             List articleImages = JournalArticleImageUtil.findByG_A_V(
344                 context.getGroupId(), article.getArticleId(),
345                 article.getVersion());
346 
347             Iterator itr = articleImages.iterator();
348 
349             while (itr.hasNext()) {
350                 JournalArticleImage articleImage =
351                     (JournalArticleImage)itr.next();
352 
353                 try {
354                     Image image = ImageUtil.findByPrimaryKey(
355                         articleImage.getArticleImageId());
356 
357                     String fileName =
358                         articleImage.getElName() +
359                             articleImage.getLanguageId() + "." +
360                                 image.getType();
361 
362                     context.getZipWriter().addEntry(
363                         getArticleImageDir(article) + fileName,
364                         image.getTextObj());
365                 }
366                 catch (NoSuchImageException nsie) {
367                 }
368             }
369         }
370 
371         if (context.getBooleanParameter(_NAMESPACE, "comments")) {
372             context.addComments(
373                 JournalArticle.class, new Long(article.getResourcePrimKey()));
374         }
375 
376         if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
377             context.addRatingsEntries(
378                 JournalArticle.class, new Long(article.getResourcePrimKey()));
379         }
380 
381         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
382             context.addTagsEntries(
383                 JournalArticle.class, new Long(article.getResourcePrimKey()));
384         }
385     }
386 
387     protected static void exportStructure(JournalStructure structure)
388         throws SystemException {
389 
390         structure.setUserUuid(structure.getUserUuid());
391     }
392 
393     protected static void exportTemplate(
394             PortletDataContext context, JournalTemplate template)
395         throws IOException, PortalException, SystemException {
396 
397         template.setUserUuid(template.getUserUuid());
398 
399         if (template.isSmallImage()) {
400             Image smallImage = ImageUtil.fetchByPrimaryKey(
401                 template.getSmallImageId());
402 
403             template.setSmallImageType(smallImage.getType());
404 
405             context.getZipWriter().addEntry(
406                 getSmallImageDir(template), smallImage.getTextObj());
407         }
408     }
409 
410     protected static String getArticleImageDir(JournalArticle article) {
411         return _ARTICLE_IMAGES_FOLDER + article.getArticleId() + "/" +
412             article.getVersion() + "/";
413     }
414 
415     protected static String getSmallImageDir(JournalArticle article)
416         throws PortalException, SystemException {
417 
418         return _ARTICLE_SMALL_IMAGES_FOLDER + article.getSmallImageId() + "." +
419             article.getSmallImageType();
420     }
421 
422     protected static String getSmallImageDir(JournalTemplate template)
423         throws PortalException, SystemException {
424 
425         return _TEMPLATE_SMALL_IMAGES_FOLDER + template.getSmallImageId() +
426             "." + template.getSmallImageType();
427     }
428 
429     protected static JournalArticle importArticle(
430             PortletDataContext context, Map structurePKs, Map templatePKs,
431             JournalArticle article)
432         throws Exception {
433 
434         long userId = context.getUserId(article.getUserUuid());
435         long plid = context.getPlid();
436 
437         String articleId = article.getArticleId();
438         boolean autoArticleId = false;
439 
440         if ((Validator.isNumber(articleId)) ||
441             (JournalArticleUtil.fetchByG_A_V(
442                 context.getGroupId(), articleId,
443                     JournalArticleImpl.DEFAULT_VERSION) != null)) {
444 
445             autoArticleId = true;
446         }
447 
448         String parentStructureId = MapUtil.getString(
449             structurePKs, article.getStructureId(), article.getStructureId());
450         String parentTemplateId = MapUtil.getString(
451             templatePKs, article.getTemplateId(), article.getTemplateId());
452 
453         Date displayDate = article.getDisplayDate();
454 
455         int displayDateMonth = 0;
456         int displayDateDay = 0;
457         int displayDateYear = 0;
458         int displayDateHour = 0;
459         int displayDateMinute = 0;
460 
461         if (displayDate != null) {
462             Calendar displayCal = CalendarFactoryUtil.getCalendar();
463 
464             displayCal.setTime(displayDate);
465 
466             displayDateMonth = displayCal.get(Calendar.MONTH);
467             displayDateDay = displayCal.get(Calendar.DATE);
468             displayDateYear = displayCal.get(Calendar.YEAR);
469             displayDateHour = displayCal.get(Calendar.HOUR);
470             displayDateMinute = displayCal.get(Calendar.MINUTE);
471         }
472 
473         Date expirationDate = article.getExpirationDate();
474 
475         int expirationDateMonth = 0;
476         int expirationDateDay = 0;
477         int expirationDateYear = 0;
478         int expirationDateHour = 0;
479         int expirationDateMinute = 0;
480         boolean neverExpire = true;
481 
482         if (expirationDate != null) {
483             Calendar expirationCal = CalendarFactoryUtil.getCalendar();
484 
485             expirationCal.setTime(expirationDate);
486 
487             expirationDateMonth = expirationCal.get(Calendar.MONTH);
488             expirationDateDay = expirationCal.get(Calendar.DATE);
489             expirationDateYear = expirationCal.get(Calendar.YEAR);
490             expirationDateHour = expirationCal.get(Calendar.HOUR);
491             expirationDateMinute = expirationCal.get(Calendar.MINUTE);
492             neverExpire = false;
493         }
494 
495         Date reviewDate = article.getReviewDate();
496 
497         int reviewDateMonth = 0;
498         int reviewDateDay = 0;
499         int reviewDateYear = 0;
500         int reviewDateHour = 0;
501         int reviewDateMinute = 0;
502         boolean neverReview = true;
503 
504         if (reviewDate != null) {
505             Calendar reviewCal = CalendarFactoryUtil.getCalendar();
506 
507             reviewCal.setTime(reviewDate);
508 
509             reviewDateMonth = reviewCal.get(Calendar.MONTH);
510             reviewDateDay = reviewCal.get(Calendar.DATE);
511             reviewDateYear = reviewCal.get(Calendar.YEAR);
512             reviewDateHour = reviewCal.get(Calendar.HOUR);
513             reviewDateMinute = reviewCal.get(Calendar.MINUTE);
514             neverReview = false;
515         }
516 
517         File smallFile = null;
518 
519         if (article.isSmallImage()) {
520             byte[] byteArray = context.getZipReader().getEntryAsByteArray(
521                 getSmallImageDir(article));
522 
523             smallFile = File.createTempFile(
524                 String.valueOf(article.getSmallImageId()),
525                 StringPool.PERIOD + article.getSmallImageType());
526 
527             FileUtil.write(smallFile, byteArray);
528         }
529 
530         Map images = CollectionFactory.getHashMap();
531 
532         if (context.getBooleanParameter(_NAMESPACE, "images")) {
533             List imageFiles =
534                 (List)context.getZipReader().getFolderEntries().get(
535                     getArticleImageDir(article));
536 
537             if (imageFiles != null && imageFiles.size() > 0) {
538                 Iterator itr = imageFiles.iterator();
539 
540                 while (itr.hasNext()) {
541                     ObjectValuePair imageFile = (ObjectValuePair)itr.next();
542 
543                     String fileName = (String)imageFile.getKey();
544 
545                     int pos = fileName.lastIndexOf(".");
546 
547                     if (pos != -1) {
548                         fileName = fileName.substring(0, pos);
549                     }
550 
551                     images.put(fileName, imageFile.getValue());
552                 }
553             }
554         }
555 
556         String articleURL = null;
557 
558         PortletPreferences prefs = null;
559 
560         String[] tagsEntries = null;
561 
562         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
563             tagsEntries = context.getTagsEntries(
564                 JournalArticle.class, new Long(article.getResourcePrimKey()));
565         }
566 
567         boolean addCommunityPermissions = true;
568         boolean addGuestPermissions = true;
569         boolean incrementVersion = false;
570 
571         JournalArticle existingArticle = null;
572 
573         if (context.getDataStrategy().equals(
574                 PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
575 
576             existingArticle = JournalArticleUtil.fetchByUUID_G(
577                 article.getUuid(), context.getGroupId());
578 
579             if (existingArticle == null) {
580                 existingArticle =  JournalArticleLocalServiceUtil.addArticle(
581                     article.getUuid(), userId, articleId, autoArticleId, plid,
582                     article.getTitle(), article.getDescription(),
583                     article.getContent(), article.getType(), parentStructureId,
584                     parentTemplateId, displayDateMonth, displayDateDay,
585                     displayDateYear, displayDateHour, displayDateMinute,
586                     expirationDateMonth, expirationDateDay, expirationDateYear,
587                     expirationDateHour, expirationDateMinute, neverExpire,
588                     reviewDateMonth, reviewDateDay, reviewDateYear,
589                     reviewDateHour, reviewDateMinute, neverReview,
590                     article.getIndexable(), article.getSmallImage(),
591                     article.getSmallImageURL(), smallFile, images, articleURL,
592                     prefs, tagsEntries, addCommunityPermissions,
593                     addGuestPermissions);
594             }
595             else {
596                 existingArticle =  JournalArticleLocalServiceUtil.updateArticle(
597                     userId, existingArticle.getGroupId(),
598                     existingArticle.getArticleId(),
599                     existingArticle.getVersion(), incrementVersion,
600                     article.getTitle(), article.getDescription(),
601                     article.getContent(), article.getType(),
602                     existingArticle.getStructureId(),
603                     existingArticle.getTemplateId(), displayDateMonth,
604                     displayDateDay, displayDateYear, displayDateHour,
605                     displayDateMinute, expirationDateMonth,
606                     expirationDateDay, expirationDateYear, expirationDateHour,
607                     expirationDateMinute, neverExpire, reviewDateMonth,
608                     reviewDateDay, reviewDateYear, reviewDateHour,
609                     reviewDateMinute, neverReview, article.getIndexable(),
610                     article.getSmallImage(), article.getSmallImageURL(),
611                     smallFile, images, articleURL, prefs, tagsEntries);
612             }
613         }
614         else {
615             existingArticle = JournalArticleLocalServiceUtil.addArticle(
616                 userId, articleId, autoArticleId, plid, article.getTitle(),
617                 article.getDescription(), article.getContent(),
618                 article.getType(), parentStructureId, parentTemplateId,
619                 displayDateMonth, displayDateDay, displayDateYear,
620                 displayDateHour, displayDateMinute, expirationDateMonth,
621                 expirationDateDay, expirationDateYear, expirationDateHour,
622                 expirationDateMinute, neverExpire, reviewDateMonth,
623                 reviewDateDay, reviewDateYear, reviewDateHour, reviewDateMinute,
624                 neverReview, article.getIndexable(), article.getSmallImage(),
625                 article.getSmallImageURL(), smallFile, images, articleURL,
626                 prefs, tagsEntries, addCommunityPermissions,
627                 addGuestPermissions);
628         }
629 
630         if (article.isApproved() && !existingArticle.isApproved()) {
631             long approvedByUserId = context.getUserId(
632                 article.getApprovedByUserUuid());
633 
634             JournalArticleLocalServiceUtil.approveArticle(
635                 approvedByUserId, context.getGroupId(),
636                 existingArticle.getArticleId(), existingArticle.getVersion(),
637                 articleURL, prefs);
638         }
639 
640         if (context.getBooleanParameter(_NAMESPACE, "comments")) {
641             context.importComments(
642                 JournalArticle.class, new Long(article.getResourcePrimKey()),
643                 new Long(existingArticle.getResourcePrimKey()),
644                 context.getGroupId());
645         }
646 
647         if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
648             context.importRatingsEntries(
649                 JournalArticle.class, new Long(article.getResourcePrimKey()),
650                 new Long(existingArticle.getResourcePrimKey()));
651         }
652 
653         if (!articleId.equals(existingArticle.getArticleId())) {
654             if (_log.isWarnEnabled()) {
655                 _log.warn(
656                     "An article with the ID " + articleId + " already " +
657                         "exists. The new generated ID is " +
658                             existingArticle.getArticleId());
659             }
660         }
661 
662         return existingArticle;
663     }
664 
665     protected static void importStructure(
666             PortletDataContext context, Map structurePKs,
667             JournalStructure structure)
668         throws Exception {
669 
670         long userId = context.getUserId(structure.getUserUuid());
671         long plid = context.getPlid();
672 
673         String structureId = structure.getStructureId();
674         boolean autoStructureId = false;
675 
676         if ((Validator.isNumber(structureId)) ||
677             (JournalStructureUtil.fetchByG_S(
678                 context.getGroupId(), structureId) != null)) {
679 
680             autoStructureId = true;
681         }
682 
683         boolean addCommunityPermissions = true;
684         boolean addGuestPermissions = true;
685 
686         JournalStructure existingStructure = null;
687 
688         if (context.getDataStrategy().equals(
689                 PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
690 
691             existingStructure = JournalStructureUtil.fetchByUUID_G(
692                 structure.getUuid(), context.getGroupId());
693 
694             if (existingStructure == null) {
695                 existingStructure =
696                     JournalStructureLocalServiceUtil.addStructure(
697                         structure.getUuid(), userId, structureId,
698                         autoStructureId, plid, structure.getName(),
699                         structure.getDescription(), structure.getXsd(),
700                         addCommunityPermissions, addGuestPermissions);
701             }
702             else {
703                 existingStructure =
704                     JournalStructureLocalServiceUtil.updateStructure(
705                         existingStructure.getGroupId(),
706                         existingStructure.getStructureId(), structure.getName(),
707                         structure.getDescription(), structure.getXsd());
708             }
709         }
710         else {
711             existingStructure =
712                 JournalStructureLocalServiceUtil.addStructure(
713                     userId, structureId, autoStructureId, plid,
714                     structure.getName(), structure.getDescription(),
715                     structure.getXsd(), addCommunityPermissions,
716                     addGuestPermissions);
717         }
718 
719         structurePKs.put(
720             structure.getStructureId(), existingStructure.getStructureId());
721 
722         if (!structureId.equals(existingStructure.getStructureId())) {
723             if (_log.isWarnEnabled()) {
724                 _log.warn(
725                     "A structure with the ID " + structureId + " already " +
726                         "exists. The new generated ID is " +
727                             existingStructure.getStructureId());
728             }
729         }
730     }
731 
732     protected static void importTemplate(
733             PortletDataContext context, Map structurePKs, Map templatePKs,
734             JournalTemplate template)
735         throws Exception {
736 
737         long userId = context.getUserId(template.getUserUuid());
738         long plid = context.getPlid();
739 
740         String templateId = template.getTemplateId();
741         boolean autoTemplateId = false;
742 
743         if ((Validator.isNumber(templateId)) ||
744             (JournalTemplateUtil.fetchByG_T(
745                 context.getGroupId(), templateId) != null)) {
746 
747             autoTemplateId = true;
748         }
749 
750         String parentStructureId = MapUtil.getString(
751             structurePKs, template.getStructureId(), template.getStructureId());
752 
753         boolean formatXsl = false;
754 
755         boolean addCommunityPermissions = true;
756         boolean addGuestPermissions = true;
757 
758         File smallFile = null;
759 
760         if (template.isSmallImage()) {
761             byte[] byteArray = context.getZipReader().getEntryAsByteArray(
762                 getSmallImageDir(template));
763 
764             smallFile = File.createTempFile(
765                 String.valueOf(template.getSmallImageId()),
766                 StringPool.PERIOD + template.getSmallImageType());
767 
768             FileUtil.write(smallFile, byteArray);
769         }
770 
771         JournalTemplate existingTemplate = null;
772 
773         if (context.getDataStrategy().equals(
774                 PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
775 
776             existingTemplate = JournalTemplateUtil.fetchByUUID_G(
777                 template.getUuid(), context.getGroupId());
778 
779             if (existingTemplate == null) {
780                 existingTemplate =
781                     JournalTemplateLocalServiceUtil.addTemplate(
782                         template.getUuid(), userId, templateId, autoTemplateId,
783                         plid, parentStructureId, template.getName(),
784                         template.getDescription(), template.getXsl(), formatXsl,
785                         template.getLangType(), template.getCacheable(),
786                         template.isSmallImage(), template.getSmallImageURL(),
787                         smallFile, addCommunityPermissions,
788                         addGuestPermissions);
789             }
790             else {
791                 existingTemplate =
792                     JournalTemplateLocalServiceUtil.updateTemplate(
793                         existingTemplate.getGroupId(),
794                         existingTemplate.getTemplateId(),
795                         existingTemplate.getStructureId(), template.getName(),
796                         template.getDescription(), template.getXsl(), formatXsl,
797                         template.getLangType(), template.getCacheable(),
798                         template.isSmallImage(), template.getSmallImageURL(),
799                         smallFile);
800             }
801         }
802         else {
803             existingTemplate =
804                 JournalTemplateLocalServiceUtil.addTemplate(
805                     userId, templateId, autoTemplateId, plid, parentStructureId,
806                     template.getName(), template.getDescription(),
807                     template.getXsl(), formatXsl, template.getLangType(),
808                     template.getCacheable(), template.isSmallImage(),
809                     template.getSmallImageURL(), smallFile,
810                     addCommunityPermissions, addGuestPermissions);
811         }
812 
813         templatePKs.put(
814             template.getTemplateId(), existingTemplate.getTemplateId());
815 
816         if (!templateId.equals(existingTemplate.getTemplateId())) {
817             if (_log.isWarnEnabled()) {
818                 _log.warn(
819                     "A template with the ID " + templateId + " already " +
820                         "exists. The new generated ID is " +
821                             existingTemplate.getTemplateId());
822             }
823         }
824     }
825 
826     private static final String _NAMESPACE = "journal";
827 
828     private static final String _ARTICLE_IMAGES_FOLDER =
829         "article-images/";
830 
831     private static final String _ARTICLE_SMALL_IMAGES_FOLDER =
832         "article-thumbnails/";
833 
834     private static final String _TEMPLATE_SMALL_IMAGES_FOLDER =
835         "template-thumbnails/";
836 
837     private static final PortletDataHandlerBoolean
838         _articlesStructuresAndTemplates = new PortletDataHandlerBoolean(
839             _NAMESPACE, "articles-structures-and-templates", true, true);
840 
841     private static final PortletDataHandlerBoolean _images =
842         new PortletDataHandlerBoolean(_NAMESPACE, "images");
843 
844     private static final PortletDataHandlerBoolean _comments =
845         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
846 
847     private static final PortletDataHandlerBoolean _ratings =
848         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
849 
850     private static final PortletDataHandlerBoolean _tags =
851         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
852 
853     private static Log _log =
854         LogFactory.getLog(JournalPortletDataHandlerImpl.class);
855 
856 }