Skip to content

Commit

Permalink
feat: Create the news root page if it does not exist when creating th…
Browse files Browse the repository at this point in the history
…e first draft article - EXO-70331 (#1160)
  • Loading branch information
sofyenne authored Mar 26, 2024
1 parent 3ba9c1c commit 123a7dc
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -69,7 +70,10 @@
import org.exoplatform.wiki.WikiException;
import org.exoplatform.wiki.model.DraftPage;
import org.exoplatform.wiki.model.Page;
import org.exoplatform.wiki.model.Wiki;
import org.exoplatform.wiki.model.WikiType;
import org.exoplatform.wiki.service.NoteService;
import org.exoplatform.wiki.service.WikiService;

public class NewsServiceImplV2 implements NewsService {

Expand All @@ -89,6 +93,7 @@ public class NewsServiceImplV2 implements NewsService {

public static final String NEWS_UPLOAD_ID = "uploadId";


private static final Log LOG = ExoLogger.getLogger(NewsServiceImplV2.class);

private final SpaceService spaceService;
Expand All @@ -111,6 +116,8 @@ public class NewsServiceImplV2 implements NewsService {

private final ActivityManager activityManager;

private final WikiService wikiService;

public NewsServiceImplV2(SpaceService spaceService,
NoteService noteService,
MetadataService metadataService,
Expand All @@ -120,6 +127,7 @@ public NewsServiceImplV2(SpaceService spaceService,
IdentityManager identityManager,
UserACL userACL,
ActivityManager activityManager,
WikiService wikiService,
UploadService uploadService) {
this.spaceService = spaceService;
this.noteService = noteService;
Expand All @@ -131,6 +139,7 @@ public NewsServiceImplV2(SpaceService spaceService,
this.identityManager = identityManager;
this.userACL = userACL;
this.activityManager = activityManager;
this.wikiService = wikiService;
}

/**
Expand Down Expand Up @@ -486,14 +495,28 @@ public boolean canArchiveNews(Identity currentIdentity, String newsAuthor) {
}

private News createDraftArticleForNewPage(News draftArticle, String pageOwnerId, String draftArticleCreator) throws Exception {
Page articlesRootPage = getArticlesRootPage(pageOwnerId);
if (articlesRootPage != null) {
Wiki wiki = wikiService.getWikiByTypeAndOwner(WikiType.GROUP.name().toLowerCase(), pageOwnerId);
Page newsArticlesRootNotePage = null;
if (wiki != null) {
newsArticlesRootNotePage = noteService.getNoteOfNoteBookByName(WikiType.GROUP.name().toLowerCase(), pageOwnerId, NEWS_ARTICLES_ROOT_NOTE_PAGE_NAME);
// create the news root page if the wiki exist
if (newsArticlesRootNotePage == null) {
newsArticlesRootNotePage = createNewsArticlesNoteRootPage(wiki);
}
} else {
// create the wiki
pageOwnerId = formatWikiOwnerToGroupId(pageOwnerId);
wiki = wikiService.createWiki(WikiType.GROUP.name().toLowerCase(), pageOwnerId);
// create the news root page
newsArticlesRootNotePage = createNewsArticlesNoteRootPage(wiki);
}
if (newsArticlesRootNotePage != null) {
DraftPage draftArticlePage = new DraftPage();
draftArticlePage.setNewPage(true);
draftArticlePage.setTargetPageId(null);
draftArticlePage.setTitle(draftArticle.getTitle());
draftArticlePage.setContent(draftArticle.getBody());
draftArticlePage.setParentPageId(articlesRootPage.getId());
draftArticlePage.setParentPageId(newsArticlesRootNotePage.getId());
draftArticlePage.setAuthor(draftArticle.getAuthor());
draftArticlePage = noteService.createDraftForNewPage(draftArticlePage, System.currentTimeMillis());

Expand Down Expand Up @@ -527,15 +550,6 @@ private News createDraftArticleForNewPage(News draftArticle, String pageOwnerId,
return null;
}

private Page getArticlesRootPage(String ownerId) {
List<Page> notes =
noteService.getNotesOfWiki("group", ownerId)
.stream()
.filter(e -> e.getName().equals(NEWS_ARTICLES_ROOT_NOTE_PAGE_NAME) && e.getParentPageId() == null)
.toList();
return notes.isEmpty() ? null : notes.get(0);
}

private News recreateIfDraftDeleted(News news) throws Exception {
return null;
}
Expand Down Expand Up @@ -756,6 +770,37 @@ private boolean isMemberOfsharedInSpaces(News news, String username) {
return false;
}

private Page createNewsArticlesNoteRootPage(Wiki wiki) throws WikiException {
if (wiki != null) {
Page newsArticlesRootNotePage = new Page();
newsArticlesRootNotePage.setWikiType(wiki.getType());
newsArticlesRootNotePage.setWikiOwner(wiki.getOwner());
newsArticlesRootNotePage.setName(NEWS_ARTICLES_ROOT_NOTE_PAGE_NAME);
newsArticlesRootNotePage.setTitle(NEWS_ARTICLES_ROOT_NOTE_PAGE_NAME);
Date now = Calendar.getInstance().getTime();
newsArticlesRootNotePage.setCreatedDate(now);
newsArticlesRootNotePage.setUpdatedDate(now);
newsArticlesRootNotePage.setContent("");
// inherit syntax from wiki
newsArticlesRootNotePage.setSyntax(wiki.getPreferences().getWikiPreferencesSyntax().getDefaultSyntax());
return noteService.createNote(wiki, null, newsArticlesRootNotePage);
}
return null;
}

private String formatWikiOwnerToGroupId(String wikiOwner) {
if (wikiOwner == null || wikiOwner.length() == 0) {
return null;
}
if (!wikiOwner.startsWith("/")) {
wikiOwner = "/" + wikiOwner;
}
if (wikiOwner.endsWith("/")) {
wikiOwner = wikiOwner.substring(0, wikiOwner.length() - 1);
}
return wikiOwner;
}

private void sendNotification(String currentUserId,
News news,
NotificationConstants.NOTIFICATION_CONTEXT context) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

import org.exoplatform.portal.config.UserACL;
import org.exoplatform.social.core.manager.ActivityManager;
import org.exoplatform.wiki.model.Wiki;
import org.exoplatform.wiki.service.WikiService;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -100,6 +102,9 @@ public class NewsServiceImplV2Test {
@Mock
ActivityManager activityManager;

@Mock
WikiService wikiService;

private NewsService newsService;

private UserACL userACL;
Expand All @@ -122,6 +127,7 @@ public void setUp() {
identityManager,
userACL,
activityManager,
wikiService,
uploadService);
}

Expand Down Expand Up @@ -156,6 +162,8 @@ public void testCreateDraftArticle() throws Exception {
when(identity.getUserId()).thenReturn("john");
when(spaceService.getSpaceById(any())).thenReturn(space);
when(spaceService.isSuperManager(anyString())).thenReturn(true);
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);
when(rootPage.getName()).thenReturn(NEWS_ARTICLES_ROOT_NOTE_PAGE_NAME);
//
Expand All @@ -164,7 +172,7 @@ public void testCreateDraftArticle() throws Exception {
//
assertNull(savedDraftArticle);
//
when(noteService.getNotesOfWiki("group", space.getGroupId())).thenReturn(Arrays.asList(rootPage));
when(noteService.getNoteOfNoteBookByName("group", space.getGroupId(), NEWS_ARTICLES_ROOT_NOTE_PAGE_NAME)).thenReturn(rootPage);
when(noteService.createDraftForNewPage(any(DraftPage.class), anyLong())).thenReturn(draftPage);
when(rootPage.getId()).thenReturn("1");
org.exoplatform.social.core.identity.model.Identity identity1 =
Expand Down

0 comments on commit 123a7dc

Please sign in to comment.