001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journal.lar;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.lar.BaseStagedModelDataHandler;
022    import com.liferay.portal.kernel.lar.ExportImportHelperUtil;
023    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
024    import com.liferay.portal.kernel.lar.PortletDataContext;
025    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
026    import com.liferay.portal.kernel.log.Log;
027    import com.liferay.portal.kernel.log.LogFactoryUtil;
028    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
029    import com.liferay.portal.kernel.util.FileUtil;
030    import com.liferay.portal.kernel.util.LocaleUtil;
031    import com.liferay.portal.kernel.util.LocalizationUtil;
032    import com.liferay.portal.kernel.util.MapUtil;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.Validator;
036    import com.liferay.portal.kernel.workflow.WorkflowConstants;
037    import com.liferay.portal.kernel.xml.Element;
038    import com.liferay.portal.model.Image;
039    import com.liferay.portal.model.User;
040    import com.liferay.portal.service.ImageLocalServiceUtil;
041    import com.liferay.portal.service.ServiceContext;
042    import com.liferay.portal.service.UserLocalServiceUtil;
043    import com.liferay.portal.util.PortalUtil;
044    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
045    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
046    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
047    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
048    import com.liferay.portlet.journal.NoSuchArticleException;
049    import com.liferay.portlet.journal.model.JournalArticle;
050    import com.liferay.portlet.journal.model.JournalArticleConstants;
051    import com.liferay.portlet.journal.model.JournalArticleImage;
052    import com.liferay.portlet.journal.model.JournalArticleResource;
053    import com.liferay.portlet.journal.model.JournalFolder;
054    import com.liferay.portlet.journal.model.JournalFolderConstants;
055    import com.liferay.portlet.journal.service.JournalArticleImageLocalServiceUtil;
056    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
057    import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
058    
059    import java.io.File;
060    
061    import java.util.Calendar;
062    import java.util.Date;
063    import java.util.HashMap;
064    import java.util.List;
065    import java.util.Locale;
066    import java.util.Map;
067    
068    /**
069     * @author Daniel Kocsis
070     */
071    public class JournalArticleStagedModelDataHandler
072            extends BaseStagedModelDataHandler<JournalArticle> {
073    
074            public static final String[] CLASS_NAMES = {JournalArticle.class.getName()};
075    
076            @Override
077            public void deleteStagedModel(
078                            String uuid, long groupId, String className, String extraData)
079                    throws PortalException, SystemException {
080    
081                    JournalArticleResource articleResource =
082                            JournalArticleResourceLocalServiceUtil.fetchArticleResource(
083                                    uuid, groupId);
084    
085                    if (articleResource == null) {
086                            return;
087                    }
088    
089                    if (Validator.isNull(extraData)) {
090                            JournalArticleLocalServiceUtil.deleteArticle(
091                                    groupId, articleResource.getArticleId(), null);
092                    }
093                    else {
094                            JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject(
095                                    extraData);
096    
097                            double version = extraDataJSONObject.getDouble("version");
098    
099                            JournalArticle article = JournalArticleLocalServiceUtil.getArticle(
100                                    groupId, articleResource.getArticleId(), version);
101    
102                            JournalArticleLocalServiceUtil.deleteArticle(article);
103                    }
104            }
105    
106            @Override
107            public String[] getClassNames() {
108                    return CLASS_NAMES;
109            }
110    
111            @Override
112            public String getDisplayName(JournalArticle article) {
113                    return article.getTitleCurrentValue();
114            }
115    
116            @Override
117            public int[] getExportableStatuses() {
118                    return new int[] {
119                            WorkflowConstants.STATUS_APPROVED, WorkflowConstants.STATUS_EXPIRED
120                    };
121            }
122    
123            @Override
124            protected boolean countStagedModel(
125                    PortletDataContext portletDataContext, JournalArticle article) {
126    
127                    if (portletDataContext.isPathProcessed(
128                                    ExportImportPathUtil.getModelPath(
129                                            article.getGroupId(),
130                                            JournalArticleResource.class.getName(),
131                                            article.getResourcePrimKey()))) {
132    
133                            return false;
134                    }
135    
136                    return true;
137            }
138    
139            @Override
140            protected void doExportStagedModel(
141                            PortletDataContext portletDataContext, JournalArticle article)
142                    throws Exception {
143    
144                    Element articleElement = portletDataContext.getExportDataElement(
145                            article);
146    
147                    articleElement.addAttribute(
148                            "article-resource-uuid", article.getArticleResourceUuid());
149    
150                    if (article.getFolderId() !=
151                                    JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
152    
153                            StagedModelDataHandlerUtil.exportStagedModel(
154                                    portletDataContext, article.getFolder());
155    
156                            portletDataContext.addReferenceElement(
157                                    article, articleElement, article.getFolder(),
158                                    PortletDataContext.REFERENCE_TYPE_STRONG, false);
159                    }
160    
161                    if (Validator.isNotNull(article.getStructureId())) {
162                            DDMStructure ddmStructure =
163                                    DDMStructureLocalServiceUtil.getStructure(
164                                            article.getGroupId(),
165                                            PortalUtil.getClassNameId(JournalArticle.class),
166                                            article.getStructureId(), true);
167    
168                            StagedModelDataHandlerUtil.exportStagedModel(
169                                    portletDataContext, ddmStructure);
170    
171                            portletDataContext.addReferenceElement(
172                                    article, articleElement, ddmStructure,
173                                    PortletDataContext.REFERENCE_TYPE_STRONG, false);
174                    }
175    
176                    if (Validator.isNotNull(article.getTemplateId())) {
177                            DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.getTemplate(
178                                    article.getGroupId(),
179                                    PortalUtil.getClassNameId(DDMStructure.class),
180                                    article.getTemplateId(), true);
181    
182                            StagedModelDataHandlerUtil.exportStagedModel(
183                                    portletDataContext, ddmTemplate);
184    
185                            portletDataContext.addReferenceElement(
186                                    article, articleElement, ddmTemplate,
187                                    PortletDataContext.REFERENCE_TYPE_STRONG, false);
188                    }
189    
190                    if (article.isSmallImage()) {
191                            Image smallImage = ImageLocalServiceUtil.fetchImage(
192                                    article.getSmallImageId());
193    
194                            if (Validator.isNotNull(article.getSmallImageURL())) {
195                                    String smallImageURL =
196                                            ExportImportHelperUtil.replaceExportContentReferences(
197                                                    portletDataContext, article, articleElement,
198                                                    article.getSmallImageURL().concat(StringPool.SPACE),
199                                                    true);
200    
201                                    article.setSmallImageURL(smallImageURL);
202                            }
203                            else if (smallImage != null) {
204                                    String smallImagePath = ExportImportPathUtil.getModelPath(
205                                            article,
206                                            smallImage.getImageId() + StringPool.PERIOD +
207                                                    smallImage.getType());
208    
209                                    articleElement.addAttribute("small-image-path", smallImagePath);
210    
211                                    article.setSmallImageType(smallImage.getType());
212    
213                                    portletDataContext.addZipEntry(
214                                            smallImagePath, smallImage.getTextObj());
215                            }
216                    }
217    
218                    List<JournalArticleImage> articleImages =
219                            JournalArticleImageLocalServiceUtil.getArticleImages(
220                                    article.getGroupId(), article.getArticleId(),
221                                    article.getVersion());
222    
223                    for (JournalArticleImage articleImage : articleImages) {
224                            exportArticleImage(
225                                    portletDataContext, articleImage, article, articleElement);
226                    }
227    
228                    article.setStatusByUserUuid(article.getStatusByUserUuid());
229    
230                    String content = ExportImportHelperUtil.replaceExportContentReferences(
231                            portletDataContext, article, articleElement, article.getContent(),
232                            portletDataContext.getBooleanParameter(
233                                    JournalPortletDataHandler.NAMESPACE, "referenced-content"));
234    
235                    article.setContent(content);
236    
237                    portletDataContext.addClassedModel(
238                            articleElement, ExportImportPathUtil.getModelPath(article), article,
239                            JournalPortletDataHandler.NAMESPACE);
240            }
241    
242            @Override
243            protected void doImportStagedModel(
244                            PortletDataContext portletDataContext, JournalArticle article)
245                    throws Exception {
246    
247                    prepareLanguagesForImport(article);
248    
249                    long userId = portletDataContext.getUserId(article.getUserUuid());
250    
251                    JournalCreationStrategy creationStrategy =
252                            JournalCreationStrategyFactory.getInstance();
253    
254                    long authorId = creationStrategy.getAuthorUserId(
255                            portletDataContext, article);
256    
257                    if (authorId != JournalCreationStrategy.USE_DEFAULT_USER_ID_STRATEGY) {
258                            userId = authorId;
259                    }
260    
261                    User user = UserLocalServiceUtil.getUser(userId);
262    
263                    Map<Long, Long> folderIds =
264                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
265                                    JournalFolder.class);
266    
267                    if (article.getFolderId() !=
268                                    JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
269    
270                            Element folderElement = portletDataContext.getReferenceDataElement(
271                                    article, JournalFolder.class, article.getFolderId());
272    
273                            StagedModelDataHandlerUtil.importStagedModel(
274                                    portletDataContext, folderElement);
275                    }
276    
277                    long folderId = MapUtil.getLong(
278                            folderIds, article.getFolderId(), article.getFolderId());
279    
280                    String articleId = article.getArticleId();
281    
282                    boolean autoArticleId = false;
283    
284                    if (Validator.isNumber(articleId) ||
285                            (JournalArticleLocalServiceUtil.fetchArticle(
286                                    portletDataContext.getScopeGroupId(), articleId,
287                                    JournalArticleConstants.VERSION_DEFAULT) != null)) {
288    
289                            autoArticleId = true;
290                    }
291    
292                    Map<String, String> articleIds =
293                            (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
294                                    JournalArticle.class + ".articleId");
295    
296                    String newArticleId = articleIds.get(articleId);
297    
298                    if (Validator.isNotNull(newArticleId)) {
299    
300                            // A sibling of a different version was already assigned a new
301                            // article id
302    
303                            articleId = newArticleId;
304                            autoArticleId = false;
305                    }
306    
307                    String content = article.getContent();
308    
309                    Element articleElement =
310                            portletDataContext.getImportDataStagedModelElement(article);
311    
312                    content = ExportImportHelperUtil.replaceImportContentReferences(
313                            portletDataContext, articleElement, content, true);
314    
315                    article.setContent(content);
316    
317                    String newContent = creationStrategy.getTransformedContent(
318                            portletDataContext, article);
319    
320                    if (newContent != JournalCreationStrategy.ARTICLE_CONTENT_UNCHANGED) {
321                            article.setContent(newContent);
322                    }
323    
324                    Date displayDate = article.getDisplayDate();
325    
326                    int displayDateMonth = 0;
327                    int displayDateDay = 0;
328                    int displayDateYear = 0;
329                    int displayDateHour = 0;
330                    int displayDateMinute = 0;
331    
332                    if (displayDate != null) {
333                            Calendar displayCal = CalendarFactoryUtil.getCalendar(
334                                    user.getTimeZone());
335    
336                            displayCal.setTime(displayDate);
337    
338                            displayDateMonth = displayCal.get(Calendar.MONTH);
339                            displayDateDay = displayCal.get(Calendar.DATE);
340                            displayDateYear = displayCal.get(Calendar.YEAR);
341                            displayDateHour = displayCal.get(Calendar.HOUR);
342                            displayDateMinute = displayCal.get(Calendar.MINUTE);
343    
344                            if (displayCal.get(Calendar.AM_PM) == Calendar.PM) {
345                                    displayDateHour += 12;
346                            }
347                    }
348    
349                    Date expirationDate = article.getExpirationDate();
350    
351                    int expirationDateMonth = 0;
352                    int expirationDateDay = 0;
353                    int expirationDateYear = 0;
354                    int expirationDateHour = 0;
355                    int expirationDateMinute = 0;
356                    boolean neverExpire = true;
357    
358                    if (expirationDate != null) {
359                            Calendar expirationCal = CalendarFactoryUtil.getCalendar(
360                                    user.getTimeZone());
361    
362                            expirationCal.setTime(expirationDate);
363    
364                            expirationDateMonth = expirationCal.get(Calendar.MONTH);
365                            expirationDateDay = expirationCal.get(Calendar.DATE);
366                            expirationDateYear = expirationCal.get(Calendar.YEAR);
367                            expirationDateHour = expirationCal.get(Calendar.HOUR);
368                            expirationDateMinute = expirationCal.get(Calendar.MINUTE);
369                            neverExpire = false;
370    
371                            if (expirationCal.get(Calendar.AM_PM) == Calendar.PM) {
372                                    expirationDateHour += 12;
373                            }
374                    }
375    
376                    Date reviewDate = article.getReviewDate();
377    
378                    int reviewDateMonth = 0;
379                    int reviewDateDay = 0;
380                    int reviewDateYear = 0;
381                    int reviewDateHour = 0;
382                    int reviewDateMinute = 0;
383                    boolean neverReview = true;
384    
385                    if (reviewDate != null) {
386                            Calendar reviewCal = CalendarFactoryUtil.getCalendar(
387                                    user.getTimeZone());
388    
389                            reviewCal.setTime(reviewDate);
390    
391                            reviewDateMonth = reviewCal.get(Calendar.MONTH);
392                            reviewDateDay = reviewCal.get(Calendar.DATE);
393                            reviewDateYear = reviewCal.get(Calendar.YEAR);
394                            reviewDateHour = reviewCal.get(Calendar.HOUR);
395                            reviewDateMinute = reviewCal.get(Calendar.MINUTE);
396                            neverReview = false;
397    
398                            if (reviewCal.get(Calendar.AM_PM) == Calendar.PM) {
399                                    reviewDateHour += 12;
400                            }
401                    }
402    
403                    String parentDDMStructureKey = StringPool.BLANK;
404    
405                    long ddmStructureId = 0;
406    
407                    List<Element> structureElements =
408                            portletDataContext.getReferenceDataElements(
409                                    article, DDMStructure.class);
410    
411                    if (!structureElements.isEmpty()) {
412                            Element structureElement = structureElements.get(0);
413    
414                            String structurePath = structureElement.attributeValue("path");
415    
416                            DDMStructure ddmStructure =
417                                    (DDMStructure)portletDataContext.getZipEntryAsObject(
418                                            structurePath);
419    
420                            DDMStructure existingDDMStructure =
421                                    DDMStructureLocalServiceUtil.fetchDDMStructureByUuidAndGroupId(
422                                            ddmStructure.getUuid(),
423                                            portletDataContext.getScopeGroupId());
424    
425                            if (existingDDMStructure == null) {
426                                    existingDDMStructure =
427                                            DDMStructureLocalServiceUtil.
428                                                    fetchDDMStructureByUuidAndGroupId(
429                                                            ddmStructure.getUuid(),
430                                                            portletDataContext.getCompanyGroupId());
431                            }
432    
433                            if (existingDDMStructure == null) {
434                                    StagedModelDataHandlerUtil.importStagedModel(
435                                            portletDataContext, ddmStructure);
436    
437                                    Map<Long, Long> ddmStructureIds =
438                                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
439                                                    DDMStructure.class);
440    
441                                    ddmStructureId = MapUtil.getLong(
442                                            ddmStructureIds, ddmStructure.getStructureId(),
443                                            ddmStructure.getStructureId());
444    
445                                    existingDDMStructure =
446                                            DDMStructureLocalServiceUtil.getDDMStructure(
447                                                    ddmStructureId);
448                            }
449    
450                            if (existingDDMStructure == null) {
451                                    if (_log.isWarnEnabled()) {
452                                            StringBundler sb = new StringBundler();
453    
454                                            sb.append("Structure ");
455                                            sb.append(article.getStructureId());
456                                            sb.append(" is missing for article ");
457                                            sb.append(article.getArticleId());
458                                            sb.append(", skipping this article.");
459    
460                                            _log.warn(sb.toString());
461                                    }
462    
463                                    return;
464                            }
465    
466                            parentDDMStructureKey = existingDDMStructure.getStructureKey();
467                    }
468    
469                    String parentDDMTemplateKey = StringPool.BLANK;
470    
471                    List<Element> ddmTemplateElements =
472                            portletDataContext.getReferenceDataElements(
473                                    article, DDMTemplate.class);
474    
475                    if (!ddmTemplateElements.isEmpty()) {
476                            Element templateElement = ddmTemplateElements.get(0);
477    
478                            String ddmTemplatePath = templateElement.attributeValue("path");
479    
480                            DDMTemplate ddmTemplate=
481                                    (DDMTemplate)portletDataContext.getZipEntryAsObject(
482                                            ddmTemplatePath);
483    
484                            DDMTemplate existingDDMTemplate =
485                                    DDMTemplateLocalServiceUtil.fetchDDMTemplateByUuidAndGroupId(
486                                            ddmTemplate.getUuid(),
487                                            portletDataContext.getScopeGroupId());
488    
489                            if (existingDDMTemplate == null) {
490                                    existingDDMTemplate =
491                                            DDMTemplateLocalServiceUtil.
492                                                    fetchDDMTemplateByUuidAndGroupId(
493                                                            ddmTemplate.getUuid(),
494                                                            portletDataContext.getCompanyGroupId());
495                            }
496    
497                            if (existingDDMTemplate == null) {
498                                    StagedModelDataHandlerUtil.importStagedModel(
499                                            portletDataContext, ddmTemplate);
500    
501                                    Map<Long, Long> ddmTemplateIds =
502                                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
503                                                    DDMTemplate.class);
504    
505                                    long ddmTemplateId = MapUtil.getLong(
506                                            ddmTemplateIds, ddmTemplate.getTemplateId(),
507                                            ddmTemplate.getTemplateId());
508    
509                                    existingDDMTemplate =
510                                            DDMTemplateLocalServiceUtil.getDDMTemplate(ddmTemplateId);
511                            }
512    
513                            if (existingDDMTemplate == null) {
514                                    if (_log.isWarnEnabled()) {
515                                            StringBundler sb = new StringBundler();
516    
517                                            sb.append("Template ");
518                                            sb.append(article.getTemplateId());
519                                            sb.append(" is missing for article ");
520                                            sb.append(article.getArticleId());
521                                            sb.append(", skipping this article.");
522    
523                                            _log.warn(sb.toString());
524                                    }
525    
526                                    return;
527                            }
528    
529                            parentDDMTemplateKey = existingDDMTemplate.getTemplateKey();
530                    }
531    
532                    File smallFile = null;
533    
534                    try {
535                            if (article.isSmallImage()) {
536                                    String smallImagePath = articleElement.attributeValue(
537                                            "small-image-path");
538    
539                                    if (Validator.isNotNull(article.getSmallImageURL())) {
540                                            String smallImageURL =
541                                                    ExportImportHelperUtil.replaceImportContentReferences(
542                                                            portletDataContext, articleElement,
543                                                            article.getSmallImageURL(), true);
544    
545                                            article.setSmallImageURL(smallImageURL);
546                                    }
547                                    else if (Validator.isNotNull(smallImagePath)) {
548                                            byte[] bytes = portletDataContext.getZipEntryAsByteArray(
549                                                    smallImagePath);
550    
551                                            if (bytes != null) {
552                                                    smallFile = FileUtil.createTempFile(
553                                                            article.getSmallImageType());
554    
555                                                    FileUtil.write(smallFile, bytes);
556                                            }
557                                    }
558                            }
559    
560                            Map<String, byte[]> images = new HashMap<String, byte[]>();
561    
562                            List<Element> imagesElements =
563                                    portletDataContext.getReferenceDataElements(
564                                            article, Image.class);
565    
566                            for (Element imageElement : imagesElements) {
567                                    String imagePath = imageElement.attributeValue("path");
568    
569                                    String fileName = imageElement.attributeValue("file-name");
570    
571                                    images.put(
572                                            fileName,
573                                            portletDataContext.getZipEntryAsByteArray(imagePath));
574                            }
575    
576                            String articleURL = null;
577    
578                            boolean addGroupPermissions = creationStrategy.addGroupPermissions(
579                                    portletDataContext, article);
580                            boolean addGuestPermissions = creationStrategy.addGuestPermissions(
581                                    portletDataContext, article);
582    
583                            ServiceContext serviceContext =
584                                    portletDataContext.createServiceContext(
585                                            article, JournalPortletDataHandler.NAMESPACE);
586    
587                            serviceContext.setAddGroupPermissions(addGroupPermissions);
588                            serviceContext.setAddGuestPermissions(addGuestPermissions);
589    
590                            if (article.getStatus() != WorkflowConstants.STATUS_APPROVED) {
591                                    serviceContext.setWorkflowAction(
592                                            WorkflowConstants.ACTION_SAVE_DRAFT);
593                            }
594    
595                            JournalArticle importedArticle = null;
596    
597                            String articleResourceUuid = articleElement.attributeValue(
598                                    "article-resource-uuid");
599    
600                            if (portletDataContext.isDataStrategyMirror()) {
601                                    JournalArticleResource articleResource =
602                                            JournalArticleResourceLocalServiceUtil.
603                                                    fetchJournalArticleResourceByUuidAndGroupId(
604                                                            articleResourceUuid,
605                                                            portletDataContext.getScopeGroupId());
606    
607                                    if (articleResource == null) {
608                                            articleResource =
609                                                    JournalArticleResourceLocalServiceUtil.
610                                                            fetchJournalArticleResourceByUuidAndGroupId(
611                                                                    articleResourceUuid,
612                                                                    portletDataContext.getCompanyGroupId());
613                                    }
614    
615                                    serviceContext.setUuid(articleResourceUuid);
616                                    serviceContext.setAttribute("urlTitle", article.getUrlTitle());
617    
618                                    JournalArticle existingArticle = null;
619    
620                                    if (articleResource != null) {
621                                            try {
622                                                    existingArticle =
623                                                            JournalArticleLocalServiceUtil.getLatestArticle(
624                                                                    articleResource.getResourcePrimKey(),
625                                                                    WorkflowConstants.STATUS_ANY, false);
626                                            }
627                                            catch (NoSuchArticleException nsae) {
628                                            }
629                                    }
630    
631                                    if (existingArticle == null) {
632                                            existingArticle =
633                                                    JournalArticleLocalServiceUtil.fetchArticle(
634                                                            portletDataContext.getScopeGroupId(), newArticleId,
635                                                            article.getVersion());
636                                    }
637    
638                                    if (existingArticle == null) {
639                                            importedArticle =
640                                                    JournalArticleLocalServiceUtil.addArticle(
641                                                            userId, portletDataContext.getScopeGroupId(),
642                                                            folderId, article.getClassNameId(), ddmStructureId,
643                                                            articleId, autoArticleId, article.getVersion(),
644                                                            article.getTitleMap(), article.getDescriptionMap(),
645                                                            article.getContent(), article.getType(),
646                                                            parentDDMStructureKey, parentDDMTemplateKey,
647                                                            article.getLayoutUuid(), displayDateMonth,
648                                                            displayDateDay, displayDateYear, displayDateHour,
649                                                            displayDateMinute, expirationDateMonth,
650                                                            expirationDateDay, expirationDateYear,
651                                                            expirationDateHour, expirationDateMinute,
652                                                            neverExpire, reviewDateMonth, reviewDateDay,
653                                                            reviewDateYear, reviewDateHour, reviewDateMinute,
654                                                            neverReview, article.isIndexable(),
655                                                            article.isSmallImage(), article.getSmallImageURL(),
656                                                            smallFile, images, articleURL, serviceContext);
657                                    }
658                                    else if (portletDataContext.isCompanyStagedGroupedModel(
659                                                            existingArticle)) {
660    
661                                            return;
662                                    }
663                                    else {
664                                            importedArticle =
665                                                    JournalArticleLocalServiceUtil.updateArticle(
666                                                            userId, existingArticle.getGroupId(), folderId,
667                                                            existingArticle.getArticleId(),
668                                                            article.getVersion(), article.getTitleMap(),
669                                                            article.getDescriptionMap(), article.getContent(),
670                                                            article.getType(), parentDDMStructureKey,
671                                                            parentDDMTemplateKey, article.getLayoutUuid(),
672                                                            displayDateMonth, displayDateDay, displayDateYear,
673                                                            displayDateHour, displayDateMinute,
674                                                            expirationDateMonth, expirationDateDay,
675                                                            expirationDateYear, expirationDateHour,
676                                                            expirationDateMinute, neverExpire, reviewDateMonth,
677                                                            reviewDateDay, reviewDateYear, reviewDateHour,
678                                                            reviewDateMinute, neverReview,
679                                                            article.isIndexable(), article.isSmallImage(),
680                                                            article.getSmallImageURL(), smallFile, images,
681                                                            articleURL, serviceContext);
682                                    }
683                            }
684                            else {
685                                    importedArticle = JournalArticleLocalServiceUtil.addArticle(
686                                            userId, portletDataContext.getScopeGroupId(), folderId,
687                                            article.getClassNameId(), ddmStructureId, articleId,
688                                            autoArticleId, article.getVersion(), article.getTitleMap(),
689                                            article.getDescriptionMap(), article.getContent(),
690                                            article.getType(), parentDDMStructureKey,
691                                            parentDDMTemplateKey, article.getLayoutUuid(),
692                                            displayDateMonth, displayDateDay, displayDateYear,
693                                            displayDateHour, displayDateMinute, expirationDateMonth,
694                                            expirationDateDay, expirationDateYear, expirationDateHour,
695                                            expirationDateMinute, neverExpire, reviewDateMonth,
696                                            reviewDateDay, reviewDateYear, reviewDateHour,
697                                            reviewDateMinute, neverReview, article.isIndexable(),
698                                            article.isSmallImage(), article.getSmallImageURL(),
699                                            smallFile, images, articleURL, serviceContext);
700                            }
701    
702                            portletDataContext.importClassedModel(
703                                    article, importedArticle, JournalPortletDataHandler.NAMESPACE);
704    
705                            if (Validator.isNull(newArticleId)) {
706                                    articleIds.put(
707                                            article.getArticleId(), importedArticle.getArticleId());
708                            }
709                    }
710                    finally {
711                            if (smallFile != null) {
712                                    smallFile.delete();
713                            }
714                    }
715            }
716    
717            protected void exportArticleImage(
718                            PortletDataContext portletDataContext,
719                            JournalArticleImage articleImage, JournalArticle article,
720                            Element articleElement)
721                    throws SystemException {
722    
723                    Image image = ImageLocalServiceUtil.fetchImage(
724                            articleImage.getArticleImageId());
725    
726                    if ((image == null) || (image.getTextObj() == null)) {
727                            return;
728                    }
729    
730                    String fileName =
731                            image.getImageId() + StringPool.PERIOD + image.getType();
732    
733                    String articleImagePath = ExportImportPathUtil.getModelPath(
734                            article, fileName);
735    
736                    if (!portletDataContext.isPathNotProcessed(articleImagePath)) {
737                            return;
738                    }
739    
740                    Element imageElement = portletDataContext.getExportDataElement(image);
741    
742                    imageElement.addAttribute("path", articleImagePath);
743    
744                    if (Validator.isNotNull(fileName)) {
745                            imageElement.addAttribute("file-name", fileName);
746                    }
747    
748                    portletDataContext.addZipEntry(articleImagePath, image.getTextObj());
749    
750                    portletDataContext.addReferenceElement(
751                            article, articleElement, image, articleImagePath, false);
752            }
753    
754            protected void prepareLanguagesForImport(JournalArticle article)
755                    throws PortalException {
756    
757                    Locale articleDefaultLocale = LocaleUtil.fromLanguageId(
758                            article.getDefaultLanguageId());
759    
760                    Locale[] articleAvailableLocales = LocaleUtil.fromLanguageIds(
761                            article.getAvailableLanguageIds());
762    
763                    Locale defaultImportLocale = LocalizationUtil.getDefaultImportLocale(
764                            JournalArticle.class.getName(), article.getPrimaryKey(),
765                            articleDefaultLocale, articleAvailableLocales);
766    
767                    article.prepareLocalizedFieldsForImport(defaultImportLocale);
768            }
769    
770            @Override
771            protected boolean validateMissingReference(
772                            String uuid, long companyId, long groupId)
773                    throws Exception {
774    
775                    JournalArticle article =
776                            JournalArticleLocalServiceUtil.fetchJournalArticleByUuidAndGroupId(
777                                    uuid, groupId);
778    
779                    if (article == null) {
780                            return false;
781                    }
782    
783                    return true;
784            }
785    
786            private static Log _log = LogFactoryUtil.getLog(
787                    JournalArticleStagedModelDataHandler.class);
788    
789    }