IDEMPIERE-3146 CSV Import issues with resolveForeign when key is duplicated on foreign table
This commit is contained in:
parent
ab552fb5a1
commit
666fbe011e
|
@ -0,0 +1,11 @@
|
||||||
|
SET SQLBLANKLINES ON
|
||||||
|
SET DEFINE OFF
|
||||||
|
|
||||||
|
-- I forgot to set the DICTIONARY_ID_COMMENTS System Configurator
|
||||||
|
-- Aug 3, 2016 12:25:05 PM CEST
|
||||||
|
INSERT INTO AD_Message (MsgType,MsgText,AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,AD_Message_ID,Value,EntityType,AD_Message_UU) VALUES ('E','{0} resolved multiple times = ({1})',0,0,'Y',TO_DATE('2016-08-03 12:25:05','YYYY-MM-DD HH24:MI:SS'),100,TO_DATE('2016-08-03 12:25:05','YYYY-MM-DD HH24:MI:SS'),100,200405,'ForeignMultipleResolved','D','3c350f49-c813-4ebc-ae1a-5d4b2fc76faa')
|
||||||
|
;
|
||||||
|
|
||||||
|
SELECT register_migration_script('201608031225_IDEMPIERE-3146.sql') FROM dual
|
||||||
|
;
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
-- I forgot to set the DICTIONARY_ID_COMMENTS System Configurator
|
||||||
|
-- Aug 3, 2016 12:25:05 PM CEST
|
||||||
|
INSERT INTO AD_Message (MsgType,MsgText,AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,AD_Message_ID,Value,EntityType,AD_Message_UU) VALUES ('E','{0} resolved multiple times = ({1})',0,0,'Y',TO_TIMESTAMP('2016-08-03 12:25:05','YYYY-MM-DD HH24:MI:SS'),100,TO_TIMESTAMP('2016-08-03 12:25:05','YYYY-MM-DD HH24:MI:SS'),100,200405,'ForeignMultipleResolved','D','3c350f49-c813-4ebc-ae1a-5d4b2fc76faa')
|
||||||
|
;
|
||||||
|
|
||||||
|
SELECT register_migration_script('201608031225_IDEMPIERE-3146.sql') FROM dual
|
||||||
|
;
|
||||||
|
|
|
@ -958,7 +958,7 @@ public class GridTabCSVImporter implements IGridTabImporter
|
||||||
if(idS == null && id < 0){
|
if(idS == null && id < 0){
|
||||||
//it could be that record still doesn't exist if import mode is inserting or merging
|
//it could be that record still doesn't exist if import mode is inserting or merging
|
||||||
if(isUpdateMode())
|
if(isUpdateMode())
|
||||||
return new StringBuilder(Msg.getMsg(Env.getCtx(),"ForeignNotResolved",new Object[]{header.get(i),value}));
|
return new StringBuilder(Msg.getMsg(Env.getCtx(),id==-2?"ForeignMultipleResolved":"ForeignNotResolved",new Object[]{header.get(i),value}));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// no validation here
|
// no validation here
|
||||||
|
@ -1016,8 +1016,9 @@ public class GridTabCSVImporter implements IGridTabImporter
|
||||||
|
|
||||||
if(isForeing && !"(null)".equals(value)){
|
if(isForeing && !"(null)".equals(value)){
|
||||||
String foreignTable = columnName.substring(0,columnName.length()-3);
|
String foreignTable = columnName.substring(0,columnName.length()-3);
|
||||||
if(resolveForeign(foreignTable,foreignColumn,value,null) < 0)
|
int id = resolveForeign(foreignTable,foreignColumn,value,null);
|
||||||
return new StringBuilder(Msg.getMsg(Env.getCtx(), "ForeignNotResolved" ,new Object[]{header.get(j),value}));
|
if (id < 0)
|
||||||
|
return new StringBuilder(Msg.getMsg(Env.getCtx(), id==-2?"ForeignMultipleResolved":"ForeignNotResolved" ,new Object[]{header.get(j),value}));
|
||||||
}
|
}
|
||||||
isEmptyRow=false;
|
isEmptyRow=false;
|
||||||
}
|
}
|
||||||
|
@ -1118,7 +1119,7 @@ public class GridTabCSVImporter implements IGridTabImporter
|
||||||
id = resolveForeign(foreignTable,foreignColumn,value,trx);
|
id = resolveForeign(foreignTable,foreignColumn,value,trx);
|
||||||
|
|
||||||
if(idS == null && id < 0)
|
if(idS == null && id < 0)
|
||||||
return Msg.getMsg(Env.getCtx(),"ForeignNotResolved",new Object[]{header.get(i),value});
|
return Msg.getMsg(Env.getCtx(),id==-2?"ForeignMultipleResolved":"ForeignNotResolved",new Object[]{header.get(i),value});
|
||||||
|
|
||||||
if(id >= 0)
|
if(id >= 0)
|
||||||
logMsg = gridTab.setValue(field,id);
|
logMsg = gridTab.setValue(field,id);
|
||||||
|
@ -1161,7 +1162,7 @@ public class GridTabCSVImporter implements IGridTabImporter
|
||||||
|
|
||||||
int id = resolveForeign(foreignTable, foreignColumn, value,trx);
|
int id = resolveForeign(foreignTable, foreignColumn, value,trx);
|
||||||
if (id < 0)
|
if (id < 0)
|
||||||
return Msg.getMsg(Env.getCtx(),"ForeignNotResolved",new Object[]{header.get(i),value});
|
return Msg.getMsg(Env.getCtx(),id==-2?"ForeignMultipleResolved":"ForeignNotResolved",new Object[]{header.get(i),value});
|
||||||
|
|
||||||
setValue = id;
|
setValue = id;
|
||||||
if (field.isParentValue()) {
|
if (field.isParentValue()) {
|
||||||
|
@ -1279,7 +1280,7 @@ public class GridTabCSVImporter implements IGridTabImporter
|
||||||
} else {
|
} else {
|
||||||
int id = resolveForeign(foreignTable, foreignColumn, setValue,trx);
|
int id = resolveForeign(foreignTable, foreignColumn, setValue,trx);
|
||||||
if (id < 0)
|
if (id < 0)
|
||||||
return Msg.getMsg(Env.getCtx(),"ForeignNotResolved",new Object[]{columnName,setValue});
|
return Msg.getMsg(Env.getCtx(),id==-2?"ForeignMultipleResolved":"ForeignNotResolved",new Object[]{columnName,setValue});
|
||||||
|
|
||||||
setValue = id;
|
setValue = id;
|
||||||
}
|
}
|
||||||
|
@ -1452,22 +1453,40 @@ public class GridTabCSVImporter implements IGridTabImporter
|
||||||
|
|
||||||
private int resolveForeign(String foreignTable, String foreignColumn, Object value,Trx trx) {
|
private int resolveForeign(String foreignTable, String foreignColumn, Object value,Trx trx) {
|
||||||
int id = -1;
|
int id = -1;
|
||||||
String trxName = (trx!=null?trx.getTrxName():null);
|
|
||||||
StringBuilder select = new StringBuilder("SELECT ")
|
boolean systemAccess = false;
|
||||||
.append(foreignTable).append("_ID FROM ")
|
if (!"AD_Client".equals(foreignTable)) {
|
||||||
.append(foreignTable).append(" WHERE ")
|
|
||||||
.append(foreignColumn).append("=? AND IsActive='Y' AND AD_Client_ID=?");
|
|
||||||
id = DB.getSQLValueEx(trxName, select.toString(), value, Env.getAD_Client_ID(Env.getCtx()));
|
|
||||||
if (id == -1 && !"AD_Client".equals(foreignTable)) {
|
|
||||||
MTable ft = MTable.get(Env.getCtx(), foreignTable);
|
MTable ft = MTable.get(Env.getCtx(), foreignTable);
|
||||||
String accessLevel = ft.getAccessLevel();
|
String accessLevel = ft.getAccessLevel();
|
||||||
if ( MTable.ACCESSLEVEL_All.equals(accessLevel)
|
if ( MTable.ACCESSLEVEL_All.equals(accessLevel)
|
||||||
|| MTable.ACCESSLEVEL_SystemOnly.equals(accessLevel)
|
|| MTable.ACCESSLEVEL_SystemOnly.equals(accessLevel)
|
||||||
|| MTable.ACCESSLEVEL_SystemPlusClient.equals(accessLevel)) {
|
|| MTable.ACCESSLEVEL_SystemPlusClient.equals(accessLevel)) {
|
||||||
// try System client if the table has System access
|
systemAccess = true;
|
||||||
id = DB.getSQLValueEx(trxName, select.toString(), value, 0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String trxName = (trx!=null?trx.getTrxName():null);
|
||||||
|
|
||||||
|
StringBuilder postSelect = new StringBuilder(" FROM ")
|
||||||
|
.append(foreignTable).append(" WHERE ")
|
||||||
|
.append(foreignColumn).append("=? AND IsActive='Y' AND AD_Client_ID");
|
||||||
|
if (systemAccess) {
|
||||||
|
postSelect.append(" IN (0,?)");
|
||||||
|
} else {
|
||||||
|
postSelect.append("=?");
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder selectCount = new StringBuilder("SELECT COUNT(*)").append(postSelect);
|
||||||
|
int count = DB.getSQLValueEx(trxName, selectCount.toString(), value, Env.getAD_Client_ID(Env.getCtx()));
|
||||||
|
if (count == 1) { // single value found, OK
|
||||||
|
StringBuilder selectId = new StringBuilder("SELECT ").append(foreignTable).append("_ID").append(postSelect);
|
||||||
|
id = DB.getSQLValueEx(trxName, selectId.toString(), value, Env.getAD_Client_ID(Env.getCtx()));
|
||||||
|
} else if (count > 1) { // multiple values found, error ForeignMultipleResolved
|
||||||
|
id = -2;
|
||||||
|
} else if (count == 0) { // no values found, error ForeignNotResolved
|
||||||
|
id = -3;
|
||||||
|
}
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue