public static void updateTreeNode(PO po) { int nodeId = po.get_ID(); if (nodeId < 0) { return; // nothing to do, because our PO has no ID to match against a tree node } final int AD_Table_ID = po.get_Table_ID(); if (!MTree.hasTree(AD_Table_ID)) { return; } final String nodeTableName = MTree.getNodeTableName(AD_Table_ID); if (nodeTableName == null) { return; } // // services final IPOTreeSupportFactory treeSupportFactory = Services.get(IPOTreeSupportFactory.class); final String trxName = po.get_TrxName(); final IPOTreeSupport treeSupport = treeSupportFactory.get(po.get_TableName()); if (po.is_ValueChanged("IsSummary") && !po.get_ValueAsBoolean("IsSummary")) { // Move all its children to parent final List<MTreeNode> children = fetchNodes(AD_Table_ID, "Parent_ID=?", new Object[] {nodeId}, trxName); if (children.size() > 0) { throw new AdempiereException("TreeNodeHasChildren"); // TODO: translate } } if (treeSupport.isParentChanged(po)) { int parentId = treeSupport.getParent_ID(po); int oldParentId = treeSupport.getOldParent_ID(po); int seqNo = -1; // compute final String sql = "SELECT AD_Tree_ID, TreeType FROM AD_Tree WHERE AD_Table_ID=? AND AD_Client_ID=?"; PreparedStatement pstmt = null; ResultSet rs = null; try { pstmt = DB.prepareStatement(sql, trxName); DB.setParameters(pstmt, new Object[] {AD_Table_ID, po.getAD_Client_ID()}); rs = pstmt.executeQuery(); while (rs.next()) { int AD_Tree_ID = rs.getInt(COLUMNNAME_AD_Tree_ID); String treeType = rs.getString(COLUMNNAME_TreeType); updateNode( po, AD_Tree_ID, treeType, AD_Table_ID, nodeId, parentId, oldParentId, seqNo, trxName); } } catch (SQLException e) { throw new DBException(e); } finally { DB.close(rs, pstmt); rs = null; pstmt = null; } } }
/* * Update of user tree for ordered menu nodes (favorites, create new list) * favorites @param menuIDs List<Integer> ordered list of menuIDs to put in * the tree @param menuTree MTree the tree to be reordered @return true if * updated */ private boolean updateUserTree(List<Integer> menuIDs, MTree menuTree) { CTreeNode root = menuTree.getRoot(); if (root != null) { Enumeration<?> nodes = root.preorderEnumeration(); while (nodes.hasMoreElements()) { CTreeNode nd = (CTreeNode) nodes.nextElement(); if (!menuIDs.contains(nd.getNode_ID())) { MTreeNodeMM node = null; if ((node = MTreeNodeMM.get(menuTree, nd.getNode_ID())) != null) { if (!node.delete(true)) return false; } } } } int seq = 0; for (int id : menuIDs) { MTreeNodeMM node = null; if ((node = MTreeNodeMM.get(menuTree, id)) == null) { node = new MTreeNodeMM(menuTree, id); } node.setSeqNo(++seq); if (!node.save()) return false; } return true; }
/** * Delete ID Tree Nodes * * @return true if actually deleted (could be non existing) */ protected static boolean deleteTreeNode(PO po) { // TODO: check self contained tree int id = po.get_ID(); if (id <= 0) id = po.get_IDOld(); final int AD_Table_ID = po.get_Table_ID(); if (!MTree.hasTree(AD_Table_ID)) return false; final String treeTableName = MTree.getNodeTableName(AD_Table_ID); if (treeTableName == null) return false; final String trxName = po.get_TrxName(); final Logger log = po.get_Logger(); // // Check childrens: List<MTreeNode> children = fetchNodes(AD_Table_ID, "Parent_ID=?", new Object[] {id}, trxName); if (children.size() > 0) { throw new AdempiereException("TreeNodeHasChildren"); // TODO: translate } // final StringBuffer sb = new StringBuffer("DELETE FROM ") .append(treeTableName) .append(" n WHERE Node_ID=") .append(id) .append(" AND EXISTS (SELECT * FROM AD_Tree t ") .append("WHERE t.AD_Tree_ID=n.AD_Tree_ID AND t.AD_Table_ID=") .append(AD_Table_ID) .append(")"); // final int no = DB.executeUpdateEx(sb.toString(), trxName); if (no <= 0) { log.warn("#" + no + " - AD_Table_ID=" + AD_Table_ID); return false; } log.debug("#" + no + " - AD_Table_ID=" + AD_Table_ID); // MigrationLogger.instance.logMigrationSQL(po, sb.toString()); // metas: not needed because // it's called directly from PO listeners.onNodeDeleted(po); // // TODO Update children return true; } // delete_Tree
/** * Get a menu tree representation based on a AD_Tree_ID * * @param AD_Tree_ID A tree based on AD_Menu * @return menu as array list */ private ArrayList<CTreeNode> getMenuTree(int AD_Tree_ID, boolean edit) { MTree tree = new MTree(m_context, AD_Tree_ID, edit, true, true, null); // Language // set // in // WLogin // Trim tree CTreeNode root = tree.getRoot(); Enumeration<?> en = root.preorderEnumeration(); while (en.hasMoreElements()) { CTreeNode nd = (CTreeNode) en.nextElement(); if (nd.isTask() || nd.isWorkbench() // || nd.isWorkFlow() // server ) { CTreeNode parent = (CTreeNode) nd.getParent(); parent.remove(nd); } } tree.trimTree(); en = root.preorderEnumeration(); ArrayList<CTreeNode> retValue = new ArrayList<CTreeNode>(); while (en.hasMoreElements()) { CTreeNode nd = (CTreeNode) en.nextElement(); // Issue #420: removed menu entries for un-implemented forms if (nd.getAD_Form_ID() == 119 || nd.getAD_Form_ID() == 102 // || nd.getAD_Workflow_ID() == 106 // || nd.getAD_Workflow_ID() == 104 // // Review // || nd.getAD_Workflow_ID() == 112 // // Setup // || nd.getAD_Workflow_ID() == 113 // || nd.getAD_Workflow_ID() == 110 // || nd.getAD_Workflow_ID() == 111 // || nd.getAD_Process_ID() == 205 ) { } else retValue.add(nd); } return retValue; }
private static List<MTreeNode> fetchNodes( int AD_Table_ID, String whereClause, Object[] params, String trxName) { List<MTreeNode> list = new ArrayList<MTreeNode>(); final String nodeTableName = MTree.getNodeTableName(AD_Table_ID); if (nodeTableName == null) return list; PreparedStatement pstmt = null; ResultSet rs = null; StringBuffer sql = new StringBuffer("SELECT Node_ID, Parent_ID, SeqNo, AD_Tree_ID ") .append(" FROM ") .append(nodeTableName) .append(" WHERE ") .append(" EXISTS (SELECT * FROM AD_Tree t ") .append("WHERE t.AD_Tree_ID=" + nodeTableName + ".AD_Tree_ID AND t.AD_Table_ID=") .append(AD_Table_ID) .append(")") .append(" AND (") .append(whereClause) .append(")") .append(" ORDER BY AD_Tree_ID, Parent_ID, Node_ID"); try { pstmt = DB.prepareStatement(sql.toString(), trxName); DB.setParameters(pstmt, params); rs = pstmt.executeQuery(); while (rs.next()) { int node_ID = rs.getInt("Node_ID"); int parent_ID = rs.getInt("Node_ID"); int seqNo = rs.getInt("SeqNo"); int AD_Tree_ID = rs.getInt("AD_Tree_ID"); MTreeNode node = new MTreeNode(node_ID, seqNo, null, null, parent_ID, false, null, false, null); node.setAD_Tree_ID(AD_Tree_ID); node.setAD_Table_ID(AD_Table_ID); list.add(node); } } catch (SQLException e) { throw new DBException(e); } finally { DB.close(rs, pstmt); rs = null; pstmt = null; } return list; }
/** * ************************************************************************ Insert id data into * Tree * * @return true if inserted */ protected static boolean insertTreeNode(PO po) { // // services final IPOTreeSupportFactory treeSupportFactory = Services.get(IPOTreeSupportFactory.class); // TODO: check self contained tree final int AD_Table_ID = po.get_Table_ID(); if (!MTree.hasTree(AD_Table_ID)) { return false; } final int id = po.get_ID(); final int AD_Client_ID = po.getAD_Client_ID(); final String treeTableName = MTree.getNodeTableName(AD_Table_ID); final String trxName = po.get_TrxName(); final Logger log = po.get_Logger(); final IPOTreeSupport treeSupport = treeSupportFactory.get(po.get_TableName()); final int AD_Tree_ID = treeSupport.getAD_Tree_ID(po); int parentId = treeSupport.getParent_ID(po); if (parentId < 0 || parentId == IPOTreeSupport.UNKNOWN_ParentID) { parentId = ROOT_Node_ID; } // // Insert final StringBuilder sb = new StringBuilder("INSERT INTO ") .append(treeTableName) .append(" (AD_Client_ID,AD_Org_ID, IsActive,Created,CreatedBy,Updated,UpdatedBy, ") .append("AD_Tree_ID, Node_ID, Parent_ID, SeqNo) ") // .append( "SELECT t.AD_Client_ID,0, 'Y', now(), " + po.getUpdatedBy() + ", now(), " + po.getUpdatedBy() + ",") .append("t.AD_Tree_ID, ") .append(id) .append(", ") .append(parentId) .append(", 999 ") .append("FROM AD_Tree t ") .append("WHERE t.AD_Client_ID=") .append(AD_Client_ID) .append(" AND t.IsActive='Y'"); if (AD_Tree_ID > 0) { sb.append(" AND t.AD_Tree_ID=").append(AD_Tree_ID); } else // std trees { sb.append(" AND t.IsAllNodes='Y' AND t.AD_Table_ID=").append(AD_Table_ID); } // Duplicate Check sb.append(" AND NOT EXISTS (SELECT * FROM ") .append(treeTableName) .append(" e ") .append("WHERE e.AD_Tree_ID=t.AD_Tree_ID AND Node_ID=") .append(id) .append(")"); // int no = DB.executeUpdateEx(sb.toString(), trxName); if (no < 0) { log.warn("#" + no + " - AD_Table_ID=" + AD_Table_ID); return false; } log.debug("#" + no + " - AD_Table_ID=" + AD_Table_ID); // MigrationLogger.instance.logMigrationSQL(po, sb.toString()); // metas: not needed because // it's called directly from PO listeners.onNodeInserted(po); return true; } // insert_Tree