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 5606498d..9a8fb90a 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 @@ -183,20 +183,20 @@ public void processUpgrade(String oldVersion, String newVersion) { Query query = queryManager.createQuery(queryString, Query.SQL); Iterator newsIterator = query.execute().getNodes(); - List newsArticlesNodes = new ArrayList(); + List newsArticlesNodesUUIDs = new ArrayList(); while (newsIterator.hasNext()) { Node newsArticleNode = newsIterator.next(); if (!getBooleanProperty(newsArticleNode, "exo:archived")) { - newsArticlesNodes.add(newsArticleNode); + newsArticlesNodesUUIDs.add(newsArticleNode.getUUID()); } else { newsArticleNode.remove(); session.save(); } } - totalNewsArticlesCount = newsArticlesNodes.size(); + totalNewsArticlesCount = newsArticlesNodesUUIDs.size(); LOG.info("Total number of news articles to be migrated: {}", totalNewsArticlesCount); - for (List newsArticlesChunk : ListUtils.partition(newsArticlesNodes, 10)) { - int notMigratedNewsArticlesCountByTransaction = manageNewsArticles(newsArticlesChunk, session); + for (List newsArticlesChunk : ListUtils.partition(newsArticlesNodesUUIDs, 10)) { + int notMigratedNewsArticlesCountByTransaction = manageNewsArticles(newsArticlesChunk, sessionProvider); int processedNewsArticlesCountByTransaction = newsArticlesChunk.size(); processedNewsArticlesCount += processedNewsArticlesCountByTransaction; migratedNewsArticlesCount += processedNewsArticlesCountByTransaction - notMigratedNewsArticlesCountByTransaction; @@ -215,7 +215,7 @@ public void processUpgrade(String oldVersion, String newVersion) { sessionProvider.close(); } } - if (notMigratedNewsArticlesCount == 0) { + if (totalNewsArticlesCount == migratedNewsArticlesCount) { LOG.info("End news articles migration successful migration: total={} succeeded={} error={}. It tooks {} ms.", totalNewsArticlesCount, migratedNewsArticlesCount, @@ -260,16 +260,22 @@ public boolean shouldProceedToUpgrade(String newVersion, return shouldUpgrade; } - public int manageNewsArticles(List newsArticlesNodes, Session session) throws Exception { + public int manageNewsArticles(List newsArticlesNodesUUIDs, SessionProvider sessionProvider) throws Exception { int notMigratedNewsArticlesCount = 0; - for (Node newsArticleNode : newsArticlesNodes) { - indexingService.unindex(NewsIndexingServiceConnector.TYPE, newsArticleNode.getUUID()); + for (String newsArticleNodeUUID : newsArticlesNodesUUIDs) { News article = null; News draftArticle = null; try { + Session session = sessionProvider.getSession( + repositoryService.getCurrentRepository() + .getConfiguration() + .getDefaultWorkspaceName(), + repositoryService.getCurrentRepository()); + Node newsArticleNode = session.getNodeByUUID(newsArticleNodeUUID); + indexingService.unindex(NewsIndexingServiceConnector.TYPE, newsArticleNodeUUID); News news = convertNewsNodeToNewEntity(newsArticleNode, null); NotePageProperties properties = news.getProperties(); - LOG.info("Migrating news article with id '{}' and title '{}'", newsArticleNode.getUUID(), news.getTitle()); + LOG.info("Migrating news article with id '{}' and title '{}'", newsArticleNodeUUID, news.getTitle()); Space space = spaceService.getSpaceById(news.getSpaceId()); // existing published and staged articles @@ -284,10 +290,16 @@ public int manageNewsArticles(List newsArticlesNodes, Session session) thr Long.valueOf(identityManager.getOrCreateUserIdentity(news.getAuthor()).getId())); } PageVersion pageVersion = noteService.getPublishedVersionByPageIdAndLang(Long.parseLong(article.getId()), null); + session = sessionProvider.getSession( + repositoryService.getCurrentRepository() + .getConfiguration() + .getDefaultWorkspaceName(), + repositoryService.getCurrentRepository()); + newsArticleNode = session.getNodeByUUID(newsArticleNodeUUID); setArticleIllustration(pageVersion.getParent(), article.getSpaceId(), newsArticleNode, "notePage"); setArticleAttachments(pageVersion.getId(), newsArticleNode); /* upgrade news id for news targets and favorite metadatata items */ - setArticleMetadatasItems(article.getId(), newsArticleNode.getUUID()); + setArticleMetadatasItems(article.getId(), newsArticleNodeUUID); if (getStringProperty(newsArticleNode, "publication:currentState").equals("published")) { setArticleActivities(article, newsArticleNode); setArticleViews(article, newsArticleNode); @@ -304,18 +316,25 @@ public int manageNewsArticles(List newsArticlesNodes, Session session) thr if (!newsArticleNode.hasProperty(AuthoringPublicationConstant.LIVE_REVISION_PROP)) { // upgrade the drafts of not existing articles /* attachments will not be migrated for drafts */ + properties.setDraft(true); + news.setProperties(properties); draftArticle = newsService.createDraftArticleForNewPage(news, space.getGroupId(), news.getAuthor(), news.getCreationDate().getTime()); DraftPage draftPage = noteService.getDraftNoteById(draftArticle.getId(), draftArticle.getAuthor()); properties.setNoteId(Long.parseLong(draftPage.getId())); - properties.setDraft(true); if (news.getAuthor() != null) { noteService.saveNoteMetadata(properties, draftPage.getLang(), Long.valueOf(identityManager.getOrCreateUserIdentity(news.getAuthor()).getId())); } + session = sessionProvider.getSession( + repositoryService.getCurrentRepository() + .getConfiguration() + .getDefaultWorkspaceName(), + repositoryService.getCurrentRepository()); + newsArticleNode = session.getNodeByUUID(newsArticleNodeUUID); setArticleIllustration(draftPage, draftArticle.getSpaceId(), newsArticleNode, "noteDraftPage"); } else {// drafts of existing articles @@ -335,10 +354,16 @@ public int manageNewsArticles(List newsArticlesNodes, Session session) thr .getId())); } PageVersion pageVersion = noteService.getPublishedVersionByPageIdAndLang(Long.parseLong(article.getId()), null); + session = sessionProvider.getSession( + repositoryService.getCurrentRepository() + .getConfiguration() + .getDefaultWorkspaceName(), + repositoryService.getCurrentRepository()); + newsArticleNode = session.getNodeByUUID(newsArticleNodeUUID); setArticleIllustration(pageVersion.getParent(), article.getSpaceId(), publishedNode, "notePage"); setArticleAttachments(pageVersion.getId(), publishedNode); /* upgrade news id for news targets and favorite metadatata items */ - setArticleMetadatasItems(article.getId(), newsArticleNode.getUUID()); + setArticleMetadatasItems(article.getId(), newsArticleNodeUUID); setArticleActivities(article, publishedNode); setArticleViews(article, newsArticleNode); Page publishedPage = noteService.getNoteById(article.getId()); @@ -354,6 +379,12 @@ public int manageNewsArticles(List newsArticlesNodes, Session session) thr space); DraftPage draftPage = noteService.getDraftNoteById(draftForExistingArticle.getId(), draftForExistingArticle.getAuthor()); + session = sessionProvider.getSession( + repositoryService.getCurrentRepository() + .getConfiguration() + .getDefaultWorkspaceName(), + repositoryService.getCurrentRepository()); + newsArticleNode = session.getNodeByUUID(newsArticleNodeUUID); setArticleIllustration(draftPage, draftForExistingArticle.getSpaceId(), newsArticleNode, "noteDraftPage"); // set the update and the created date setArticleCreateAndUpdateDate(article.getId(), article.getSpaceId(), newsArticleNode); @@ -362,14 +393,14 @@ public int manageNewsArticles(List newsArticlesNodes, Session session) thr newsArticleNode.remove(); session.save(); } catch (Exception e) { - LOG.warn("Error migrating news article with id '{}'. Continue to migrate other items", newsArticleNode.getUUID(), e); + notMigratedNewsArticlesCount++; + LOG.warn("Error migrating news article with id '{}'. Continue to migrate other items", newsArticleNodeUUID, e); if (article != null) { newsService.deleteArticle(article, article.getAuthor()); - setArticleMetadatasItems(newsArticleNode.getUUID(), article.getId()); + setArticleMetadatasItems(newsArticleNodeUUID, article.getId()); } else if (draftArticle != null) { newsService.deleteDraftArticle(draftArticle.getId(), draftArticle.getAuthor()); } - notMigratedNewsArticlesCount++; } } return notMigratedNewsArticlesCount; @@ -628,4 +659,4 @@ private void setArticleCreateAndUpdateDate(String articleId, String spaceId, Nod metadataService.updateMetadataItem(articleMetaData, articleMetaData.getCreatorId()); } } -} +} \ No newline at end of file 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 502c3743..80b0aa4d 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 @@ -16,14 +16,18 @@ */ package org.exoplatform.news.upgrade.jcr; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.nullable; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import java.lang.reflect.Method; import java.util.ArrayList; @@ -37,19 +41,10 @@ import javax.jcr.Session; import javax.jcr.Value; import javax.jcr.Workspace; -import javax.jcr.nodetype.NodeType; -import javax.jcr.nodetype.NodeTypeManager; import javax.jcr.query.Query; import javax.jcr.query.QueryManager; import javax.jcr.query.QueryResult; -import io.meeds.notes.model.NotePageProperties; -import org.exoplatform.commons.api.settings.SettingService; -import org.exoplatform.commons.api.settings.SettingValue; -import org.exoplatform.commons.upgrade.UpgradePluginExecutionContext; -import org.exoplatform.services.attachments.storage.AttachmentStorage; -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; @@ -58,11 +53,15 @@ import org.mockito.MockedStatic; import org.mockito.junit.MockitoJUnitRunner; +import org.exoplatform.commons.api.settings.SettingService; +import org.exoplatform.commons.api.settings.SettingValue; import org.exoplatform.commons.file.services.FileService; import org.exoplatform.commons.search.index.IndexingService; +import org.exoplatform.commons.upgrade.UpgradePluginExecutionContext; import org.exoplatform.commons.utils.CommonsUtils; import org.exoplatform.container.xml.InitParams; import org.exoplatform.container.xml.ValueParam; +import org.exoplatform.services.attachments.storage.AttachmentStorage; import org.exoplatform.services.jcr.RepositoryService; import org.exoplatform.services.jcr.config.RepositoryEntry; import org.exoplatform.services.jcr.core.ManageableRepository; @@ -71,7 +70,9 @@ import org.exoplatform.services.jcr.impl.core.nodetype.NodeTypeManagerImpl; import org.exoplatform.services.wcm.extensions.publication.lifecycle.authoring.AuthoringPublicationConstant; import org.exoplatform.social.core.activity.model.ExoSocialActivity; +import org.exoplatform.social.core.identity.model.Identity; import org.exoplatform.social.core.manager.ActivityManager; +import org.exoplatform.social.core.manager.IdentityManager; import org.exoplatform.social.core.space.spi.SpaceService; import org.exoplatform.social.core.utils.MentionUtils; import org.exoplatform.social.metadata.MetadataService; @@ -203,6 +204,8 @@ public void testProcessUpgrade() throws Exception { when(iterator.next()).thenReturn(node1, node2); // Mock the necessary properties of the node1 + when(node1.getUUID()).thenReturn("uuid1"); + when(session.getNodeByUUID("uuid1")).thenReturn(node1); when(node1.hasProperty("publication:currentState")).thenReturn(true); Property archivedProperty = mock(Property.class); when(node1.hasProperty("exo:archived")).thenReturn(true); @@ -279,6 +282,8 @@ public void testProcessUpgrade() throws Exception { when(attachmentsIdsProperty.getValues()).thenReturn(attachmentsIdsPropertyValues); // Mock the necessary properties of the node2 + when(node2.getUUID()).thenReturn("uuid2"); + when(session.getNodeByUUID("uuid2")).thenReturn(node2); when(node2.hasProperty("publication:currentState")).thenReturn(true); Property stagedStateProperty = mock(Property.class); when(node2.getProperty("publication:currentState")).thenReturn(stagedStateProperty);