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.util.CalendarFactoryUtil;
027    import com.liferay.portal.kernel.util.FileUtil;
028    import com.liferay.portal.kernel.util.LocaleUtil;
029    import com.liferay.portal.kernel.util.LocalizationUtil;
030    import com.liferay.portal.kernel.util.MapUtil;
031    import com.liferay.portal.kernel.util.StringPool;
032    import com.liferay.portal.kernel.util.Validator;
033    import com.liferay.portal.kernel.workflow.WorkflowConstants;
034    import com.liferay.portal.kernel.xml.Element;
035    import com.liferay.portal.model.Image;
036    import com.liferay.portal.model.User;
037    import com.liferay.portal.service.ImageLocalServiceUtil;
038    import com.liferay.portal.service.ServiceContext;
039    import com.liferay.portal.service.UserLocalServiceUtil;
040    import com.liferay.portal.util.PortalUtil;
041    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
042    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
043    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
044    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
045    import com.liferay.portlet.journal.NoSuchArticleException;
046    import com.liferay.portlet.journal.model.JournalArticle;
047    import com.liferay.portlet.journal.model.JournalArticleConstants;
048    import com.liferay.portlet.journal.model.JournalArticleImage;
049    import com.liferay.portlet.journal.model.JournalArticleResource;
050    import com.liferay.portlet.journal.model.JournalFolder;
051    import com.liferay.portlet.journal.model.JournalFolderConstants;
052    import com.liferay.portlet.journal.service.JournalArticleImageLocalServiceUtil;
053    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
054    import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
055    
056    import java.io.File;
057    
058    import java.util.Calendar;
059    import java.util.Date;
060    import java.util.HashMap;
061    import java.util.List;
062    import java.util.Locale;
063    import java.util.Map;
064    
065    /**
066     * @author Daniel Kocsis
067     * @author Mate Thurzo
068     */
069    public class JournalArticleStagedModelDataHandler
070            extends BaseStagedModelDataHandler<JournalArticle> {
071    
072            public static final String[] CLASS_NAMES = {JournalArticle.class.getName()};
073    
074            @Override
075            public void deleteStagedModel(
076                            String uuid, long groupId, String className, String extraData)
077                    throws PortalException, SystemException {
078    
079                    JournalArticleResource articleResource =
080                            JournalArticleResourceLocalServiceUtil.fetchArticleResource(
081                                    uuid, groupId);
082    
083                    if (articleResource == null) {
084                            return;
085                    }
086    
087                    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject(
088                            extraData);
089    
090                    if (Validator.isNull(extraData) ||
091                            extraDataJSONObject.getBoolean("inTrash")) {
092    
093                            JournalArticleLocalServiceUtil.deleteArticle(
094                                    groupId, articleResource.getArticleId(), null);
095                    }
096                    else {
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 doImportCompanyStagedModel(
235                            PortletDataContext portletDataContext, JournalArticle article)
236                    throws Exception {
237    
238                    JournalArticleResource existingArticleResource =
239                            JournalArticleResourceLocalServiceUtil.
240                                    fetchJournalArticleResourceByUuidAndGroupId(
241                                            article.getArticleResourceUuid(),
242                                            portletDataContext.getCompanyGroupId());
243    
244                    JournalArticle existingArticle =
245                            JournalArticleLocalServiceUtil.getLatestArticle(
246                                    existingArticleResource.getResourcePrimKey(),
247                                    WorkflowConstants.STATUS_ANY, false);
248    
249                    Map<String, String> articleArticleIds =
250                            (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
251                                    JournalArticle.class + ".articleId");
252    
253                    articleArticleIds.put(
254                            article.getArticleId(), existingArticle.getArticleId());
255    
256                    Map<Long, Long> articleIds =
257                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
258                                    JournalArticle.class);
259    
260                    articleIds.put(article.getId(), existingArticle.getId());
261            }
262    
263            @Override
264            protected void doImportStagedModel(
265                            PortletDataContext portletDataContext, JournalArticle article)
266                    throws Exception {
267    
268                    prepareLanguagesForImport(article);
269    
270                    long userId = portletDataContext.getUserId(article.getUserUuid());
271    
272                    JournalCreationStrategy creationStrategy =
273                            JournalCreationStrategyFactory.getInstance();
274    
275                    long authorId = creationStrategy.getAuthorUserId(
276                            portletDataContext, article);
277    
278                    if (authorId != JournalCreationStrategy.USE_DEFAULT_USER_ID_STRATEGY) {
279                            userId = authorId;
280                    }
281    
282                    User user = UserLocalServiceUtil.getUser(userId);
283    
284                    Map<Long, Long> folderIds =
285                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
286                                    JournalFolder.class);
287    
288                    if (article.getFolderId() !=
289                                    JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
290    
291                            Element folderElement = portletDataContext.getReferenceDataElement(
292                                    article, JournalFolder.class, article.getFolderId());
293    
294                            StagedModelDataHandlerUtil.importStagedModel(
295                                    portletDataContext, folderElement);
296                    }
297    
298                    long folderId = MapUtil.getLong(
299                            folderIds, article.getFolderId(), article.getFolderId());
300    
301                    String articleId = article.getArticleId();
302    
303                    boolean autoArticleId = false;
304    
305                    if (Validator.isNumber(articleId) ||
306                            (JournalArticleLocalServiceUtil.fetchArticle(
307                                    portletDataContext.getScopeGroupId(), articleId,
308                                    JournalArticleConstants.VERSION_DEFAULT) != null)) {
309    
310                            autoArticleId = true;
311                    }
312    
313                    Map<String, String> articleIds =
314                            (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
315                                    JournalArticle.class + ".articleId");
316    
317                    String newArticleId = articleIds.get(articleId);
318    
319                    if (Validator.isNotNull(newArticleId)) {
320    
321                            // A sibling of a different version was already assigned a new
322                            // article id
323    
324                            articleId = newArticleId;
325                            autoArticleId = false;
326                    }
327    
328                    String content = article.getContent();
329    
330                    Element articleElement =
331                            portletDataContext.getImportDataStagedModelElement(article);
332    
333                    content = ExportImportHelperUtil.replaceImportContentReferences(
334                            portletDataContext, articleElement, content, true);
335    
336                    article.setContent(content);
337    
338                    String newContent = creationStrategy.getTransformedContent(
339                            portletDataContext, article);
340    
341                    if (newContent != JournalCreationStrategy.ARTICLE_CONTENT_UNCHANGED) {
342                            article.setContent(newContent);
343                    }
344    
345                    Date displayDate = article.getDisplayDate();
346    
347                    int displayDateMonth = 0;
348                    int displayDateDay = 0;
349                    int displayDateYear = 0;
350                    int displayDateHour = 0;
351                    int displayDateMinute = 0;
352    
353                    if (displayDate != null) {
354                            Calendar displayCal = CalendarFactoryUtil.getCalendar(
355                                    user.getTimeZone());
356    
357                            displayCal.setTime(displayDate);
358    
359                            displayDateMonth = displayCal.get(Calendar.MONTH);
360                            displayDateDay = displayCal.get(Calendar.DATE);
361                            displayDateYear = displayCal.get(Calendar.YEAR);
362                            displayDateHour = displayCal.get(Calendar.HOUR);
363                            displayDateMinute = displayCal.get(Calendar.MINUTE);
364    
365                            if (displayCal.get(Calendar.AM_PM) == Calendar.PM) {
366                                    displayDateHour += 12;
367                            }
368                    }
369    
370                    Date expirationDate = article.getExpirationDate();
371    
372                    int expirationDateMonth = 0;
373                    int expirationDateDay = 0;
374                    int expirationDateYear = 0;
375                    int expirationDateHour = 0;
376                    int expirationDateMinute = 0;
377                    boolean neverExpire = true;
378    
379                    if (expirationDate != null) {
380                            Calendar expirationCal = CalendarFactoryUtil.getCalendar(
381                                    user.getTimeZone());
382    
383                            expirationCal.setTime(expirationDate);
384    
385                            expirationDateMonth = expirationCal.get(Calendar.MONTH);
386                            expirationDateDay = expirationCal.get(Calendar.DATE);
387                            expirationDateYear = expirationCal.get(Calendar.YEAR);
388                            expirationDateHour = expirationCal.get(Calendar.HOUR);
389                            expirationDateMinute = expirationCal.get(Calendar.MINUTE);
390                            neverExpire = false;
391    
392                            if (expirationCal.get(Calendar.AM_PM) == Calendar.PM) {
393                                    expirationDateHour += 12;
394                            }
395                    }
396    
397                    Date reviewDate = article.getReviewDate();
398    
399                    int reviewDateMonth = 0;
400                    int reviewDateDay = 0;
401                    int reviewDateYear = 0;
402                    int reviewDateHour = 0;
403                    int reviewDateMinute = 0;
404                    boolean neverReview = true;
405    
406                    if (reviewDate != null) {
407                            Calendar reviewCal = CalendarFactoryUtil.getCalendar(
408                                    user.getTimeZone());
409    
410                            reviewCal.setTime(reviewDate);
411    
412                            reviewDateMonth = reviewCal.get(Calendar.MONTH);
413                            reviewDateDay = reviewCal.get(Calendar.DATE);
414                            reviewDateYear = reviewCal.get(Calendar.YEAR);
415                            reviewDateHour = reviewCal.get(Calendar.HOUR);
416                            reviewDateMinute = reviewCal.get(Calendar.MINUTE);
417                            neverReview = false;
418    
419                            if (reviewCal.get(Calendar.AM_PM) == Calendar.PM) {
420                                    reviewDateHour += 12;
421                            }
422                    }
423    
424                    String parentDDMStructureKey = StringPool.BLANK;
425    
426                    long ddmStructureId = 0;
427    
428                    List<Element> structureElements =
429                            portletDataContext.getReferenceDataElements(
430                                    article, DDMStructure.class);
431    
432                    if (!structureElements.isEmpty()) {
433                            Element structureElement = structureElements.get(0);
434    
435                            String structurePath = structureElement.attributeValue("path");
436    
437                            DDMStructure ddmStructure =
438                                    (DDMStructure)portletDataContext.getZipEntryAsObject(
439                                            structurePath);
440    
441                            StagedModelDataHandlerUtil.importReferenceStagedModel(
442                                    portletDataContext, ddmStructure);
443    
444                            Map<String, String> ddmStructureKeys =
445                                    (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
446                                            DDMStructure.class + ".ddmStructureKey");
447    
448                            parentDDMStructureKey = MapUtil.getString(
449                                    ddmStructureKeys, ddmStructure.getStructureKey(),
450                                    ddmStructure.getStructureKey());
451                    }
452    
453                    String parentDDMTemplateKey = StringPool.BLANK;
454    
455                    List<Element> ddmTemplateElements =
456                            portletDataContext.getReferenceDataElements(
457                                    article, DDMTemplate.class);
458    
459                    if (!ddmTemplateElements.isEmpty()) {
460                            Element templateElement = ddmTemplateElements.get(0);
461    
462                            String ddmTemplatePath = templateElement.attributeValue("path");
463    
464                            DDMTemplate ddmTemplate =
465                                    (DDMTemplate)portletDataContext.getZipEntryAsObject(
466                                            ddmTemplatePath);
467    
468                            StagedModelDataHandlerUtil.importReferenceStagedModel(
469                                    portletDataContext, ddmTemplate);
470    
471                            Map<String, String> ddmTemplateKeys =
472                                    (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
473                                            DDMTemplate.class + ".ddmTemplateKey");
474    
475                            parentDDMTemplateKey = MapUtil.getString(
476                                    ddmTemplateKeys, ddmTemplate.getTemplateKey(),
477                                    ddmTemplate.getTemplateKey());
478                    }
479    
480                    File smallFile = null;
481    
482                    try {
483                            if (article.isSmallImage()) {
484                                    String smallImagePath = articleElement.attributeValue(
485                                            "small-image-path");
486    
487                                    if (Validator.isNotNull(article.getSmallImageURL())) {
488                                            String smallImageURL =
489                                                    ExportImportHelperUtil.replaceImportContentReferences(
490                                                            portletDataContext, articleElement,
491                                                            article.getSmallImageURL(), true);
492    
493                                            article.setSmallImageURL(smallImageURL);
494                                    }
495                                    else if (Validator.isNotNull(smallImagePath)) {
496                                            byte[] bytes = portletDataContext.getZipEntryAsByteArray(
497                                                    smallImagePath);
498    
499                                            if (bytes != null) {
500                                                    smallFile = FileUtil.createTempFile(
501                                                            article.getSmallImageType());
502    
503                                                    FileUtil.write(smallFile, bytes);
504                                            }
505                                    }
506                            }
507    
508                            Map<String, byte[]> images = new HashMap<String, byte[]>();
509    
510                            List<Element> imagesElements =
511                                    portletDataContext.getReferenceDataElements(
512                                            article, Image.class);
513    
514                            for (Element imageElement : imagesElements) {
515                                    String imagePath = imageElement.attributeValue("path");
516    
517                                    String fileName = imageElement.attributeValue("file-name");
518    
519                                    images.put(
520                                            fileName,
521                                            portletDataContext.getZipEntryAsByteArray(imagePath));
522                            }
523    
524                            String articleURL = null;
525    
526                            boolean addGroupPermissions = creationStrategy.addGroupPermissions(
527                                    portletDataContext, article);
528                            boolean addGuestPermissions = creationStrategy.addGuestPermissions(
529                                    portletDataContext, article);
530    
531                            ServiceContext serviceContext =
532                                    portletDataContext.createServiceContext(
533                                            article, JournalPortletDataHandler.NAMESPACE);
534    
535                            serviceContext.setAddGroupPermissions(addGroupPermissions);
536                            serviceContext.setAddGuestPermissions(addGuestPermissions);
537    
538                            if (article.getStatus() != WorkflowConstants.STATUS_APPROVED) {
539                                    serviceContext.setWorkflowAction(
540                                            WorkflowConstants.ACTION_SAVE_DRAFT);
541                            }
542    
543                            JournalArticle importedArticle = null;
544    
545                            String articleResourceUuid = articleElement.attributeValue(
546                                    "article-resource-uuid");
547    
548                            if (portletDataContext.isDataStrategyMirror()) {
549                                    JournalArticleResource articleResource =
550                                            JournalArticleResourceLocalServiceUtil.
551                                                    fetchJournalArticleResourceByUuidAndGroupId(
552                                                            articleResourceUuid,
553                                                            portletDataContext.getScopeGroupId());
554    
555                                    serviceContext.setUuid(articleResourceUuid);
556                                    serviceContext.setAttribute("urlTitle", article.getUrlTitle());
557    
558                                    JournalArticle existingArticle = null;
559    
560                                    if (articleResource != null) {
561                                            try {
562                                                    existingArticle =
563                                                            JournalArticleLocalServiceUtil.getLatestArticle(
564                                                                    articleResource.getResourcePrimKey(),
565                                                                    WorkflowConstants.STATUS_ANY, false);
566                                            }
567                                            catch (NoSuchArticleException nsae) {
568                                            }
569                                    }
570    
571                                    if (existingArticle == null) {
572                                            existingArticle =
573                                                    JournalArticleLocalServiceUtil.fetchArticle(
574                                                            portletDataContext.getScopeGroupId(), newArticleId,
575                                                            article.getVersion());
576                                    }
577    
578                                    if (existingArticle == null) {
579                                            importedArticle =
580                                                    JournalArticleLocalServiceUtil.addArticle(
581                                                            userId, portletDataContext.getScopeGroupId(),
582                                                            folderId, article.getClassNameId(), ddmStructureId,
583                                                            articleId, autoArticleId, article.getVersion(),
584                                                            article.getTitleMap(), article.getDescriptionMap(),
585                                                            article.getContent(), article.getType(),
586                                                            parentDDMStructureKey, parentDDMTemplateKey,
587                                                            article.getLayoutUuid(), displayDateMonth,
588                                                            displayDateDay, displayDateYear, displayDateHour,
589                                                            displayDateMinute, expirationDateMonth,
590                                                            expirationDateDay, expirationDateYear,
591                                                            expirationDateHour, expirationDateMinute,
592                                                            neverExpire, reviewDateMonth, reviewDateDay,
593                                                            reviewDateYear, reviewDateHour, reviewDateMinute,
594                                                            neverReview, article.isIndexable(),
595                                                            article.isSmallImage(), article.getSmallImageURL(),
596                                                            smallFile, images, articleURL, serviceContext);
597                                    }
598                                    else {
599                                            importedArticle =
600                                                    JournalArticleLocalServiceUtil.updateArticle(
601                                                            userId, existingArticle.getGroupId(), folderId,
602                                                            existingArticle.getArticleId(),
603                                                            article.getVersion(), article.getTitleMap(),
604                                                            article.getDescriptionMap(), article.getContent(),
605                                                            article.getType(), parentDDMStructureKey,
606                                                            parentDDMTemplateKey, article.getLayoutUuid(),
607                                                            displayDateMonth, displayDateDay, displayDateYear,
608                                                            displayDateHour, displayDateMinute,
609                                                            expirationDateMonth, expirationDateDay,
610                                                            expirationDateYear, expirationDateHour,
611                                                            expirationDateMinute, neverExpire, reviewDateMonth,
612                                                            reviewDateDay, reviewDateYear, reviewDateHour,
613                                                            reviewDateMinute, neverReview,
614                                                            article.isIndexable(), article.isSmallImage(),
615                                                            article.getSmallImageURL(), smallFile, images,
616                                                            articleURL, serviceContext);
617                                    }
618                            }
619                            else {
620                                    importedArticle = JournalArticleLocalServiceUtil.addArticle(
621                                            userId, portletDataContext.getScopeGroupId(), folderId,
622                                            article.getClassNameId(), ddmStructureId, articleId,
623                                            autoArticleId, article.getVersion(), article.getTitleMap(),
624                                            article.getDescriptionMap(), article.getContent(),
625                                            article.getType(), parentDDMStructureKey,
626                                            parentDDMTemplateKey, article.getLayoutUuid(),
627                                            displayDateMonth, displayDateDay, displayDateYear,
628                                            displayDateHour, displayDateMinute, expirationDateMonth,
629                                            expirationDateDay, expirationDateYear, expirationDateHour,
630                                            expirationDateMinute, neverExpire, reviewDateMonth,
631                                            reviewDateDay, reviewDateYear, reviewDateHour,
632                                            reviewDateMinute, neverReview, article.isIndexable(),
633                                            article.isSmallImage(), article.getSmallImageURL(),
634                                            smallFile, images, articleURL, serviceContext);
635                            }
636    
637                            portletDataContext.importClassedModel(
638                                    article, importedArticle, JournalPortletDataHandler.NAMESPACE);
639    
640                            if (Validator.isNull(newArticleId)) {
641                                    articleIds.put(
642                                            article.getArticleId(), importedArticle.getArticleId());
643                            }
644                    }
645                    finally {
646                            if (smallFile != null) {
647                                    smallFile.delete();
648                            }
649                    }
650            }
651    
652            protected void exportArticleImage(
653                            PortletDataContext portletDataContext,
654                            JournalArticleImage articleImage, JournalArticle article,
655                            Element articleElement)
656                    throws SystemException {
657    
658                    Image image = ImageLocalServiceUtil.fetchImage(
659                            articleImage.getArticleImageId());
660    
661                    if ((image == null) || (image.getTextObj() == null)) {
662                            return;
663                    }
664    
665                    String fileName =
666                            image.getImageId() + StringPool.PERIOD + image.getType();
667    
668                    String articleImagePath = ExportImportPathUtil.getModelPath(
669                            article, fileName);
670    
671                    if (!portletDataContext.isPathNotProcessed(articleImagePath)) {
672                            return;
673                    }
674    
675                    Element imageElement = portletDataContext.getExportDataElement(image);
676    
677                    imageElement.addAttribute("path", articleImagePath);
678    
679                    if (Validator.isNotNull(fileName)) {
680                            imageElement.addAttribute("file-name", fileName);
681                    }
682    
683                    portletDataContext.addZipEntry(articleImagePath, image.getTextObj());
684    
685                    portletDataContext.addReferenceElement(
686                            article, articleElement, image, articleImagePath, false);
687            }
688    
689            protected void prepareLanguagesForImport(JournalArticle article)
690                    throws PortalException {
691    
692                    Locale articleDefaultLocale = LocaleUtil.fromLanguageId(
693                            article.getDefaultLanguageId());
694    
695                    Locale[] articleAvailableLocales = LocaleUtil.fromLanguageIds(
696                            article.getAvailableLanguageIds());
697    
698                    Locale defaultImportLocale = LocalizationUtil.getDefaultImportLocale(
699                            JournalArticle.class.getName(), article.getPrimaryKey(),
700                            articleDefaultLocale, articleAvailableLocales);
701    
702                    article.prepareLocalizedFieldsForImport(defaultImportLocale);
703            }
704    
705            @Override
706            protected boolean validateMissingReference(
707                            String uuid, long companyId, long groupId)
708                    throws Exception {
709    
710                    JournalArticle article =
711                            JournalArticleLocalServiceUtil.fetchJournalArticleByUuidAndGroupId(
712                                    uuid, groupId);
713    
714                    if (article == null) {
715                            return false;
716                    }
717    
718                    return true;
719            }
720    
721    }