From 1640fec33370f06f4bbea3201bef4cb8414d1791 Mon Sep 17 00:00:00 2001 From: Carlos Ruiz Date: Wed, 6 Feb 2013 10:50:35 -0500 Subject: [PATCH] IDEMPIERE-90 Restricted items on Combobox / refactor to avoid direct SQL and use cache --- .../src/org/compiere/model/MTable.java | 50 +++++++++++++++++-- .../src/org/compiere/grid/ed/VLookup.java | 7 +-- .../webui/editor/WTableDirEditor.java | 8 +-- 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/model/MTable.java b/org.adempiere.base/src/org/compiere/model/MTable.java index 390ece0e89..2ceb8c62a0 100644 --- a/org.adempiere.base/src/org/compiere/model/MTable.java +++ b/org.adempiere.base/src/org/compiere/model/MTable.java @@ -20,8 +20,10 @@ package org.compiere.model; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Properties; import java.util.logging.Level; @@ -53,7 +55,7 @@ public class MTable extends X_AD_Table /** * */ - private static final long serialVersionUID = 8264472455498363565L; + private static final long serialVersionUID = -8904670462021706436L; public final static int MAX_OFFICIAL_ID = 999999; @@ -211,6 +213,10 @@ public class MTable extends X_AD_Table /** Columns */ private MColumn[] m_columns = null; + /** column name to index map **/ + private Map m_columnNameMap; + /** ad_column_id to index map **/ + private Map m_columnIdMap; /** * Get Columns @@ -221,6 +227,8 @@ public class MTable extends X_AD_Table { if (m_columns != null && !requery) return m_columns; + m_columnNameMap = new HashMap(); + m_columnIdMap = new HashMap(); String sql = "SELECT * FROM AD_Column WHERE AD_Table_ID=? ORDER BY ColumnName"; ArrayList list = new ArrayList(); PreparedStatement pstmt = null; @@ -229,8 +237,12 @@ public class MTable extends X_AD_Table pstmt = DB.prepareStatement (sql, get_TrxName()); pstmt.setInt (1, getAD_Table_ID()); ResultSet rs = pstmt.executeQuery (); - while (rs.next ()) - list.add (new MColumn (getCtx(), rs, get_TrxName())); + while (rs.next ()) { + MColumn column = new MColumn (getCtx(), rs, get_TrxName()); + list.add (column); + m_columnNameMap.put(column.getColumnName().toUpperCase(), list.size() - 1); + m_columnIdMap.put(column.getAD_Column_ID(), list.size() - 1); + } rs.close (); pstmt.close (); pstmt = null; @@ -274,6 +286,38 @@ public class MTable extends X_AD_Table return null; } // getColumn + /** + * Get Column Index + * @param ColumnName column name + * @return index of column with ColumnName or -1 if not found + */ + public int getColumnIndex (String ColumnName) + { + if (m_columns == null) + getColumns(false); + Integer i = m_columnNameMap.get(ColumnName.toUpperCase()); + if (i != null) + return i.intValue(); + + return -1; + } // getColumnIndex + + /** + * Get Column Index + * @param AD_Column_ID column + * @return index of column with ColumnName or -1 if not found + */ + public int getColumnIndex (int AD_Column_ID) + { + if (m_columns == null) + getColumns(false); + Integer i = m_columnIdMap.get(AD_Column_ID); + if (i != null) + return i.intValue(); + + return -1; + } // getColumnIndex + /** * Table has a single Key * @return true if table has single key column diff --git a/org.adempiere.ui.swing/src/org/compiere/grid/ed/VLookup.java b/org.adempiere.ui.swing/src/org/compiere/grid/ed/VLookup.java index 4669786766..4d05bc30f2 100644 --- a/org.adempiere.ui.swing/src/org/compiere/grid/ed/VLookup.java +++ b/org.adempiere.ui.swing/src/org/compiere/grid/ed/VLookup.java @@ -281,11 +281,8 @@ public class VLookup extends JComponent String tableName_temp = m_lookup.getColumnName(); // Returns AD_Org.AD_Org_ID int posPoint = tableName_temp.indexOf("."); String tableName = tableName_temp.substring(0, posPoint); - int table_id = MTable.getTable_ID(tableName); // now we got the ad_table_id - - String sql = "SELECT COUNT(*) FROM AD_Column WHERE ColumnName = 'IsShortList' AND IsActive='Y' AND AD_Table_ID = " + table_id; - isShortListAvailable = DB.getSQLValue(null, sql) == 1; // if the table has an active IsShortList column, we could use the restrict search ! - + MTable table = MTable.get(Env.getCtx(), tableName); + isShortListAvailable = (table.getColumnIndex("IsShortList") >= 0); if (isShortListAvailable) { setComboShortList(true); diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/editor/WTableDirEditor.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/editor/WTableDirEditor.java index ded84de208..68986a0589 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/editor/WTableDirEditor.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/editor/WTableDirEditor.java @@ -38,7 +38,6 @@ import org.compiere.model.MBPartnerLocation; import org.compiere.model.MLocation; import org.compiere.model.MTable; import org.compiere.util.CLogger; -import org.compiere.util.DB; import org.compiere.util.DisplayType; import org.compiere.util.Env; import org.compiere.util.KeyNamePair; @@ -173,11 +172,8 @@ ContextMenuListener, IZoomableEditor String tableName_temp = lookup.getColumnName(); // Returns AD_Org.AD_Org_ID int posPoint = tableName_temp.indexOf("."); String tableName = tableName_temp.substring(0, posPoint); - int table_id = MTable.getTable_ID(tableName); // now we got the ad_table_id - - String sql = "SELECT COUNT(*) FROM AD_Column WHERE ColumnName = 'IsShortList' AND IsActive='Y' AND AD_Table_ID = " + table_id; - isShortListAvailable = DB.getSQLValue(null, sql) == 1; // if the table has an active isShortList column, we could use the restrict search ! - + MTable table = MTable.get(Env.getCtx(), tableName); + isShortListAvailable = (table.getColumnIndex("IsShortList") >= 0); if (isShortListAvailable) { onlyShortListItems=true;