Skip to content

Commit

Permalink
feat: Overwrite display type on reference to Button fields. (#900)
Browse files Browse the repository at this point in the history
* feat: Overwrite display type on reference to Button fields.

* update descriptor.

* update kafka connector.
  • Loading branch information
EdwinBetanc0urt authored Oct 12, 2024
1 parent 2f86f0f commit 834e552
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 263 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ dependencies {
compileOnly 'org.apache.tomcat:annotations-api:6.0.53'

// ADempiere Core + Patches + Features
implementation "com.solop:adempiere.solop_libs:3.9.4.001-1.1.6"
implementation "com.solop:adempiere.solop_libs:3.9.4.001-1.1.7"
}

configurations.all {
Expand Down
Binary file modified resources/adempiere-grpc-server.pb
Binary file not shown.
75 changes: 15 additions & 60 deletions src/main/java/org/spin/base/db/QueryUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;

import org.adempiere.core.domains.models.I_AD_ChangeLog;
import org.adempiere.core.domains.models.X_AD_Reference;
import org.adempiere.model.MBrowse;
import org.adempiere.model.MBrowseField;
import org.adempiere.model.MView;
Expand Down Expand Up @@ -94,27 +92,11 @@ public static String getTableQueryWithReferences(MTable table, String tableAlias
// Reference Value
int referenceValueId = column.getAD_Reference_Value_ID();

if (DisplayType.Button == displayTypeId) {
// Reference Value
if (referenceValueId > 0) {
X_AD_Reference reference = new X_AD_Reference(column.getCtx(), referenceValueId, null);
if (reference != null && reference.getAD_Reference_ID() > 0) {
// overwrite display type to Table or List
if (X_AD_Reference.VALIDATIONTYPE_TableValidation.equals(reference.getValidationType())) {
displayTypeId = DisplayType.Table;
} else {
displayTypeId = DisplayType.List;
}
}
} else if (columnName.equals(I_AD_ChangeLog.COLUMNNAME_Record_ID)) {
// int tableId = Env.getContextAsInt(column.getCtx(), 0, I_AD_Table.COLUMNNAME_AD_Table_ID);
// MTable tableButton = MTable.get(column.getCtx(), tableId);
// String tableKeyColumn = tableButton.getTableName() + "_ID";
// columnName = tableKeyColumn;
// // overwrite display type to Table Direct
// displayTypeId = DisplayType.TableDir;
}
}
// overwrite display type `Button` to `List`, example `PaymentRule` or `Posted`
displayTypeId = ReferenceUtil.overwriteDisplayType(
displayTypeId,
referenceValueId
);

if (ReferenceUtil.validateReference(displayTypeId)) {
// Add display virutal column
Expand Down Expand Up @@ -221,27 +203,11 @@ public static String getTabQueryWithReferences(Properties context, MTab tab, Str
referenceValueId = column.getAD_Reference_Value_ID();
}

if (DisplayType.Button == displayTypeId) {
// Reference Value
if (referenceValueId > 0) {
X_AD_Reference reference = new X_AD_Reference(field.getCtx(), referenceValueId, null);
if (reference != null && reference.getAD_Reference_ID() > 0) {
// overwrite display type to Table or List
if (X_AD_Reference.VALIDATIONTYPE_TableValidation.equals(reference.getValidationType())) {
displayTypeId = DisplayType.Table;
} else {
displayTypeId = DisplayType.List;
}
}
} else if (columnName.equals(I_AD_ChangeLog.COLUMNNAME_Record_ID)) {
// int tableId = Env.getContextAsInt(field.getCtx(), 0, I_AD_Table.COLUMNNAME_AD_Table_ID);
// MTable tableButton = MTable.get(field.getCtx(), tableId);
// String tableKeyColumn = tableButton.getTableName() + "_ID";
// columnName = tableKeyColumn;
// // overwrite display type to Table Direct
// displayTypeId = DisplayType.TableDir;
}
}
// overwrite display type `Button` to `List`, example `PaymentRule` or `Posted`
displayTypeId = ReferenceUtil.overwriteDisplayType(
displayTypeId,
referenceValueId
);

if (ReferenceUtil.validateReference(displayTypeId)) {
// Add display virutal column
Expand Down Expand Up @@ -364,25 +330,14 @@ public static String getBrowserQueryWithReferences(MBrowse browser) {
continue;
}

int displayTypeId = browseField.getAD_Reference_ID();

// Reference Value
int referenceValueId = browseField.getAD_Reference_Value_ID();

if (DisplayType.Button == displayTypeId) {
// Reference Value
if (referenceValueId > 0) {
X_AD_Reference reference = new X_AD_Reference(browseField.getCtx(), referenceValueId, null);
if (reference != null && reference.getAD_Reference_ID() > 0) {
// overwrite display type to Table or List
if (X_AD_Reference.VALIDATIONTYPE_TableValidation.equals(reference.getValidationType())) {
displayTypeId = DisplayType.Table;
} else {
displayTypeId = DisplayType.List;
}
}
}
}
// overwrite display type `Button` to `List`, example `PaymentRule` or `Posted`
int displayTypeId = ReferenceUtil.overwriteDisplayType(
browseField.getAD_Reference_ID(),
referenceValueId
);

if (ReferenceUtil.validateReference(displayTypeId)) {
MViewColumn viewColumn = MViewColumn.getById(browseField.getCtx(), browseField.getAD_View_Column_ID(), null);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/spin/base/util/ReferenceInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.model.MBrowseField;
import org.adempiere.model.MViewColumn;
import org.adempiere.core.domains.models.I_C_Order;
import org.adempiere.core.domains.models.X_AD_Reference;
import org.compiere.model.MColumn;
import org.compiere.model.MField;
Expand Down Expand Up @@ -421,10 +420,11 @@ static public MLookupInfo getInfoFromRequest(
validationRuleId = 0;
}

// overwrite `Button` to `List` display type on `PaymentRule`.
if (displayTypeId == DisplayType.Button && I_C_Order.COLUMNNAME_PaymentRule.equals(columnName)) {
displayTypeId = DisplayType.List;
}
// overwrite display type `Button` to `List`, example `PaymentRule` or `Posted`
displayTypeId = ReferenceUtil.overwriteDisplayType(
displayTypeId,
referenceValueId
);

return ReferenceUtil.getReferenceLookupInfo(
displayTypeId,
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/spin/base/util/ReferenceUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,29 @@ public static boolean validateReference(int displayTypeId) {
return false;
}

public static int overwriteDisplayType(int displayTypeId, int referenceValueId) {
int newDisplayType = displayTypeId;
if (DisplayType.Button == displayTypeId) {
// Reference Value
if (referenceValueId > 0) {
X_AD_Reference reference = new X_AD_Reference(
Env.getCtx(),
referenceValueId,
null
);
if (reference != null && reference.getAD_Reference_ID() > 0) {
// overwrite display type to Table or List
if (X_AD_Reference.VALIDATIONTYPE_TableValidation.equals(reference.getValidationType())) {
newDisplayType = DisplayType.Table;
} else {
newDisplayType = DisplayType.List;
}
}
}
}
return newDisplayType;
}

/**
* Get Reference information, can return null if reference is invalid or not exists
* @param referenceId
Expand Down Expand Up @@ -481,6 +504,8 @@ public static MLookupInfo getReferenceLookupInfo(int referenceId, int referenceV
// set new query
lookupInformation.Query = queryForLookup;
lookupInformation.QueryDirect = directQuery;
lookupInformation.DisplayType = referenceId;
lookupInformation.AD_Reference_Value_ID = referenceValueId;

MValRule validationRule = null;
// For validation rule
Expand Down
110 changes: 19 additions & 91 deletions src/main/java/org/spin/grpc/service/BusinessData.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand All @@ -36,7 +33,6 @@
import org.adempiere.model.MBrowse;
import org.adempiere.model.MBrowseField;
import org.adempiere.model.MViewDefinition;
import org.compiere.model.MColumn;
import org.compiere.model.MMenu;
import org.compiere.model.MPInstance;
import org.compiere.model.MProcess;
Expand All @@ -47,7 +43,6 @@
import org.compiere.process.DocAction;
import org.compiere.process.ProcessInfo;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import org.compiere.util.Trx;
Expand Down Expand Up @@ -77,12 +72,10 @@
import org.spin.service.grpc.authentication.SessionManager;
// import org.spin.service.grpc.util.db.CountUtil;
import org.spin.service.grpc.util.db.LimitUtil;
import org.spin.service.grpc.util.db.ParameterUtil;
import org.spin.service.grpc.util.query.SortingManager;
import org.spin.service.grpc.util.value.ValueManager;

import com.google.protobuf.Empty;
import com.google.protobuf.Struct;
import com.google.protobuf.Value;

import io.grpc.Status;
Expand Down Expand Up @@ -705,17 +698,28 @@ private ListEntitiesResponse.Builder listEntities(Properties context, ListEntiti
// parsedSQL = LimitUtil.getQueryWithLimit(parsedSQL, limit, offset);
// // Add Order By
// parsedSQL = parsedSQL + orderByClause;
// builder = convertListEntitiesResult(MTable.get(context, criteria.getTableName()), parsedSQL, params);
// builder = RecordUtil.convertListEntitiesResult(MTable.get(context, criteria.getTableName()), parsedSQL, params);
// }
Query query = new Query(context, request.getTableName(), whereClause.toString(), null)
.setParameters(params);
Query query = new Query(
context,
request.getTableName(),
whereClause.toString(),
null
)
.setParameters(params)
;
count = query.count();
if(!Util.isEmpty(request.getSortBy())) {
query.setOrderBy(SortingManager.newInstance(request.getSortBy()).getSotingAsSQL());
if(!Util.isEmpty(request.getSortBy(), true)) {
query.setOrderBy(
SortingManager.newInstance(
request.getSortBy()
).getSotingAsSQL()
);
}
List<PO> entityList = query
.setLimit(limit, offset)
.<PO>list();
.setLimit(limit, offset)
.<PO>list()
;
//
for(PO entity : entityList) {
Entity.Builder valueObject = ConvertUtil.convertEntity(entity);
Expand All @@ -732,81 +736,5 @@ private ListEntitiesResponse.Builder listEntities(Properties context, ListEntiti
// Return
return builder;
}

/**
* Convert Entities List
* @param table
* @param sql
* @return
*/
private ListEntitiesResponse.Builder convertListEntitiesResult(MTable table, String sql, List<Object> params) {
PreparedStatement pstmt = null;
ResultSet rs = null;
ListEntitiesResponse.Builder builder = ListEntitiesResponse.newBuilder();
long recordCount = 0;
try {
LinkedHashMap<String, MColumn> columnsMap = new LinkedHashMap<>();
// Add field to map
for(MColumn column: table.getColumnsAsList()) {
columnsMap.put(column.getColumnName().toUpperCase(), column);
}
// SELECT Key, Value, Name FROM ...
pstmt = DB.prepareStatement(sql, null);
ParameterUtil.setParametersFromObjectsList(pstmt, params);

// Get from Query
rs = pstmt.executeQuery();
while(rs.next()) {
Entity.Builder valueObjectBuilder = Entity.newBuilder();
Struct.Builder rowValues = Struct.newBuilder();
ResultSetMetaData metaData = rs.getMetaData();
for (int index = 1; index <= metaData.getColumnCount(); index++) {
try {
String columnName = metaData.getColumnName (index);
MColumn field = columnsMap.get(columnName.toUpperCase());
// Display Columns
if(field == null) {
String displayValue = rs.getString(index);
Value.Builder displayValueBuilder = ValueManager.getValueFromString(displayValue);

rowValues.putFields(
columnName,
displayValueBuilder.build()
);
continue;
}
// From field
String fieldColumnName = field.getColumnName();
Object value = rs.getObject(index);
Value.Builder valueBuilder = ValueManager.getValueFromReference(
value,
field.getAD_Reference_ID()
);
if(!valueBuilder.getNullValue().equals(com.google.protobuf.NullValue.NULL_VALUE)) {
rowValues.putFields(
fieldColumnName,
valueBuilder.build()
);
}
} catch (Exception e) {
log.severe(e.getLocalizedMessage());
e.printStackTrace();
}
}
//
valueObjectBuilder.setValues(rowValues);
builder.addRecords(valueObjectBuilder.build());
recordCount++;
}
} catch (Exception e) {
log.severe(e.getLocalizedMessage());
e.printStackTrace();
} finally {
DB.close(rs, pstmt);
}
// Set record counts
builder.setRecordCount(recordCount);
// Return
return builder;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,11 @@ public static Field.Builder convertBrowseField(Properties context, MBrowseField
ValueManager.validateNull(elementName))
;

//
int displayTypeId = browseField.getAD_Reference_ID();
// overwrite display type `Button` to `List`, example `PaymentRule` or `Posted`
int displayTypeId = ReferenceUtil.overwriteDisplayType(
browseField.getAD_Reference_ID(),
browseField.getAD_Reference_Value_ID()
);
if (ReferenceUtil.validateReference(displayTypeId)) {
// Reference Value
int referenceValueId = browseField.getAD_Reference_Value_ID();
Expand Down
Loading

0 comments on commit 834e552

Please sign in to comment.