IDEMPIERE-454 Easy import

This commit is contained in:
Juan David Arboleda 2013-03-28 13:45:41 -05:00
parent 001a033914
commit eed4b34f19
3 changed files with 292 additions and 111 deletions

View File

@ -44,6 +44,7 @@ import org.compiere.util.CLogger;
import org.compiere.util.DB; import org.compiere.util.DB;
import org.compiere.util.DisplayType; import org.compiere.util.DisplayType;
import org.compiere.util.Env; import org.compiere.util.Env;
import org.compiere.util.Evaluator;
import org.compiere.util.Msg; import org.compiere.util.Msg;
import org.compiere.util.Util; import org.compiere.util.Util;
import org.supercsv.cellprocessor.FmtBool; import org.supercsv.cellprocessor.FmtBool;
@ -75,12 +76,14 @@ public class GridTabCSVExporter implements IGridTabExporter
try { try {
mapWriter = new CsvMapWriter(new FileWriter(file), CsvPreference.STANDARD_PREFERENCE); mapWriter = new CsvMapWriter(new FileWriter(file), CsvPreference.STANDARD_PREFERENCE);
String isValidTab = isValidTabToExport(gridTab); String isValidTab = isValidTabToExport(gridTab);
if (isValidTab!=null){ if(isValidTab!=null){
if (log.isLoggable(Level.INFO)) log.info(isValidTab); throw new AdempiereException(isValidTab);
return;
} }
GridTable gt = gridTab.getTableModel(); GridTable gt = gridTab.getTableModel();
GridField[] gridFields = getFields(gridTab); GridField[] gridFields = getFields(gridTab);
if(gridFields.length==0)
throw new AdempiereException(gridTab.getName()+": Did not find any available field to be exported.");
List<String> headArray = new ArrayList<String>(); List<String> headArray = new ArrayList<String>();
List<CellProcessor> procArray = new ArrayList<CellProcessor>(); List<CellProcessor> procArray = new ArrayList<CellProcessor>();
table = MTable.get(Env.getCtx(), gridTab.getTableName()); table = MTable.get(Env.getCtx(), gridTab.getTableName());
@ -115,18 +118,29 @@ public class GridTabCSVExporter implements IGridTabExporter
if(specialHDispayType > 0){ if(specialHDispayType > 0){
for(String specialHeader:resolveSpecialColumnName(specialHDispayType)){ for(String specialHeader:resolveSpecialColumnName(specialHDispayType)){
headArray.add(specialHeader); headArray.add(gridTab.getTableName()+">"+specialHeader);
procArray.add(null); procArray.add(null);
} }
} }
//Details up to tab level 1 //Details up to tab level 1
if(childs.size() > 0){ if(childs.size() > 0){
int specialDetDispayType = 0; int specialDetDispayType = 0;
int numOfTabs=0;
for(GridTab detail: childs){ for(GridTab detail: childs){
//comment this line if you want to export all tabs
if(detail.getTabNo()>1) if(!detail.isDisplayed())
break; continue;
if(detail.getDisplayLogic()!=null){
if(!currentRowOnly)
numOfTabs--;
else if(!Evaluator.evaluateLogic(detail,detail.getDisplayLogic()))
continue;
}
//comment this line if you want to export all tabs
if(numOfTabs > 0)
break;
if(detail.getTabLevel()>1) if(detail.getTabLevel()>1)
continue; continue;
@ -163,11 +177,13 @@ public class GridTabCSVExporter implements IGridTabExporter
} }
if(specialDetDispayType > 0){ if(specialDetDispayType > 0){
for(String specialHeader:resolveSpecialColumnName(specialDetDispayType)){ for(String specialHeader:resolveSpecialColumnName(specialDetDispayType)){
headArray.add(specialHeader); headArray.add(detail.getTableName()+">"+specialHeader);
procArray.add(null); procArray.add(null);
} }
specialDetDispayType = 0;
} }
tabMapDetails.put(detail,gridFields); tabMapDetails.put(detail,gridFields);
numOfTabs++;
} }
gridFields = null; gridFields = null;
} }
@ -192,26 +208,32 @@ public class GridTabCSVExporter implements IGridTabExporter
int index =0; int index =0;
int rowDetail=0; int rowDetail=0;
int record_Id = 0; int record_Id = 0;
boolean isActiveRow = true;
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());
if(DisplayType.Location == column.getAD_Reference_ID()){ if(DisplayType.Location == column.getAD_Reference_ID()){
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;
} }
String headName = header[idxfld]; String headName = header[idxfld];
Object value = resolveValue(gridTab, table, column, idxrow, headName); Object value = resolveValue(gridTab, table, column, idxrow, headName);
//Ignore row
if("IsActive".equals(headName) && value!=null && Boolean.valueOf((Boolean)value)==false){
isActiveRow=false;
break;
}
row.put(headName,value); row.put(headName,value);
idxfld++; idxfld++;
index++; index++;
} }
if(!isActiveRow)
continue;
if(specialHDispayType > 0 && record_Id > 0){ if(specialHDispayType > 0 && record_Id > 0){
switch(specialHDispayType) { switch(specialHDispayType) {
case DisplayType.Location: case DisplayType.Location:
@ -229,7 +251,7 @@ public class GridTabCSVExporter implements IGridTabExporter
}else{ }else{
sValue = address.get_Value(columnName); sValue = address.get_Value(columnName);
} }
row.put(specialHeader, sValue); row.put(gridTab.getTableName()+">"+specialHeader,sValue);
idxfld++; idxfld++;
index++; index++;
} }
@ -290,19 +312,21 @@ public class GridTabCSVExporter implements IGridTabExporter
} }
private Map<String, Object> resolveMasterDetailRow(int currentDetRow,Map<GridTab,GridField[]> tabMapDetails,List<String>headArray,int idxfld,int record_Id,String keyColumnParent){ private Map<String, Object> resolveMasterDetailRow(int currentDetRow,Map<GridTab,GridField[]> tabMapDetails,List<String>headArray,int idxfld,int record_Id,String keyColumnParent){
Map<String, Object> row = new HashMap<String, Object>(); Map<String,Object> activeRow = new HashMap<String,Object>();
Object value = null; Object value = null;
boolean hasDetails = false; boolean hasDetails = false;
int specialDetDispayType = 0; int specialDetDispayType = 0;
if (currentDetRow > 0 ) if (currentDetRow > 0 )
for(int j =0;j<idxfld;j++) for(int j =0;j<idxfld;j++)
row.put(headArray.get(j), null); activeRow.put(headArray.get(j), null);
for(Map.Entry<GridTab, GridField[]> childTabDetail : tabMapDetails.entrySet()) { for(Map.Entry<GridTab, GridField[]> childTabDetail : tabMapDetails.entrySet()) {
GridTab childTab = childTabDetail.getKey(); GridTab childTab = childTabDetail.getKey();
String whereCla = getWhereClause (childTab ,record_Id ,keyColumnParent); String whereCla = getWhereClause (childTab ,record_Id ,keyColumnParent);
childTab.getTableModel().dataRequery(whereCla, false, 0); childTab.getTableModel().dataRequery(whereCla, false, 0);
Map<String,Object> row = new HashMap<String,Object>();
boolean isActiveRow = true;
if (childTab.getRowCount() > 0) { if (childTab.getRowCount() > 0) {
int specialRecordId = 0; int specialRecordId = 0;
for(GridField field : childTabDetail.getValue()){ for(GridField field : childTabDetail.getValue()){
@ -318,12 +342,16 @@ 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()));
row.put(headName, value); row.put(headName,value);
if (value!=null) if(value!=null)
hasDetails = true; hasDetails = true;
//Ignore row
if(headName.contains("IsActive")&& value!=null && Boolean.valueOf((Boolean)value)==false){
isActiveRow=false;
break;
}
} }
if(specialDetDispayType > 0 && specialRecordId > 0){ if(isActiveRow && specialDetDispayType > 0 && specialRecordId > 0){
MLocation address = new MLocation (Env.getCtx(),specialRecordId,null); MLocation address = new MLocation (Env.getCtx(),specialRecordId,null);
for(String specialHeader:resolveSpecialColumnName(specialDetDispayType)){ for(String specialHeader:resolveSpecialColumnName(specialDetDispayType)){
String columnName = specialHeader.substring(specialHeader.indexOf(">")+1,specialHeader.length()); String columnName = specialHeader.substring(specialHeader.indexOf(">")+1,specialHeader.length());
@ -338,13 +366,15 @@ public class GridTabCSVExporter implements IGridTabExporter
}else{ }else{
sValue = address.get_Value(columnName); sValue = address.get_Value(columnName);
} }
row.put(specialHeader, sValue); row.put(childTab.getTableName()+">"+specialHeader,sValue);
} }
} }
} }
if(isActiveRow)
activeRow.putAll(row);
} }
if (hasDetails) if (hasDetails)
return row; return activeRow;
else else
return null; return null;
} }

View File

@ -75,7 +75,7 @@ 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 static String IMPORT_MODE = null;
/** Logger */ /** Logger */
private static CLogger log = CLogger.getCLogger(GridTabCSVImporter.class); private static CLogger log = CLogger.getCLogger(GridTabCSVImporter.class);
@ -87,7 +87,6 @@ public class GridTabCSVImporter implements IGridTabImporter
File logFile = null; File logFile = null;
PrintWriter errFileW = null; PrintWriter errFileW = null;
PrintWriter logFileW = null; PrintWriter logFileW = null;
GridTab currentDetailTab = null;
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());
@ -135,13 +134,13 @@ public class GridTabCSVImporter implements IGridTabImporter
GridField field = gridTab.getField(columnName); GridField field = gridTab.getField(columnName);
if (field == null) if (field == null)
throw new AdempiereException(Msg.getMsg(Env.getCtx(), "FieldNotFound")+" "+columnName); throw new AdempiereException(Msg.getMsg(Env.getCtx(), "FieldNotFound" , new Object[] {columnName}) );
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(MColumn.get(Env.getCtx(),field.getAD_Column_ID())));
indxDetail++; indxDetail++;
} }
} }
if((IMPORT_MODE.equals("U") || IMPORT_MODE.equals("M")) && !isThereKey) if((IMPORT_MODE.equals("U") || IMPORT_MODE.equals("M")) && !isThereKey)
@ -149,26 +148,34 @@ public class GridTabCSVImporter implements IGridTabImporter
tabMapIndexes.put(gridTab,indxDetail-1); tabMapIndexes.put(gridTab,indxDetail-1);
String childTableName = null; String childTableName = null;
String initTabName = null;
isThereKey = false; isThereKey = false;
locationFields = null; locationFields = null;
GridTab currentDetailTab = null;
//Mapping details //Mapping details
for(int idx = indxDetail; idx < header.size(); idx++) { for(int idx = indxDetail; idx < header.size(); idx++) {
String detailName = header.get(idx); String detailName = header.get(idx);
if(detailName.indexOf(">") > 0){ if(detailName!=null && detailName.indexOf(">") > 0){
childTableName = detailName.substring(0,detailName.indexOf(">")); childTableName = detailName.substring(0,detailName.indexOf(">"));
if (currentDetailTab==null ||
if (currentDetailTab==null){ (currentDetailTab!=null && !childTableName.equals(currentDetailTab.getTableName()))){
for(GridTab detail: childs){
if(currentDetailTab!=null){
//check out key per Tab
if((IMPORT_MODE.equals("U") || IMPORT_MODE.equals("M")) && !isThereKey){
throw new AdempiereException(currentDetailTab.getTableName()+": "+Msg.getMsg(Env.getCtx(), "NoKeyFound"));
}else{
tabMapIndexes.put(currentDetailTab,idx-1);
isThereKey =false;
}
}
for(GridTab detail: childs){
if(detail.getTableName().equals(childTableName)){ if(detail.getTableName().equals(childTableName)){
currentDetailTab = detail; currentDetailTab = detail;
initTabName = currentDetailTab.getTableName();
break; break;
} }
} }
}else if (!childTableName.contains(MTable.getTableName(Env.getCtx(), MLocation.Table_ID)) }
&& !childTableName.equals(initTabName))//Process just one detail
break;
if(currentDetailTab == null) if(currentDetailTab == null)
throw new AdempiereException(Msg.getMsg(Env.getCtx(),"NoChildTab",new Object[] {childTableName})); throw new AdempiereException(Msg.getMsg(Env.getCtx(),"NoChildTab",new Object[] {childTableName}));
@ -180,6 +187,7 @@ public class GridTabCSVImporter implements IGridTabImporter
readProcArray.add(getProccesorFromColumn(MColumn.get(Env.getCtx(),sField.getAD_Column_ID()))); readProcArray.add(getProccesorFromColumn(MColumn.get(Env.getCtx(),sField.getAD_Column_ID())));
idx++; idx++;
} }
idx--;
}else{ }else{
boolean isKeyColumn= columnName.indexOf("/") > 0; boolean isKeyColumn= columnName.indexOf("/") > 0;
boolean isForeing = columnName.indexOf("[") > 0 && columnName.indexOf("]")>0; boolean isForeing = columnName.indexOf("[") > 0 && columnName.indexOf("]")>0;
@ -187,14 +195,14 @@ public class GridTabCSVImporter implements IGridTabImporter
GridField field = currentDetailTab.getField(columnName); GridField field = currentDetailTab.getField(columnName);
if(field == null) if(field == null)
throw new AdempiereException(Msg.getMsg(Env.getCtx(), "FieldNotFound")+" "+columnName); throw new AdempiereException(Msg.getMsg(Env.getCtx(), "FieldNotFound",new Object[] {detailName}));
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(MColumn.get(Env.getCtx(),field.getAD_Column_ID())));
} }
}else }else
throw new AdempiereException(Msg.getMsg(Env.getCtx(),"WrongDetailName",new Object[] {idx,detailName})); throw new AdempiereException(Msg.getMsg(Env.getCtx(),"WrongDetailName",new Object[] {" col("+idx+") ",detailName}));
} }
@ -212,7 +220,7 @@ public class GridTabCSVImporter implements IGridTabImporter
sortedtTabMapIndexes.putAll(tabMapIndexes); sortedtTabMapIndexes.putAll(tabMapIndexes);
}else{ }else{
sortedtTabMapIndexes = new TreeMap<GridTab,Integer>(); sortedtTabMapIndexes = new TreeMap<GridTab,Integer>();
sortedtTabMapIndexes.put(currentDetailTab,header.size()-1); sortedtTabMapIndexes.put(gridTab,header.size()-1);
} }
CellProcessor[] processors = readProcArray.toArray(new CellProcessor[readProcArray.size()]); CellProcessor[] processors = readProcArray.toArray(new CellProcessor[readProcArray.size()]);
@ -280,9 +288,9 @@ public class GridTabCSVImporter implements IGridTabImporter
// no errors found - process header and then details // no errors found - process header and then details
boolean isMasterok = true; boolean isMasterok = true;
PO masterRecord = null; PO masterRecord = null;
String trxName= null;
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 trxName= null;
String logMsg = null; String logMsg = null;
StringBuilder rowResult = new StringBuilder(); StringBuilder rowResult = new StringBuilder();
Trx trx = null; Trx trx = null;
@ -304,39 +312,44 @@ public class GridTabCSVImporter implements IGridTabImporter
try { try {
Map<String, Object> map = data.get(idx); Map<String, Object> map = data.get(idx);
isMasterok = true; isMasterok = true;
if(!isDetail) if(!isDetail){
masterRecord = null; masterRecord = null;
trxName = "Import_" + gridTab.getTableName() + "_" + UUID.randomUUID();
}else if (trxName == null){
trxName ="Import_" + gridTab.getTableName() + "_" + UUID.randomUUID();
}
for(Map.Entry<GridTab, Integer> tabIndex : sortedtTabMapIndexes.entrySet()) { for(Map.Entry<GridTab, Integer> tabIndex : sortedtTabMapIndexes.entrySet()) {
currentGridTab =tabIndex.getKey(); currentGridTab = tabIndex.getKey();
if(isDetail && gridTab.equals(currentGridTab)){ if(isDetail && gridTab.equals(currentGridTab)){
currentColumn=indxDetail; currentColumn=indxDetail;
continue; continue;
} }
trxName = "Import_" + currentGridTab.getTableName() + "_" + UUID.randomUUID();
currentGridTab.getTableModel().setImportingMode(true,trxName); currentGridTab.getTableModel().setImportingMode(true,trxName);
trx = Trx.get(trxName,true); trx = Trx.get(trxName,true);
int j = tabIndex.getValue(); int j = tabIndex.getValue();
logMsg = areValidKeysAndColumns(currentGridTab,map,header,currentColumn,j); logMsg = areValidKeysAndColumns(currentGridTab,map,header,currentColumn,j,masterRecord);
if (logMsg == null){ if (logMsg == null){
if (IMPORT_MODE.equals("I")){ if (IMPORT_MODE.equals("I")){
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 ?
if (!currentGridTab.dataNew(false))
logMsg = "["+currentGridTab.getName()+"]"+"- Was not able to create a new record!";
}
if(logMsg==null)
logMsg = proccessRow(currentGridTab,header,map,currentColumn,j,masterRecord);
currentGridTab.dataNew(false); currentColumn = j + 1;
}
logMsg = proccessRow(currentGridTab,header,map,currentColumn,j,masterRecord);
currentColumn = j + 1;
if(!(logMsg == null)){ if(!(logMsg == null)){
IMPORT_MODE =importMode; IMPORT_MODE =importMode;
//Ignore row since there is no data //Ignore row since there is no data
if(logMsg.equals("NO_DATA_TO_IMPORT")){ if("NO_DATA_TO_IMPORT".equals(logMsg)){
logMsg =""; logMsg ="";
currentGridTab.dataIgnore(); currentGridTab.dataIgnore();
continue; continue;
@ -360,13 +373,12 @@ public class GridTabCSVImporter implements IGridTabImporter
logMsg = Msg.getMsg(Env.getCtx(), "Updated")+" "+ po.toString(); logMsg = Msg.getMsg(Env.getCtx(), "Updated")+" "+ po.toString();
trx.commit(); trx.commit();
if(isDetail){ if(isDetail || currentGridTab.isDetail()){
currentGridTab.refreshParentTabs(); currentGridTab.refreshParentTabs();
currentGridTab.getTableModel().dataRequery(masterRecord.get_WhereClause(true), false, 0); currentGridTab.getTableModel().dataRequery(masterRecord.get_WhereClause(true), false, 0);
} }
currentGridTab.dataRefreshAll(true); currentGridTab.dataRefresh(true);
} else { } else {
error = true;
ValueNamePair ppE = CLogger.retrieveWarning(); ValueNamePair ppE = CLogger.retrieveWarning();
if (ppE==null) if (ppE==null)
ppE = CLogger.retrieveError(); ppE = CLogger.retrieveError();
@ -382,11 +394,14 @@ public class GridTabCSVImporter implements IGridTabImporter
currentGridTab.dataIgnore(); currentGridTab.dataIgnore();
trx.rollback(); trx.rollback();
if(!isDetail && masterRecord==null){ if(currentGridTab.equals(gridTab) && masterRecord==null){
isMasterok = false; isMasterok = false;
break; break;
} }
} }
rowResult.append("<"+currentGridTab.getTableName()+">: ");
rowResult.append(logMsg);
rowResult.append(" / ");
} else { } else {
currentGridTab.dataIgnore(); currentGridTab.dataIgnore();
error = false; error = false;
@ -395,20 +410,15 @@ public class GridTabCSVImporter implements IGridTabImporter
rowResult.append("<"+currentGridTab.getTableName()+">: "); rowResult.append("<"+currentGridTab.getTableName()+">: ");
rowResult.append(logMsg); rowResult.append(logMsg);
rowResult.append(" / "); rowResult.append(" / ");
currentGridTab.getTableModel().setImportingMode(false, null);
IMPORT_MODE = importMode;
//Master Failed, thus details cannot be imported //Master Failed, thus details cannot be imported
if(!isDetail && masterRecord==null){ if(currentGridTab.equals(gridTab) && masterRecord==null){
isMasterok = false; isMasterok = false;
break; break;
} }
} }
rowResult.append("<"+currentGridTab.getTableName()+">: "); IMPORT_MODE = importMode;
rowResult.append(logMsg); currentGridTab.getTableModel().setImportingMode(false, null);
rowResult.append(" / ");
currentGridTab.getTableModel().setImportingMode(false, null);
IMPORT_MODE =importMode;
} }
} catch (Exception e) { } catch (Exception e) {
rowResult.append("<"+currentGridTab.getTableName()+">: "); rowResult.append("<"+currentGridTab.getTableName()+">: ");
@ -470,9 +480,11 @@ public class GridTabCSVImporter implements IGridTabImporter
if(isForeing) if(isForeing)
headName = headName.substring(0, headName.indexOf("[")); headName = headName.substring(0, headName.indexOf("["));
if(isDetail) if(isDetail){
headName = headName.substring(headName.indexOf(">")+ 1,headName.length()); headName = headName.substring(headName.indexOf(">")+ 1,headName.length());
if (headName.indexOf(">")>0)
headName = headName.substring(headName.indexOf(">")+ 1,headName.length());
}
return headName; return headName;
} }
@ -491,7 +503,7 @@ public class GridTabCSVImporter implements IGridTabImporter
String columnName = getColumnName (isKeyColumn,isForeing,true,header.get(i)); String columnName = getColumnName (isKeyColumn,isForeing,true,header.get(i));
GridField field = m_mTab.getField(columnName); GridField field = m_mTab.getField(columnName);
if (field == null) if (field == null)
throw new AdempiereException(Msg.getMsg(Env.getCtx(), "FieldNotFound")+" "+header.get(i)); throw new AdempiereException(Msg.getMsg(Env.getCtx(), "FieldNotFound", new Object[] {header.get(i)}));
lsField.add(field); lsField.add(field);
}else }else
@ -540,7 +552,7 @@ public class GridTabCSVImporter implements IGridTabImporter
isEmptyRow=false; isEmptyRow=false;
if (log.isLoggable(Level.FINE)) log.fine("Setting " + columnName + " to " + value); if (log.isLoggable(Level.FINE)) log.fine("Setting " + columnName + " to " + value);
boolean isKeyColumn = columnName.indexOf("/") > 0; boolean isKeyColumn = columnName.indexOf("/") > 0;
boolean isForeing = columnName.indexOf("[") > 0 && columnName.indexOf("]")>0; boolean isForeing = columnName.indexOf("[") > 0 && columnName.indexOf("]")>0;
boolean isDetail = columnName.indexOf(">") > 0; boolean isDetail = columnName.indexOf(">") > 0;
@ -551,16 +563,16 @@ public class GridTabCSVImporter implements IGridTabImporter
GridField field=gridTab.getField(columnName); GridField field=gridTab.getField(columnName);
if (field == null) if (field == null)
return new StringBuilder(Msg.getMsg(Env.getCtx(), "NotAWindowField")+" "+header.get(i)); return new StringBuilder(Msg.getMsg(Env.getCtx(), "NotAWindowField" , new Object[] {header.get(i)}));
if (field.isParentValue()) if (field.isParentValue())
continue; continue;
if (field.isReadOnly() && !field.isParentValue()) if (field.isReadOnly() && !field.isParentValue())
return new StringBuilder(Msg.getMsg(Env.getCtx(), "FieldIsReadOnly")+" "+header.get(i)); return new StringBuilder(Msg.getMsg(Env.getCtx(), "FieldIsReadOnly",new Object[] {header.get(i)}));
if (!(field.isDisplayed() || field.isDisplayedGrid())) if (!(field.isDisplayed() || field.isDisplayedGrid()))
return new StringBuilder(Msg.getMsg(Env.getCtx(), "FieldNotDisplayed")+" "+header.get(i)); return new StringBuilder(Msg.getMsg(Env.getCtx(), "FieldNotDisplayed",new Object[] {header.get(i)}));
MColumn column = MColumn.get(Env.getCtx(), field.getAD_Column_ID()); MColumn column = MColumn.get(Env.getCtx(), field.getAD_Column_ID());
if((field.isMandatory(true) || column.isMandatory()) && value == null && field.getDefault()==null){ if((field.isMandatory(true) || column.isMandatory()) && value == null && field.getDefault()==null){
@ -568,7 +580,7 @@ public class GridTabCSVImporter implements IGridTabImporter
mandatoryColumns.append(header.get(i)); mandatoryColumns.append(header.get(i));
} }
if (isForeing && value != null && !value.equals("(null)")){ if (isForeing && value != null && !"(null)".equals(value)){
String foreignTable = column.getReferenceTableName(); String foreignTable = column.getReferenceTableName();
String idS = null; String idS = null;
int id = -1; int id = -1;
@ -577,8 +589,11 @@ public class GridTabCSVImporter implements IGridTabImporter
else else
id = resolveForeign(foreignTable,foreignColumn,value); id = resolveForeign(foreignTable,foreignColumn,value);
if(idS == null && id < 0) if(idS == null && id < 0){
return new StringBuilder(Msg.getMsg(Env.getCtx(), "ForeignNotResolved")+" "+header.get(i) +" - "+value); //it could be that record still doesn't exist if import mode is inserting or merging
if(IMPORT_MODE.equals("U"))
return new StringBuilder(Msg.getMsg(Env.getCtx(),"ForeignNotResolved",new Object[]{header.get(i),value}));
}
} else { } else {
// no validation here // no validation here
// TODO: we could validate length of string or min/max // TODO: we could validate length of string or min/max
@ -595,13 +610,13 @@ public class GridTabCSVImporter implements IGridTabImporter
GridField field = gridTab.getField(sField); GridField field = gridTab.getField(sField);
if(field == null) if(field == null)
return new StringBuilder(Msg.getMsg(Env.getCtx(), "NotAWindowField")+" "+sField); return new StringBuilder(Msg.getMsg(Env.getCtx(), "NotAWindowField",new Object[] {sField}));
if(field.isReadOnly() && !field.isParentValue()) if(field.isReadOnly() && !field.isParentValue())
return new StringBuilder(Msg.getMsg(Env.getCtx(), "FieldIsReadOnly")+" "+field.getColumnName()); return new StringBuilder(Msg.getMsg(Env.getCtx(), "FieldIsReadOnly",new Object[] {field.getColumnName()}));
if(!(field.isDisplayed() || field.isDisplayedGrid())) if(!(field.isDisplayed() || field.isDisplayedGrid()))
return new StringBuilder(Msg.getMsg(Env.getCtx(), "FieldNotDisplayed")+" "+field.getColumnName()); return new StringBuilder(Msg.getMsg(Env.getCtx(), "FieldNotDisplayed",new Object[] {field.getColumnName()}));
if (header.get(i).contains(MTable.getTableName(Env.getCtx(),MLocation.Table_ID))) if (header.get(i).contains(MTable.getTableName(Env.getCtx(),MLocation.Table_ID)))
{ {
@ -614,7 +629,7 @@ public class GridTabCSVImporter implements IGridTabImporter
break; break;
String columnName = header.get(j); String columnName = header.get(j);
Object value = tmpRow.get(j); Object value = tmpRow.get(j);
if(value!=null){ if(value!=null){
if(columnName.contains("RegionName")||columnName.contains("C_Region_ID")) if(columnName.contains("RegionName")||columnName.contains("C_Region_ID"))
thereIsRegion = true; thereIsRegion = true;
@ -622,7 +637,7 @@ public class GridTabCSVImporter implements IGridTabImporter
thereIsCountry= true; thereIsCountry= true;
}else }else
continue; continue;
boolean isKeyColumn = columnName.indexOf("/") > 0; boolean isKeyColumn = columnName.indexOf("/") > 0;
boolean isForeing = columnName.indexOf("[") > 0 && columnName.indexOf("]")>0; boolean isForeing = columnName.indexOf("[") > 0 && columnName.indexOf("]")>0;
boolean isDetail = columnName.indexOf(">") > 0; boolean isDetail = columnName.indexOf(">") > 0;
@ -631,10 +646,10 @@ public class GridTabCSVImporter implements IGridTabImporter
if(isForeing) if(isForeing)
foreignColumn = header.get(j).substring(header.get(j).indexOf("[")+1, header.get(j).indexOf("]")); foreignColumn = header.get(j).substring(header.get(j).indexOf("[")+1, header.get(j).indexOf("]"));
if(isForeing && value!=null && !value.equals("(null)")){ 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) < 0)
return new StringBuilder(Msg.getMsg(Env.getCtx(), "ForeignNotResolved")+" "+header.get(j) +" - "+value); return new StringBuilder(Msg.getMsg(Env.getCtx(), "ForeignNotResolved" ,new Object[]{header.get(j),value}));
} }
isEmptyRow=false; isEmptyRow=false;
} }
@ -650,13 +665,17 @@ public class GridTabCSVImporter implements IGridTabImporter
String logMsg = null; String logMsg = null;
boolean isThereRow = false; boolean isThereRow = false;
MLocation address = null; MLocation address = null;
List<String> parentColumns = new ArrayList<String>();
for(int i = startindx ; i < endindx + 1 ; i++){ for(int i = startindx ; i < endindx + 1 ; i++){
String columnName = header.get(i); String columnName = header.get(i);
Object value = map.get(header.get(i)); Object value = map.get(header.get(i));
boolean isDetail = false; boolean isDetail = false;
if(value == null) if(value == null)
continue; continue;
if(columnName.endsWith("_ID") && "0".equals(value))
continue;
boolean isKeyColumn= columnName.indexOf("/") > 0; boolean isKeyColumn= columnName.indexOf("/") > 0;
boolean isForeing = columnName.indexOf("[") > 0 && columnName.indexOf("]")>0; boolean isForeing = columnName.indexOf("[") > 0 && columnName.indexOf("]")>0;
isDetail = columnName.indexOf(">") > 0; isDetail = columnName.indexOf(">") > 0;
@ -671,19 +690,19 @@ public class GridTabCSVImporter implements IGridTabImporter
if(address == null){ if(address == null){
if(IMPORT_MODE.equals("I")){ if(IMPORT_MODE.equals("I")){
address = new MLocation (Env.getCtx(),0,null); 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();
int C_Location_ID = Integer.parseInt(location.toString()); int C_Location_ID = Integer.parseInt(location.toString());
address = new MLocation (Env.getCtx(),C_Location_ID,null); address = new MLocation (Env.getCtx(),C_Location_ID,masterRecord.get_TrxName());
} }
} }
if(!value.toString().trim().equals("(null)")){ 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);
if(foreignTable.equals("C_City")) if("C_City".equals(foreignTable))
address.setCity(value.toString()); address.setCity(value.toString());
}else }else
setValue = value; setValue = value;
@ -696,17 +715,20 @@ public class GridTabCSVImporter implements IGridTabImporter
GridField field = gridTab.getField(columnName); GridField field = gridTab.getField(columnName);
if (field.isParentValue()){ if (field.isParentValue()){
if(value.toString().trim().equals("(null)")){ if("(null)".equals(value.toString())){
logMsg = Msg.getMsg(Env.getCtx(),"NoParentDelete", new Object[] {header.get(i)}); logMsg = Msg.getMsg(Env.getCtx(),"NoParentDelete", new Object[] {header.get(i)});
break; break;
} }
if(isForeing && masterRecord!=null){ if(isForeing && masterRecord!=null){
if (masterRecord.get_Value(foreignColumn).toString().equals(value)){ if (masterRecord.get_Value(foreignColumn).toString().equals(value)){
gridTab.setValue(field,masterRecord.get_ID()); logMsg = gridTab.setValue(field,masterRecord.get_ID());
if(logMsg.equals(""))
logMsg= null;
else break;
}else{ }else{
if(value!=null){ if(value!=null){
logMsg = header.get(i)+" - " +Msg.getMsg(Env.getCtx(),"DiffParentValue", new Object[] {masterRecord.get_Value(foreignColumn).toString(),value}); logMsg = header.get(i)+" - "+Msg.getMsg(Env.getCtx(),"DiffParentValue", new Object[] {masterRecord.get_Value(foreignColumn).toString(),value});
break; break;
} }
} }
@ -721,20 +743,34 @@ public class GridTabCSVImporter implements IGridTabImporter
else else
id = resolveForeign(foreignTable, foreignColumn, value); id = resolveForeign(foreignTable, foreignColumn, value);
if(idS == null && id < 0)
return Msg.getMsg(Env.getCtx(),"ForeignNotResolved",new Object[]{header.get(i),value});
if(id >= 0) if(id >= 0)
gridTab.setValue(field,id); logMsg = gridTab.setValue(field,id);
else if (idS != null) else if (idS != null)
gridTab.setValue(field,idS); logMsg = gridTab.setValue(field,idS);
if(logMsg !=null && logMsg.equals(""))
logMsg = null;
else break;
} }
parentColumns.add(columnName);
continue; continue;
} }
//this field should not be inserted or updated
if (!field.isEditable(true) && value!=null) { if(!field.isDisplayed(true))
continue;
if (!IMPORT_MODE.equals("I") && !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;
} }
if(value.toString().trim().equals("(null)")){ if("(null)".equals(value.toString().trim())){
gridTab.setValue(field,null); logMsg = gridTab.setValue(field,null);
if(logMsg.equals(""))
logMsg= null;
else break;
}else{ }else{
MColumn column = MColumn.get(Env.getCtx(),field.getAD_Column_ID()); MColumn column = MColumn.get(Env.getCtx(),field.getAD_Column_ID());
@ -742,15 +778,21 @@ public class GridTabCSVImporter implements IGridTabImporter
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);
if(idS == null)
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);
if(id < 0)
return Msg.getMsg(Env.getCtx(),"ForeignNotResolved",new Object[]{header.get(i),value});
setValue = id; setValue = id;
if (field.isParentValue()) { if (field.isParentValue()) {
int actualId = (Integer) field.getValue(); int actualId = (Integer) field.getValue();
if (actualId != id) { if (actualId != id) {
logMsg = Msg.getMsg(Env.getCtx(), "ParentCannotChange")+" "+header.get(i); logMsg = Msg.getMsg(Env.getCtx(), "ParentCannotChange",new Object[]{header.get(i)});
break; break;
} }
} }
@ -767,7 +809,11 @@ public class GridTabCSVImporter implements IGridTabImporter
} }
if(setValue != null) if(setValue != null)
gridTab.setValue(field,setValue); logMsg = gridTab.setValue(field,setValue);
if(logMsg!=null && logMsg.equals(""))
logMsg= null;
else break;
} }
} }
} }
@ -776,12 +822,66 @@ public class GridTabCSVImporter implements IGridTabImporter
if (!address.save()){ if (!address.save()){
logMsg = CLogger.retrieveError()+" Address : "+address; logMsg = CLogger.retrieveError()+" Address : "+address;
}else { }else {
gridTab.setValue("C_Location_ID",address.get_ID()); logMsg = gridTab.setValue("C_Location_ID",address.get_ID());
isThereRow =true; if(logMsg.equals(""))
logMsg= null;
isThereRow =true;
} }
} }
if(!isThereRow) boolean checkParentKey = parentColumns.size()!=gridTab.getParentColumnNames().size();
if(isThereRow && logMsg==null && masterRecord!=null && checkParentKey){
for(String linkColumn : gridTab.getParentColumnNames()){
String columnName = linkColumn;
Object setValue = masterRecord.get_Value(linkColumn);
//resolve missing key
if(setValue==null){
columnName = null;
for(int j = startindx;j < endindx + 1;j++){
if(header.get(j).contains(linkColumn)){
columnName = header.get(j);
setValue = map.get(columnName);
break;
}
}
if( columnName!=null ){
String foreignColumn = null;
boolean isForeing = columnName.indexOf("[") > 0 && columnName.indexOf("]")>0;
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());
if (isForeing){
String foreignTable = column.getReferenceTableName();
if ("AD_Ref_List".equals(foreignTable)) {
String idS = resolveForeignList(column,foreignColumn,setValue);
if(idS == null)
return Msg.getMsg(Env.getCtx(),"ForeignNotResolved",new Object[]{columnName,setValue});
setValue = idS;
} else {
int id = resolveForeign(foreignTable, foreignColumn, setValue);
if(id < 0)
return Msg.getMsg(Env.getCtx(),"ForeignNotResolved",new Object[]{columnName,setValue});
setValue = id;
}
}
}else{
logMsg = "Key: "+linkColumn+" "+ Msg.getMsg(Env.getCtx(),"NotFound");
break;
}
}
logMsg = gridTab.setValue(linkColumn,setValue);
if(logMsg.equals(""))
logMsg= null;
else continue;
}
}
if(logMsg == null && !isThereRow)
logMsg ="NO_DATA_TO_IMPORT"; logMsg ="NO_DATA_TO_IMPORT";
return logMsg; return logMsg;
@ -808,12 +908,13 @@ public class GridTabCSVImporter implements IGridTabImporter
} }
} }
private String areValidKeysAndColumns(GridTab gridTab, Map<String, Object> map,List<String> header,int startindx,int endindx){ private String areValidKeysAndColumns(GridTab gridTab, Map<String, Object> map,List<String> header,int startindx,int endindx, PO masterRecord){
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;
String columnwithKey=null; String columnwithKey=null;
Object setValue = null; Object setValue = null;
List<String> parentColumns = new ArrayList<String>();
//Process columnKeys + Foreign to add restrictions. //Process columnKeys + Foreign to add restrictions.
for (int i = startindx ; i < endindx + 1 ; i++){ for (int i = startindx ; i < endindx + 1 ; i++){
boolean isKeyColumn = header.get(i).indexOf("/") > 0 && header.get(i).endsWith("K"); boolean isKeyColumn = header.get(i).indexOf("/") > 0 && header.get(i).endsWith("K");
@ -830,10 +931,14 @@ public class GridTabCSVImporter implements IGridTabImporter
if (tmpValue==null) if (tmpValue==null)
continue; continue;
GridField field = gridTab.getField(columnwithKey);
MColumn column = MColumn.get(Env.getCtx(), field.getAD_Column_ID());
if(field.isParentValue()){
parentColumns.add(column.getColumnName());
}
String foreignColumn = null; String foreignColumn = null;
if(isForeing){ if(isForeing){
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("]"));
MColumn column = MColumn.get(Env.getCtx(), gridTab.getField(columnwithKey).getAD_Column_ID());
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);
@ -850,10 +955,48 @@ public class GridTabCSVImporter implements IGridTabImporter
} }
if (pquery.getRestrictionCount() > 0){ if (pquery.getRestrictionCount() > 0){
//check out if parent keys were completed properly
if (gridTab.isDetail()){
for(String linkColumn : gridTab.getParentColumnNames()){
if(!pquery.getWhereClause().contains(linkColumn)){
Object value = masterRecord.get_Value(linkColumn);
//resolve key
if(value==null){
String columnName = null;
for(int j = startindx;j<endindx + 1;j++){
if(header.get(j).contains(linkColumn)){
columnName = header.get(j);
value = map.get(header.get(j));
break;
}
}
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());
String foreignColumn = null;
if(isForeing){
foreignColumn = columnName.substring(columnName.indexOf("[")+1,columnName.indexOf("]"));
String foreignTable = column.getReferenceTableName();
if ("AD_Ref_List".equals(foreignTable)) {
String idS = resolveForeignList(column,foreignColumn,value);
value = idS;
}else {
int id = resolveForeign(foreignTable,foreignColumn,value);
value = id;
}
}
}else{ //mandatory key not found
return Msg.getMsg(Env.getCtx(),"FillMandatory")+" "+linkColumn;
}
}
if(value!=null)
pquery.addRestriction(linkColumn,MQuery.EQUAL,value);
}
}
}
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 (IMPORT_MODE.equals("I")){
if(gridTab.getTableModel().getRowCount()>=1) if(gridTab.getTableModel().getRowCount()>=1)
logMsg = Msg.getMsg(Env.getCtx(), "AlreadyExists")+" "+pquery; logMsg = Msg.getMsg(Env.getCtx(), "AlreadyExists")+" "+pquery;

View File

@ -1361,9 +1361,17 @@ public abstract class AbstractADWindowContent extends AbstractUIPart implements
// Confirm Warning // Confirm Warning
else if (e.isWarning() && !e.isConfirmed()) else if (e.isWarning() && !e.isConfirmed())
{ {
if (! adTabbox.getSelectedGridTab().getTableModel().isImporting()) { boolean isImporting = false;
FDialog.warn(curWindowNo, null, e.getAD_Message(), e.getInfo()); if (e.getSource() instanceof GridTab) {
e.setConfirmed(true); // show just once - if MTable.setCurrentRow is involved the status event is re-issued GridTab gridTab = (GridTab)e.getSource();
isImporting = gridTab.getTableModel().isImporting();
} else if (e.getSource() instanceof GridTable) {
GridTable gridTable = (GridTable) e.getSource();
isImporting = gridTable.isImporting();
}
if (!isImporting) {
FDialog.warn(curWindowNo, null, e.getAD_Message(), e.getInfo());
e.setConfirmed(true); // show just once - if MTable.setCurrentRow is involved the status event is re-issued
} }
} }