IDEMPIERE-5992 - Move the getIdentifier methods from WRecordEditor to MLookup class (#2183)

* IDEMPIERE-5992 - Move the getIdentifier methods from WRecordEditor to MLookup class

* IDEMPIERE-5992 - refactor logic

* IDEMPIERE-5992 - replace keyColumn parameter with useUUIDKey
This commit is contained in:
Peter Takács 2024-01-31 05:08:00 +01:00 committed by Carlos Ruiz
parent 8422123487
commit e5eebba4a6
5 changed files with 125 additions and 87 deletions

View File

@ -38,6 +38,7 @@ import org.compiere.util.DisplayType;
import org.compiere.util.Env; import org.compiere.util.Env;
import org.compiere.util.KeyNamePair; import org.compiere.util.KeyNamePair;
import org.compiere.util.NamePair; import org.compiere.util.NamePair;
import org.compiere.util.Util;
import org.compiere.util.ValueNamePair; import org.compiere.util.ValueNamePair;
/** /**
@ -995,6 +996,87 @@ public final class MLookup extends Lookup implements Serializable
return vnpCache; return vnpCache;
} }
/**
* Get Lookup
* @param tableID
* @param windowNo
* @param tabNo
* @return null if tableID <= 0 or the table doesn't have any key column, else {@link MLookup}
*/
public static MLookup getRecordsLookup(int tableID, int windowNo, int tabNo) {
return getRecordsLookup(tableID, windowNo, tabNo, false);
}
/**
* Get Lookup
* @param tableID
* @param windowNo
* @param tabNo
* @param useUUIDKey - default false
* @return null if tableID <= 0 or the table doesn't have any key column, else {@link MLookup}
*/
public static MLookup getRecordsLookup(int tableID, int windowNo, int tabNo, boolean useUUIDKey) {
if(tableID <= 0)
return null;
MTable mTable = MTable.get(Env.getCtx(), tableID, null);
// load key column
String keyColumn = "";
if(!useUUIDKey) {
String[] keyColumns = mTable.getKeyColumns();
// the table has a single key column
if(keyColumns != null && keyColumns.length == 1)
keyColumn = keyColumns[0];
}
if(Util.isEmpty(keyColumn)) {
keyColumn = PO.getUUIDColumnName(mTable.getTableName());
}
if(Util.isEmpty(keyColumn))
return null;
MColumn mColumn = MColumn.get(Env.getCtx(), mTable.getTableName(), keyColumn);
MLookupInfo lookupInfo = MLookupFactory.getLookupInfo (Env.getCtx(), windowNo, tabNo, mColumn.getAD_Column_ID(), DisplayType.Search);
return new MLookup(lookupInfo, tabNo);
}
/**
* Get Identifier String from AD_Table_ID and Record_ID
* @param tableID
* @param recordID
* @return String
*/
public static String getIdentifier(int tableID, Serializable recordID) {
return getIdentifier(tableID, recordID, 0, 0);
}
/**
* Get Identifier String from AD_Table_ID and Record_ID
* @param tableID
* @param recordID
* @param windowNo
* @param tabNo
* @return String
*/
public static String getIdentifier(int tableID, Serializable recordID, int windowNo, int tabNo) {
return getIdentifier(tableID, recordID, windowNo, tabNo, false);
}
/**
* Get Identifier String from AD_Table_ID and Record_ID
* @param tableID
* @param recordID
* @param windowNo
* @param tabNo
* @param useUUIDKey - default false
* @return String
*/
public static String getIdentifier(int tableID, Serializable recordID, int windowNo, int tabNo, boolean useUUIDKey) {
MLookup lookup = getRecordsLookup(tableID, windowNo, tabNo, useUUIDKey);
return lookup != null ? lookup.getDisplay(recordID) : "";
}
/** /**
* Background Data Loader * Background Data Loader
*/ */

View File

@ -25,6 +25,8 @@
**********************************************************************/ **********************************************************************/
package org.adempiere.webui.editor; package org.adempiere.webui.editor;
import java.io.Serializable;
import org.adempiere.util.GridRowCtx; import org.adempiere.util.GridRowCtx;
import org.adempiere.webui.ValuePreference; import org.adempiere.webui.ValuePreference;
import org.adempiere.webui.component.Textbox; import org.adempiere.webui.component.Textbox;
@ -39,11 +41,7 @@ import org.adempiere.webui.window.WFieldRecordInfo;
import org.adempiere.webui.window.WRecordIDDialog; import org.adempiere.webui.window.WRecordIDDialog;
import org.compiere.model.GridField; import org.compiere.model.GridField;
import org.compiere.model.Lookup; import org.compiere.model.Lookup;
import org.compiere.model.MColumn;
import org.compiere.model.MLookup; import org.compiere.model.MLookup;
import org.compiere.model.MLookupFactory;
import org.compiere.model.MLookupInfo;
import org.compiere.model.MTable;
import org.compiere.util.DisplayType; import org.compiere.util.DisplayType;
import org.compiere.util.Env; import org.compiere.util.Env;
import org.compiere.util.Msg; import org.compiere.util.Msg;
@ -72,6 +70,10 @@ public abstract class WRecordEditor<T> extends WEditor implements ContextMenuLis
/** Current tab's AD_Table_ID GridField */ /** Current tab's AD_Table_ID GridField */
protected GridField tableIDGridField; protected GridField tableIDGridField;
/** Window Number */
int windowNo;
/** Tab Number */
int tabNo;
// UI components // UI components
protected Textbox recordTextBox; protected Textbox recordTextBox;
@ -90,7 +92,8 @@ public abstract class WRecordEditor<T> extends WEditor implements ContextMenuLis
*/ */
public WRecordEditor(GridField gridField, boolean tableEditor, IEditorConfiguration editorConfiguration) { public WRecordEditor(GridField gridField, boolean tableEditor, IEditorConfiguration editorConfiguration) {
super(new Div(), gridField, tableEditor, editorConfiguration); super(new Div(), gridField, tableEditor, editorConfiguration);
tabNo = gridTab != null ? gridTab.getTabNo() : FindWindow.TABNO;
windowNo = gridTab != null ? gridTab.getWindowNo() : gridField.getWindowNo();
getComponent().setSclass("recordid-editor"); getComponent().setSclass("recordid-editor");
getComponent().addEventListener(Events.ON_RIGHT_CLICK, this); getComponent().addEventListener(Events.ON_RIGHT_CLICK, this);
@ -261,9 +264,10 @@ public abstract class WRecordEditor<T> extends WEditor implements ContextMenuLis
if(value != null && tableIDValue != null) { if(value != null && tableIDValue != null) {
int tableID = Integer.parseInt(String.valueOf(tableIDValue)); int tableID = Integer.parseInt(String.valueOf(tableIDValue));
if (tableID > 0) { if (tableID > 0) {
Object recordID = toKeyValue(value); Serializable recordID = (Serializable) toKeyValue(value);
if(recordID != null && tableID > 0) if(recordID != null && tableID > 0) {
recordTextBox.setValue(getIdentifier(tableID, recordID)); recordTextBox.setValue(MLookup.getIdentifier(tableID, recordID, tabNo, windowNo, isUseUUIDKey()));
}
} }
} }
} }
@ -292,14 +296,14 @@ public abstract class WRecordEditor<T> extends WEditor implements ContextMenuLis
return ""; return "";
if((tableIDValue != null) && (recordIDValue != null)) { if((tableIDValue != null) && (recordIDValue != null)) {
int tableID; int tableID;
Object recordID; Serializable recordID;
try { try {
tableID = Integer.parseInt(String.valueOf(tableIDValue)); tableID = Integer.parseInt(String.valueOf(tableIDValue));
recordID = toKeyValue(recordIDValue); recordID = (Serializable) toKeyValue(recordIDValue);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
return recordIDValue.toString(); return recordIDValue.toString();
} }
return getIdentifier(tableID, recordID); return MLookup.getIdentifier(tableID, recordID, tabNo, windowNo, isUseUUIDKey());
} }
else { else {
return recordIDValue.toString(); return recordIDValue.toString();
@ -314,14 +318,14 @@ public abstract class WRecordEditor<T> extends WEditor implements ContextMenuLis
String key = gridTab.getWindowNo() + "|AD_Table_ID"; String key = gridTab.getWindowNo() + "|AD_Table_ID";
Object rowTableIdValue = gridRowCtx.get(key); Object rowTableIdValue = gridRowCtx.get(key);
int rowTableID; int rowTableID;
Object rowRecordID; Serializable rowRecordID;
try { try {
rowTableID = Integer.parseInt(String.valueOf(rowTableIdValue)); rowTableID = Integer.parseInt(String.valueOf(rowTableIdValue));
rowRecordID = toKeyValue(value); rowRecordID = (Serializable) toKeyValue(value);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
return value.toString(); return value.toString();
} }
return getIdentifier(rowTableID, rowRecordID); return MLookup.getIdentifier(rowTableID, rowRecordID, tabNo, windowNo, isUseUUIDKey());
} else { } else {
return value.toString(); return value.toString();
} }
@ -360,65 +364,6 @@ public abstract class WRecordEditor<T> extends WEditor implements ContextMenuLis
} }
} }
/**
* Get Lookup
* @param tableID
* @return null if tableID &lt;= 0 or the table doesn't have any key column, else {@link MLookup}
*/
public MLookup getRecordsLookup(int tableID) {
return getRecordsLookup(tableID, false);
}
/**
* Get Lookup
* @param tableID
* @param tableBased - if the lookup checks for the UUID Key Column based on the table (true), or on the columnName (false)
* @return null if tableID <= 0 or the table doesn't have any key column, else {@link MLookup}
*/
private MLookup getRecordsLookup(int tableID, boolean tableBased) {
if(tableID <= 0)
return null;
MTable mTable = MTable.get(Env.getCtx(), tableID, null);
String keyColumn = "";
if (tableBased) {
String[] keyColumns = mTable.getKeyColumns();
keyColumn = keyColumns != null && keyColumns.length > 0 ? keyColumns[0] : null;
} else {
keyColumn = getKeyColumn(mTable);
}
if (Util.isEmpty(keyColumn))
return null;
MColumn mColumn = MColumn.get(Env.getCtx(), mTable.getTableName(), keyColumn);
int tabNo = gridTab != null ? gridTab.getTabNo() : FindWindow.TABNO;
int windowNo = gridTab != null ? gridTab.getWindowNo() : gridField.getWindowNo();
MLookupInfo lookupInfo = MLookupFactory.getLookupInfo (Env.getCtx(), windowNo, tabNo, mColumn.getAD_Column_ID(), DisplayType.Search);
return new MLookup(lookupInfo, tabNo);
}
/**
* Get Identifier String from AD_Table_ID and Record_ID
* @param tableID
* @param recordID
* @return String
*/
public String getIdentifier(int tableID, Object recordID) {
MLookup lookup = getRecordsLookup(tableID);
return lookup != null ? lookup.getDisplay(recordID) : "";
}
/**
* Get Parent Identifier String from AD_Table_ID and Record_ID
* @param tableID
* @param recordID
* @return String
*/
public String getParentIdentifier(int tableID, Object recordID) {
MLookup lookup = getRecordsLookup(tableID, true);
return lookup != null ? lookup.getDisplay(recordID) : "";
}
/** /**
* Get AD_Table_ID * Get AD_Table_ID
* @return AD_Table_ID value * @return AD_Table_ID value
@ -453,10 +398,10 @@ public abstract class WRecordEditor<T> extends WEditor implements ContextMenuLis
} }
/** /**
* @param mTable Reference table * Use UUID as key column
* @return Key column name * @return Key column name
*/ */
protected abstract String getKeyColumn(MTable mTable); public abstract boolean isUseUUIDKey();
/** /**
* Convert value to key value type * Convert value to key value type

View File

@ -95,9 +95,8 @@ public class WRecordIDEditor extends WRecordEditor<Integer> {
} }
@Override @Override
protected String getKeyColumn(MTable mTable) { public boolean isUseUUIDKey() {
String[] keyColumns = mTable.getKeyColumns(); return false;
return keyColumns != null && keyColumns.length > 0 ? keyColumns[0] : null;
} }
@Override @Override

View File

@ -36,7 +36,6 @@ import org.adempiere.webui.window.WRecordIDDialog;
import org.compiere.model.GridField; import org.compiere.model.GridField;
import org.compiere.model.MRole; import org.compiere.model.MRole;
import org.compiere.model.MTable; import org.compiere.model.MTable;
import org.compiere.model.PO;
import org.compiere.util.DisplayType; import org.compiere.util.DisplayType;
import org.compiere.util.Env; import org.compiere.util.Env;
import org.compiere.util.Msg; import org.compiere.util.Msg;
@ -96,8 +95,8 @@ public class WRecordUUIDEditor extends WRecordEditor<String> {
} }
@Override @Override
protected String getKeyColumn(MTable mTable) { public boolean isUseUUIDKey() {
return PO.getUUIDColumnName(mTable.getTableName()); return true;
} }
@Override @Override

View File

@ -24,6 +24,7 @@
**********************************************************************/ **********************************************************************/
package org.adempiere.webui.window; package org.adempiere.webui.window;
import java.io.Serializable;
import java.util.Objects; import java.util.Objects;
import org.adempiere.webui.component.Button; import org.adempiere.webui.component.Button;
@ -38,6 +39,7 @@ import org.adempiere.webui.event.ValueChangeEvent;
import org.adempiere.webui.event.ValueChangeListener; import org.adempiere.webui.event.ValueChangeListener;
import org.adempiere.webui.factory.ButtonFactory; import org.adempiere.webui.factory.ButtonFactory;
import org.compiere.model.GridField; import org.compiere.model.GridField;
import org.compiere.model.GridTab;
import org.compiere.model.MLookup; import org.compiere.model.MLookup;
import org.compiere.model.MTable; import org.compiere.model.MTable;
import org.compiere.model.PO; import org.compiere.model.PO;
@ -74,6 +76,14 @@ public class WRecordIDDialog extends Window implements EventListener<Event>, Val
private Object recordIDValue; private Object recordIDValue;
/** Current AD_Table_ID value from {@link #editor} */ /** Current AD_Table_ID value from {@link #editor} */
private Integer tableIDValue; private Integer tableIDValue;
/** Grid Tab */
GridTab gridTab;
/** Grid Field */
GridField gridField;
/** Window Number */
int windowNo;
/** Tab Number */
int tabNo;
// UI components // UI components
private Div contentDiv; private Div contentDiv;
@ -97,7 +107,10 @@ public class WRecordIDDialog extends Window implements EventListener<Event>, Val
super(); super();
this.editor = editor; this.editor = editor;
gridTab = editor.getGridField().getGridTab();
gridField = editor.getGridField();
tabNo = gridTab != null ? gridTab.getTabNo() : FindWindow.TABNO;
windowNo = gridTab != null ? gridTab.getWindowNo() : gridField.getWindowNo();
if (editor.getAD_Table_ID() instanceof Integer) { if (editor.getAD_Table_ID() instanceof Integer) {
tableIDValue = (Integer) editor.getAD_Table_ID(); tableIDValue = (Integer) editor.getAD_Table_ID();
} else { } else {
@ -137,7 +150,7 @@ public class WRecordIDDialog extends Window implements EventListener<Event>, Val
tableIDEditor.setValue(tableIDValue); tableIDEditor.setValue(tableIDValue);
int tableID = tableIDValue != null ? tableIDValue.intValue() : 0; int tableID = tableIDValue != null ? tableIDValue.intValue() : 0;
MLookup recordsLookup = editor.getRecordsLookup(tableID); MLookup recordsLookup = MLookup.getRecordsLookup(tableID, tabNo, windowNo, editor.isUseUUIDKey());
if(recordsLookup != null) if(recordsLookup != null)
recordsEditor = new WSearchEditor(editor.getColumnName(), false, false, true, recordsLookup); recordsEditor = new WSearchEditor(editor.getColumnName(), false, false, true, recordsLookup);
@ -154,12 +167,12 @@ public class WRecordIDDialog extends Window implements EventListener<Event>, Val
parentTextBox = new Textbox(); parentTextBox = new Textbox();
parentTextBox.setReadonly(true); parentTextBox.setReadonly(true);
MTable parentTable = MTable.get(editor.getGridField().getGridTab().getAD_Table_ID()); MTable parentTable = MTable.get(editor.getGridField().getGridTab().getAD_Table_ID());
Object parentRecordId; Serializable parentRecordId;
if (parentTable.isUUIDKeyTable()) if (parentTable.isUUIDKeyTable())
parentRecordId = editor.getGridField().getGridTab().getValue(PO.getUUIDColumnName(parentTable.getTableName())); parentRecordId = (Serializable) editor.getGridField().getGridTab().getValue(PO.getUUIDColumnName(parentTable.getTableName()));
else else
parentRecordId = editor.getGridField().getGridTab().getRecord_ID(); parentRecordId = editor.getGridField().getGridTab().getRecord_ID();
parentTextBox.setValue(editor.getParentIdentifier(parentTable.getAD_Table_ID(), parentRecordId)); parentTextBox.setValue(MLookup.getIdentifier(parentTable.getAD_Table_ID(), parentRecordId, tabNo, windowNo));
} }
if (recordsEditor != null) if (recordsEditor != null)
@ -239,7 +252,7 @@ public class WRecordIDDialog extends Window implements EventListener<Event>, Val
recordsEditorLabel.detach(); recordsEditorLabel.detach();
recordsEditor.getComponent().detach(); recordsEditor.getComponent().detach();
} }
MLookup recordsLookup = editor.getRecordsLookup(tableID); MLookup recordsLookup = MLookup.getRecordsLookup(tableID, tabNo, windowNo, editor.isUseUUIDKey());
if(recordsLookup != null) { if(recordsLookup != null) {
recordsEditor = new WSearchEditor(editor.getColumnName(), false, false, true, recordsLookup); recordsEditor = new WSearchEditor(editor.getColumnName(), false, false, true, recordsLookup);
labelsDiv.appendChild(recordsEditorLabel); labelsDiv.appendChild(recordsEditorLabel);