From fb594a5f565c72c4c1c2bbbec8364a1b8de9d7b0 Mon Sep 17 00:00:00 2001 From: Carlos Ruiz Date: Thu, 11 Apr 2024 05:53:44 +0200 Subject: [PATCH] IDEMPIERE-6102 Performance: avoid SQL on AD_TreeNode when the table doesn't have a custom tree (#2309) --- .../src/org/compiere/model/MTable.java | 20 ++++++++++++++++++- .../src/org/compiere/model/PO.java | 18 ++++++++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/model/MTable.java b/org.adempiere.base/src/org/compiere/model/MTable.java index ef40b27e0d..00500f8172 100644 --- a/org.adempiere.base/src/org/compiere/model/MTable.java +++ b/org.adempiere.base/src/org/compiere/model/MTable.java @@ -67,7 +67,11 @@ public class MTable extends X_AD_Table implements ImmutablePOSupport /** * */ - private static final long serialVersionUID = -167824144142429242L; + private static final long serialVersionUID = -2459194178797758731L; + + /** + * + */ public final static int MAX_OFFICIAL_ID = 999999; @@ -1098,6 +1102,20 @@ public class MTable extends X_AD_Table implements ImmutablePOSupport return indexName.toString(); } + private Boolean hasCustomTree = null; + + /** + * If the table has a custom tree defined + * @return + */ + public boolean hasCustomTree() { + if (hasCustomTree == null) { + int exists = DB.getSQLValueEx(get_TrxName(), "SELECT 1 FROM AD_Tree WHERE TreeType=? AND AD_Table_ID=? AND IsActive='Y'", MTree_Base.TREETYPE_CustomTable, getAD_Table_ID()); + hasCustomTree = Boolean.valueOf(exists == 1); + } + return hasCustomTree.booleanValue(); + } + /** * Get Partition Name of the table of the given level * @param tableName diff --git a/org.adempiere.base/src/org/compiere/model/PO.java b/org.adempiere.base/src/org/compiere/model/PO.java index be645b0902..47bda21f69 100644 --- a/org.adempiere.base/src/org/compiere/model/PO.java +++ b/org.adempiere.base/src/org/compiere/model/PO.java @@ -2641,10 +2641,10 @@ public abstract class PO // table with potential tree if (get_ColumnIndex("IsSummary") >= 0) { - if (newRecord) + if (newRecord && getTable().hasCustomTree()) insert_Tree(MTree_Base.TREETYPE_CustomTable); int idxValue = get_ColumnIndex("Value"); - if (newRecord || (idxValue >= 0 && is_ValueChanged(idxValue))) + if (getTable().hasCustomTree() && (newRecord || (idxValue >= 0 && is_ValueChanged(idxValue)))) update_Tree(MTree_Base.TREETYPE_CustomTable); } } @@ -2749,8 +2749,16 @@ public abstract class PO } // saveFinish /** - * Update Value or create new record. - * To reload call load() - not updated + * Get the MTable object associated to this PO + * @return MTable + */ + private MTable getTable() { + return MTable.get(getCtx(), get_TableName()); + } + + /** + * Update or insert new record.
+ * To reload call load(). * @param trxName transaction * @return true if saved */ @@ -4063,7 +4071,7 @@ public abstract class PO { // deleteTranslations(localTrxName); - if (get_ColumnIndex("IsSummary") >= 0) { + if (get_ColumnIndex("IsSummary") >= 0 && getTable().hasCustomTree()) { delete_Tree(MTree_Base.TREETYPE_CustomTable); }