IDEMPIERE-3635 Cache getLookupInfo method to avoid multiple queries execution

This commit is contained in:
Carlos Ruiz 2018-02-09 22:10:28 +01:00
parent 45e1f0c654
commit 969d02f63c
4 changed files with 45 additions and 109 deletions

View File

@ -1312,9 +1312,8 @@ public class GridTab implements DataStatusListener, Evaluatee, Serializable
public void setLinkColumnName (String linkColumnName)
{
// set parent column name
String sql = "SELECT ColumnName FROM AD_Column WHERE AD_Column_ID=?";
if (m_vo.Parent_Column_ID > 0)
m_parentColumnName = DB.getSQLValueString(null, sql, m_vo.Parent_Column_ID );
m_parentColumnName = MColumn.getColumnName(m_vo.ctx, m_vo.Parent_Column_ID);
if ( m_parentColumnName == null )
m_parentColumnName = "";
@ -1329,27 +1328,7 @@ public class GridTab implements DataStatusListener, Evaluatee, Serializable
// we have a link column identified (primary parent column)
else
{
String SQL = "SELECT ColumnName FROM AD_Column WHERE AD_Column_ID=?";
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement(SQL, null);
pstmt.setInt(1, m_vo.AD_Column_ID); // Parent Link Column
rs = pstmt.executeQuery();
if (rs.next())
m_linkColumnName = rs.getString(1);
}
catch (SQLException e)
{
log.log(Level.SEVERE, "", e);
}
finally
{
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
m_linkColumnName = MColumn.getColumnName(m_vo.ctx, m_vo.AD_Column_ID); // Parent Link Column
if (log.isLoggable(Level.FINE)) log.fine("AD_Column_ID=" + m_vo.AD_Column_ID + " - " + m_linkColumnName);
}
}

View File

@ -91,42 +91,18 @@ public class MLookupFactory
public static MLookupInfo getLookupInfo(Properties ctx, int WindowNo, int TabNo, int Column_ID, int AD_Reference_ID)
{
String ColumnName = "";
int AD_Reference_Value_ID = 0;
boolean IsParent = false;
MColumn column = MColumn.get(ctx, Column_ID);
if (column.get_ID() == 0)
s_log.log(Level.SEVERE, "Column Not Found - AD_Column_ID=" + Column_ID);
String ColumnName = column.getColumnName();
int AD_Reference_Value_ID = column.getAD_Reference_Value_ID();
boolean IsParent = column.isParent();
String ValidationCode = "";
//
String sql = "SELECT c.ColumnName, c.AD_Reference_Value_ID, c.IsParent, vr.Code "
+ "FROM AD_Column c"
+ " LEFT OUTER JOIN AD_Val_Rule vr ON (c.AD_Val_Rule_ID=vr.AD_Val_Rule_ID) "
+ "WHERE c.AD_Column_ID=?";
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, Column_ID);
//
rs = pstmt.executeQuery();
if (rs.next())
{
ColumnName = rs.getString(1);
AD_Reference_Value_ID = rs.getInt(2);
IsParent = "Y".equals(rs.getString(3));
ValidationCode = rs.getString(4);
}
else
s_log.log(Level.SEVERE, "Column Not Found - AD_Column_ID=" + Column_ID);
}
catch (SQLException ex)
{
s_log.log(Level.SEVERE, "create", ex);
}
finally
{
DB.close(rs, pstmt);
rs = null;
pstmt = null;
if (column.getAD_Val_Rule_ID() > 0) {
MValRule valRule = MValRule.get(ctx, column.getAD_Val_Rule_ID());
ValidationCode = valRule.getCode();
}
//
MLookupInfo info = getLookupInfo (ctx, WindowNo, TabNo, Column_ID, AD_Reference_ID,
@ -993,41 +969,12 @@ public class MLookupFactory
} // getLookup_TableDirEmbed
private static ArrayList<LookupDisplayColumn> getListIdentifiers(String TableName) {
// get display column name (first identifier column)
String sql = "SELECT c.ColumnName,c.IsTranslated,c.AD_Reference_ID,c.AD_Reference_Value_ID "
+ ", c.ColumnSQL " // 5
+ "FROM AD_Table t INNER JOIN AD_Column c ON (t.AD_Table_ID=c.AD_Table_ID) "
+ "WHERE TableName=?"
+ " AND c.IsIdentifier='Y' "
+ "ORDER BY c.SeqNo";
//
ArrayList<LookupDisplayColumn> list = new ArrayList<LookupDisplayColumn>();
//
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement(sql, null);
pstmt.setString(1, TableName);
rs = pstmt.executeQuery();
while (rs.next())
{
LookupDisplayColumn ldc = new LookupDisplayColumn (rs.getString(1),
rs.getString(5),
"Y".equals(rs.getString(2)), rs.getInt(3), rs.getInt(4));
list.add (ldc);
}
}
catch (SQLException e)
{
s_log.log(Level.SEVERE, sql, e);
return null;
}
finally
{
DB.close(rs, pstmt);
rs = null;
pstmt = null;
MTable table = MTable.get(Env.getCtx(), TableName);
for (String idColumnName : table.getIdentifierColumns()) {
MColumn column = table.getColumn(idColumnName);
LookupDisplayColumn ldc = new LookupDisplayColumn(column.getColumnName(), column.getColumnSQL(), column.isTranslated(), column.getAD_Reference_ID(), column.getAD_Reference_Value_ID());
list.add (ldc);
}
return list;
}

View File

@ -20,6 +20,8 @@ package org.compiere.model;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@ -33,6 +35,7 @@ import org.adempiere.model.GenericPO;
import org.compiere.util.CCache;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.KeyNamePair;
/**
* Persistent Table Model
@ -356,19 +359,26 @@ public class MTable extends X_AD_Table
* Get Identifier Columns of Table
* @return Identifier columns
*/
public String[] getIdentifierColumns()
{
getColumns(false);
ArrayList<String> list = new ArrayList<String>();
//
for (int i = 0; i < m_columns.length; i++)
{
MColumn column = m_columns[i];
public String[] getIdentifierColumns() {
ArrayList<KeyNamePair> listkn = new ArrayList<KeyNamePair>();
for (MColumn column : getColumns(false)) {
if (column.isIdentifier())
list.add(column.getColumnName());
listkn.add(new KeyNamePair(column.getSeqNo(), column.getColumnName()));
}
// Order by SeqNo
Collections.sort(listkn, new Comparator<KeyNamePair>(){
public int compare(KeyNamePair s1,KeyNamePair s2){
if (s1.getKey() < s2.getKey())
return -1;
else if (s1.getKey() > s2.getKey())
return 1;
else
return 0;
}});
String[] retValue = new String[listkn.size()];
for (int i = 0; i < listkn.size(); i++) {
retValue[i] = listkn.get(i).getName();
}
String[] retValue = new String[list.size()];
retValue = list.toArray(retValue);
return retValue;
} // getIdentifierColumns

View File

@ -27,6 +27,8 @@ import org.adempiere.webui.window.WFieldSuggestion;
import org.compiere.model.GridField;
import org.compiere.model.Lookup;
import org.compiere.model.MRole;
import org.compiere.model.MTable;
import org.compiere.model.MZoomCondition;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;
@ -134,16 +136,14 @@ public class WEditorPopupMenu extends Menupopup implements EventListener<Event>
this.updateEnabled = false;
// check possible zoom conditions to enable back zoom
for (int zoomCondWinID :
DB.getIDsEx(null,
"SELECT AD_Window_ID FROM AD_ZoomCondition WHERE IsActive='Y' AND AD_Table_ID IN (SELECT AD_Table_ID FROM AD_Table WHERE TableName=?)",
tableName)) {
Boolean canAccessZoom = MRole.getDefault().getWindowAccess(zoomCondWinID);
MTable table = MTable.get(Env.getCtx(), tableName);
for (MZoomCondition zoomCondition : MZoomCondition.getConditions(table.getAD_Table_ID())) {
Boolean canAccessZoom = MRole.getDefault().getWindowAccess(zoomCondition.getAD_Window_ID());
if (canAccessZoom != null && canAccessZoom) {
this.zoomEnabled = true;
break;
}
}
}
} else {
int cnt = DB.getSQLValueEx(null,
"SELECT COUNT(*) "