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