diff --git a/notes-service/src/main/java/org/exoplatform/wiki/service/rest/NotesRestService.java b/notes-service/src/main/java/org/exoplatform/wiki/service/rest/NotesRestService.java index 8c28c6171..fb7ea7d20 100644 --- a/notes-service/src/main/java/org/exoplatform/wiki/service/rest/NotesRestService.java +++ b/notes-service/src/main/java/org/exoplatform/wiki/service/rest/NotesRestService.java @@ -1496,7 +1496,7 @@ public Response reorderNotes(@RequestBody NoteReorder request) { private List getJsonTree(WikiPageParams params, Map context, Identity identity, Locale locale) throws Exception { Wiki noteBook = noteBookService.getWikiByTypeAndOwner(params.getType(), params.getOwner()); WikiTreeNode noteBookNode = new WikiTreeNode(noteBook); - noteBookNode.pushDescendants(context, ConversationState.getCurrent().getIdentity().getUserId()); + noteBookNode.pushDescendants(context, ConversationState.getCurrent().getIdentity().getUserId(), locale); return TreeUtils.tranformToJson(noteBookNode, context, identity, locale); } @@ -1504,7 +1504,7 @@ private List getJsonDescendants(WikiPageParams params, Map context, Identity identity, Locale locale) throws Exception { - TreeNode treeNode = TreeUtils.getDescendants(params, context, ConversationState.getCurrent().getIdentity().getUserId()); + TreeNode treeNode = TreeUtils.getDescendants(params, context, ConversationState.getCurrent().getIdentity().getUserId(), locale); return TreeUtils.tranformToJson(treeNode, context, identity, locale); } diff --git a/notes-service/src/main/java/org/exoplatform/wiki/tree/PageTreeNode.java b/notes-service/src/main/java/org/exoplatform/wiki/tree/PageTreeNode.java index 849c08d45..495df899f 100644 --- a/notes-service/src/main/java/org/exoplatform/wiki/tree/PageTreeNode.java +++ b/notes-service/src/main/java/org/exoplatform/wiki/tree/PageTreeNode.java @@ -28,8 +28,6 @@ import org.exoplatform.services.log.ExoLogger; import org.exoplatform.services.log.Log; import org.exoplatform.services.security.ConversationState; -import org.exoplatform.social.core.space.model.Space; -import org.exoplatform.social.core.space.spi.SpaceService; import org.exoplatform.wiki.model.Page; import org.exoplatform.wiki.model.PermissionType; import org.exoplatform.wiki.service.NoteService; @@ -38,27 +36,25 @@ import org.exoplatform.wiki.utils.Utils; public class PageTreeNode extends TreeNode { + private static final Log log = ExoLogger.getLogger(PageTreeNode.class); @Setter @Getter - private Page page; + private Page page; - private NoteService noteService; - - private SpaceService spaceService; + private NoteService noteService; - public PageTreeNode(Page page) throws Exception { + public PageTreeNode(Page page) { super(page.getTitle(), TreeNodeType.PAGE); this.noteService = ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(NoteService.class); - - this.spaceService = ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(SpaceService.class); this.page = page; this.id = page.getId(); this.path = buildPath(); this.hasChild = !page.isDraftPage() && noteService.hasChildren(Long.parseLong(page.getId())); + this.position = page.getPosition(); } @Override diff --git a/notes-service/src/main/java/org/exoplatform/wiki/tree/TreeNode.java b/notes-service/src/main/java/org/exoplatform/wiki/tree/TreeNode.java index 95f5d11fa..a6172400e 100644 --- a/notes-service/src/main/java/org/exoplatform/wiki/tree/TreeNode.java +++ b/notes-service/src/main/java/org/exoplatform/wiki/tree/TreeNode.java @@ -18,6 +18,7 @@ */ package org.exoplatform.wiki.tree; +import lombok.Data; import org.apache.commons.lang3.StringUtils; import org.exoplatform.commons.comparators.NaturalComparator; import org.exoplatform.wiki.model.Page; @@ -27,7 +28,7 @@ import java.util.*; - +@Data public class TreeNode { protected String name; @@ -44,6 +45,8 @@ public class TreeNode { protected List children = new ArrayList(); + protected Integer position; + final static public String STACK_PARAMS = "stackParams"; final static public String PATH = "path"; @@ -81,66 +84,6 @@ public TreeNode(String name, TreeNodeType nodeType) { this.nodeType = nodeType; } - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public TreeNodeType getNodeType() { - return nodeType; - } - - public void setNodeType(TreeNodeType nodeType) { - this.nodeType = nodeType; - } - - public boolean isHasChild() { - return hasChild; - } - - public void setHasChild(boolean hasChild) { - this.hasChild = hasChild; - } - - public List getChildren() { - return children; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public boolean isSelected() { - return isSelected; - } - - public void setSelected(boolean isSelected) { - this.isSelected = isSelected; - } - - public void setChildren(List children) { - this.children = children; - } - - public String getPath() { - return path; - } - - public boolean isRetricted() { - return isRetricted; - } - - public void setRetricted(boolean isRetricted) { - this.isRetricted = isRetricted; - } - @Override public int hashCode() { final int prime = 31; @@ -180,16 +123,24 @@ public boolean equals(Object obj) { } else if (!nodeType.equals(other.nodeType)) return false; if (path == null) { - if (other.path != null) - return false; - } else if (!path.equals(other.path)) - return false; - return true; - } - - public void pushDescendants(Map context, String userId) throws Exception { + return other.path == null; + } else + return path.equals(other.path); + } + + public void pushDescendants(Map context, String userId, Locale locale) throws Exception { addChildren(context, userId); - pushChildren(context, userId); + pushChildren(context, userId, locale); + + boolean isDefaultSort = this.children.stream() + .allMatch(child -> child.getPosition() == null); + + if (isDefaultSort) { + // Sort naturally by name only if all positions are null + this.children = this.children.stream() + .sorted((c1, c2) -> new NaturalComparator(locale).compare(c1.getName(), c2.getName())) + .toList(); + } } protected void addChildren(Map context, String userId) throws Exception { @@ -209,10 +160,10 @@ protected int getNumberOfChildren(Map context, int size) { return childrenNUm; } - private void pushChildren(Map context, String userId) throws Exception { + private void pushChildren(Map context, String userId, Locale locale) throws Exception { Deque paramsStk = (Deque) context.get(STACK_PARAMS); if (paramsStk == null) { - pushChild(context, userId); + pushChild(context, userId, locale); } else { if (paramsStk.isEmpty()) { this.isSelected = true; @@ -222,23 +173,23 @@ private void pushChildren(Map context, String userId) throws Exc context.put(STACK_PARAMS, paramsStk); if (this instanceof RootTreeNode) { SpaceTreeNode spaceNode = new SpaceTreeNode(params.getType()); - pushChild(spaceNode, context, userId); + pushChild(spaceNode, context, userId, locale); } else if (this instanceof SpaceTreeNode) { Wiki wiki = (Wiki) Utils.getObjectFromParams(params); WikiTreeNode wikiNode = new WikiTreeNode(wiki); - pushChild(wikiNode, context, userId); + pushChild(wikiNode, context, userId, locale); } else if (this instanceof WikiTreeNode) { - pushChild(context, userId); + pushChild(context, userId, locale); } else if (this instanceof WikiHomeTreeNode || this instanceof PageTreeNode) { Page page = (Page) Utils.getObjectFromParams(params); PageTreeNode pageNode = new PageTreeNode(page); - pushChild(pageNode, context, userId); + pushChild(pageNode, context, userId, locale); } } } } - private void pushChild(TreeNode child, Map context, String userId) throws Exception { + private void pushChild(TreeNode child, Map context, String userId, Locale locale) throws Exception { Boolean showDesCdt = (Boolean) context.get(SHOW_DESCENDANT); String depthCdt = (String) context.get(DEPTH); @@ -253,9 +204,9 @@ private void pushChild(TreeNode child, Map context, String userI for (int i = 0; i < children.size(); i++) { temp = children.get(i); if (child == null) { - temp.pushDescendants(context, userId); + temp.pushDescendants(context, userId, locale); } else if (child.equals(temp)) { - temp.pushDescendants(context, userId); + temp.pushDescendants(context, userId, locale); return; } } @@ -263,8 +214,8 @@ private void pushChild(TreeNode child, Map context, String userI } } - private void pushChild(Map context, String userId) throws Exception { - pushChild(null, context, userId); + private void pushChild(Map context, String userId, Locale locale) throws Exception { + pushChild(null, context, userId, locale); } public String buildPath() { diff --git a/notes-service/src/main/java/org/exoplatform/wiki/tree/utils/TreeUtils.java b/notes-service/src/main/java/org/exoplatform/wiki/tree/utils/TreeUtils.java index 5e423c060..2dcbf7ff7 100644 --- a/notes-service/src/main/java/org/exoplatform/wiki/tree/utils/TreeUtils.java +++ b/notes-service/src/main/java/org/exoplatform/wiki/tree/utils/TreeUtils.java @@ -18,7 +18,6 @@ */ package org.exoplatform.wiki.tree.utils; -import java.text.Collator; import java.util.*; import java.util.stream.Collectors; @@ -83,9 +82,9 @@ public static TreeNode getTreeNode(WikiPageParams params) throws Exception { * @return TreeNode * @throws Exception if error occured */ - public static TreeNode getDescendants(WikiPageParams params, Map context, String userId) throws Exception { + public static TreeNode getDescendants(WikiPageParams params, Map context, String userId, Locale locale) throws Exception { TreeNode treeNode = getTreeNode(params); - treeNode.pushDescendants(context, userId); + treeNode.pushDescendants(context, userId, locale); return treeNode; } @@ -131,7 +130,6 @@ public static List tranformToJson(TreeNode treeNode, children.add(jsonNodeData); counter++; } - sortNodes(children, locale); return children; } @@ -314,18 +312,4 @@ private static ResourceBundleService getResourceBundleService() { } return resourceBundleService; } - - private static void sortNodes(List nodes, Locale locale) { - if (nodes == null || nodes.isEmpty()) { - return; - } - Collator collator = Collator.getInstance(locale); - collator.setStrength(Collator.PRIMARY); - - nodes.sort((a, b) -> { - String nameA = a.getName() != null ? a.getName() : ""; - String nameB = b.getName() != null ? b.getName() : ""; - return collator.compare(nameA, nameB); - }); - } }