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