IDEMPIERE-3049 String autocomplete NPE / refactor to apply best practices / drop the AD_Org_ID filter (doesn't make sense for non-org tables) / add ReQuery to context menu

This commit is contained in:
Carlos Ruiz 2017-08-30 14:26:08 +02:00
parent 205edcdb73
commit e287b987c2
2 changed files with 48 additions and 54 deletions

View File

@ -2361,54 +2361,42 @@ public class GridField
/**
* Returns a list containing all existing entries of this field
* with the actual AD_Org_ID and AD_Client_ID.
* with the actual AD_Client_ID.
* @return List of existing entries for this field
*/
public List<String> getEntries() {
/* TODO: consider caching the list to avoid repeating queries on every window open (twice, for find and for field) */
MColumn column = MColumn.get(Env.getCtx(), getAD_Column_ID());
MTable table = MTable.get(Env.getCtx(), column.getAD_Table_ID());
String tableName = table.getTableName();
String columnName = column.getColumnName();
ArrayList<String> list = new ArrayList<String>();
PreparedStatement pstmt1;
PreparedStatement pstmt2;
String sql = "";
try
{
String tableName = null;
String columnName = null;
int AD_Org_ID = Env.getAD_Org_ID(Env.getCtx());
if (tableName != null && columnName != null) {
int AD_Client_ID = Env.getAD_Client_ID(Env.getCtx());
sql = "SELECT t.TableName, c.ColumnName " +
" FROM AD_COLUMN c INNER JOIN AD_Table t ON (c.AD_Table_ID=t.AD_Table_ID)" +
" WHERE AD_Column_ID=?";
pstmt1 = DB.prepareStatement(sql, null);
pstmt1.setInt(1, getAD_Column_ID());
ResultSet rs1 = pstmt1.executeQuery();
if (rs1.next())
{
tableName = rs1.getString(1);
columnName = rs1.getString(2);
}
DB.close(rs1, pstmt1);
if (tableName != null && columnName != null) {
sql = "SELECT DISTINCT " + columnName + " FROM " + tableName + " WHERE AD_Client_ID=? "
+ " AND AD_Org_ID=?";
pstmt2 = DB.prepareStatement(sql, null);
pstmt2.setInt(1, AD_Client_ID);
pstmt2.setInt(2, AD_Org_ID);
ResultSet rs2 = pstmt2.executeQuery();
while (rs2.next())
{
list.add(rs2.getString(1));
PreparedStatement pstmt = null;
ResultSet rs = null;
StringBuilder sql = new StringBuilder()
.append("SELECT DISTINCT ")
.append(columnName)
.append(" FROM ")
.append(tableName)
.append(" WHERE AD_Client_ID=? AND ")
.append(columnName)
.append(" IS NOT NULL ORDER BY 1");
try {
pstmt = DB.prepareStatement(sql.toString(), null);
pstmt.setInt(1, AD_Client_ID);
rs = pstmt.executeQuery();
while (rs.next()) {
list.add(rs.getString(1));
}
DB.close(rs2, pstmt2);
} catch (Exception e) {
log.log(Level.SEVERE, sql.toString(), e);
} finally {
DB.close(rs, pstmt);
}
}
catch (Exception e)
{
log.log(Level.SEVERE, sql, e);
}
return list;
}

View File

@ -145,7 +145,8 @@ public class WStringEditor extends WEditor implements ContextMenuListener
}
else
getComponent().setMultiline(false);
getComponent().setRows(gridField.getNumLines() <= 0 || tableEditor ? 1 : gridField.getNumLines());
if (! gridField.isAutocomplete()) // avoid -> Combobox doesn't support multiple rows
getComponent().setRows(gridField.getNumLines() <= 0 || tableEditor ? 1 : gridField.getNumLines());
if (getComponent().getRows() > 1)
ZKUpdateUtil.setHeight(getComponent(), "100%");
@ -154,7 +155,7 @@ public class WStringEditor extends WEditor implements ContextMenuListener
if(!(this instanceof WPasswordEditor)){ // check password field
popupMenu = new WEditorPopupMenu(false, false, isShowPreference());
popupMenu = new WEditorPopupMenu(false, gridField.isAutocomplete(), isShowPreference());
addTextEditorMenu(popupMenu);
addChangeLogMenu(popupMenu);
}
@ -289,23 +290,28 @@ public class WStringEditor extends WEditor implements ContextMenuListener
{
WFieldRecordInfo.start(gridField);
}
else if (WEditorPopupMenu.REQUERY_EVENT.equals(evt.getContextEvent()))
{
actionRefresh();
}
}
@Override
public void dynamicDisplay() {
super.dynamicDisplay();
//referesh auto complete list
actionRefresh();
}
public void actionRefresh() {
//refresh auto complete list
if (gridField.isAutocomplete()) {
Combobox combo = (Combobox)getComponent();
List<String> items = gridField.getEntries();
if (items.size() != combo.getItemCount())
{
combo.removeAllItems();
for(String s : items) {
combo.appendItem(s);
}
}
}
Combobox combo = (Combobox)getComponent();
List<String> items = gridField.getEntries();
combo.removeAllItems();
for(String s : items) {
combo.appendItem(s);
}
}
}
private AbstractADWindowContent findADWindowContent() {