IDEMPIERE-454 Easy import - V6 -> Add DocAction event for documents

This commit is contained in:
Juan David Arboleda 2013-04-23 18:27:37 -05:00
parent 2fa71e092f
commit 53c8d8d9d9
1 changed files with 127 additions and 32 deletions

View File

@ -15,6 +15,7 @@
package org.adempiere.impexp; package org.adempiere.impexp;
import static org.compiere.model.SystemIDs.REFERENCE_PAYMENTRULE; import static org.compiere.model.SystemIDs.REFERENCE_PAYMENTRULE;
import static org.compiere.model.SystemIDs.REFERENCE_DOCUMENTACTION;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -38,16 +39,19 @@ import java.util.logging.Level;
import org.adempiere.base.IGridTabImporter; import org.adempiere.base.IGridTabImporter;
import org.adempiere.exceptions.AdempiereException; import org.adempiere.exceptions.AdempiereException;
import org.adempiere.util.ProcessUtil;
import org.compiere.model.GridField; import org.compiere.model.GridField;
import org.compiere.model.GridTab; import org.compiere.model.GridTab;
import org.compiere.model.GridWindow; import org.compiere.model.GridWindow;
import org.compiere.model.GridWindowVO; 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.MProcess;
import org.compiere.model.MQuery; import org.compiere.model.MQuery;
import org.compiere.model.MRefList; 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.process.ProcessInfo;
import org.compiere.tools.FileUtil; import org.compiere.tools.FileUtil;
import org.compiere.util.CLogger; import org.compiere.util.CLogger;
import org.compiere.util.DB; import org.compiere.util.DB;
@ -57,6 +61,7 @@ import org.compiere.util.Language;
import org.compiere.util.Msg; import org.compiere.util.Msg;
import org.compiere.util.Trx; import org.compiere.util.Trx;
import org.compiere.util.ValueNamePair; import org.compiere.util.ValueNamePair;
import org.compiere.wf.MWFProcess;
import org.supercsv.cellprocessor.Optional; import org.supercsv.cellprocessor.Optional;
import org.supercsv.cellprocessor.ParseBigDecimal; import org.supercsv.cellprocessor.ParseBigDecimal;
import org.supercsv.cellprocessor.ParseBool; import org.supercsv.cellprocessor.ParseBool;
@ -113,6 +118,7 @@ public class GridTabCSVImporter implements IGridTabImporter
int indxDetail=0; int indxDetail=0;
List<GridField> locationFields = null; List<GridField> locationFields = null;
boolean isThereKey = false; boolean isThereKey = false;
boolean isThereDocAction = false;
//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);
@ -148,6 +154,9 @@ public class GridTabCSVImporter implements IGridTabImporter
throw new AdempiereException(Msg.getMsg(Env.getCtx(), "FieldNotFound" , new Object[] {columnName}) ); throw new AdempiereException(Msg.getMsg(Env.getCtx(), "FieldNotFound" , new Object[] {columnName}) );
else if(isKeyColumn && !isThereKey) else if(isKeyColumn && !isThereKey)
isThereKey =true; isThereKey =true;
else if (!isThereDocAction &&
MColumn.get(Env.getCtx(),field.getAD_Column_ID()).getAD_Reference_Value_ID() == REFERENCE_DOCUMENTACTION )
isThereDocAction= true;
readProcArray.add(getProccesorFromColumn(field)); readProcArray.add(getProccesorFromColumn(field));
indxDetail++; indxDetail++;
@ -340,24 +349,51 @@ public class GridTabCSVImporter implements IGridTabImporter
if(!isDetail){ if(!isDetail){
if(trx!=null){ if(trx!=null){
if(error){ if(error){
trx.rollback(); trx.rollback();
for(String row:rowsTmpResult){ for(String row:rowsTmpResult){
row =row.replaceAll("Updated","RolledBack"); row =row.replaceAll("Updated","RolledBack");
row =row.replaceAll("Inserted","RolledBack"); row =row.replaceAll("Inserted","RolledBack");
logFileW.write(row); logFileW.write(row);
} }
trx.close();
trx=null;
error =false; error =false;
}else { }else {
trx.commit();
for(String row:rowsTmpResult) if(isThereDocAction){
logFileW.write(row); boolean isError = false;
int AD_Process_ID = MColumn.get(Env.getCtx(),gridTab.getField("DocAction").getAD_Column_ID()).getAD_Process_ID();
trx.close();
trx=null; if( AD_Process_ID > 0 ){
} String docResult = processDocAction(masterRecord,AD_Process_ID);
if(docResult.contains("error"))
isError = true;
rowsTmpResult.set(0,rowsTmpResult.get(0).replace(quoteChar + "\n",docResult + quoteChar + "\n"));
}else {
throw new AdempiereException("No Process found for document action.");
}
if(isError){
trx.rollback();
for(String row:rowsTmpResult){
row = row.replaceAll("Updated","RolledBack");
row = row.replaceAll("Inserted","RolledBack");
logFileW.write(row);
}
}else{
trx.commit();
for(String row:rowsTmpResult)
logFileW.write(row);
}
}else{
trx.commit();
for(String row:rowsTmpResult)
logFileW.write(row);
}
}
trx.close();
trx=null;
} }
trxName = "Import_" + gridTab.getTableName() + "_" + UUID.randomUUID(); trxName = "Import_" + gridTab.getTableName() + "_" + UUID.randomUUID();
gridTab.getTableModel().setImportingMode(true,trxName); gridTab.getTableModel().setImportingMode(true,trxName);
@ -509,27 +545,52 @@ public class GridTabCSVImporter implements IGridTabImporter
} }
gridTab.dataRefreshAll(); gridTab.dataRefreshAll();
}else{ }else{
trx.commit(); if(isThereDocAction){
for(String row:rowsTmpResult)
logFileW.write(row); boolean isError = false;
int AD_Process_ID = MColumn.get(Env.getCtx(),gridTab.getField("DocAction").getAD_Column_ID()).getAD_Process_ID();
if(masterRecord!=null){
gridTab.getTableModel().dataRequery(masterRecord.get_WhereClause(true),false,0,false); if( AD_Process_ID > 0 ){
gridTab.dataRefresh(); String docResult = processDocAction(masterRecord,AD_Process_ID);
if(docResult.contains("error"))
isError = true;
rowsTmpResult.set(0,rowsTmpResult.get(0).replace(quoteChar + "\n",docResult + quoteChar + "\n"));
}else {
throw new AdempiereException("No Process found for document action.");
}
if(isError){
trx.rollback();
for(String row:rowsTmpResult){
row = row.replaceAll("Updated","RolledBack");
row = row.replaceAll("Inserted","RolledBack");
logFileW.write(row);
}
}else{
trx.commit();
for(String row:rowsTmpResult)
logFileW.write(row);
}
}else {
trx.commit();
for(String row:rowsTmpResult)
logFileW.write(row);
} }
} }
if(masterRecord!=null){
gridTab.getTableModel().dataRequery(masterRecord.get_WhereClause(true),false,0,false);
gridTab.navigateCurrent();
gridTab.getTableModel().setImportingMode(false, null);
for(GridTab detail: childs)
if(detail.getTableModel().isOpen())
detail.getTableModel().setImportingMode(false,null);
}
trx.close(); trx.close();
trx=null; 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) {
@ -557,6 +618,24 @@ public class GridTabCSVImporter implements IGridTabImporter
return errFile; return errFile;
} }
private String processDocAction(PO document, int AD_Process_ID){
int AD_Workflow_ID = MProcess.get(Env.getCtx(),AD_Process_ID).getAD_Workflow_ID();
if (AD_Workflow_ID > 0){
ProcessInfo wfProcess = new ProcessInfo (document.get_TrxName(),AD_Process_ID,document.get_Table_ID(),document.get_ID());
wfProcess.setTransactionName(document.get_TrxName());
MWFProcess wdPro = ProcessUtil.startWorkFlow(Env.getCtx(),wfProcess, AD_Workflow_ID);
if(wdPro == null)
return "Document action could not be proccesed";
else if (wfProcess.isError())
return "Document action error: "+wfProcess.getSummary();
else
return "Document action processed ["+wfProcess.getSummary()+"]";
}else {
return "No workflow was found";
}
}
private boolean isInsertMode() { private boolean isInsertMode() {
return IMPORT_MODE_INSERT.equals(m_import_mode); return IMPORT_MODE_INSERT.equals(m_import_mode);
} }
@ -874,7 +953,7 @@ public class GridTabCSVImporter implements IGridTabImporter
if(!field.isDisplayed(true)) if(!field.isDisplayed(true))
continue; continue;
if(!isInsertMode() && (!field.isEditable(true) && !MColumn.get(Env.getCtx(), field.getAD_Column_ID()).isUpdateable()) && 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;
} }
@ -928,7 +1007,23 @@ public class GridTabCSVImporter implements IGridTabImporter
value = oldValue; value = oldValue;
else else
return Msg.getMsg(Env.getCtx(),"ForeignNotResolved",new Object[]{header.get(i),value}); return Msg.getMsg(Env.getCtx(),"ForeignNotResolved",new Object[]{header.get(i),value});
} }else if(DisplayType.Button == field.getDisplayType()){
if(column.getAD_Reference_Value_ID()== REFERENCE_DOCUMENTACTION){
String oldValue = value.toString();
for(ValueNamePair pList: MRefList.getList(Env.getCtx(),REFERENCE_DOCUMENTACTION,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});
}else{
return Msg.getMsg(Env.getCtx(),"Invalid") + " Column ["+column.getColumnName()+"]";
}
}
setValue = value; setValue = value;
isThereRow =true; isThereRow =true;
} }