IDEMPIERE-454 Easy import / apply best practices on import mode variables

This commit is contained in:
Carlos Ruiz 2013-03-30 12:34:29 -05:00
parent 31c808cf7c
commit 7ad9d21ef8
1 changed files with 40 additions and 21 deletions

View File

@ -75,11 +75,14 @@ public class GridTabCSVImporter implements IGridTabImporter
{ {
private static final String ERROR_HEADER = "_ERROR_"; private static final String ERROR_HEADER = "_ERROR_";
private static final String LOG_HEADER = "_LOG_"; private static final String LOG_HEADER = "_LOG_";
private String IMPORT_MODE = null; private boolean m_isError = false;
private String m_import_mode = null;
private static final String IMPORT_MODE_MERGE = "M";
private static final String IMPORT_MODE_UPDATE = "U";
private static final String IMPORT_MODE_INSERT = "I";
/** Logger */ /** Logger */
private static CLogger log = CLogger.getCLogger(GridTabCSVImporter.class); private static CLogger log = CLogger.getCLogger(GridTabCSVImporter.class);
boolean m_isError = false;
public File fileImport(GridTab gridTab, List<GridTab> childs, InputStream filestream, Charset charset , String importMode) { public File fileImport(GridTab gridTab, List<GridTab> childs, InputStream filestream, Charset charset , String importMode) {
ICsvMapReader mapReader = null; ICsvMapReader mapReader = null;
@ -90,9 +93,9 @@ public class GridTabCSVImporter implements IGridTabImporter
CsvPreference csvpref = CsvPreference.STANDARD_PREFERENCE; CsvPreference csvpref = CsvPreference.STANDARD_PREFERENCE;
String delimiter = String.valueOf((char) csvpref.getDelimiterChar()); String delimiter = String.valueOf((char) csvpref.getDelimiterChar());
String quoteChar = String.valueOf((char) csvpref.getQuoteChar()); String quoteChar = String.valueOf((char) csvpref.getQuoteChar());
IMPORT_MODE = importMode; m_import_mode = importMode;
if(!gridTab.isInsertRecord() && IMPORT_MODE.equals("I")) if(!gridTab.isInsertRecord() && isInsertMode())
throw new AdempiereException("Insert record disabled for Tab"); throw new AdempiereException("Insert record disabled for Tab");
try { try {
@ -143,7 +146,7 @@ public class GridTabCSVImporter implements IGridTabImporter
} }
} }
if((IMPORT_MODE.equals("U") || IMPORT_MODE.equals("M")) && !isThereKey) if(isUpdateOrMergeMode() && !isThereKey)
throw new AdempiereException(gridTab.getTableName()+": "+Msg.getMsg(Env.getCtx(), "NoKeyFound")); throw new AdempiereException(gridTab.getTableName()+": "+Msg.getMsg(Env.getCtx(), "NoKeyFound"));
tabMapIndexes.put(gridTab,indxDetail-1); tabMapIndexes.put(gridTab,indxDetail-1);
@ -161,7 +164,7 @@ public class GridTabCSVImporter implements IGridTabImporter
if(currentDetailTab!=null){ if(currentDetailTab!=null){
//check out key per Tab //check out key per Tab
if((IMPORT_MODE.equals("U") || IMPORT_MODE.equals("M")) && !isThereKey){ if(isUpdateOrMergeMode() && !isThereKey){
throw new AdempiereException(currentDetailTab.getTableName()+": "+Msg.getMsg(Env.getCtx(), "NoKeyFound")); throw new AdempiereException(currentDetailTab.getTableName()+": "+Msg.getMsg(Env.getCtx(), "NoKeyFound"));
}else{ }else{
tabMapIndexes.put(currentDetailTab,idx-1); tabMapIndexes.put(currentDetailTab,idx-1);
@ -207,7 +210,7 @@ public class GridTabCSVImporter implements IGridTabImporter
} }
if(currentDetailTab!=null){ if(currentDetailTab!=null){
if((IMPORT_MODE.equals("U") || IMPORT_MODE.equals("M")) && !isThereKey) if(isUpdateOrMergeMode() && !isThereKey)
throw new AdempiereException(currentDetailTab.getTableName()+": "+Msg.getMsg(Env.getCtx(), "NoKeyFound")); throw new AdempiereException(currentDetailTab.getTableName()+": "+Msg.getMsg(Env.getCtx(), "NoKeyFound"));
tabMapIndexes.put(currentDetailTab,header.size()-1); tabMapIndexes.put(currentDetailTab,header.size()-1);
@ -337,7 +340,7 @@ public class GridTabCSVImporter implements IGridTabImporter
logMsg = areValidKeysAndColumns(currentGridTab,map,header,currentColumn,j,masterRecord); logMsg = areValidKeysAndColumns(currentGridTab,map,header,currentColumn,j,masterRecord);
if (logMsg == null){ if (logMsg == null){
if (IMPORT_MODE.equals("I")){ if (isInsertMode()){
if(!currentGridTab.getTableModel().isOpen()) if(!currentGridTab.getTableModel().isOpen())
currentGridTab.getTableModel().open(0); currentGridTab.getTableModel().open(0);
//how to read from status since the warning is coming empty ? //how to read from status since the warning is coming empty ?
@ -350,7 +353,7 @@ public class GridTabCSVImporter implements IGridTabImporter
currentColumn = j + 1; currentColumn = j + 1;
if(!(logMsg == null)){ if(!(logMsg == null)){
IMPORT_MODE =importMode; m_import_mode =importMode;
//Ignore row since there is no data //Ignore row since there is no data
if("NO_DATA_TO_IMPORT".equals(logMsg)){ if("NO_DATA_TO_IMPORT".equals(logMsg)){
logMsg =""; logMsg ="";
@ -370,7 +373,7 @@ public class GridTabCSVImporter implements IGridTabImporter
if(currentGridTab.equals(gridTab)) if(currentGridTab.equals(gridTab))
masterRecord = po; masterRecord = po;
if(IMPORT_MODE.equals("I")) if(isInsertMode())
logMsg = Msg.getMsg(Env.getCtx(), "Inserted")+" "+ po.toString(); logMsg = Msg.getMsg(Env.getCtx(), "Inserted")+" "+ po.toString();
else else
logMsg = Msg.getMsg(Env.getCtx(), "Updated")+" "+ po.toString(); logMsg = Msg.getMsg(Env.getCtx(), "Updated")+" "+ po.toString();
@ -420,7 +423,7 @@ public class GridTabCSVImporter implements IGridTabImporter
break; break;
} }
} }
IMPORT_MODE = importMode; m_import_mode = importMode;
currentGridTab.getTableModel().setImportingMode(false, null); currentGridTab.getTableModel().setImportingMode(false, null);
} }
} catch (Exception e) { } catch (Exception e) {
@ -435,7 +438,7 @@ public class GridTabCSVImporter implements IGridTabImporter
trx.close(); trx.close();
trx = null; trx = null;
} }
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";
@ -468,7 +471,23 @@ public class GridTabCSVImporter implements IGridTabImporter
else else
return errFile; return errFile;
} }
private boolean isInsertMode() {
return IMPORT_MODE_INSERT.equals(m_import_mode);
}
private boolean isUpdateMode() {
return IMPORT_MODE_UPDATE.equals(m_import_mode);
}
private boolean isMergeMode() {
return IMPORT_MODE_MERGE.equals(m_import_mode);
}
private boolean isUpdateOrMergeMode() {
return isUpdateMode() || isMergeMode();
}
private String getColumnName(boolean isKey ,boolean isForeing ,boolean isDetail , String headName){ private String getColumnName(boolean isKey ,boolean isForeing ,boolean isDetail , String headName){
if(isKey){ if(isKey){
@ -594,7 +613,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(IMPORT_MODE.equals("U")) if(isUpdateMode())
return new StringBuilder(Msg.getMsg(Env.getCtx(),"ForeignNotResolved",new Object[]{header.get(i),value})); return new StringBuilder(Msg.getMsg(Env.getCtx(),"ForeignNotResolved",new Object[]{header.get(i),value}));
} }
} else { } else {
@ -692,7 +711,7 @@ 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){
if(IMPORT_MODE.equals("I")){ if(isInsertMode()){
address = new MLocation (Env.getCtx(),0,masterRecord.get_TrxName()); address = new MLocation (Env.getCtx(),0,masterRecord.get_TrxName());
}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();
@ -712,7 +731,7 @@ public class GridTabCSVImporter implements IGridTabImporter
} }
address.set_ValueOfColumn(columnName,setValue); address.set_ValueOfColumn(columnName,setValue);
}else{ }else{
if(isKeyColumn && IMPORT_MODE.equals("U")) if(isKeyColumn && isUpdateMode())
continue; continue;
GridField field = gridTab.getField(columnName); GridField field = gridTab.getField(columnName);
@ -765,7 +784,7 @@ public class GridTabCSVImporter implements IGridTabImporter
if(!field.isDisplayed(true)) if(!field.isDisplayed(true))
continue; continue;
if (!IMPORT_MODE.equals("I") && !field.isEditable(true) && value!=null) { if (!isInsertMode() && !field.isEditable(true) && value!=null) {
logMsg = Msg.getMsg(Env.getCtx(), "FieldNotEditable", new Object[] {header.get(i)}) + "{" + value + "}"; logMsg = Msg.getMsg(Env.getCtx(), "FieldNotEditable", new Object[] {header.get(i)}) + "{" + value + "}";
break; break;
} }
@ -1000,13 +1019,13 @@ public class GridTabCSVImporter implements IGridTabImporter
} }
gridTab.getTableModel().dataRequery(pquery.getWhereClause(),false,0); gridTab.getTableModel().dataRequery(pquery.getWhereClause(),false,0);
gridTab.setCurrentRow(0,true); gridTab.setCurrentRow(0,true);
if (IMPORT_MODE.equals("I")){ if (isInsertMode()){
if(gridTab.getTableModel().getRowCount()>=1) if(gridTab.getTableModel().getRowCount()>=1)
logMsg = Msg.getMsg(Env.getCtx(), "AlreadyExists")+" "+pquery; logMsg = Msg.getMsg(Env.getCtx(), "AlreadyExists")+" "+pquery;
else else
return null; return null;
} }
if (IMPORT_MODE.equals("U")){ if (isUpdateMode()){
if(gridTab.getTableModel().getRowCount()==1) if(gridTab.getTableModel().getRowCount()==1)
return null; return null;
else if(gridTab.getTableModel().getRowCount()<=0) else if(gridTab.getTableModel().getRowCount()<=0)
@ -1014,11 +1033,11 @@ public class GridTabCSVImporter implements IGridTabImporter
else if(gridTab.getTableModel().getRowCount()>1) else if(gridTab.getTableModel().getRowCount()>1)
logMsg = Msg.getMsg(Env.getCtx(),"TooManyRows")+" "+pquery; logMsg = Msg.getMsg(Env.getCtx(),"TooManyRows")+" "+pquery;
} }
if (IMPORT_MODE.equals("M")){ if (isMergeMode()){
if(gridTab.getTableModel().getRowCount()==1) if(gridTab.getTableModel().getRowCount()==1)
IMPORT_MODE = "U"; m_import_mode = IMPORT_MODE_UPDATE;
else if(gridTab.getTableModel().getRowCount()<=0) else if(gridTab.getTableModel().getRowCount()<=0)
IMPORT_MODE = "I"; m_import_mode = IMPORT_MODE_INSERT;
else if(gridTab.getTableModel().getRowCount()>1) else if(gridTab.getTableModel().getRowCount()>1)
logMsg = Msg.getMsg(Env.getCtx(),"TooManyRows")+" "+pquery; logMsg = Msg.getMsg(Env.getCtx(),"TooManyRows")+" "+pquery;
} }