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