IDEMPIERE-454 Easy import - V5 -> Add transaction management per master key for the importer

This commit is contained in:
Juan David Arboleda 2013-04-19 18:42:23 -05:00
parent 08f4e0dce9
commit 2292bc09b0
4 changed files with 195 additions and 95 deletions

View File

@ -14,6 +14,8 @@
*****************************************************************************/ *****************************************************************************/
package org.adempiere.impexp; package org.adempiere.impexp;
import static org.compiere.model.SystemIDs.REFERENCE_PAYMENTRULE;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
@ -93,23 +95,23 @@ public class GridTabCSVExporter implements IGridTabExporter
GridField field = gridFields[idxfld]; GridField field = gridFields[idxfld];
MColumn column = MColumn.get(Env.getCtx(), field.getAD_Column_ID()); MColumn column = MColumn.get(Env.getCtx(), field.getAD_Column_ID());
//Special Columns //Special Columns
if(DisplayType.Location == column.getAD_Reference_ID()){ if(DisplayType.Location == field.getDisplayType()){
specialHDispayType = DisplayType.Location; specialHDispayType = DisplayType.Location;
continue; continue;
} }
String headName = resolveColumnName(table, column); String headName = resolveColumnName(table, column);
headArray.add(headName); headArray.add(headName);
if (DisplayType.Date == column.getAD_Reference_ID()) { if (DisplayType.Date == field.getDisplayType()) {
procArray.add(new Optional(new FmtDate(DisplayType.DEFAULT_DATE_FORMAT))); procArray.add(new Optional(new FmtDate(DisplayType.DEFAULT_DATE_FORMAT)));
} else if (DisplayType.DateTime == column.getAD_Reference_ID()) { } else if (DisplayType.DateTime == field.getDisplayType()) {
procArray.add(new Optional(new FmtDate(DisplayType.DEFAULT_TIMESTAMP_FORMAT))); procArray.add(new Optional(new FmtDate(DisplayType.DEFAULT_TIMESTAMP_FORMAT)));
} else if (DisplayType.Time == column.getAD_Reference_ID()) { } else if (DisplayType.Time == field.getDisplayType()) {
procArray.add(new Optional(new FmtDate("DisplayType.DEFAULT_TIME_FORMAT"))); procArray.add(new Optional(new FmtDate("DisplayType.DEFAULT_TIME_FORMAT")));
} else if (DisplayType.Integer == column.getAD_Reference_ID() || DisplayType.isNumeric(column.getAD_Reference_ID())) { } else if (DisplayType.Integer == field.getDisplayType() || DisplayType.isNumeric(field.getDisplayType())) {
DecimalFormat nf = DisplayType.getNumberFormat(column.getAD_Reference_ID()); DecimalFormat nf = DisplayType.getNumberFormat(field.getDisplayType());
nf.setGroupingUsed(false); nf.setGroupingUsed(false);
procArray.add(new Optional(new FmtNumber(nf))); procArray.add(new Optional(new FmtNumber(nf)));
} else if (DisplayType.YesNo == column.getAD_Reference_ID()) { } else if (DisplayType.YesNo == field.getDisplayType()) {
procArray.add(new Optional(new FmtBool("Y", "N"))); procArray.add(new Optional(new FmtBool("Y", "N")));
} else { // lookups } else { // lookups
procArray.add(new Optional()); procArray.add(new Optional());
@ -153,23 +155,23 @@ public class GridTabCSVExporter implements IGridTabExporter
gridFields = getFields(detail); gridFields = getFields(detail);
for(GridField field : gridFields){ for(GridField field : gridFields){
MColumn columnDetail = MColumn.get(Env.getCtx(), field.getAD_Column_ID()); MColumn columnDetail = MColumn.get(Env.getCtx(), field.getAD_Column_ID());
if(DisplayType.Location == columnDetail.getAD_Reference_ID()){ if(DisplayType.Location == field.getDisplayType()){
specialDetDispayType = DisplayType.Location; specialDetDispayType = DisplayType.Location;
continue; continue;
} }
String headNameDetail= detail.getTableName()+">"+resolveColumnName(tableDetail, columnDetail); String headNameDetail= detail.getTableName()+">"+resolveColumnName(tableDetail, columnDetail);
headArray.add(headNameDetail); headArray.add(headNameDetail);
if (DisplayType.Date == columnDetail.getAD_Reference_ID()) { if (DisplayType.Date == field.getDisplayType()) {
procArray.add(new Optional(new FmtDate(DisplayType.DEFAULT_DATE_FORMAT))); procArray.add(new Optional(new FmtDate(DisplayType.DEFAULT_DATE_FORMAT)));
} else if (DisplayType.DateTime == columnDetail.getAD_Reference_ID()) { } else if (DisplayType.DateTime == field.getDisplayType()) {
procArray.add(new Optional(new FmtDate(DisplayType.DEFAULT_TIMESTAMP_FORMAT))); procArray.add(new Optional(new FmtDate(DisplayType.DEFAULT_TIMESTAMP_FORMAT)));
} else if (DisplayType.Time == columnDetail.getAD_Reference_ID()) { } else if (DisplayType.Time == field.getDisplayType()) {
procArray.add(new Optional(new FmtDate("DisplayType.DEFAULT_TIME_FORMAT"))); procArray.add(new Optional(new FmtDate("DisplayType.DEFAULT_TIME_FORMAT")));
} else if (DisplayType.Integer == columnDetail.getAD_Reference_ID() || DisplayType.isNumeric(columnDetail.getAD_Reference_ID())) { } else if (DisplayType.Integer == field.getDisplayType() || DisplayType.isNumeric(field.getDisplayType())) {
DecimalFormat nf = DisplayType.getNumberFormat(columnDetail.getAD_Reference_ID()); DecimalFormat nf = DisplayType.getNumberFormat(field.getDisplayType());
nf.setGroupingUsed(false); nf.setGroupingUsed(false);
procArray.add(new Optional(new FmtNumber(nf))); procArray.add(new Optional(new FmtNumber(nf)));
} else if (DisplayType.YesNo == columnDetail.getAD_Reference_ID()) { } else if (DisplayType.YesNo == field.getDisplayType()) {
procArray.add(new Optional(new FmtBool("Y", "N"))); procArray.add(new Optional(new FmtBool("Y", "N")));
} else { // lookups and text } else { // lookups and text
procArray.add(new Optional()); procArray.add(new Optional());
@ -212,16 +214,19 @@ public class GridTabCSVExporter implements IGridTabExporter
for(GridField field : getFields(gridTab)){ for(GridField field : getFields(gridTab)){
MColumn column = MColumn.get(Env.getCtx(), field.getAD_Column_ID()); MColumn column = MColumn.get(Env.getCtx(), field.getAD_Column_ID());
Object value = null;
if(DisplayType.Location == column.getAD_Reference_ID()){ String headName = header[idxfld];
if(DisplayType.Location == field.getDisplayType()){
Object fResolved =resolveValue(gridTab, table, column, idxrow, column.getColumnName()); Object fResolved =resolveValue(gridTab, table, column, idxrow, column.getColumnName());
if (fResolved!=null) if (fResolved!=null)
record_Id= Integer.parseInt(fResolved.toString()); record_Id= Integer.parseInt(fResolved.toString());
continue; continue;
}else if (DisplayType.Payment == field.getDisplayType()){
value = MRefList.getListName(Env.getCtx(),REFERENCE_PAYMENTRULE, gridTab.getValue(idxrow, header[idxfld]).toString());
}else{
value = resolveValue(gridTab, table, column, idxrow, headName);
} }
String headName = header[idxfld];
Object value = resolveValue(gridTab, table, column, idxrow, headName);
//Ignore row //Ignore row
if("IsActive".equals(headName) && value!=null && Boolean.valueOf((Boolean)value)==false){ if("IsActive".equals(headName) && value!=null && Boolean.valueOf((Boolean)value)==false){
isActiveRow=false; isActiveRow=false;
@ -342,6 +347,10 @@ public class GridTabCSVExporter implements IGridTabExporter
MTable tableDetail = MTable.get(Env.getCtx(), childTab.getTableName()); MTable tableDetail = MTable.get(Env.getCtx(), childTab.getTableName());
String headName = headArray.get(headArray.indexOf(childTab.getTableName()+">"+resolveColumnName(tableDetail,column))); String headName = headArray.get(headArray.indexOf(childTab.getTableName()+">"+resolveColumnName(tableDetail,column)));
value = resolveValue(childTab, MTable.get(Env.getCtx(),childTab.getTableName()), column, currentDetRow, headName.substring(headName.indexOf(">")+ 1,headName.length())); value = resolveValue(childTab, MTable.get(Env.getCtx(),childTab.getTableName()), column, currentDetRow, headName.substring(headName.indexOf(">")+ 1,headName.length()));
if(DisplayType.Payment == field.getDisplayType())
value = MRefList.getListName(Env.getCtx(),REFERENCE_PAYMENTRULE, value.toString());
row.put(headName,value); row.put(headName,value);
if(value!=null) if(value!=null)
hasDetails = true; hasDetails = true;

View File

@ -14,6 +14,8 @@
*****************************************************************************/ *****************************************************************************/
package org.adempiere.impexp; package org.adempiere.impexp;
import static org.compiere.model.SystemIDs.REFERENCE_PAYMENTRULE;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -43,6 +45,7 @@ import org.compiere.model.GridWindowVO;
import org.compiere.model.MColumn; import org.compiere.model.MColumn;
import org.compiere.model.MLocation; import org.compiere.model.MLocation;
import org.compiere.model.MQuery; import org.compiere.model.MQuery;
import org.compiere.model.MRefList;
import org.compiere.model.MTable; import org.compiere.model.MTable;
import org.compiere.model.PO; import org.compiere.model.PO;
import org.compiere.tools.FileUtil; import org.compiere.tools.FileUtil;
@ -113,6 +116,10 @@ public class GridTabCSVImporter implements IGridTabImporter
//Mapping header //Mapping header
for(int idx = 0; idx < header.size(); idx++) { for(int idx = 0; idx < header.size(); idx++) {
String headName = header.get(idx); String headName = header.get(idx);
if (headName==null)
throw new AdempiereException("Header column cannot be empty, Col: " + (idx + 1));
if (headName.equals(ERROR_HEADER) || headName.equals(LOG_HEADER)){ if (headName.equals(ERROR_HEADER) || headName.equals(LOG_HEADER)){
header.set(idx, null); header.set(idx, null);
readProcArray.add(null); readProcArray.add(null);
@ -124,7 +131,7 @@ public class GridTabCSVImporter implements IGridTabImporter
}else if (headName.contains(MTable.getTableName(Env.getCtx(), MLocation.Table_ID)) && locationFields==null){ }else if (headName.contains(MTable.getTableName(Env.getCtx(), MLocation.Table_ID)) && locationFields==null){
locationFields = getSpecialMColumn(header,MTable.getTableName(Env.getCtx(), MLocation.Table_ID),idx); locationFields = getSpecialMColumn(header,MTable.getTableName(Env.getCtx(), MLocation.Table_ID),idx);
for(GridField sField:locationFields){ for(GridField sField:locationFields){
readProcArray.add(getProccesorFromColumn(MColumn.get(Env.getCtx(),sField.getAD_Column_ID()))); readProcArray.add(getProccesorFromColumn(sField));
indxDetail++; indxDetail++;
} }
idx=indxDetail; idx=indxDetail;
@ -142,7 +149,7 @@ public class GridTabCSVImporter implements IGridTabImporter
else if(isKeyColumn && !isThereKey) else if(isKeyColumn && !isThereKey)
isThereKey =true; isThereKey =true;
readProcArray.add(getProccesorFromColumn(MColumn.get(Env.getCtx(),field.getAD_Column_ID()))); readProcArray.add(getProccesorFromColumn(field));
indxDetail++; indxDetail++;
} }
} }
@ -188,7 +195,7 @@ public class GridTabCSVImporter implements IGridTabImporter
if (columnName.contains(MTable.getTableName(Env.getCtx(), MLocation.Table_ID)) && locationFields==null){ if (columnName.contains(MTable.getTableName(Env.getCtx(), MLocation.Table_ID)) && locationFields==null){
locationFields = getSpecialMColumn(header,MTable.getTableName(Env.getCtx(), MLocation.Table_ID),idx); locationFields = getSpecialMColumn(header,MTable.getTableName(Env.getCtx(), MLocation.Table_ID),idx);
for(GridField sField:locationFields){ for(GridField sField:locationFields){
readProcArray.add(getProccesorFromColumn(MColumn.get(Env.getCtx(),sField.getAD_Column_ID()))); readProcArray.add(getProccesorFromColumn(sField));
idx++; idx++;
} }
idx--; idx--;
@ -203,7 +210,7 @@ public class GridTabCSVImporter implements IGridTabImporter
else if(isKeyColumn && !isThereKey) else if(isKeyColumn && !isThereKey)
isThereKey =true; isThereKey =true;
readProcArray.add(getProccesorFromColumn(MColumn.get(Env.getCtx(),field.getAD_Column_ID()))); readProcArray.add(getProccesorFromColumn(field));
} }
}else }else
throw new AdempiereException(Msg.getMsg(Env.getCtx(),"WrongDetailName",new Object[] {" col("+idx+") ",detailName})); throw new AdempiereException(Msg.getMsg(Env.getCtx(),"WrongDetailName",new Object[] {" col("+idx+") ",detailName}));
@ -294,14 +301,16 @@ public class GridTabCSVImporter implements IGridTabImporter
logFileW.write(rawHeader + delimiter + LOG_HEADER + "\n"); logFileW.write(rawHeader + delimiter + LOG_HEADER + "\n");
// no errors found - process header and then details // no errors found - process header and then details
boolean isMasterok = true; boolean isMasterok = true;
boolean isDetailok = true;
boolean error=false;
Trx trx = null;
String trxName= null; String trxName= null;
List<String> rowsTmpResult = new ArrayList<String>();
for (int idx = 0; idx < data.size(); idx++) { for (int idx = 0; idx < data.size(); idx++) {
String rawLine = rawData.get(idx); String rawLine = rawData.get(idx);
String logMsg = null; String logMsg = null;
StringBuilder rowResult = new StringBuilder(); StringBuilder rowResult = new StringBuilder();
Trx trx = null;
GridTab currentGridTab=null; GridTab currentGridTab=null;
boolean error=false;
boolean isDetail=false; boolean isDetail=false;
int currentColumn=0; int currentColumn=0;
@ -316,20 +325,47 @@ public class GridTabCSVImporter implements IGridTabImporter
} }
} }
} }
//if master failed, then ignore any detail from it.
if (!isMasterok && isDetail){ if (!isMasterok && isDetail){
rawLine = rawLine + delimiter + quoteChar + Msg.getMsg(Env.getCtx(),"NotProccesed") + quoteChar + "\n"; rawLine = rawLine + delimiter + quoteChar + Msg.getMsg(Env.getCtx(),"NotProccesed") + quoteChar + "\n";
logFileW.write(rawLine); rowsTmpResult.add(rawLine);
continue; continue;
} }else if(isMasterok && isDetail && !isDetailok){
rawLine = rawLine + delimiter + quoteChar + "Record not proccesed due to detail record failure" + quoteChar + "\n";
rowsTmpResult.add(rawLine);
continue;
}
try { try {
isMasterok = true;
if(!isDetail){ if(!isDetail){
masterRecord = null; if(trx!=null){
trxName = "Import_" + gridTab.getTableName() + "_" + UUID.randomUUID(); if(error){
}else if (trxName == null){ trx.rollback();
trxName ="Import_" + gridTab.getTableName() + "_" + UUID.randomUUID(); for(String row:rowsTmpResult){
row =row.replaceAll("Updated","RolledBack");
row =row.replaceAll("Inserted","RolledBack");
logFileW.write(row);
}
trx.close();
trx=null;
error =false;
}else {
trx.commit();
for(String row:rowsTmpResult)
logFileW.write(row);
trx.close();
trx=null;
}
}
trxName = "Import_" + gridTab.getTableName() + "_" + UUID.randomUUID();
gridTab.getTableModel().setImportingMode(true,trxName);
trx = Trx.get(trxName,true);
masterRecord = null;
rowsTmpResult.clear();
isMasterok = true;
isDetailok = true;
} }
for(Map.Entry<GridTab, Integer> tabIndex : sortedtTabMapIndexes.entrySet()) { for(Map.Entry<GridTab, Integer> tabIndex : sortedtTabMapIndexes.entrySet()) {
@ -340,11 +376,14 @@ public class GridTabCSVImporter implements IGridTabImporter
continue; continue;
} }
currentGridTab.getTableModel().setImportingMode(true,trxName); //Assign master trx to its children
trx = Trx.get(trxName,true); if(!gridTab.equals(currentGridTab)){
int j = tabIndex.getValue(); currentGridTab.getTableModel().setImportingMode(true,trxName);
isDetail=true;
}
logMsg = areValidKeysAndColumns(currentGridTab,data.get(idx),header,currentColumn,j,masterRecord); int j = tabIndex.getValue();
logMsg = areValidKeysAndColumns(currentGridTab,data.get(idx),header,currentColumn,j,masterRecord,trx);
if (logMsg == null){ if (logMsg == null){
if (isInsertMode()){ if (isInsertMode()){
@ -359,7 +398,7 @@ public class GridTabCSVImporter implements IGridTabImporter
} }
if(logMsg==null) if(logMsg==null)
logMsg = proccessRow(currentGridTab,header,data.get(idx),currentColumn,j,masterRecord); logMsg = proccessRow(currentGridTab,header,data.get(idx),currentColumn,j,masterRecord,trx);
currentColumn = j + 1; currentColumn = j + 1;
if(!(logMsg == null)){ if(!(logMsg == null)){
@ -387,7 +426,6 @@ public class GridTabCSVImporter implements IGridTabImporter
else else
logMsg = Msg.getMsg(Env.getCtx(), "Updated")+" "+ po.toString(); logMsg = Msg.getMsg(Env.getCtx(), "Updated")+" "+ po.toString();
trx.commit();
//if it's parent record and there are details, parent needs to be refreshed //if it's parent record and there are details, parent needs to be refreshed
if(currentGridTab.equals(gridTab) && sortedtTabMapIndexes.size()>1) if(currentGridTab.equals(gridTab) && sortedtTabMapIndexes.size()>1)
currentGridTab.dataRefresh(false); currentGridTab.dataRefresh(false);
@ -405,20 +443,22 @@ public class GridTabCSVImporter implements IGridTabImporter
logMsg = Msg.getMsg(Env.getCtx(), "Error") + " " + Msg.getMsg(Env.getCtx(), "SaveError") + " (" + info + ")"; logMsg = Msg.getMsg(Env.getCtx(), "Error") + " " + Msg.getMsg(Env.getCtx(), "SaveError") + " (" + info + ")";
currentGridTab.dataIgnore(); currentGridTab.dataIgnore();
trx.rollback();
if(currentGridTab.equals(gridTab) && masterRecord==null){ if(currentGridTab.equals(gridTab) && masterRecord==null){
isMasterok = false; isMasterok = false;
break; break;
} }
if(!currentGridTab.equals(gridTab) && masterRecord!=null){
isDetailok = false;
break;
}
} }
rowResult.append("<"+currentGridTab.getTableName()+">: "); rowResult.append("<"+currentGridTab.getTableName()+">: ");
rowResult.append(logMsg); rowResult.append(logMsg);
rowResult.append(" / "); rowResult.append(" / ");
} else { } else {
currentGridTab.dataIgnore(); currentGridTab.dataIgnore();
error = false;
trx.rollback();
rowResult.append("<"+currentGridTab.getTableName()+">: "); rowResult.append("<"+currentGridTab.getTableName()+">: ");
rowResult.append(logMsg); rowResult.append(logMsg);
@ -429,31 +469,67 @@ public class GridTabCSVImporter implements IGridTabImporter
isMasterok = false; isMasterok = false;
break; break;
} }
if(!currentGridTab.equals(gridTab) && masterRecord!=null){
isDetailok = false;
break;
}
} }
m_import_mode = importMode; m_import_mode = importMode;
currentGridTab.getTableModel().setImportingMode(false, null);
} }
} catch (Exception e) { } catch (Exception e) {
rowResult.append("<"+currentGridTab.getTableName()+">: "); rowResult.append("<"+currentGridTab.getTableName()+">: ");
rowResult.append(Msg.getMsg(Env.getCtx(), "Error") + " " + e); rowResult.append(Msg.getMsg(Env.getCtx(), "Error") + " " + e);
rowResult.append(" / "); rowResult.append(" / ");
currentGridTab.dataIgnore(); currentGridTab.dataIgnore();
trx.rollback();
error = true; error = true;
//Master Failed, thus details cannot be imported //Master Failed, thus details cannot be imported
if(currentGridTab.equals(gridTab) && masterRecord==null){ if(currentGridTab.equals(gridTab) && masterRecord==null)
isMasterok = false; isMasterok = false;
}
if(!currentGridTab.equals(gridTab) && masterRecord!=null)
isDetailok = false;
} finally { } finally {
if (trx != null) {
trx.close();
trx = null;
}
m_import_mode =importMode; m_import_mode =importMode;
} }
// write // write
rawLine = rawLine + delimiter + quoteChar + rowResult.toString().replaceAll(delimiter, "") + quoteChar + "\n"; rawLine = rawLine + delimiter + quoteChar + rowResult.toString().replaceAll(delimiter, "") + quoteChar + "\n";
logFileW.write(rawLine); rowsTmpResult.add(rawLine);
}
if(trx!=null){
if(error){
trx.rollback();
for(String row:rowsTmpResult){
row =row.replaceAll("Updated","RolledBack");
row =row.replaceAll("Inserted","RolledBack");
logFileW.write(row);
}
gridTab.dataRefreshAll();
}else{
trx.commit();
for(String row:rowsTmpResult)
logFileW.write(row);
if(masterRecord!=null){
gridTab.getTableModel().dataRequery(masterRecord.get_WhereClause(true),false,0,false);
gridTab.dataRefresh();
}
}
trx.close();
trx=null;
gridTab.getTableModel().setImportingMode(false, null);
Env.clearTabContext(Env.getCtx(),gridTab.getWindowNo(),gridTab.getTabNo());
for(GridTab detail: childs){
if(detail.getTableModel().isOpen()){
detail.getTableModel().setImportingMode(false,null);
Env.clearTabContext(Env.getCtx(),detail.getWindowNo(),detail.getTabNo());
}
}
} }
} }
} catch (IOException e) { } catch (IOException e) {
@ -474,11 +550,7 @@ public class GridTabCSVImporter implements IGridTabImporter
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
if(masterRecord!=null){
Env.setContext(Env.getCtx(),gridTab.getWindowNo(),gridTab.getTabNo(),GridTab.CTX_SQL,masterRecord.get_WhereClause(true));
gridTab.dataRefresh();
}
if (logFile != null) if (logFile != null)
return logFile; return logFile;
else else
@ -620,9 +692,9 @@ public class GridTabCSVImporter implements IGridTabImporter
String idS = null; String idS = null;
int id = -1; int id = -1;
if("AD_Ref_List".equals(foreignTable)) if("AD_Ref_List".equals(foreignTable))
idS= resolveForeignList(column,foreignColumn,value); idS= resolveForeignList(column,foreignColumn,value,null);
else else
id = resolveForeign(foreignTable,foreignColumn,value); id = resolveForeign(foreignTable,foreignColumn,value,null);
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
@ -688,7 +760,7 @@ 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) < 0) if(resolveForeign(foreignTable,foreignColumn,value,null) < 0)
return new StringBuilder(Msg.getMsg(Env.getCtx(), "ForeignNotResolved" ,new Object[]{header.get(j),value})); return new StringBuilder(Msg.getMsg(Env.getCtx(), "ForeignNotResolved" ,new Object[]{header.get(j),value}));
} }
isEmptyRow=false; isEmptyRow=false;
@ -700,7 +772,7 @@ public class GridTabCSVImporter implements IGridTabImporter
return null; return null;
} }
private String proccessRow(GridTab gridTab,List<String> header, Map<String, Object> map,int startindx,int endindx,PO masterRecord){ private String proccessRow(GridTab gridTab,List<String> header, Map<String, Object> map,int startindx,int endindx,PO masterRecord,Trx trx){
String logMsg = null; String logMsg = null;
boolean isThereRow = false; boolean isThereRow = false;
@ -729,20 +801,19 @@ public class GridTabCSVImporter implements IGridTabImporter
if(header.get(i).contains(MTable.getTableName(Env.getCtx(),MLocation.Table_ID))){ if(header.get(i).contains(MTable.getTableName(Env.getCtx(),MLocation.Table_ID))){
if(address == null){ if(address == null){
String trxName= (masterRecord==null?null:masterRecord.get_TrxName());
if(isInsertMode()){ if(isInsertMode()){
address = new MLocation (Env.getCtx(),0,trxName); address = new MLocation (Env.getCtx(),0,trx.getTrxName());
}else{ }else{
Object location = gridTab.getValue("C_Location_ID")==null?0:gridTab.getValue("C_Location_ID").toString(); Object location = gridTab.getValue("C_Location_ID")==null?0:gridTab.getValue("C_Location_ID").toString();
int C_Location_ID = Integer.parseInt(location.toString()); int C_Location_ID = Integer.parseInt(location.toString());
address = new MLocation (Env.getCtx(),C_Location_ID,trxName); address = new MLocation (Env.getCtx(),C_Location_ID,trx.getTrxName());
} }
} }
if(!"(null)".equals(value.toString().trim())){ if(!"(null)".equals(value.toString().trim())){
if(isForeing) { if(isForeing) {
String foreignTable = columnName.substring(0,columnName.length()-3); String foreignTable = columnName.substring(0,columnName.length()-3);
setValue = resolveForeign(foreignTable,foreignColumn,value); setValue = resolveForeign(foreignTable,foreignColumn,value,trx);
if("C_City".equals(foreignTable)) if("C_City".equals(foreignTable))
address.setCity(value.toString()); address.setCity(value.toString());
}else }else
@ -780,9 +851,9 @@ public class GridTabCSVImporter implements IGridTabImporter
int id = -1; int id = -1;
if ("AD_Ref_List".equals(foreignTable)) if ("AD_Ref_List".equals(foreignTable))
idS= resolveForeignList(column, foreignColumn, value); idS= resolveForeignList(column, foreignColumn, value,trx);
else else
id = resolveForeign(foreignTable, foreignColumn, value); 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(),"ForeignNotResolved",new Object[]{header.get(i),value});
@ -818,14 +889,15 @@ public class GridTabCSVImporter implements IGridTabImporter
if (isForeing){ if (isForeing){
String foreignTable = column.getReferenceTableName(); String foreignTable = column.getReferenceTableName();
if ("AD_Ref_List".equals(foreignTable)) { if ("AD_Ref_List".equals(foreignTable)) {
String idS = resolveForeignList(column, foreignColumn, value); String idS = resolveForeignList(column, foreignColumn, value,trx);
if(idS == null) if(idS == null)
return Msg.getMsg(Env.getCtx(),"ForeignNotResolved",new Object[]{header.get(i),value}); return Msg.getMsg(Env.getCtx(),"ForeignNotResolved",new Object[]{header.get(i),value});
setValue = idS; setValue = idS;
isThereRow =true; isThereRow =true;
} else { } else {
int id = resolveForeign(foreignTable, foreignColumn, value);
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(),"ForeignNotResolved",new Object[]{header.get(i),value});
@ -844,6 +916,19 @@ public class GridTabCSVImporter implements IGridTabImporter
if(value instanceof java.util.Date) if(value instanceof java.util.Date)
value = new Timestamp(((java.util.Date)value).getTime()); value = new Timestamp(((java.util.Date)value).getTime());
if(DisplayType.Payment == field.getDisplayType()){
String oldValue = value.toString();
for(ValueNamePair pList: MRefList.getList(Env.getCtx(),REFERENCE_PAYMENTRULE,false)){
if(pList.getName().equals(oldValue.toString())){
oldValue = pList.getValue();
break;
}
}
if(!value.toString().equals(oldValue))
value = oldValue;
else
return Msg.getMsg(Env.getCtx(),"ForeignNotResolved",new Object[]{header.get(i),value});
}
setValue = value; setValue = value;
isThereRow =true; isThereRow =true;
} }
@ -863,7 +948,7 @@ public class GridTabCSVImporter implements IGridTabImporter
if (!address.save()){ if (!address.save()){
logMsg = CLogger.retrieveError()+" Address : "+address; logMsg = CLogger.retrieveError()+" Address : "+address;
}else { }else {
logMsg = gridTab.setValue("C_Location_ID",address.get_ID()); logMsg = gridTab.setValue("C_Location_ID",address.getC_Location_ID());
if(logMsg.equals("")) if(logMsg.equals(""))
logMsg= null; logMsg= null;
else else
@ -899,13 +984,13 @@ public class GridTabCSVImporter implements IGridTabImporter
if (isForeing){ if (isForeing){
String foreignTable = column.getReferenceTableName(); String foreignTable = column.getReferenceTableName();
if ("AD_Ref_List".equals(foreignTable)) { if ("AD_Ref_List".equals(foreignTable)) {
String idS = resolveForeignList(column,foreignColumn,setValue); String idS = resolveForeignList(column,foreignColumn,setValue,trx);
if(idS == null) if(idS == null)
return Msg.getMsg(Env.getCtx(),"ForeignNotResolved",new Object[]{columnName,setValue}); return Msg.getMsg(Env.getCtx(),"ForeignNotResolved",new Object[]{columnName,setValue});
setValue = idS; setValue = idS;
} else { } else {
int id = resolveForeign(foreignTable, foreignColumn, setValue); 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(),"ForeignNotResolved",new Object[]{columnName,setValue});
@ -930,28 +1015,28 @@ public class GridTabCSVImporter implements IGridTabImporter
return logMsg; return logMsg;
} }
private CellProcessor getProccesorFromColumn(MColumn column) { private CellProcessor getProccesorFromColumn(GridField field) {
// TODO: List columns can use RequireSubStr constraint // TODO: List columns can use RequireSubStr constraint
if (DisplayType.Date == column.getAD_Reference_ID()) { if (DisplayType.Date == field.getDisplayType()) {
return (new Optional(new ParseDate(DisplayType.DEFAULT_DATE_FORMAT))); return (new Optional(new ParseDate(DisplayType.DEFAULT_DATE_FORMAT)));
} else if (DisplayType.DateTime == column.getAD_Reference_ID()) { } else if (DisplayType.DateTime == field.getDisplayType()) {
return (new Optional(new ParseDate(DisplayType.DEFAULT_TIMESTAMP_FORMAT))); return (new Optional(new ParseDate(DisplayType.DEFAULT_TIMESTAMP_FORMAT)));
} else if (DisplayType.Time == column.getAD_Reference_ID()) { } else if (DisplayType.Time == field.getDisplayType()) {
return (new Optional(new ParseDate("DisplayType.DEFAULT_TIME_FORMAT"))); return (new Optional(new ParseDate("DisplayType.DEFAULT_TIME_FORMAT")));
} else if (DisplayType.Integer == column.getAD_Reference_ID()) { } else if (DisplayType.Integer == field.getDisplayType()) {
return (new Optional(new ParseInt())); return (new Optional(new ParseInt()));
} else if (DisplayType.isNumeric(column.getAD_Reference_ID())) { } else if (DisplayType.isNumeric(field.getDisplayType())) {
return (new Optional(new ParseBigDecimal(new DecimalFormatSymbols(Language.getLoginLanguage().getLocale())))); return (new Optional(new ParseBigDecimal(new DecimalFormatSymbols(Language.getLoginLanguage().getLocale()))));
} else if (DisplayType.YesNo == column.getAD_Reference_ID()) { } else if (DisplayType.YesNo == field.getDisplayType()) {
return (new Optional(new ParseBool("y", "n"))); return (new Optional(new ParseBool("y", "n")));
} else if (DisplayType.isText(column.getAD_Reference_ID())) { } else if (DisplayType.isText(field.getDisplayType())) {
return (new Optional(new StrMinMax(1, column.getFieldLength()))); return (new Optional(new StrMinMax(1, field.getFieldLength())));
} else { // optional lookups and text } else { // optional lookups and text
return null; return null;
} }
} }
private String areValidKeysAndColumns(GridTab gridTab, Map<String, Object> map,List<String> header,int startindx,int endindx, PO masterRecord){ private String areValidKeysAndColumns(GridTab gridTab, Map<String, Object> map,List<String> header,int startindx,int endindx,PO masterRecord,Trx trx){
MQuery pquery = new MQuery(gridTab.getAD_Table_ID()); MQuery pquery = new MQuery(gridTab.getAD_Table_ID());
String logMsg= null; String logMsg= null;
Object tmpValue=null; Object tmpValue=null;
@ -984,10 +1069,10 @@ public class GridTabCSVImporter implements IGridTabImporter
foreignColumn = header.get(i).substring(header.get(i).indexOf("[")+1,header.get(i).indexOf("]")); foreignColumn = header.get(i).substring(header.get(i).indexOf("[")+1,header.get(i).indexOf("]"));
String foreignTable = column.getReferenceTableName(); String foreignTable = column.getReferenceTableName();
if ("AD_Ref_List".equals(foreignTable)) { if ("AD_Ref_List".equals(foreignTable)) {
String idS = resolveForeignList(column, foreignColumn, tmpValue); String idS = resolveForeignList(column, foreignColumn, tmpValue,trx);
setValue = idS; setValue = idS;
}else { }else {
int id = resolveForeign(foreignTable, foreignColumn, tmpValue); int id = resolveForeign(foreignTable, foreignColumn, tmpValue,trx);
setValue = id; setValue = id;
} }
}else{ }else{
@ -1022,10 +1107,10 @@ public class GridTabCSVImporter implements IGridTabImporter
foreignColumn = columnName.substring(columnName.indexOf("[")+1,columnName.indexOf("]")); foreignColumn = columnName.substring(columnName.indexOf("[")+1,columnName.indexOf("]"));
String foreignTable = column.getReferenceTableName(); String foreignTable = column.getReferenceTableName();
if ("AD_Ref_List".equals(foreignTable)) { if ("AD_Ref_List".equals(foreignTable)) {
String idS = resolveForeignList(column,foreignColumn,value); String idS = resolveForeignList(column,foreignColumn,value,trx);
value = idS; value = idS;
}else { }else {
int id = resolveForeign(foreignTable,foreignColumn,value); int id = resolveForeign(foreignTable,foreignColumn,value,trx);
value = id; value = id;
} }
} }
@ -1069,21 +1154,23 @@ public class GridTabCSVImporter implements IGridTabImporter
return logMsg; return logMsg;
} }
private String resolveForeignList(MColumn column, String foreignColumn, Object value) { private String resolveForeignList(MColumn column, String foreignColumn, Object value ,Trx trx) {
String idS = null; String idS = null;
String trxName = (trx!=null?trx.getTrxName():null);
StringBuilder select = new StringBuilder("SELECT Value FROM AD_Ref_List WHERE ") StringBuilder select = new StringBuilder("SELECT Value FROM AD_Ref_List WHERE ")
.append(foreignColumn).append("=? AND AD_Reference_ID=? AND IsActive='Y'"); .append(foreignColumn).append("=? AND AD_Reference_ID=? AND IsActive='Y'");
idS = DB.getSQLValueStringEx(null, select.toString(), value, column.getAD_Reference_Value_ID()); idS = DB.getSQLValueStringEx(trxName, select.toString(), value, column.getAD_Reference_Value_ID());
return idS; return idS;
} }
private int resolveForeign(String foreignTable, String foreignColumn, Object value) { 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 ") StringBuilder select = new StringBuilder("SELECT ")
.append(foreignTable).append("_ID FROM ") .append(foreignTable).append("_ID FROM ")
.append(foreignTable).append(" WHERE ") .append(foreignTable).append(" WHERE ")
.append(foreignColumn).append("=? AND IsActive='Y' AND AD_Client_ID=?"); .append(foreignColumn).append("=? AND IsActive='Y' AND AD_Client_ID=?");
id = DB.getSQLValueEx(null, select.toString(), value, Env.getAD_Client_ID(Env.getCtx())); id = DB.getSQLValueEx(trxName, select.toString(), value, Env.getAD_Client_ID(Env.getCtx()));
if (id == -1 && !"AD_Client".equals(foreignTable)) { 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();
@ -1091,7 +1178,7 @@ public class GridTabCSVImporter implements IGridTabImporter
|| 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 // try System client if the table has System access
id = DB.getSQLValueEx(null, select.toString(), value, 0); id = DB.getSQLValueEx(trxName, select.toString(), value, 0);
} }
} }
return id; return id;

View File

@ -3822,4 +3822,7 @@ public class GridTable extends AbstractTableModel
return m_importing; return m_importing;
} }
public String get_TrxName() {
return m_trxName;
}
} }

View File

@ -117,7 +117,8 @@ public class WLocationEditor extends WEditor implements EventListener<Event>, Pr
} }
else else
{ {
m_value = m_Location.getLocation(value, null); String trxName = this.gridField.getGridTab().getTableModel().get_TrxName();
m_value = m_Location.getLocation(value, trxName);
if (m_value == null) if (m_value == null)
getComponent().setText("<" + value + ">"); getComponent().setText("<" + value + ">");
else else