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