diff --git a/content-service/src/main/java/io/meeds/news/model/News.java b/content-service/src/main/java/io/meeds/news/model/News.java index ae36779dd..8ede3a6db 100644 --- a/content-service/src/main/java/io/meeds/news/model/News.java +++ b/content-service/src/main/java/io/meeds/news/model/News.java @@ -144,4 +144,7 @@ public class News { private NotePageProperties properties; private boolean fromDraft; + + private boolean fromExternalPage; + } diff --git a/content-service/src/main/java/io/meeds/news/service/NewsService.java b/content-service/src/main/java/io/meeds/news/service/NewsService.java index adf9371c3..5d94816cc 100644 --- a/content-service/src/main/java/io/meeds/news/service/NewsService.java +++ b/content-service/src/main/java/io/meeds/news/service/NewsService.java @@ -265,13 +265,13 @@ News scheduleNews(News news, /** * Un-schedule publishing a News * - * @param news - * @param pageOwnerId - * @param newsArticleCreator + * @param news news article + * @param space owner space + * @param newsArticleCreator article creator * @return unscheduled News * @throws Exception when error occurs */ - News unScheduleNews(News news, String pageOwnerId, String newsArticleCreator) throws Exception; + News unScheduleNews(News news, Space space, String newsArticleCreator) throws Exception; /** * Search news by term diff --git a/content-service/src/main/java/io/meeds/news/service/impl/NewsServiceImpl.java b/content-service/src/main/java/io/meeds/news/service/impl/NewsServiceImpl.java index 27662e39b..ba3f51ac2 100644 --- a/content-service/src/main/java/io/meeds/news/service/impl/NewsServiceImpl.java +++ b/content-service/src/main/java/io/meeds/news/service/impl/NewsServiceImpl.java @@ -168,7 +168,7 @@ public class NewsServiceImpl implements NewsService { /** The Constant NEWS_METADATA_LATEST_DRAFT_OBJECT_TYPE. */ public static final String NEWS_METADATA_LATEST_DRAFT_OBJECT_TYPE = "newsLatestDraftPage"; - public static final String NEWS_ATTACHMENTS_IDS = "attachmentsIds"; + public static final String EXTERNAL_PAGE = "externalPage"; public static final String ARTICLE_CONTENT = "content"; @@ -222,7 +222,7 @@ public News createNews(News news, Identity currentIdentity) throws Exception { if (POSTED.equals(news.getPublicationState())) { createdNews = postNews(news, currentIdentity.getUserId()); } else if (news.getSchedulePostDate() != null) { - createdNews = unScheduleNews(news, space.getGroupId(), currentIdentity.getUserId()); + createdNews = unScheduleNews(news, space, currentIdentity.getUserId()); } else { createdNews = createDraftArticleForNewPage(news, space.getGroupId(), @@ -777,12 +777,25 @@ public News scheduleNews(News news, Identity currentIdentity, String newsObjectT * {@inheritDoc} */ @Override - public News unScheduleNews(News news, String pageOwnerId, String articleCreator) throws Exception { + public News unScheduleNews(News news, Space space, String articleCreator) throws Exception { News existingNews = getNewsArticleById(news.getId()); - if (existingNews != null) { - news = createDraftArticleForNewPage(news, pageOwnerId, articleCreator, System.currentTimeMillis()); + if (existingNews != null && !existingNews.isFromExternalPage()) { + news = createDraftArticleForNewPage(news, space.getGroupId(), articleCreator, System.currentTimeMillis()); deleteArticle(existingNews, articleCreator); return buildDraftArticle(news.getId(), articleCreator); + } else if (existingNews != null) { + PageVersion pageVersion = noteService.getPublishedVersionByPageIdAndLang(Long.parseLong(existingNews.getId()), null); + NewsPageVersionObject articleVersionMetaDataObject = new NewsPageVersionObject(NEWS_METADATA_PAGE_VERSION_OBJECT_TYPE, + pageVersion.getId(), + null, + Long.parseLong(space.getId())); + NewsPageObject newsPageObject = new NewsPageObject(NEWS_METADATA_PAGE_OBJECT_TYPE, + existingNews.getId(), + null, + Long.parseLong(space.getId())); + metadataService.deleteMetadataItemsByObject(articleVersionMetaDataObject); + metadataService.deleteMetadataItemsByObject(newsPageObject); + return existingNews; } return null; } @@ -973,7 +986,7 @@ public News createArticleFromExistingPage(News article, String creator) throws E if (articlePage != null && space != null) { PageVersion pageVersion = noteService.getPublishedVersionByPageIdAndLang(Long.parseLong(articlePage.getId()), null); article.setIllustrationURL(NewsUtils.buildIllustrationUrl(articlePage.getProperties(), articlePage.getLang())); - buildNewArticleProperties(article, articlePage, creator, space.getId(), pageVersion.getId()); + buildNewArticleProperties(article, articlePage, creator, space.getId(), pageVersion.getId(), true); } return article; } @@ -1034,7 +1047,7 @@ public News createNewsArticlePage(News newsArticle, String newsArticleCreator) t newsArticle.setLatestVersionId(pageVersion.getId()); newsArticle.setIllustrationURL(NewsUtils.buildIllustrationUrl(newsArticlePage.getProperties(), newsArticle.getLang())); - buildNewArticleProperties(newsArticle, newsArticlePage, newsArticleCreator, space.getId(), pageVersion.getId()); + buildNewArticleProperties(newsArticle, newsArticlePage, newsArticleCreator, space.getId(), pageVersion.getId(), false); // delete the draft deleteDraftArticle(draftNewsId, poster.getUserId()); return newsArticle; @@ -1171,7 +1184,8 @@ private void buildNewArticleProperties(News article, Page articlePage, String creator, String spaceId, - String versionId) throws Exception { + String versionId, + boolean externalPage) throws Exception { NewsPageVersionObject articleVersionMetaDataObject = new NewsPageVersionObject(NEWS_METADATA_PAGE_VERSION_OBJECT_TYPE, versionId, null, @@ -1199,6 +1213,7 @@ private void buildNewArticleProperties(News article, if (StringUtils.isNotEmpty(article.getPublicationState())) { newsPageProperties.put(NEWS_PUBLICATION_STATE, article.getPublicationState()); } + newsPageProperties.put(EXTERNAL_PAGE, String.valueOf(externalPage)); newsPageProperties.put(NEWS_ACTIVITY_POSTED, String.valueOf(article.isActivityPosted())); newsPageProperties.put(PUBLISHED, String.valueOf(article.isPublished())); newsPageProperties.put(NEWS_DELETED, String.valueOf(articlePage.isDeleted())); @@ -1337,6 +1352,9 @@ private void buildArticleProperties(News article, String currentUsername, Metada if (properties.containsKey(PUBLISHED) && StringUtils.isNotEmpty(properties.get(PUBLISHED))) { article.setPublished(Boolean.parseBoolean(properties.get(PUBLISHED))); } + if (properties.containsKey(EXTERNAL_PAGE) && StringUtils.isNotEmpty(properties.get(EXTERNAL_PAGE))) { + article.setFromExternalPage(Boolean.parseBoolean(properties.get(EXTERNAL_PAGE))); + } if (properties.containsKey(NEWS_VIEWS) && StringUtils.isNotEmpty(properties.get(NEWS_VIEWS))) { article.setViewsCount(Long.parseLong(properties.get(NEWS_VIEWS))); } diff --git a/content-service/src/test/java/io/meeds/news/service/impl/NewsServiceImplTest.java b/content-service/src/test/java/io/meeds/news/service/impl/NewsServiceImplTest.java index b0ad0d7fe..830d58c8b 100644 --- a/content-service/src/test/java/io/meeds/news/service/impl/NewsServiceImplTest.java +++ b/content-service/src/test/java/io/meeds/news/service/impl/NewsServiceImplTest.java @@ -947,6 +947,7 @@ public void testUnScheduleNews() throws Exception { mockBuildArticle(metadataItems); Space space = mockSpace(); + when(space.getGroupId()).thenReturn("/spaces/test"); Wiki wiki = mock(Wiki.class); when(wikiService.getWikiByTypeAndOwner(anyString(), anyString())).thenReturn(wiki); org.exoplatform.wiki.model.Page rootPage = mock(org.exoplatform.wiki.model.Page.class); @@ -973,10 +974,17 @@ public void testUnScheduleNews() throws Exception { when(identityManager.getOrCreateUserIdentity(anyString())).thenReturn(identity1); when(identity1.getId()).thenReturn("1"); - newsService.unScheduleNews(newsArticle, space.getGroupId(), "john"); + newsService.unScheduleNews(newsArticle, space, "john"); verify(noteService, times(1)).createDraftForNewPage(any(DraftPage.class), anyLong(), anyLong()); verify(noteService, times(1)).deleteNote(anyString(), anyString(), anyString()); + + clearInvocations(noteService); + properties.put(EXTERNAL_PAGE, "true"); + newsService.unScheduleNews(newsArticle, space, "john"); + verify(noteService, times(0)).createDraftForNewPage(any(DraftPage.class), anyLong(), anyLong()); + verify(noteService, times(0)).deleteNote(anyString(), anyString(), anyString()); + verify(metadataService, times(2)).deleteMetadataItemsByObject(any()); } @Test