Skip to content

Commit

Permalink
fix: Fix issues in the listed note drafts treeview - EXO-74426 - Meed…
Browse files Browse the repository at this point in the history
…s-io/meeds#2440 (#1134)

Issues in listed note drafts treeview:
1 - fix the drafts tree build
2 - fix the unneeded displayed drafts of non targeted home pages
  • Loading branch information
hakermi committed Oct 1, 2024
1 parent ffe1a34 commit 3c83eba
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,24 @@ public PageVersion getPageVersionById(long versionId) {
* {@inheritDoc}
*/
@Override
public List<DraftPage> getDraftsOfWiki(String wikiOwner, String wikiType) {
return convertDraftPageEntitiesToDraftPages(draftPageDAO.findDraftsOfWiki(wikiOwner, wikiType));
public List<DraftPage> getDraftsOfWiki(String wikiOwner, String wikiType, String wikiHome) {
// The Note API allows multiple home pages to be created within the wiki.
// To avoid retrieving drafts from all home pages,
// we need to specifically fetch all pages under the target home page and search
// for drafts.
// Although this approach may seem performance-intensive, it is the only
// reliable solution.
PageEntity pageEntity = pageDAO.getPageOfWikiByName(wikiType, wikiOwner, wikiHome);
List<DraftPageEntity> draftPageEntities = new ArrayList<>();
getDraftsOfPage(pageEntity, draftPageEntities);
return convertDraftPageEntitiesToDraftPages(draftPageEntities);
}

private void getDraftsOfPage(PageEntity pageEntity, List<DraftPageEntity> drafts) {
drafts.addAll(draftPageDAO.findDraftPagesByParentPage(pageEntity.getId()));
List<PageEntity> childrenPages = pageDAO.getChildrenPages(pageEntity);
for (PageEntity child : childrenPages) {
getDraftsOfPage(child, drafts);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,4 @@ public Long countDraftPagesByParentPage(long parentPageId) {
.setParameter("parentPageId", parentPageId)
.getSingleResult();
}

public List<DraftPageEntity> findDraftsOfWiki(String wikiOwner, String wikiType) {
Query query = getEntityManager().createNativeQuery("""
SELECT * FROM WIKI_DRAFT_PAGES wdp WHERE wdp.PARENT_PAGE_ID IN
(SELECT wp.PAGE_ID FROM WIKI_PAGES wp INNER JOIN WIKI_WIKIS ww ON wp.WIKI_ID = ww.WIKI_ID
WHERE ww.OWNER=:wikiOwner and ww.type=:wikiType)""", DraftPageEntity.class);
query.setParameter("wikiOwner", wikiOwner);
query.setParameter("wikiType", wikiType);
return query.getResultList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,14 @@ public default List<Attachment> getAttachmentsOfPage(Page page, boolean loadCont
*/
PageVersion getPageVersionById(long versionId);


/**
* Gets draft pages of a given wiki
*
*
* @param wikiOwner wiki owner
* @param wikiType wiki type
* @param wikiHome wiki home page
* @return {@link List} of {@link DraftPage}
*/
List<DraftPage> getDraftsOfWiki(String wikiOwner, String wikiType);
List<DraftPage> getDraftsOfWiki(String wikiOwner, String wikiType, String wikiHome);
}
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,8 @@ Page getNoteByIdAndLang(Long pageId, Identity userIdentity, String source, Strin
*
* @param wikiOwner wiki owner
* @param wikiType wiki type
* @param wikiHome wiki home page
* @return {@link List} of {@link DraftPage}
*/
List<DraftPage> getDraftsOfWiki(String wikiOwner, String wikiType);
List<DraftPage> getDraftsOfWiki(String wikiOwner, String wikiType, String wikiHome);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1531,12 +1531,13 @@ public void deleteVersionsByNoteIdAndLang(Long noteId, String userName, String l
deleteVersionsByNoteIdAndLang(noteId, lang);
}


/**
* {@inheritDoc}
*/
@Override
public List<DraftPage> getDraftsOfWiki(String wikiOwner, String wikiType) {
return dataStorage.getDraftsOfWiki(wikiOwner, wikiType);
public List<DraftPage> getDraftsOfWiki(String wikiOwner, String wikiType, String wikiHome) {
return dataStorage.getDraftsOfWiki(wikiOwner, wikiType, wikiHome);
}

public ExoCache<Integer, MarkupData> getRenderingCache() {
Expand All @@ -1547,7 +1548,6 @@ public Map<WikiPageParams, List<WikiPageParams>> getPageLinksMap() {
return pageLinksMap;
}


// ******* Listeners *******/

public void postUpdatePageVersionLanguage(String versionPageId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,6 @@ public Response getNoteTreeData(@Parameter(description = "Tree of selected path
rootNodeData = getJsonTree(noteParam, context, identity, request.getLocale()).getFirst();
treeNodeData.addAll(rootNodeData.getChildren());
}

if (rootNodeData != null) {
responseData = List.of(rootNodeData);
}
Expand Down Expand Up @@ -1504,7 +1503,7 @@ private void buildNoteDraftsTree(JsonNodeData rootNode,
Map<String, Object> context,
Identity identity,
Locale locale) {
List<DraftPage> draftPages = noteService.getDraftsOfWiki(params.getOwner(), params.getType());
List<DraftPage> draftPages = noteService.getDraftsOfWiki(params.getOwner(), params.getType(), params.getPageName());
Map<String, List<JsonNodeData>> draftsByParentPage = draftPages.stream().map(draftPage -> {
try {
PageTreeNode pageTreeNode = new PageTreeNode(draftPage);
Expand All @@ -1522,20 +1521,23 @@ private void buildNoteDraftsTree(JsonNodeData rootNode,
draftsByParentPage.forEach((parentPageId, drafts) -> {
try {
drafts = TreeUtils.cleanDraftChildren(drafts, locale);
Page parent = noteService.getNoteById(String.valueOf(parentPageId));
PageTreeNode pageTreeNode = new PageTreeNode(parent);
Boolean canEdit = context != null && context.get(TreeNode.CAN_EDIT) != null && (Boolean) context.get(TreeNode.CAN_EDIT);
JsonNodeData parentJsonData = TreeUtils.toJsonNodeData(pageTreeNode,
path,
parent,
context,
canEdit,
true,
identity,
locale,
noteService);
JsonNodeData parentJsonData = findParent(parentPageId, treeNodeData);
if (parentJsonData == null) {
Page parent = noteService.getNoteById(String.valueOf(parentPageId));
PageTreeNode pageTreeNode = new PageTreeNode(parent);
Boolean canEdit = context != null && context.get(TreeNode.CAN_EDIT) != null && (Boolean) context.get(TreeNode.CAN_EDIT);
parentJsonData = TreeUtils.toJsonNodeData(pageTreeNode,
path,
parent,
context,
canEdit,
true,
identity,
locale,
noteService);
}
if (!Objects.equals(parentJsonData.getNoteId(), rootNode.getNoteId())) {
parentJsonData.setChildren(drafts);
parentJsonData.addChildren(drafts);
treeNodeData.add(parentJsonData);
buildPageAscendants(rootNode, parentJsonData, treeNodeData, context, identity, locale, path);
} else {
Expand All @@ -1546,7 +1548,7 @@ private void buildNoteDraftsTree(JsonNodeData rootNode,
}
});
}

private void buildPageAscendants(JsonNodeData rootNode,
JsonNodeData parentJsonData,
List<JsonNodeData> treeNodeData,
Expand All @@ -1556,23 +1558,28 @@ private void buildPageAscendants(JsonNodeData rootNode,
String path) throws Exception {
String parentPageId = parentJsonData.getParentPageId();
if (parentPageId != null && !parentPageId.equals(rootNode.getNoteId())) {
Page parent = noteService.getNoteById(parentJsonData.getParentPageId());
PageTreeNode pageTreeNode = new PageTreeNode(parent);
Boolean canEdit = context != null && context.get(TreeNode.CAN_EDIT) != null && (Boolean) context.get(TreeNode.CAN_EDIT);
JsonNodeData parentNode = TreeUtils.toJsonNodeData(pageTreeNode,
path,
parent,
context,
canEdit,
true,
identity,
locale,
noteService);
parentNode.addChildren(List.of(parentJsonData));
JsonNodeData parentNode = findParent(parentPageId, treeNodeData);
if (parentNode == null) {
Page parent = noteService.getNoteById(parentJsonData.getParentPageId());
PageTreeNode pageTreeNode = new PageTreeNode(parent);
Boolean canEdit = context != null && context.get(TreeNode.CAN_EDIT) != null && (Boolean) context.get(TreeNode.CAN_EDIT);
parentNode = TreeUtils.toJsonNodeData(pageTreeNode, path, parent, context, canEdit, true, identity, locale, noteService);
}
addChildIfNoExists(parentNode, parentJsonData);
treeNodeData.add(parentNode);
buildPageAscendants(rootNode, parentNode, treeNodeData, context, identity, locale, path);
} else {
rootNode.addChildren(List.of(parentJsonData));
addChildIfNoExists(rootNode, parentJsonData);
}
}

private JsonNodeData findParent(String parentId, List<JsonNodeData> list) {
return list.stream().filter(obj -> obj.getNoteId().equals(parentId)).findFirst().orElse(null);
}

private void addChildIfNoExists(JsonNodeData parent, JsonNodeData child) {
if (findParent(child.getNoteId(), parent.getChildren()) == null) {
parent.addChildren(List.of(child));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,26 +126,6 @@ public Boolean isHasDraftDescendant() {
return hasDraftDescendant;
}

public void setHasDraftDescendant(Boolean hasDraftDescendant) {
this.hasDraftDescendant = hasDraftDescendant;
}

public String getLang() {
return lang;
}

public void setLang(String lang) {
this.lang = lang;
}

public String getTargetPageId() {
return targetPageId;
}

public void setTargetPageId(String targetPageId) {
this.targetPageId = targetPageId;
}

public void addChildren(List<JsonNodeData> children) {
if (this.children == null) {
this.children = new ArrayList<JsonNodeData>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ public void testGetDraftsOfWiki() throws Exception {
noteService.createDraftForExistPage(draftPage, note, null, new Date().getTime(), "root");
draftPage.setParentPageId(note2.getId());
noteService.createDraftForExistPage(draftPage, note, null, new Date().getTime(), "root");
assertEquals(2, noteService.getDraftsOfWiki(portalWiki.getOwner(), portalWiki.getType()).size());
assertEquals(2, noteService.getDraftsOfWiki(portalWiki.getOwner(), portalWiki.getType(), portalWiki.getWikiHome().getName()).size());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
-->
<template>
<v-list-item
ripple
:href="noteLink"
ripple
@click.prevent="openNote">
<span class="primary--text subtitle-1 ps-2">
{{ note.name }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,8 +682,18 @@ export default {
}
return itemsArray;
},
naturalSort(items) {
if (items?.length) {
const collator = new Intl.Collator(eXo.env.portal.language, {numeric: true, sensitivity: 'base'});
items.sort((a, b) => collator.compare(a.name, b.name));
}
},
retrieveNoteTree(noteBookType, noteOwner, noteName, treeType) {
const noteType = this.isDraftFilter && 'drafts' || 'published';
if (this.isDraftFilter) {
noteName = this.note?.breadcrumb[0]?.id;
treeType = 'all';
}
this.isLoading = true;
this.items = [];
this.$notesService.getNoteTree(noteBookType, noteOwner, noteName, treeType, noteType).then(data => {
Expand All @@ -697,6 +707,9 @@ export default {
this.mapItems(this.items[0]?.children);
}
}
if (this.isDraftFilter) {
this.naturalSort(this.items);
}
this.allItems = data.treeNodeData;
this.allItemsHome = data.jsonList[0].children;
}
Expand Down

0 comments on commit 3c83eba

Please sign in to comment.