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