001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.journal.lar;
022:
023: import com.liferay.portal.NoSuchImageException;
024: import com.liferay.portal.PortalException;
025: import com.liferay.portal.SystemException;
026: import com.liferay.portal.kernel.lar.PortletDataContext;
027: import com.liferay.portal.kernel.lar.PortletDataException;
028: import com.liferay.portal.kernel.lar.PortletDataHandler;
029: import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
030: import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
031: import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
032: import com.liferay.portal.kernel.util.CalendarFactoryUtil;
033: import com.liferay.portal.kernel.util.ObjectValuePair;
034: import com.liferay.portal.kernel.util.StringPool;
035: import com.liferay.portal.kernel.util.Validator;
036: import com.liferay.portal.model.Image;
037: import com.liferay.portal.service.persistence.ImageUtil;
038: import com.liferay.portal.util.PortalUtil;
039: import com.liferay.portlet.journal.model.JournalArticle;
040: import com.liferay.portlet.journal.model.JournalArticleImage;
041: import com.liferay.portlet.journal.model.JournalStructure;
042: import com.liferay.portlet.journal.model.JournalTemplate;
043: import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
044: import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
045: import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
046: import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
047: import com.liferay.portlet.journal.service.persistence.JournalArticleImageUtil;
048: import com.liferay.portlet.journal.service.persistence.JournalArticleUtil;
049: import com.liferay.portlet.journal.service.persistence.JournalStructureUtil;
050: import com.liferay.portlet.journal.service.persistence.JournalTemplateUtil;
051: import com.liferay.util.CollectionFactory;
052: import com.liferay.util.FileUtil;
053: import com.liferay.util.MapUtil;
054:
055: import com.thoughtworks.xstream.XStream;
056:
057: import java.io.File;
058: import java.io.IOException;
059:
060: import java.util.Calendar;
061: import java.util.Date;
062: import java.util.Iterator;
063: import java.util.List;
064: import java.util.Map;
065:
066: import javax.portlet.PortletPreferences;
067:
068: import org.apache.commons.logging.Log;
069: import org.apache.commons.logging.LogFactory;
070:
071: import org.dom4j.Document;
072: import org.dom4j.DocumentHelper;
073: import org.dom4j.Element;
074:
075: /**
076: * <a href="JournalPortletDataHandlerImpl.java.html"><b><i>View Source</i></b>
077: * </a>
078: *
079: * <p>
080: * Provides the Journal portlet export and import functionality, which is to
081: * clone all articles, structures, and templates associated with the layout's
082: * group. Upon import, new instances of the corresponding articles, structures,
083: * and templates are created or updated according to the DATA_MIRROW strategy
084: * The author of the newly created objects are determined by the
085: * JournalCreationStrategy class defined in <i>portal.properties</i>. That
086: * strategy also allows the text of the journal article to be modified prior
087: * to import.
088: * </p>
089: *
090: * <p>
091: * This <code>PortletDataHandler</code> differs from
092: * <code>JournalContentPortletDataHandlerImpl</code> in that it exports all
093: * articles owned by the group whether or not they are actually displayed in a
094: * portlet in the layout set.
095: * </p>
096: *
097: * @author Raymond Augé
098: * @author Joel Kozikowski
099: * @author Brian Wing Shun Chan
100: * @author Bruno Farache
101: *
102: * @see com.liferay.portal.kernel.lar.PortletDataHandler
103: * @see com.liferay.portlet.journal.lar.JournalContentPortletDataHandlerImpl
104: * @see com.liferay.portlet.journal.lar.JournalCreationStrategy
105: *
106: */
107: public class JournalPortletDataHandlerImpl implements
108: PortletDataHandler {
109:
110: public PortletPreferences deleteData(PortletDataContext context,
111: String portletId, PortletPreferences prefs)
112: throws PortletDataException {
113:
114: try {
115: if (!context.addPrimaryKey(
116: JournalPortletDataHandlerImpl.class, "deleteData")) {
117:
118: List articles = JournalArticleUtil
119: .findByGroupId(context.getGroupId());
120:
121: Iterator itr = articles.iterator();
122:
123: while (itr.hasNext()) {
124: JournalArticle article = (JournalArticle) itr
125: .next();
126:
127: // Templates
128:
129: JournalTemplateLocalServiceUtil.deleteTemplate(
130: context.getGroupId(), article
131: .getTemplateId());
132:
133: // Structures
134:
135: JournalStructureLocalServiceUtil.deleteStructure(
136: context.getGroupId(), article
137: .getStructureId());
138: }
139:
140: // Articles
141:
142: JournalArticleLocalServiceUtil.deleteArticles(context
143: .getGroupId());
144: }
145:
146: return null;
147: } catch (Exception e) {
148: throw new PortletDataException(e);
149: }
150: }
151:
152: public String exportData(PortletDataContext context,
153: String portletId, PortletPreferences prefs)
154: throws PortletDataException {
155:
156: try {
157: XStream xStream = new XStream();
158:
159: Document doc = DocumentHelper.createDocument();
160:
161: Element root = doc.addElement("journal-data");
162:
163: root.addAttribute("group-id", String.valueOf(context
164: .getGroupId()));
165:
166: // Structures
167:
168: List obj = JournalStructureUtil.findByGroupId(context
169: .getGroupId());
170:
171: Iterator itr = obj.iterator();
172:
173: while (itr.hasNext()) {
174: JournalStructure structure = (JournalStructure) itr
175: .next();
176:
177: if (context.addPrimaryKey(JournalStructure.class,
178: structure.getPrimaryKeyObj())) {
179:
180: itr.remove();
181: } else {
182: exportStructure(structure);
183: }
184: }
185:
186: String xml = xStream.toXML(obj);
187:
188: Document tempDoc = PortalUtil.readDocumentFromXML(xml);
189:
190: Element el = root.addElement("journal-structures");
191:
192: el.content().add(tempDoc.getRootElement().createCopy());
193:
194: // Templates
195:
196: obj = JournalTemplateUtil.findByGroupId(context
197: .getGroupId());
198:
199: itr = obj.iterator();
200:
201: while (itr.hasNext()) {
202: JournalTemplate template = (JournalTemplate) itr.next();
203:
204: if (context.addPrimaryKey(JournalTemplate.class,
205: template.getPrimaryKeyObj())) {
206:
207: itr.remove();
208: } else {
209: exportTemplate(context, template);
210: }
211: }
212:
213: xml = xStream.toXML(obj);
214:
215: el = root.addElement("journal-templates");
216:
217: tempDoc = PortalUtil.readDocumentFromXML(xml);
218:
219: el.content().add(tempDoc.getRootElement().createCopy());
220:
221: // Articles
222:
223: obj = JournalArticleUtil
224: .findByGroupId(context.getGroupId());
225:
226: itr = obj.iterator();
227:
228: while (itr.hasNext()) {
229: JournalArticle article = (JournalArticle) itr.next();
230:
231: if (context.addPrimaryKey(JournalArticle.class, article
232: .getPrimaryKeyObj())) {
233:
234: itr.remove();
235: } else {
236: exportArticle(context, article);
237: }
238: }
239:
240: xml = xStream.toXML(obj);
241:
242: el = root.addElement("journal-articles");
243:
244: tempDoc = PortalUtil.readDocumentFromXML(xml);
245:
246: el.content().add(tempDoc.getRootElement().createCopy());
247:
248: return doc.asXML();
249: } catch (Exception e) {
250: throw new PortletDataException(e);
251: }
252: }
253:
254: public PortletDataHandlerControl[] getExportControls()
255: throws PortletDataException {
256:
257: return new PortletDataHandlerControl[] {
258: _articlesStructuresAndTemplates, _images, _comments,
259: _ratings, _tags };
260: }
261:
262: public PortletDataHandlerControl[] getImportControls()
263: throws PortletDataException {
264:
265: return new PortletDataHandlerControl[] {
266: _articlesStructuresAndTemplates, _images, _comments,
267: _ratings, _tags };
268: }
269:
270: public PortletPreferences importData(PortletDataContext context,
271: String portletId, PortletPreferences prefs, String data)
272: throws PortletDataException {
273:
274: try {
275: XStream xStream = new XStream();
276:
277: Document doc = PortalUtil.readDocumentFromXML(data);
278:
279: Element root = doc.getRootElement();
280:
281: // Structures
282:
283: Element el = root.element("journal-structures").element(
284: "list");
285:
286: Document tempDoc = DocumentHelper.createDocument();
287:
288: tempDoc.content().add(el.createCopy());
289:
290: Map structurePKs = CollectionFactory.getHashMap();
291:
292: List structures = (List) xStream.fromXML(tempDoc.asXML());
293:
294: Iterator itr = structures.iterator();
295:
296: while (itr.hasNext()) {
297: JournalStructure structure = (JournalStructure) itr
298: .next();
299:
300: importStructure(context, structurePKs, structure);
301: }
302:
303: // Templates
304:
305: el = root.element("journal-templates").element("list");
306:
307: tempDoc = DocumentHelper.createDocument();
308:
309: tempDoc.content().add(el.createCopy());
310:
311: Map templatePKs = CollectionFactory.getHashMap();
312:
313: List templates = (List) xStream.fromXML(tempDoc.asXML());
314:
315: itr = templates.iterator();
316:
317: while (itr.hasNext()) {
318: JournalTemplate template = (JournalTemplate) itr.next();
319:
320: importTemplate(context, structurePKs, templatePKs,
321: template);
322: }
323:
324: // Articles
325:
326: el = root.element("journal-articles").element("list");
327:
328: tempDoc = DocumentHelper.createDocument();
329:
330: tempDoc.content().add(el.createCopy());
331:
332: List articles = (List) xStream.fromXML(tempDoc.asXML());
333:
334: itr = articles.iterator();
335:
336: while (itr.hasNext()) {
337: JournalArticle article = (JournalArticle) itr.next();
338:
339: importArticle(context, structurePKs, templatePKs,
340: article);
341: }
342:
343: return null;
344: } catch (Exception e) {
345: throw new PortletDataException(e);
346: }
347: }
348:
349: protected static void exportArticle(PortletDataContext context,
350: JournalArticle article) throws IOException,
351: PortalException, SystemException {
352:
353: article.setUserUuid(article.getUserUuid());
354: article.setApprovedByUserUuid(article.getApprovedByUserUuid());
355:
356: if (article.isSmallImage()) {
357: Image smallImage = ImageUtil.fetchByPrimaryKey(article
358: .getSmallImageId());
359:
360: article.setSmallImageType(smallImage.getType());
361:
362: context.getZipWriter().addEntry(getSmallImageDir(article),
363: smallImage.getTextObj());
364: }
365:
366: if (context.getBooleanParameter(_NAMESPACE, "images")) {
367: List articleImages = JournalArticleImageUtil.findByG_A_V(
368: context.getGroupId(), article.getArticleId(),
369: article.getVersion());
370:
371: Iterator itr = articleImages.iterator();
372:
373: while (itr.hasNext()) {
374: JournalArticleImage articleImage = (JournalArticleImage) itr
375: .next();
376:
377: try {
378: Image image = ImageUtil
379: .findByPrimaryKey(articleImage
380: .getArticleImageId());
381:
382: String fileName = articleImage.getElName()
383: + articleImage.getLanguageId() + "."
384: + image.getType();
385:
386: context.getZipWriter().addEntry(
387: getArticleImageDir(article) + fileName,
388: image.getTextObj());
389: } catch (NoSuchImageException nsie) {
390: }
391: }
392: }
393:
394: if (context.getBooleanParameter(_NAMESPACE, "comments")) {
395: context.addComments(JournalArticle.class, new Long(article
396: .getResourcePrimKey()));
397: }
398:
399: if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
400: context.addRatingsEntries(JournalArticle.class, new Long(
401: article.getResourcePrimKey()));
402: }
403:
404: if (context.getBooleanParameter(_NAMESPACE, "tags")) {
405: context.addTagsEntries(JournalArticle.class, new Long(
406: article.getResourcePrimKey()));
407: }
408: }
409:
410: protected static void exportStructure(JournalStructure structure)
411: throws SystemException {
412:
413: structure.setUserUuid(structure.getUserUuid());
414: }
415:
416: protected static void exportTemplate(PortletDataContext context,
417: JournalTemplate template) throws IOException,
418: PortalException, SystemException {
419:
420: template.setUserUuid(template.getUserUuid());
421:
422: if (template.isSmallImage()) {
423: Image smallImage = ImageUtil.fetchByPrimaryKey(template
424: .getSmallImageId());
425:
426: template.setSmallImageType(smallImage.getType());
427:
428: context.getZipWriter().addEntry(getSmallImageDir(template),
429: smallImage.getTextObj());
430: }
431: }
432:
433: protected static String getArticleImageDir(JournalArticle article) {
434: return _ARTICLE_IMAGES_FOLDER + article.getArticleId() + "/"
435: + article.getVersion() + "/";
436: }
437:
438: protected static String getSmallImageDir(JournalArticle article)
439: throws PortalException, SystemException {
440:
441: return _ARTICLE_SMALL_IMAGES_FOLDER + article.getSmallImageId()
442: + "." + article.getSmallImageType();
443: }
444:
445: protected static String getSmallImageDir(JournalTemplate template)
446: throws PortalException, SystemException {
447:
448: return _TEMPLATE_SMALL_IMAGES_FOLDER
449: + template.getSmallImageId() + "."
450: + template.getSmallImageType();
451: }
452:
453: protected static JournalArticle importArticle(
454: PortletDataContext context, Map structurePKs,
455: Map templatePKs, JournalArticle article) throws Exception {
456:
457: long userId = context.getUserId(article.getUserUuid());
458: long plid = context.getPlid();
459:
460: String articleId = article.getArticleId();
461: boolean autoArticleId = false;
462:
463: if ((Validator.isNumber(articleId))
464: || (JournalArticleUtil.fetchByG_A_V(context
465: .getGroupId(), articleId,
466: JournalArticleImpl.DEFAULT_VERSION) != null)) {
467:
468: autoArticleId = true;
469: }
470:
471: boolean incrementVersion = false;
472:
473: String parentStructureId = MapUtil.getString(structurePKs,
474: article.getStructureId(), article.getStructureId());
475: String parentTemplateId = MapUtil.getString(templatePKs,
476: article.getTemplateId(), article.getTemplateId());
477:
478: Date displayDate = article.getDisplayDate();
479:
480: int displayDateMonth = 0;
481: int displayDateDay = 0;
482: int displayDateYear = 0;
483: int displayDateHour = 0;
484: int displayDateMinute = 0;
485:
486: if (displayDate != null) {
487: Calendar displayCal = CalendarFactoryUtil.getCalendar();
488:
489: displayCal.setTime(displayDate);
490:
491: displayDateMonth = displayCal.get(Calendar.MONTH);
492: displayDateDay = displayCal.get(Calendar.DATE);
493: displayDateYear = displayCal.get(Calendar.YEAR);
494: displayDateHour = displayCal.get(Calendar.HOUR);
495: displayDateMinute = displayCal.get(Calendar.MINUTE);
496: }
497:
498: Date expirationDate = article.getExpirationDate();
499:
500: int expirationDateMonth = 0;
501: int expirationDateDay = 0;
502: int expirationDateYear = 0;
503: int expirationDateHour = 0;
504: int expirationDateMinute = 0;
505: boolean neverExpire = true;
506:
507: if (expirationDate != null) {
508: Calendar expirationCal = CalendarFactoryUtil.getCalendar();
509:
510: expirationCal.setTime(expirationDate);
511:
512: expirationDateMonth = expirationCal.get(Calendar.MONTH);
513: expirationDateDay = expirationCal.get(Calendar.DATE);
514: expirationDateYear = expirationCal.get(Calendar.YEAR);
515: expirationDateHour = expirationCal.get(Calendar.HOUR);
516: expirationDateMinute = expirationCal.get(Calendar.MINUTE);
517: neverExpire = false;
518: }
519:
520: Date reviewDate = article.getReviewDate();
521:
522: int reviewDateMonth = 0;
523: int reviewDateDay = 0;
524: int reviewDateYear = 0;
525: int reviewDateHour = 0;
526: int reviewDateMinute = 0;
527: boolean neverReview = true;
528:
529: if (reviewDate != null) {
530: Calendar reviewCal = CalendarFactoryUtil.getCalendar();
531:
532: reviewCal.setTime(reviewDate);
533:
534: reviewDateMonth = reviewCal.get(Calendar.MONTH);
535: reviewDateDay = reviewCal.get(Calendar.DATE);
536: reviewDateYear = reviewCal.get(Calendar.YEAR);
537: reviewDateHour = reviewCal.get(Calendar.HOUR);
538: reviewDateMinute = reviewCal.get(Calendar.MINUTE);
539: neverReview = false;
540: }
541:
542: File smallFile = null;
543:
544: if (article.isSmallImage()) {
545: byte[] byteArray = context.getZipReader()
546: .getEntryAsByteArray(getSmallImageDir(article));
547:
548: smallFile = File.createTempFile(String.valueOf(article
549: .getSmallImageId()), StringPool.PERIOD
550: + article.getSmallImageType());
551:
552: FileUtil.write(smallFile, byteArray);
553: }
554:
555: Map images = CollectionFactory.getHashMap();
556:
557: if (context.getBooleanParameter(_NAMESPACE, "images")) {
558: List imageFiles = (List) context.getZipReader()
559: .getFolderEntries()
560: .get(getArticleImageDir(article));
561:
562: if (imageFiles != null && imageFiles.size() > 0) {
563: Iterator itr = imageFiles.iterator();
564:
565: while (itr.hasNext()) {
566: ObjectValuePair imageFile = (ObjectValuePair) itr
567: .next();
568:
569: String fileName = (String) imageFile.getKey();
570:
571: int pos = fileName.lastIndexOf(".");
572:
573: if (pos != -1) {
574: fileName = fileName.substring(0, pos);
575: }
576:
577: images.put(fileName, imageFile.getValue());
578: }
579: }
580: }
581:
582: String articleURL = null;
583:
584: PortletPreferences prefs = null;
585:
586: String[] tagsEntries = null;
587:
588: if (context.getBooleanParameter(_NAMESPACE, "tags")) {
589: tagsEntries = context.getTagsEntries(JournalArticle.class,
590: new Long(article.getResourcePrimKey()));
591: }
592:
593: JournalCreationStrategy creationStrategy = JournalCreationStrategyFactory
594: .getInstance();
595:
596: long authorId = creationStrategy.getAuthorUserId(context
597: .getCompanyId(), context.getGroupId(), article);
598:
599: if (authorId != JournalCreationStrategy.USE_DEFAULT_USER_ID_STRATEGY) {
600: userId = authorId;
601: }
602:
603: String newContent = creationStrategy.getTransformedContent(
604: context.getCompanyId(), context.getGroupId(), article);
605:
606: if (newContent != JournalCreationStrategy.ARTICLE_CONTENT_UNCHANGED) {
607: article.setContent(newContent);
608: }
609:
610: boolean addCommunityPermissions = creationStrategy
611: .addCommunityPermissions(context.getCompanyId(),
612: context.getGroupId(), article);
613: boolean addGuestPermissions = creationStrategy
614: .addGuestPermissions(context.getCompanyId(), context
615: .getGroupId(), article);
616:
617: JournalArticle existingArticle = null;
618:
619: if (context.getDataStrategy().equals(
620: PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
621:
622: existingArticle = JournalArticleUtil.fetchByUUID_G(article
623: .getUuid(), context.getGroupId());
624:
625: if (existingArticle == null) {
626: existingArticle = JournalArticleLocalServiceUtil
627: .addArticle(article.getUuid(), userId,
628: articleId, autoArticleId, plid, article
629: .getTitle(), article
630: .getDescription(), article
631: .getContent(), article
632: .getType(), parentStructureId,
633: parentTemplateId, displayDateMonth,
634: displayDateDay, displayDateYear,
635: displayDateHour, displayDateMinute,
636: expirationDateMonth, expirationDateDay,
637: expirationDateYear, expirationDateHour,
638: expirationDateMinute, neverExpire,
639: reviewDateMonth, reviewDateDay,
640: reviewDateYear, reviewDateHour,
641: reviewDateMinute, neverReview, article
642: .getIndexable(), article
643: .getSmallImage(), article
644: .getSmallImageURL(), smallFile,
645: images, articleURL, prefs, tagsEntries,
646: addCommunityPermissions,
647: addGuestPermissions);
648: } else {
649: existingArticle = JournalArticleLocalServiceUtil
650: .updateArticle(userId, existingArticle
651: .getGroupId(), existingArticle
652: .getArticleId(), existingArticle
653: .getVersion(), incrementVersion,
654: article.getTitle(), article
655: .getDescription(), article
656: .getContent(), article
657: .getType(), existingArticle
658: .getStructureId(),
659: existingArticle.getTemplateId(),
660: displayDateMonth, displayDateDay,
661: displayDateYear, displayDateHour,
662: displayDateMinute, expirationDateMonth,
663: expirationDateDay, expirationDateYear,
664: expirationDateHour,
665: expirationDateMinute, neverExpire,
666: reviewDateMonth, reviewDateDay,
667: reviewDateYear, reviewDateHour,
668: reviewDateMinute, neverReview, article
669: .getIndexable(), article
670: .getSmallImage(), article
671: .getSmallImageURL(), smallFile,
672: images, articleURL, prefs, tagsEntries);
673: }
674: } else {
675: existingArticle = JournalArticleLocalServiceUtil
676: .addArticle(userId, articleId, autoArticleId, plid,
677: article.getTitle(), article
678: .getDescription(), article
679: .getContent(), article.getType(),
680: parentStructureId, parentTemplateId,
681: displayDateMonth, displayDateDay,
682: displayDateYear, displayDateHour,
683: displayDateMinute, expirationDateMonth,
684: expirationDateDay, expirationDateYear,
685: expirationDateHour, expirationDateMinute,
686: neverExpire, reviewDateMonth,
687: reviewDateDay, reviewDateYear,
688: reviewDateHour, reviewDateMinute,
689: neverReview, article.getIndexable(),
690: article.getSmallImage(), article
691: .getSmallImageURL(), smallFile,
692: images, articleURL, prefs, tagsEntries,
693: addCommunityPermissions,
694: addGuestPermissions);
695: }
696:
697: long strategyApprovalUserId = creationStrategy
698: .getApprovalUserId(context.getCompanyId(), context
699: .getGroupId(), article);
700:
701: if ((strategyApprovalUserId != JournalCreationStrategy.USE_DEFAULT_USER_ID_STRATEGY)
702: || (article.isApproved() && !existingArticle
703: .isApproved())) {
704:
705: long approvedByUserId = strategyApprovalUserId;
706:
707: if (approvedByUserId == 0) {
708: approvedByUserId = context.getUserId(article
709: .getApprovedByUserUuid());
710: }
711:
712: JournalArticleLocalServiceUtil.approveArticle(
713: approvedByUserId, context.getGroupId(),
714: existingArticle.getArticleId(), existingArticle
715: .getVersion(), articleURL, prefs);
716: }
717:
718: if (context.getBooleanParameter(_NAMESPACE, "comments")) {
719: context.importComments(JournalArticle.class, new Long(
720: article.getResourcePrimKey()), new Long(
721: existingArticle.getResourcePrimKey()), context
722: .getGroupId());
723: }
724:
725: if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
726: context.importRatingsEntries(JournalArticle.class,
727: new Long(article.getResourcePrimKey()), new Long(
728: existingArticle.getResourcePrimKey()));
729: }
730:
731: if (!articleId.equals(existingArticle.getArticleId())) {
732: if (_log.isWarnEnabled()) {
733: _log.warn("An article with the ID " + articleId
734: + " already "
735: + "exists. The new generated ID is "
736: + existingArticle.getArticleId());
737: }
738: }
739:
740: return existingArticle;
741: }
742:
743: protected static void importStructure(PortletDataContext context,
744: Map structurePKs, JournalStructure structure)
745: throws Exception {
746:
747: long userId = context.getUserId(structure.getUserUuid());
748: long plid = context.getPlid();
749:
750: String structureId = structure.getStructureId();
751: boolean autoStructureId = false;
752:
753: if ((Validator.isNumber(structureId))
754: || (JournalStructureUtil.fetchByG_S(context
755: .getGroupId(), structureId) != null)) {
756:
757: autoStructureId = true;
758: }
759:
760: JournalCreationStrategy creationStrategy = JournalCreationStrategyFactory
761: .getInstance();
762:
763: long authorId = creationStrategy.getAuthorUserId(context
764: .getCompanyId(), context.getGroupId(), structure);
765:
766: if (authorId != JournalCreationStrategy.USE_DEFAULT_USER_ID_STRATEGY) {
767: userId = authorId;
768: }
769:
770: boolean addCommunityPermissions = creationStrategy
771: .addCommunityPermissions(context.getCompanyId(),
772: context.getGroupId(), structure);
773: boolean addGuestPermissions = creationStrategy
774: .addGuestPermissions(context.getCompanyId(), context
775: .getGroupId(), structure);
776:
777: JournalStructure existingStructure = null;
778:
779: if (context.getDataStrategy().equals(
780: PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
781:
782: existingStructure = JournalStructureUtil.fetchByUUID_G(
783: structure.getUuid(), context.getGroupId());
784:
785: if (existingStructure == null) {
786: existingStructure = JournalStructureLocalServiceUtil
787: .addStructure(structure.getUuid(), userId,
788: structureId, autoStructureId, plid,
789: structure.getName(), structure
790: .getDescription(), structure
791: .getXsd(),
792: addCommunityPermissions,
793: addGuestPermissions);
794: } else {
795: existingStructure = JournalStructureLocalServiceUtil
796: .updateStructure(
797: existingStructure.getGroupId(),
798: existingStructure.getStructureId(),
799: structure.getName(), structure
800: .getDescription(), structure
801: .getXsd());
802: }
803: } else {
804: existingStructure = JournalStructureLocalServiceUtil
805: .addStructure(userId, structureId, autoStructureId,
806: plid, structure.getName(), structure
807: .getDescription(), structure
808: .getXsd(), addCommunityPermissions,
809: addGuestPermissions);
810: }
811:
812: structurePKs.put(structure.getStructureId(), existingStructure
813: .getStructureId());
814:
815: if (!structureId.equals(existingStructure.getStructureId())) {
816: if (_log.isWarnEnabled()) {
817: _log.warn("A structure with the ID " + structureId
818: + " already "
819: + "exists. The new generated ID is "
820: + existingStructure.getStructureId());
821: }
822: }
823: }
824:
825: protected static void importTemplate(PortletDataContext context,
826: Map structurePKs, Map templatePKs, JournalTemplate template)
827: throws Exception {
828:
829: long userId = context.getUserId(template.getUserUuid());
830: long plid = context.getPlid();
831:
832: String templateId = template.getTemplateId();
833: boolean autoTemplateId = false;
834:
835: if ((Validator.isNumber(templateId))
836: || (JournalTemplateUtil.fetchByG_T(
837: context.getGroupId(), templateId) != null)) {
838:
839: autoTemplateId = true;
840: }
841:
842: String parentStructureId = MapUtil.getString(structurePKs,
843: template.getStructureId(), template.getStructureId());
844:
845: boolean formatXsl = false;
846:
847: JournalCreationStrategy creationStrategy = JournalCreationStrategyFactory
848: .getInstance();
849:
850: long authorId = creationStrategy.getAuthorUserId(context
851: .getCompanyId(), context.getGroupId(), template);
852:
853: if (authorId != JournalCreationStrategy.USE_DEFAULT_USER_ID_STRATEGY) {
854: userId = authorId;
855: }
856:
857: boolean addCommunityPermissions = creationStrategy
858: .addCommunityPermissions(context.getCompanyId(),
859: context.getGroupId(), template);
860: boolean addGuestPermissions = creationStrategy
861: .addGuestPermissions(context.getCompanyId(), context
862: .getGroupId(), template);
863:
864: File smallFile = null;
865:
866: if (template.isSmallImage()) {
867: byte[] byteArray = context.getZipReader()
868: .getEntryAsByteArray(getSmallImageDir(template));
869:
870: smallFile = File.createTempFile(String.valueOf(template
871: .getSmallImageId()), StringPool.PERIOD
872: + template.getSmallImageType());
873:
874: FileUtil.write(smallFile, byteArray);
875: }
876:
877: JournalTemplate existingTemplate = null;
878:
879: if (context.getDataStrategy().equals(
880: PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
881:
882: existingTemplate = JournalTemplateUtil.fetchByUUID_G(
883: template.getUuid(), context.getGroupId());
884:
885: if (existingTemplate == null) {
886: existingTemplate = JournalTemplateLocalServiceUtil
887: .addTemplate(template.getUuid(), userId,
888: templateId, autoTemplateId, plid,
889: parentStructureId, template.getName(),
890: template.getDescription(), template
891: .getXsl(), formatXsl, template
892: .getLangType(), template
893: .getCacheable(), template
894: .isSmallImage(), template
895: .getSmallImageURL(), smallFile,
896: addCommunityPermissions,
897: addGuestPermissions);
898: } else {
899: existingTemplate = JournalTemplateLocalServiceUtil
900: .updateTemplate(existingTemplate.getGroupId(),
901: existingTemplate.getTemplateId(),
902: existingTemplate.getStructureId(),
903: template.getName(), template
904: .getDescription(), template
905: .getXsl(), formatXsl, template
906: .getLangType(), template
907: .getCacheable(), template
908: .isSmallImage(), template
909: .getSmallImageURL(), smallFile);
910: }
911: } else {
912: existingTemplate = JournalTemplateLocalServiceUtil
913: .addTemplate(userId, templateId, autoTemplateId,
914: plid, parentStructureId,
915: template.getName(), template
916: .getDescription(), template
917: .getXsl(), formatXsl, template
918: .getLangType(), template
919: .getCacheable(), template
920: .isSmallImage(), template
921: .getSmallImageURL(), smallFile,
922: addCommunityPermissions,
923: addGuestPermissions);
924: }
925:
926: templatePKs.put(template.getTemplateId(), existingTemplate
927: .getTemplateId());
928:
929: if (!templateId.equals(existingTemplate.getTemplateId())) {
930: if (_log.isWarnEnabled()) {
931: _log.warn("A template with the ID " + templateId
932: + " already "
933: + "exists. The new generated ID is "
934: + existingTemplate.getTemplateId());
935: }
936: }
937: }
938:
939: private static final String _NAMESPACE = "journal";
940:
941: private static final String _ARTICLE_IMAGES_FOLDER = "article-images/";
942:
943: private static final String _ARTICLE_SMALL_IMAGES_FOLDER = "article-thumbnails/";
944:
945: private static final String _TEMPLATE_SMALL_IMAGES_FOLDER = "template-thumbnails/";
946:
947: private static final PortletDataHandlerBoolean _articlesStructuresAndTemplates = new PortletDataHandlerBoolean(
948: _NAMESPACE, "articles-structures-and-templates", true, true);
949:
950: private static final PortletDataHandlerBoolean _images = new PortletDataHandlerBoolean(
951: _NAMESPACE, "images");
952:
953: private static final PortletDataHandlerBoolean _comments = new PortletDataHandlerBoolean(
954: _NAMESPACE, "comments");
955:
956: private static final PortletDataHandlerBoolean _ratings = new PortletDataHandlerBoolean(
957: _NAMESPACE, "ratings");
958:
959: private static final PortletDataHandlerBoolean _tags = new PortletDataHandlerBoolean(
960: _NAMESPACE, "tags");
961:
962: private static Log _log = LogFactory
963: .getLog(JournalPortletDataHandlerImpl.class);
964:
965: }
|