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