Skip to content

Commit

Permalink
IDEMPIERE-6102 Performance: avoid SQL on AD_TreeNode when the table d…
Browse files Browse the repository at this point in the history
…oesn't have a custom tree (idempiere#2309)
  • Loading branch information
CarlosRuiz-globalqss authored Apr 11, 2024
1 parent dc59508 commit a702881
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
16 changes: 15 additions & 1 deletion org.adempiere.base/src/org/compiere/model/MTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class MTable extends X_AD_Table implements ImmutablePOSupport
/**
*
*/
private static final long serialVersionUID = -167824144142429242L;
private static final long serialVersionUID = 6774131577483620665L;

public final static int MAX_OFFICIAL_ID = 999999;

Expand Down Expand Up @@ -1089,4 +1089,18 @@ public static String getUUIDIndexName(String tableName) {
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();
}

} // MTable
14 changes: 11 additions & 3 deletions org.adempiere.base/src/org/compiere/model/PO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2649,10 +2649,10 @@ private boolean saveFinish (boolean newRecord, boolean success)

// 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);
}
}
Expand Down Expand Up @@ -2756,6 +2756,14 @@ else if (get_ID() > 0)
return success;
} // saveFinish

/**
* Get the MTable object associated to this PO
* @return MTable
*/
private MTable getTable() {
return MTable.get(getCtx(), get_TableName());
}

/**
* Update or insert new record.<br/>
* To reload call load().
Expand Down Expand Up @@ -4102,7 +4110,7 @@ else if (savepoint != null)
{
//
deleteTranslations(localTrxName);
if (get_ColumnIndex("IsSummary") >= 0) {
if (get_ColumnIndex("IsSummary") >= 0 && getTable().hasCustomTree()) {
delete_Tree(MTree_Base.TREETYPE_CustomTable);
}

Expand Down

0 comments on commit a702881

Please sign in to comment.