From a7be63439a79b653dd3e125baa72afc60bba02b6 Mon Sep 17 00:00:00 2001 From: hieplq Date: Tue, 17 Apr 2018 14:50:34 +0200 Subject: [PATCH] IDEMPIERE-3512:csv import/export:resolve foreign should respect dynamic validate --- .../adempiere/impexp/GridTabCSVImporter.java | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) mode change 100644 => 100755 org.adempiere.base/src/org/adempiere/impexp/GridTabCSVImporter.java diff --git a/org.adempiere.base/src/org/adempiere/impexp/GridTabCSVImporter.java b/org.adempiere.base/src/org/adempiere/impexp/GridTabCSVImporter.java old mode 100644 new mode 100755 index 524d44bd43..79e6bf0e81 --- a/org.adempiere.base/src/org/adempiere/impexp/GridTabCSVImporter.java +++ b/org.adempiere.base/src/org/adempiere/impexp/GridTabCSVImporter.java @@ -43,11 +43,13 @@ import org.adempiere.base.IGridTabImporter; import org.adempiere.exceptions.AdempiereException; import org.adempiere.util.IProcessUI; import org.adempiere.util.ProcessUtil; +import org.apache.commons.lang.StringUtils; import org.compiere.model.GridField; import org.compiere.model.GridTab; import org.compiere.model.GridWindow; import org.compiere.model.GridWindowVO; import org.compiere.model.MColumn; +import org.compiere.model.MField; import org.compiere.model.MLocation; import org.compiere.model.MProcess; import org.compiere.model.MQuery; @@ -962,7 +964,7 @@ public class GridTabCSVImporter implements IGridTabImporter if("AD_Ref_List".equals(foreignTable)) idS= resolveForeignList(column,foreignColumn,value,null); else - id = resolveForeign(foreignTable,foreignColumn,value,null); + id = resolveForeign(foreignTable,foreignColumn,value,field,null); if(idS == null && id < 0){ //it could be that record still doesn't exist if import mode is inserting or merging @@ -1025,7 +1027,7 @@ public class GridTabCSVImporter implements IGridTabImporter if(isForeing && !"(null)".equals(value)){ String foreignTable = columnName.substring(0,columnName.length()-3); - int id = resolveForeign(foreignTable,foreignColumn,value,null); + int id = resolveForeign(foreignTable,foreignColumn,value,field,null); if (id < 0) return new StringBuilder(Msg.getMsg(Env.getCtx(), id==-2?"ForeignMultipleResolved":"ForeignNotResolved" ,new Object[]{header.get(j),value})); } @@ -1075,11 +1077,11 @@ public class GridTabCSVImporter implements IGridTabImporter address = new MLocation (Env.getCtx(),C_Location_ID,trx.getTrxName()); } } - + GridField field = gridTab.getField(columnName); if(!"(null)".equals(value.toString().trim())){ if(isForeing) { String foreignTable = columnName.substring(0,columnName.length()-3); - setValue = resolveForeign(foreignTable,foreignColumn,value,trx); + setValue = resolveForeign(foreignTable,foreignColumn,value,field,trx); if("C_City".equals(foreignTable)) address.setCity(value.toString()); }else @@ -1125,7 +1127,7 @@ public class GridTabCSVImporter implements IGridTabImporter if ("AD_Ref_List".equals(foreignTable)) idS= resolveForeignList(column, foreignColumn, value,trx); else - id = resolveForeign(foreignTable,foreignColumn,value,trx); + id = resolveForeign(foreignTable,foreignColumn,value, field, trx); if(idS == null && id < 0) return Msg.getMsg(Env.getCtx(),id==-2?"ForeignMultipleResolved":"ForeignNotResolved",new Object[]{header.get(i),value}); @@ -1169,7 +1171,7 @@ public class GridTabCSVImporter implements IGridTabImporter isThereRow =true; } else { - int id = resolveForeign(foreignTable, foreignColumn, value,trx); + int id = resolveForeign(foreignTable, foreignColumn, value,field,trx); if (id < 0) return Msg.getMsg(Env.getCtx(),id==-2?"ForeignMultipleResolved":"ForeignNotResolved",new Object[]{header.get(i),value}); @@ -1276,8 +1278,9 @@ public class GridTabCSVImporter implements IGridTabImporter if(isForeing) foreignColumn = columnName.substring(columnName.indexOf("[")+1,columnName.indexOf("]")); - columnName = getColumnName(false,isForeing,true,columnName); - MColumn column = MColumn.get(Env.getCtx(),gridTab.getField(columnName).getAD_Column_ID()); + columnName = getColumnName(false,isForeing,true,columnName); + GridField field = gridTab.getField(columnName); + MColumn column = MColumn.get(Env.getCtx(),field.getAD_Column_ID()); if (isForeing){ String foreignTable = column.getReferenceTableName(); if ("AD_Ref_List".equals(foreignTable)) { @@ -1287,7 +1290,7 @@ public class GridTabCSVImporter implements IGridTabImporter setValue = idS; } else { - int id = resolveForeign(foreignTable, foreignColumn, setValue,trx); + int id = resolveForeign(foreignTable, foreignColumn, setValue, field, trx); if (id < 0) return Msg.getMsg(Env.getCtx(),id==-2?"ForeignMultipleResolved":"ForeignNotResolved",new Object[]{columnName,setValue}); @@ -1371,7 +1374,7 @@ public class GridTabCSVImporter implements IGridTabImporter String idS = resolveForeignList(column, foreignColumn, tmpValue,trx); setValue = idS; }else { - int id = resolveForeign(foreignTable, foreignColumn, tmpValue,trx); + int id = resolveForeign(foreignTable, foreignColumn, tmpValue,field,trx); setValue = id; } }else{ @@ -1400,7 +1403,8 @@ public class GridTabCSVImporter implements IGridTabImporter if(columnName!=null){ boolean isForeing = columnName.indexOf("[") > 0 && columnName.indexOf("]")>0; columnwithKey = getColumnName(false,isForeing,true,columnName); - MColumn column = MColumn.get(Env.getCtx(),gridTab.getField(columnwithKey).getAD_Column_ID()); + GridField field = gridTab.getField(columnwithKey); + MColumn column = MColumn.get(Env.getCtx(), field.getAD_Column_ID()); String foreignColumn = null; if(isForeing){ foreignColumn = columnName.substring(columnName.indexOf("[")+1,columnName.indexOf("]")); @@ -1409,7 +1413,7 @@ public class GridTabCSVImporter implements IGridTabImporter String idS = resolveForeignList(column,foreignColumn,value,trx); value = idS; }else { - int id = resolveForeign(foreignTable,foreignColumn,value,trx); + int id = resolveForeign(foreignTable,foreignColumn,value,field,trx); value = id; } } @@ -1462,7 +1466,7 @@ public class GridTabCSVImporter implements IGridTabImporter return idS; } - private int resolveForeign(String foreignTable, String foreignColumn, Object value,Trx trx) { + private int resolveForeign(String foreignTable, String foreignColumn, Object value, GridField field, Trx trx) { boolean systemAccess = false; if (!"AD_Client".equals(foreignTable)) { MTable ft = MTable.get(Env.getCtx(), foreignTable); @@ -1480,7 +1484,14 @@ public class GridTabCSVImporter implements IGridTabImporter StringBuilder postSelect = new StringBuilder(" FROM ") .append(foreignTable).append(" WHERE ") .append(foreignColumn).append("=? AND IsActive='Y' AND AD_Client_ID=?"); - + if (StringUtils.isNotBlank(field.getVO().ValidationCode)) { + String dynamicValid = Env.parseContext(Env.getCtx(), field.getWindowNo(), field.getGridTab().getTabNo(), field.getVO().ValidationCode, false); + if (StringUtils.isBlank(dynamicValid)) { + return 0;// it's parse error but simple consider like ForeignNotResolved + }else { + postSelect.append(" AND ").append(dynamicValid); + } + } StringBuilder selectCount = new StringBuilder("SELECT COUNT(*)").append(postSelect); StringBuilder selectId = new StringBuilder("SELECT ").append(foreignTable).append("_ID").append(postSelect); int count = DB.getSQLValueEx(trxName, selectCount.toString(), value, thisClientId);