Skip to content

Commit

Permalink
Fix: Update the NewsArticlesUpgrade plugin - EXO-71928 - Meeds-io/MIP…
Browse files Browse the repository at this point in the history
  • Loading branch information
hakermi committed Aug 1, 2024
1 parent 0624ecf commit 81b1a40
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -95,6 +97,8 @@ public class NewsArticlesUpgrade extends UpgradeProductPlugin {
private NoteService noteService;

private IndexingService indexingService;

private IdentityManager identityManager;

private int migratedNewsArticlesCount = 0;

Expand All @@ -114,6 +118,7 @@ public NewsArticlesUpgrade(InitParams initParams,
MetadataService metadataService,
FileService fileService,
NoteService noteService,
IdentityManager identityManager,
IndexingService indexingService) {
super(initParams);
this.repositoryService = repositoryService;
Expand All @@ -124,6 +129,7 @@ public NewsArticlesUpgrade(InitParams initParams,
this.metadataService = metadataService;
this.fileService = fileService;
this.noteService = noteService;
this.identityManager = identityManager;
this.indexingService = indexingService;
}

Expand Down Expand Up @@ -216,6 +222,13 @@ public int manageNewsArticles(List<Node> 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");
Expand Down Expand Up @@ -251,6 +264,13 @@ public int manageNewsArticles(List<Node> 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");
Expand Down Expand Up @@ -282,7 +302,7 @@ public int manageNewsArticles(List<Node> 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++;
}
Expand All @@ -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("&#64;", "@");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -116,6 +118,9 @@ public class NewsArticlesUpgradeTest {
@Mock
private FileService fileService;

@Mock
private IdentityManager identityManager;

@Mock
private IndexingService indexingService;

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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));
}
}
}

0 comments on commit 81b1a40

Please sign in to comment.