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.exportReferenceStagedModel(
154                                    portletDataContext, article, article.getFolder(),
155                                    PortletDataContext.REFERENCE_TYPE_PARENT);
156                    }
157    
158                    if (Validator.isNotNull(article.getStructureId())) {
159                            DDMStructure ddmStructure =
160                                    DDMStructureLocalServiceUtil.getStructure(
161                                            article.getGroupId(),
162                                            PortalUtil.getClassNameId(JournalArticle.class),
163                                            article.getStructureId(), true);
164    
165                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
166                                    portletDataContext, article, ddmStructure,
167                                    PortletDataContext.REFERENCE_TYPE_STRONG);
168                    }
169    
170                    if (Validator.isNotNull(article.getTemplateId())) {
171                            DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.getTemplate(
172                                    article.getGroupId(),
173                                    PortalUtil.getClassNameId(DDMStructure.class),
174                                    article.getTemplateId(), true);
175    
176                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
177                                    portletDataContext, article, ddmTemplate,
178                                    PortletDataContext.REFERENCE_TYPE_STRONG);
179                    }
180    
181                    if (article.isSmallImage()) {
182                            Image smallImage = ImageLocalServiceUtil.fetchImage(
183                                    article.getSmallImageId());
184    
185                            if (Validator.isNotNull(article.getSmallImageURL())) {
186                                    String smallImageURL =
187                                            ExportImportHelperUtil.replaceExportContentReferences(
188                                                    portletDataContext, article, articleElement,
189                                                    article.getSmallImageURL().concat(StringPool.SPACE),
190                                                    true);
191    
192                                    article.setSmallImageURL(smallImageURL);
193                            }
194                            else if (smallImage != null) {
195                                    String smallImagePath = ExportImportPathUtil.getModelPath(
196                                            article,
197                                            smallImage.getImageId() + StringPool.PERIOD +
198                                                    smallImage.getType());
199    
200                                    articleElement.addAttribute("small-image-path", smallImagePath);
201    
202                                    article.setSmallImageType(smallImage.getType());
203    
204                                    portletDataContext.addZipEntry(
205                                            smallImagePath, smallImage.getTextObj());
206                            }
207                    }
208    
209                    List<JournalArticleImage> articleImages =
210                            JournalArticleImageLocalServiceUtil.getArticleImages(
211                                    article.getGroupId(), article.getArticleId(),
212                                    article.getVersion());
213    
214                    for (JournalArticleImage articleImage : articleImages) {
215                            exportArticleImage(
216                                    portletDataContext, articleImage, article, articleElement);
217                    }
218    
219                    article.setStatusByUserUuid(article.getStatusByUserUuid());
220    
221                    String content = ExportImportHelperUtil.replaceExportContentReferences(
222                            portletDataContext, article, articleElement, article.getContent(),
223                            portletDataContext.getBooleanParameter(
224                                    JournalPortletDataHandler.NAMESPACE, "referenced-content"));
225    
226                    article.setContent(content);
227    
228                    portletDataContext.addClassedModel(
229                            articleElement, ExportImportPathUtil.getModelPath(article), article,
230                            JournalPortletDataHandler.NAMESPACE);
231            }
232    
233            @Override
234            protected void doImportStagedModel(
235                            PortletDataContext portletDataContext, JournalArticle article)
236                    throws Exception {
237    
238                    prepareLanguagesForImport(article);
239    
240                    long userId = portletDataContext.getUserId(article.getUserUuid());
241    
242                    JournalCreationStrategy creationStrategy =
243                            JournalCreationStrategyFactory.getInstance();
244    
245                    long authorId = creationStrategy.getAuthorUserId(
246                            portletDataContext, article);
247    
248                    if (authorId != JournalCreationStrategy.USE_DEFAULT_USER_ID_STRATEGY) {
249                            userId = authorId;
250                    }
251    
252                    User user = UserLocalServiceUtil.getUser(userId);
253    
254                    Map<Long, Long> folderIds =
255                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
256                                    JournalFolder.class);
257    
258                    if (article.getFolderId() !=
259                                    JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
260    
261                            Element folderElement = portletDataContext.getReferenceDataElement(
262                                    article, JournalFolder.class, article.getFolderId());
263    
264                            StagedModelDataHandlerUtil.importStagedModel(
265                                    portletDataContext, folderElement);
266                    }
267    
268                    long folderId = MapUtil.getLong(
269                            folderIds, article.getFolderId(), article.getFolderId());
270    
271                    String articleId = article.getArticleId();
272    
273                    boolean autoArticleId = false;
274    
275                    if (Validator.isNumber(articleId) ||
276                            (JournalArticleLocalServiceUtil.fetchArticle(
277                                    portletDataContext.getScopeGroupId(), articleId,
278                                    JournalArticleConstants.VERSION_DEFAULT) != null)) {
279    
280                            autoArticleId = true;
281                    }
282    
283                    Map<String, String> articleIds =
284                            (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
285                                    JournalArticle.class + ".articleId");
286    
287                    String newArticleId = articleIds.get(articleId);
288    
289                    if (Validator.isNotNull(newArticleId)) {
290    
291                            // A sibling of a different version was already assigned a new
292                            // article id
293    
294                            articleId = newArticleId;
295                            autoArticleId = false;
296                    }
297    
298                    String content = article.getContent();
299    
300                    Element articleElement =
301                            portletDataContext.getImportDataStagedModelElement(article);
302    
303                    content = ExportImportHelperUtil.replaceImportContentReferences(
304                            portletDataContext, articleElement, content, true);
305    
306                    article.setContent(content);
307    
308                    String newContent = creationStrategy.getTransformedContent(
309                            portletDataContext, article);
310    
311                    if (newContent != JournalCreationStrategy.ARTICLE_CONTENT_UNCHANGED) {
312                            article.setContent(newContent);
313                    }
314    
315                    Date displayDate = article.getDisplayDate();
316    
317                    int displayDateMonth = 0;
318                    int displayDateDay = 0;
319                    int displayDateYear = 0;
320                    int displayDateHour = 0;
321                    int displayDateMinute = 0;
322    
323                    if (displayDate != null) {
324                            Calendar displayCal = CalendarFactoryUtil.getCalendar(
325                                    user.getTimeZone());
326    
327                            displayCal.setTime(displayDate);
328    
329                            displayDateMonth = displayCal.get(Calendar.MONTH);
330                            displayDateDay = displayCal.get(Calendar.DATE);
331                            displayDateYear = displayCal.get(Calendar.YEAR);
332                            displayDateHour = displayCal.get(Calendar.HOUR);
333                            displayDateMinute = displayCal.get(Calendar.MINUTE);
334    
335                            if (displayCal.get(Calendar.AM_PM) == Calendar.PM) {
336                                    displayDateHour += 12;
337                            }
338                    }
339    
340                    Date expirationDate = article.getExpirationDate();
341    
342                    int expirationDateMonth = 0;
343                    int expirationDateDay = 0;
344                    int expirationDateYear = 0;
345                    int expirationDateHour = 0;
346                    int expirationDateMinute = 0;
347                    boolean neverExpire = true;
348    
349                    if (expirationDate != null) {
350                            Calendar expirationCal = CalendarFactoryUtil.getCalendar(
351                                    user.getTimeZone());
352    
353                            expirationCal.setTime(expirationDate);
354    
355                            expirationDateMonth = expirationCal.get(Calendar.MONTH);
356                            expirationDateDay = expirationCal.get(Calendar.DATE);
357                            expirationDateYear = expirationCal.get(Calendar.YEAR);
358                            expirationDateHour = expirationCal.get(Calendar.HOUR);
359                            expirationDateMinute = expirationCal.get(Calendar.MINUTE);
360                            neverExpire = false;
361    
362                            if (expirationCal.get(Calendar.AM_PM) == Calendar.PM) {
363                                    expirationDateHour += 12;
364                            }
365                    }
366    
367                    Date reviewDate = article.getReviewDate();
368    
369                    int reviewDateMonth = 0;
370                    int reviewDateDay = 0;
371                    int reviewDateYear = 0;
372                    int reviewDateHour = 0;
373                    int reviewDateMinute = 0;
374                    boolean neverReview = true;
375    
376                    if (reviewDate != null) {
377                            Calendar reviewCal = CalendarFactoryUtil.getCalendar(
378                                    user.getTimeZone());
379    
380                            reviewCal.setTime(reviewDate);
381    
382                            reviewDateMonth = reviewCal.get(Calendar.MONTH);
383                            reviewDateDay = reviewCal.get(Calendar.DATE);
384                            reviewDateYear = reviewCal.get(Calendar.YEAR);
385                            reviewDateHour = reviewCal.get(Calendar.HOUR);
386                            reviewDateMinute = reviewCal.get(Calendar.MINUTE);
387                            neverReview = false;
388    
389                            if (reviewCal.get(Calendar.AM_PM) == Calendar.PM) {
390                                    reviewDateHour += 12;
391                            }
392                    }
393    
394                    String parentDDMStructureKey = StringPool.BLANK;
395    
396                    long ddmStructureId = 0;
397    
398                    List<Element> structureElements =
399                            portletDataContext.getReferenceDataElements(
400                                    article, DDMStructure.class);
401    
402                    if (!structureElements.isEmpty()) {
403                            Element structureElement = structureElements.get(0);
404    
405                            String structurePath = structureElement.attributeValue("path");
406    
407                            DDMStructure ddmStructure =
408                                    (DDMStructure)portletDataContext.getZipEntryAsObject(
409                                            structurePath);
410    
411                            DDMStructure existingDDMStructure =
412                                    DDMStructureLocalServiceUtil.fetchDDMStructureByUuidAndGroupId(
413                                            ddmStructure.getUuid(),
414                                            portletDataContext.getScopeGroupId());
415    
416                            if (existingDDMStructure == null) {
417                                    existingDDMStructure =
418                                            DDMStructureLocalServiceUtil.
419                                                    fetchDDMStructureByUuidAndGroupId(
420                                                            ddmStructure.getUuid(),
421                                                            portletDataContext.getCompanyGroupId());
422                            }
423    
424                            if (existingDDMStructure == null) {
425                                    StagedModelDataHandlerUtil.importStagedModel(
426                                            portletDataContext, ddmStructure);
427    
428                                    Map<Long, Long> ddmStructureIds =
429                                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
430                                                    DDMStructure.class);
431    
432                                    ddmStructureId = MapUtil.getLong(
433                                            ddmStructureIds, ddmStructure.getStructureId(),
434                                            ddmStructure.getStructureId());
435    
436                                    existingDDMStructure =
437                                            DDMStructureLocalServiceUtil.getDDMStructure(
438                                                    ddmStructureId);
439                            }
440    
441                            if (existingDDMStructure == null) {
442                                    if (_log.isWarnEnabled()) {
443                                            StringBundler sb = new StringBundler();
444    
445                                            sb.append("Structure ");
446                                            sb.append(article.getStructureId());
447                                            sb.append(" is missing for article ");
448                                            sb.append(article.getArticleId());
449                                            sb.append(", skipping this article.");
450    
451                                            _log.warn(sb.toString());
452                                    }
453    
454                                    return;
455                            }
456    
457                            parentDDMStructureKey = existingDDMStructure.getStructureKey();
458                    }
459    
460                    String parentDDMTemplateKey = StringPool.BLANK;
461    
462                    List<Element> ddmTemplateElements =
463                            portletDataContext.getReferenceDataElements(
464                                    article, DDMTemplate.class);
465    
466                    if (!ddmTemplateElements.isEmpty()) {
467                            Element templateElement = ddmTemplateElements.get(0);
468    
469                            String ddmTemplatePath = templateElement.attributeValue("path");
470    
471                            DDMTemplate ddmTemplate =
472                                    (DDMTemplate)portletDataContext.getZipEntryAsObject(
473                                            ddmTemplatePath);
474    
475                            DDMTemplate existingDDMTemplate =
476                                    DDMTemplateLocalServiceUtil.fetchDDMTemplateByUuidAndGroupId(
477                                            ddmTemplate.getUuid(),
478                                            portletDataContext.getScopeGroupId());
479    
480                            if (existingDDMTemplate == null) {
481                                    existingDDMTemplate =
482                                            DDMTemplateLocalServiceUtil.
483                                                    fetchDDMTemplateByUuidAndGroupId(
484                                                            ddmTemplate.getUuid(),
485                                                            portletDataContext.getCompanyGroupId());
486                            }
487    
488                            if (existingDDMTemplate == null) {
489                                    StagedModelDataHandlerUtil.importStagedModel(
490                                            portletDataContext, ddmTemplate);
491    
492                                    Map<Long, Long> ddmTemplateIds =
493                                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
494                                                    DDMTemplate.class);
495    
496                                    long ddmTemplateId = MapUtil.getLong(
497                                            ddmTemplateIds, ddmTemplate.getTemplateId(),
498                                            ddmTemplate.getTemplateId());
499    
500                                    existingDDMTemplate =
501                                            DDMTemplateLocalServiceUtil.getDDMTemplate(ddmTemplateId);
502                            }
503    
504                            if (existingDDMTemplate == null) {
505                                    if (_log.isWarnEnabled()) {
506                                            StringBundler sb = new StringBundler();
507    
508                                            sb.append("Template ");
509                                            sb.append(article.getTemplateId());
510                                            sb.append(" is missing for article ");
511                                            sb.append(article.getArticleId());
512                                            sb.append(", skipping this article.");
513    
514                                            _log.warn(sb.toString());
515                                    }
516    
517                                    return;
518                            }
519    
520                            parentDDMTemplateKey = existingDDMTemplate.getTemplateKey();
521                    }
522    
523                    File smallFile = null;
524    
525                    try {
526                            if (article.isSmallImage()) {
527                                    String smallImagePath = articleElement.attributeValue(
528                                            "small-image-path");
529    
530                                    if (Validator.isNotNull(article.getSmallImageURL())) {
531                                            String smallImageURL =
532                                                    ExportImportHelperUtil.replaceImportContentReferences(
533                                                            portletDataContext, articleElement,
534                                                            article.getSmallImageURL(), true);
535    
536                                            article.setSmallImageURL(smallImageURL);
537                                    }
538                                    else if (Validator.isNotNull(smallImagePath)) {
539                                            byte[] bytes = portletDataContext.getZipEntryAsByteArray(
540                                                    smallImagePath);
541    
542                                            if (bytes != null) {
543                                                    smallFile = FileUtil.createTempFile(
544                                                            article.getSmallImageType());
545    
546                                                    FileUtil.write(smallFile, bytes);
547                                            }
548                                    }
549                            }
550    
551                            Map<String, byte[]> images = new HashMap<String, byte[]>();
552    
553                            List<Element> imagesElements =
554                                    portletDataContext.getReferenceDataElements(
555                                            article, Image.class);
556    
557                            for (Element imageElement : imagesElements) {
558                                    String imagePath = imageElement.attributeValue("path");
559    
560                                    String fileName = imageElement.attributeValue("file-name");
561    
562                                    images.put(
563                                            fileName,
564                                            portletDataContext.getZipEntryAsByteArray(imagePath));
565                            }
566    
567                            String articleURL = null;
568    
569                            boolean addGroupPermissions = creationStrategy.addGroupPermissions(
570                                    portletDataContext, article);
571                            boolean addGuestPermissions = creationStrategy.addGuestPermissions(
572                                    portletDataContext, article);
573    
574                            ServiceContext serviceContext =
575                                    portletDataContext.createServiceContext(
576                                            article, JournalPortletDataHandler.NAMESPACE);
577    
578                            serviceContext.setAddGroupPermissions(addGroupPermissions);
579                            serviceContext.setAddGuestPermissions(addGuestPermissions);
580    
581                            if (article.getStatus() != WorkflowConstants.STATUS_APPROVED) {
582                                    serviceContext.setWorkflowAction(
583                                            WorkflowConstants.ACTION_SAVE_DRAFT);
584                            }
585    
586                            JournalArticle importedArticle = null;
587    
588                            String articleResourceUuid = articleElement.attributeValue(
589                                    "article-resource-uuid");
590    
591                            if (portletDataContext.isDataStrategyMirror()) {
592                                    JournalArticleResource articleResource =
593                                            JournalArticleResourceLocalServiceUtil.
594                                                    fetchJournalArticleResourceByUuidAndGroupId(
595                                                            articleResourceUuid,
596                                                            portletDataContext.getScopeGroupId());
597    
598                                    if (articleResource == null) {
599                                            articleResource =
600                                                    JournalArticleResourceLocalServiceUtil.
601                                                            fetchJournalArticleResourceByUuidAndGroupId(
602                                                                    articleResourceUuid,
603                                                                    portletDataContext.getCompanyGroupId());
604                                    }
605    
606                                    serviceContext.setUuid(articleResourceUuid);
607                                    serviceContext.setAttribute("urlTitle", article.getUrlTitle());
608    
609                                    JournalArticle existingArticle = null;
610    
611                                    if (articleResource != null) {
612                                            try {
613                                                    existingArticle =
614                                                            JournalArticleLocalServiceUtil.getLatestArticle(
615                                                                    articleResource.getResourcePrimKey(),
616                                                                    WorkflowConstants.STATUS_ANY, false);
617                                            }
618                                            catch (NoSuchArticleException nsae) {
619                                            }
620                                    }
621    
622                                    if (existingArticle == null) {
623                                            existingArticle =
624                                                    JournalArticleLocalServiceUtil.fetchArticle(
625                                                            portletDataContext.getScopeGroupId(), newArticleId,
626                                                            article.getVersion());
627                                    }
628    
629                                    if (existingArticle == null) {
630                                            importedArticle =
631                                                    JournalArticleLocalServiceUtil.addArticle(
632                                                            userId, portletDataContext.getScopeGroupId(),
633                                                            folderId, article.getClassNameId(), ddmStructureId,
634                                                            articleId, autoArticleId, article.getVersion(),
635                                                            article.getTitleMap(), article.getDescriptionMap(),
636                                                            article.getContent(), article.getType(),
637                                                            parentDDMStructureKey, parentDDMTemplateKey,
638                                                            article.getLayoutUuid(), displayDateMonth,
639                                                            displayDateDay, displayDateYear, displayDateHour,
640                                                            displayDateMinute, expirationDateMonth,
641                                                            expirationDateDay, expirationDateYear,
642                                                            expirationDateHour, expirationDateMinute,
643                                                            neverExpire, reviewDateMonth, reviewDateDay,
644                                                            reviewDateYear, reviewDateHour, reviewDateMinute,
645                                                            neverReview, article.isIndexable(),
646                                                            article.isSmallImage(), article.getSmallImageURL(),
647                                                            smallFile, images, articleURL, serviceContext);
648                                    }
649                                    else if (portletDataContext.isCompanyStagedGroupedModel(
650                                                            existingArticle)) {
651    
652                                            return;
653                                    }
654                                    else {
655                                            importedArticle =
656                                                    JournalArticleLocalServiceUtil.updateArticle(
657                                                            userId, existingArticle.getGroupId(), folderId,
658                                                            existingArticle.getArticleId(),
659                                                            article.getVersion(), article.getTitleMap(),
660                                                            article.getDescriptionMap(), article.getContent(),
661                                                            article.getType(), parentDDMStructureKey,
662                                                            parentDDMTemplateKey, article.getLayoutUuid(),
663                                                            displayDateMonth, displayDateDay, displayDateYear,
664                                                            displayDateHour, displayDateMinute,
665                                                            expirationDateMonth, expirationDateDay,
666                                                            expirationDateYear, expirationDateHour,
667                                                            expirationDateMinute, neverExpire, reviewDateMonth,
668                                                            reviewDateDay, reviewDateYear, reviewDateHour,
669                                                            reviewDateMinute, neverReview,
670                                                            article.isIndexable(), article.isSmallImage(),
671                                                            article.getSmallImageURL(), smallFile, images,
672                                                            articleURL, serviceContext);
673                                    }
674                            }
675                            else {
676                                    importedArticle = JournalArticleLocalServiceUtil.addArticle(
677                                            userId, portletDataContext.getScopeGroupId(), folderId,
678                                            article.getClassNameId(), ddmStructureId, articleId,
679                                            autoArticleId, article.getVersion(), article.getTitleMap(),
680                                            article.getDescriptionMap(), article.getContent(),
681                                            article.getType(), parentDDMStructureKey,
682                                            parentDDMTemplateKey, article.getLayoutUuid(),
683                                            displayDateMonth, displayDateDay, displayDateYear,
684                                            displayDateHour, displayDateMinute, expirationDateMonth,
685                                            expirationDateDay, expirationDateYear, expirationDateHour,
686                                            expirationDateMinute, neverExpire, reviewDateMonth,
687                                            reviewDateDay, reviewDateYear, reviewDateHour,
688                                            reviewDateMinute, neverReview, article.isIndexable(),
689                                            article.isSmallImage(), article.getSmallImageURL(),
690                                            smallFile, images, articleURL, serviceContext);
691                            }
692    
693                            portletDataContext.importClassedModel(
694                                    article, importedArticle, JournalPortletDataHandler.NAMESPACE);
695    
696                            if (Validator.isNull(newArticleId)) {
697                                    articleIds.put(
698                                            article.getArticleId(), importedArticle.getArticleId());
699                            }
700                    }
701                    finally {
702                            if (smallFile != null) {
703                                    smallFile.delete();
704                            }
705                    }
706            }
707    
708            protected void exportArticleImage(
709                            PortletDataContext portletDataContext,
710                            JournalArticleImage articleImage, JournalArticle article,
711                            Element articleElement)
712                    throws SystemException {
713    
714                    Image image = ImageLocalServiceUtil.fetchImage(
715                            articleImage.getArticleImageId());
716    
717                    if ((image == null) || (image.getTextObj() == null)) {
718                            return;
719                    }
720    
721                    String fileName =
722                            image.getImageId() + StringPool.PERIOD + image.getType();
723    
724                    String articleImagePath = ExportImportPathUtil.getModelPath(
725                            article, fileName);
726    
727                    if (!portletDataContext.isPathNotProcessed(articleImagePath)) {
728                            return;
729                    }
730    
731                    Element imageElement = portletDataContext.getExportDataElement(image);
732    
733                    imageElement.addAttribute("path", articleImagePath);
734    
735                    if (Validator.isNotNull(fileName)) {
736                            imageElement.addAttribute("file-name", fileName);
737                    }
738    
739                    portletDataContext.addZipEntry(articleImagePath, image.getTextObj());
740    
741                    portletDataContext.addReferenceElement(
742                            article, articleElement, image, articleImagePath, false);
743            }
744    
745            protected void prepareLanguagesForImport(JournalArticle article)
746                    throws PortalException {
747    
748                    Locale articleDefaultLocale = LocaleUtil.fromLanguageId(
749                            article.getDefaultLanguageId());
750    
751                    Locale[] articleAvailableLocales = LocaleUtil.fromLanguageIds(
752                            article.getAvailableLanguageIds());
753    
754                    Locale defaultImportLocale = LocalizationUtil.getDefaultImportLocale(
755                            JournalArticle.class.getName(), article.getPrimaryKey(),
756                            articleDefaultLocale, articleAvailableLocales);
757    
758                    article.prepareLocalizedFieldsForImport(defaultImportLocale);
759            }
760    
761            @Override
762            protected boolean validateMissingReference(
763                            String uuid, long companyId, long groupId)
764                    throws Exception {
765    
766                    JournalArticle article =
767                            JournalArticleLocalServiceUtil.fetchJournalArticleByUuidAndGroupId(
768                                    uuid, groupId);
769    
770                    if (article == null) {
771                            return false;
772                    }
773    
774                    return true;
775            }
776    
777            private static Log _log = LogFactoryUtil.getLog(
778                    JournalArticleStagedModelDataHandler.class);
779    
780    }