From 80e7a558610c525e359c835b5580dbcb454cce65 Mon Sep 17 00:00:00 2001 From: yamelsenih Date: Wed, 24 Jul 2024 17:47:46 -0400 Subject: [PATCH] Change load nodes from tree --- .../util/support/documents/MenuTree.java | 99 +++++++++++-------- .../support/documents/TreeNodeReference.java | 60 +++++++++++ 2 files changed, 119 insertions(+), 40 deletions(-) create mode 100644 src/main/java/org/spin/eca56/util/support/documents/TreeNodeReference.java diff --git a/src/main/java/org/spin/eca56/util/support/documents/MenuTree.java b/src/main/java/org/spin/eca56/util/support/documents/MenuTree.java index 86b0a57..df15169 100644 --- a/src/main/java/org/spin/eca56/util/support/documents/MenuTree.java +++ b/src/main/java/org/spin/eca56/util/support/documents/MenuTree.java @@ -18,14 +18,14 @@ package org.spin.eca56.util.support.documents; import java.util.ArrayList; -import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.adempiere.exceptions.AdempiereException; import org.compiere.model.MTree; -import org.compiere.model.MTreeNode; import org.compiere.model.PO; +import org.compiere.util.DB; import org.spin.eca56.util.support.DictionaryDocument; /** @@ -42,61 +42,80 @@ public String getKey() { return KEY; } - /** - * Add children to menu - * @param context - * @param builder - * @param node - */ - private void addChildren(MTreeNode node, Map parent) { - Enumeration childrens = node.children(); - List> children = new ArrayList<>(); - while (childrens.hasMoreElements()) { - MTreeNode childNode = (MTreeNode)childrens.nextElement(); - Map child = convertNode(childNode); - addChildren(childNode, child); - children.add(child); - } - parent.put("children", children); - } - - private Map convertNode(MTreeNode node) { + private Map convertNode(TreeNodeReference node) { Map detail = new HashMap<>(); - detail.put("node_id", node.getNode_ID()); - detail.put("parent_id", node.getParent_ID()); - detail.put("sequence", Integer.parseInt(node.getSeqNo())); + detail.put("node_id", node.getNodeId()); + detail.put("parent_id", node.getParentId()); + detail.put("sequence", node.getSequence()); return detail; } @Override public DictionaryDocument withEntity(PO entity) { MTree tree = (MTree) entity; - MTreeNode rootNode = tree.getRoot(); - return withNode(tree, rootNode); + return withNode(tree); + } + + private List getChildren(int treeId, int parentId) { + String tableName = MTree.getNodeTableName(MTree.TREETYPE_Menu); + final String sql = "SELECT tn.Node_ID, tn.SeqNo " + + "FROM " + tableName + " tn " + + "WHERE tn.AD_Tree_ID = ? " + + "AND tn.Parent_ID = ?"; + List parameters = new ArrayList(); + parameters.add(treeId); + parameters.add(parentId); + List nodeIds = new ArrayList(); + DB.runResultSet(null, sql, parameters, resulset -> { + while (resulset.next()) { + nodeIds.add(TreeNodeReference.newInstance() + .withNodeId(resulset.getInt("Node_ID")) + .withParentId(parentId) + .withSequence(resulset.getInt("SeqNo"))); + } + }).onFailure(throwable -> { + throw new AdempiereException(throwable); + }); + return nodeIds; } - public MenuTree withNode(MTree tree, MTreeNode node) { - if(node == null) { - return this; - } - Enumeration childrens = node.children(); - Map documentDetail = convertNode(node); + + public MenuTree withNode(MTree tree) { + List children = getChildren(tree.getAD_Tree_ID(), 0); + Map documentDetail = convertNode(TreeNodeReference.newInstance()); documentDetail.put("internal_id", tree.getAD_Tree_ID()); documentDetail.put("id", tree.getUUID()); documentDetail.put("uuid", tree.getUUID()); - List> children = new ArrayList<>(); - while (childrens.hasMoreElements()) { - MTreeNode childNode = (MTreeNode)childrens.nextElement(); - Map child = convertNode(childNode); + List> childrenAsMap = new ArrayList<>(); + children.forEach(child -> { + Map nodeAsMap = convertNode(child); // Explode child - addChildren(childNode, child); - children.add(child); - } - documentDetail.put("children", children); + addChildren(tree.getAD_Tree_ID(), child, nodeAsMap); + childrenAsMap.add(nodeAsMap); + }); + documentDetail.put("children", childrenAsMap); putDocument(documentDetail); return this; } + /** + * Add children to menu + * @param context + * @param builder + * @param node + */ + private void addChildren(int treeId, TreeNodeReference node, Map parent) { + List children = getChildren(treeId, node.getNodeId()); + List> childrenAsMap = new ArrayList<>(); + children.forEach(child -> { + Map nodeAsMap = convertNode(child); + // Explode child + addChildren(treeId, child, nodeAsMap); + childrenAsMap.add(nodeAsMap); + }); + parent.put("children", childrenAsMap); + } + private MenuTree() { super(); } diff --git a/src/main/java/org/spin/eca56/util/support/documents/TreeNodeReference.java b/src/main/java/org/spin/eca56/util/support/documents/TreeNodeReference.java new file mode 100644 index 0000000..ab514f9 --- /dev/null +++ b/src/main/java/org/spin/eca56/util/support/documents/TreeNodeReference.java @@ -0,0 +1,60 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 or later of the * + * GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * For the text or an alternative of this public license, you may reach us * + * Copyright (C) 2003-2024 E.R.P. Consultores y Asociados, C.A. * + * All Rights Reserved. * + * Contributor(s): Edwin Betancourt, EdwinBetanc0urt@outlook.com * + *****************************************************************************/ +package org.spin.eca56.util.support.documents; + +/** + * A stub class for nodes + * @author Yamel Senih, ysenih@erpya.com, ERPCyA http://www.erpya.com + */ +public class TreeNodeReference { + private int nodeId; + private int parentId; + private int sequence; + + public static TreeNodeReference newInstance() { + return new TreeNodeReference(); + } + + public int getNodeId() { + return nodeId; + } + + public TreeNodeReference withNodeId(int nodeId) { + this.nodeId = nodeId; + return this; + + } + + public int getParentId() { + return parentId; + } + + public TreeNodeReference withParentId(int parentId) { + this.parentId = parentId; + return this; + } + + public int getSequence() { + return sequence; + } + + public TreeNodeReference withSequence(int sequence) { + this.sequence = sequence; + return this; + } +}