Skip to content

Commit

Permalink
Change load nodes from tree
Browse files Browse the repository at this point in the history
  • Loading branch information
yamelsenih committed Jul 24, 2024
1 parent 42447cd commit 80e7a55
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 40 deletions.
99 changes: 59 additions & 40 deletions src/main/java/org/spin/eca56/util/support/documents/MenuTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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<String, Object> parent) {
Enumeration<?> childrens = node.children();
List<Map<String, Object>> children = new ArrayList<>();
while (childrens.hasMoreElements()) {
MTreeNode childNode = (MTreeNode)childrens.nextElement();
Map<String, Object> child = convertNode(childNode);
addChildren(childNode, child);
children.add(child);
}
parent.put("children", children);
}

private Map<String, Object> convertNode(MTreeNode node) {
private Map<String, Object> convertNode(TreeNodeReference node) {
Map<String, Object> 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<TreeNodeReference> 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<Object> parameters = new ArrayList<Object>();
parameters.add(treeId);
parameters.add(parentId);
List<TreeNodeReference> nodeIds = new ArrayList<TreeNodeReference>();
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<String, Object> documentDetail = convertNode(node);

public MenuTree withNode(MTree tree) {
List<TreeNodeReference> children = getChildren(tree.getAD_Tree_ID(), 0);
Map<String, Object> documentDetail = convertNode(TreeNodeReference.newInstance());
documentDetail.put("internal_id", tree.getAD_Tree_ID());
documentDetail.put("id", tree.getUUID());
documentDetail.put("uuid", tree.getUUID());
List<Map<String, Object>> children = new ArrayList<>();
while (childrens.hasMoreElements()) {
MTreeNode childNode = (MTreeNode)childrens.nextElement();
Map<String, Object> child = convertNode(childNode);
List<Map<String, Object>> childrenAsMap = new ArrayList<>();
children.forEach(child -> {
Map<String, Object> 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<String, Object> parent) {
List<TreeNodeReference> children = getChildren(treeId, node.getNodeId());
List<Map<String, Object>> childrenAsMap = new ArrayList<>();
children.forEach(child -> {
Map<String, Object> nodeAsMap = convertNode(child);
// Explode child
addChildren(treeId, child, nodeAsMap);
childrenAsMap.add(nodeAsMap);
});
parent.put("children", childrenAsMap);
}

private MenuTree() {
super();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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, [email protected] *
*****************************************************************************/
package org.spin.eca56.util.support.documents;

/**
* A stub class for nodes
* @author Yamel Senih, [email protected], 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;
}
}

0 comments on commit 80e7a55

Please sign in to comment.