From 81b1a40f425594ce2ca3bddc250385a4a6bf0c20 Mon Sep 17 00:00:00 2001 From: Helmi Akermi Date: Fri, 19 Jul 2024 05:41:29 +0100 Subject: [PATCH] Fix: Update the NewsArticlesUpgrade plugin - EXO-71928 - Meeds-io/MIPs#128 --- .../news/upgrade/jcr/NewsArticlesUpgrade.java | 26 +++++++++++++++++-- .../upgrade/jcr/NewsArticlesUpgradeTest.java | 11 +++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/data-upgrade-news/src/main/java/org/exoplatform/news/upgrade/jcr/NewsArticlesUpgrade.java b/data-upgrade-news/src/main/java/org/exoplatform/news/upgrade/jcr/NewsArticlesUpgrade.java index e2957325f..59604bf11 100644 --- a/data-upgrade-news/src/main/java/org/exoplatform/news/upgrade/jcr/NewsArticlesUpgrade.java +++ b/data-upgrade-news/src/main/java/org/exoplatform/news/upgrade/jcr/NewsArticlesUpgrade.java @@ -36,6 +36,7 @@ import javax.jcr.query.Query; import javax.jcr.query.QueryManager; +import io.meeds.notes.model.NotePageProperties; import org.apache.commons.collections4.ListUtils; import org.exoplatform.commons.file.model.FileItem; @@ -56,6 +57,7 @@ import org.exoplatform.services.wcm.publication.lifecycle.stageversion.StageAndVersionPublicationConstant; import org.exoplatform.social.core.activity.model.ExoSocialActivity; import org.exoplatform.social.core.manager.ActivityManager; +import org.exoplatform.social.core.manager.IdentityManager; import org.exoplatform.social.core.space.model.Space; import org.exoplatform.social.core.space.spi.SpaceService; import org.exoplatform.social.core.utils.MentionUtils; @@ -95,6 +97,8 @@ public class NewsArticlesUpgrade extends UpgradeProductPlugin { private NoteService noteService; private IndexingService indexingService; + + private IdentityManager identityManager; private int migratedNewsArticlesCount = 0; @@ -114,6 +118,7 @@ public NewsArticlesUpgrade(InitParams initParams, MetadataService metadataService, FileService fileService, NoteService noteService, + IdentityManager identityManager, IndexingService indexingService) { super(initParams); this.repositoryService = repositoryService; @@ -124,6 +129,7 @@ public NewsArticlesUpgrade(InitParams initParams, this.metadataService = metadataService; this.fileService = fileService; this.noteService = noteService; + this.identityManager = identityManager; this.indexingService = indexingService; } @@ -216,6 +222,13 @@ public int manageNewsArticles(List newsArticlesNodes, Session session) thr || getStringProperty(newsArticleNode, "publication:currentState").equals("published")) { article = newsService.createNewsArticlePage(news, news.getAuthor()); + if (news.getProperties() != null && news.getAuthor() != null) { + NotePageProperties properties = news.getProperties(); + properties.setNoteId(Long.parseLong(article.getId())); + noteService.saveNoteMetadata(properties, + article.getLang(), + Long.valueOf(identityManager.getOrCreateUserIdentity(news.getAuthor()).getId())); + } PageVersion pageVersion = noteService.getPublishedVersionByPageIdAndLang(Long.parseLong(article.getId()), null); setArticleIllustration(pageVersion.getId(), article.getSpaceId(), newsArticleNode, "newsPageVersion"); setArticleAttachments(pageVersion.getId(), article.getSpaceId(), newsArticleNode, "newsPageVersion"); @@ -251,6 +264,13 @@ public int manageNewsArticles(List newsArticlesNodes, Session session) thr News publishedNews = convertNewsNodeToNewEntity(newsArticleNode, publishedNode); article = newsService.createNewsArticlePage(publishedNews, publishedNews.getAuthor()); + if (publishedNews.getProperties() != null && publishedNews.getAuthor() != null) { + NotePageProperties properties = publishedNews.getProperties(); + properties.setNoteId(Long.parseLong(article.getId())); + noteService.saveNoteMetadata(properties, + article.getLang(), + Long.valueOf(identityManager.getOrCreateUserIdentity(publishedNews.getAuthor()).getId())); + } PageVersion pageVersion = noteService.getPublishedVersionByPageIdAndLang(Long.parseLong(article.getId()), null); setArticleIllustration(pageVersion.getId(), article.getSpaceId(), publishedNode, "newsPageVersion"); setArticleAttachments(pageVersion.getId(), article.getSpaceId(), publishedNode, "newsPageVersion"); @@ -282,7 +302,7 @@ public int manageNewsArticles(List newsArticlesNodes, Session session) thr newsService.deleteArticle(article, article.getAuthor()); setArticleMetadatasItems(newsArticleNode.getUUID(), article.getId()); } else if (draftArticle != null) { - newsService.deleteDraftArticle(draftArticle.getId(), draftArticle.getAuthor(), true); + newsService.deleteDraftArticle(draftArticle.getId(), draftArticle.getAuthor()); } notMigratedNewsArticlesCount++; } @@ -295,7 +315,9 @@ private News convertNewsNodeToNewEntity(Node newsNode, Node newsVersionNode) thr String portalOwner = CommonsUtils.getCurrentPortalOwner(); news.setTitle(getStringProperty(newsVersionNode != null ? newsVersionNode : newsNode, "exo:title")); news.setName(news.getTitle() + "_" + newsNode.getUUID()); - news.setSummary(getStringProperty(newsVersionNode != null ? newsVersionNode : newsNode, "exo:summary")); + NotePageProperties properties = new NotePageProperties(); + properties.setSummary(getStringProperty(newsVersionNode != null ? newsVersionNode : newsNode, "exo:summary")); + news.setProperties(properties); String body = getStringProperty(newsVersionNode != null ? newsVersionNode : newsNode, "exo:body"); String sanitizedBody = HTMLSanitizer.sanitize(body); sanitizedBody = sanitizedBody.replaceAll("@", "@"); diff --git a/data-upgrade-news/src/test/java/org/exoplatform/news/upgrade/jcr/NewsArticlesUpgradeTest.java b/data-upgrade-news/src/test/java/org/exoplatform/news/upgrade/jcr/NewsArticlesUpgradeTest.java index 2098fe147..6bfa9a14c 100644 --- a/data-upgrade-news/src/test/java/org/exoplatform/news/upgrade/jcr/NewsArticlesUpgradeTest.java +++ b/data-upgrade-news/src/test/java/org/exoplatform/news/upgrade/jcr/NewsArticlesUpgradeTest.java @@ -45,6 +45,8 @@ import javax.jcr.query.QueryManager; import javax.jcr.query.QueryResult; +import org.exoplatform.social.core.identity.model.Identity; +import org.exoplatform.social.core.manager.IdentityManager; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; @@ -116,6 +118,9 @@ public class NewsArticlesUpgradeTest { @Mock private FileService fileService; + @Mock + private IdentityManager identityManager; + @Mock private IndexingService indexingService; @@ -144,12 +149,16 @@ public void setUp() { metadataService, fileService, noteService, + identityManager, indexingService); } @Test public void testProcessUpgrade() throws Exception { // Mock the session provider and session + Identity identity = mock(Identity.class); + when(identity.getId()).thenReturn("1"); + when(identityManager.getOrCreateUserIdentity(anyString())).thenReturn(identity); when(repositoryService.getCurrentRepository()).thenReturn(repository); when(repository.getConfiguration()).thenReturn(repositoryEntry); when(repositoryEntry.getDefaultWorkspaceName()).thenReturn("collaboration"); @@ -277,4 +286,4 @@ public void testProcessUpgrade() throws Exception { verify(activityManager, times(1)).getActivity(any()); verify(activityManager, times(1)).updateActivity(any(ExoSocialActivity.class), eq(false)); } -} \ No newline at end of file +}