/** * 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
/** * ************************************************************************ 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