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.KeyNamePair;
import org.compiere.util.NamePair;
import org.compiere.util.Util;
import org.compiere.util.ValueNamePair;
/**
@ -995,6 +996,87 @@ public final class MLookup extends Lookup implements Serializable
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
*/

View File

@ -25,6 +25,8 @@
**********************************************************************/
package org.adempiere.webui.editor;
import java.io.Serializable;
import org.adempiere.util.GridRowCtx;
import org.adempiere.webui.ValuePreference;
import org.adempiere.webui.component.Textbox;
@ -39,11 +41,7 @@ import org.adempiere.webui.window.WFieldRecordInfo;
import org.adempiere.webui.window.WRecordIDDialog;
import org.compiere.model.GridField;
import org.compiere.model.Lookup;
import org.compiere.model.MColumn;
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.Env;
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 */
protected GridField tableIDGridField;
/** Window Number */
int windowNo;
/** Tab Number */
int tabNo;
// UI components
protected Textbox recordTextBox;
@ -90,7 +92,8 @@ public abstract class WRecordEditor<T> extends WEditor implements ContextMenuLis
*/
public WRecordEditor(GridField gridField, boolean tableEditor, IEditorConfiguration 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().addEventListener(Events.ON_RIGHT_CLICK, this);
@ -261,9 +264,10 @@ public abstract class WRecordEditor<T> extends WEditor implements ContextMenuLis
if(value != null && tableIDValue != null) {
int tableID = Integer.parseInt(String.valueOf(tableIDValue));
if (tableID > 0) {
Object recordID = toKeyValue(value);
if(recordID != null && tableID > 0)
recordTextBox.setValue(getIdentifier(tableID, recordID));
Serializable recordID = (Serializable) toKeyValue(value);
if(recordID != null && tableID > 0) {
recordTextBox.setValue(MLookup.getIdentifier(tableID, recordID, tabNo, windowNo, isUseUUIDKey()));
}
}
}
}
@ -292,14 +296,14 @@ public abstract class WRecordEditor<T> extends WEditor implements ContextMenuLis
return "";
if((tableIDValue != null) && (recordIDValue != null)) {
int tableID;
Object recordID;
Serializable recordID;
try {
tableID = Integer.parseInt(String.valueOf(tableIDValue));
recordID = toKeyValue(recordIDValue);
recordID = (Serializable) toKeyValue(recordIDValue);
} catch (NumberFormatException e) {
return recordIDValue.toString();
}
return getIdentifier(tableID, recordID);
return MLookup.getIdentifier(tableID, recordID, tabNo, windowNo, isUseUUIDKey());
}
else {
return recordIDValue.toString();
@ -314,14 +318,14 @@ public abstract class WRecordEditor<T> extends WEditor implements ContextMenuLis
String key = gridTab.getWindowNo() + "|AD_Table_ID";
Object rowTableIdValue = gridRowCtx.get(key);
int rowTableID;
Object rowRecordID;
Serializable rowRecordID;
try {
rowTableID = Integer.parseInt(String.valueOf(rowTableIdValue));
rowRecordID = toKeyValue(value);
rowRecordID = (Serializable) toKeyValue(value);
} catch (NumberFormatException e) {
return value.toString();
}
return getIdentifier(rowTableID, rowRecordID);
return MLookup.getIdentifier(rowTableID, rowRecordID, tabNo, windowNo, isUseUUIDKey());
} else {
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
* @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
*/
protected abstract String getKeyColumn(MTable mTable);
public abstract boolean isUseUUIDKey();
/**
* Convert value to key value type

View File

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

View File

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

View File

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