FR [2859763] - Make Doc posting independent of hardcoded arrays

https://sourceforge.net/tracker/?func=detail&aid=2859763&group_id=176962&atid=879335
WIP
This commit is contained in:
Carlos Ruiz 2009-09-16 04:03:26 +00:00
parent 11c8872dd6
commit e8a37adc4f
14 changed files with 152 additions and 100 deletions

View File

@ -115,10 +115,10 @@ public class ClientAcctProcessor extends SvrProcess
*/
private void postSession()
{
for (int i = 0; i < Doc.documentsTableID.length; i++)
for (int i = 0; i < Doc.getDocumentsTableID().length; i++)
{
int AD_Table_ID = Doc.documentsTableID[i];
String TableName = Doc.documentsTableName[i];
int AD_Table_ID = Doc.getDocumentsTableID()[i];
String TableName = Doc.getDocumentsTableName()[i];
// Post only special documents
if (p_AD_Table_ID != 0
&& p_AD_Table_ID != AD_Table_ID)

View File

@ -28,32 +28,20 @@ import java.util.Iterator;
import java.util.Properties;
import java.util.logging.Level;
import org.adempiere.exceptions.DBException;
import org.compiere.model.MAccount;
import org.compiere.model.MAcctSchema;
import org.compiere.model.MAllocationHdr;
import org.compiere.model.MBankStatement;
import org.compiere.model.MCash;
import org.compiere.model.MClient;
import org.compiere.model.MConversionRate;
import org.compiere.model.MDocType;
import org.compiere.model.MInOut;
import org.compiere.model.MInventory;
import org.compiere.model.MInvoice;
import org.compiere.model.MJournal;
import org.compiere.model.MMatchInv;
import org.compiere.model.MMatchPO;
import org.compiere.model.MMovement;
import org.compiere.model.MNote;
import org.compiere.model.MOrder;
import org.compiere.model.MPayment;
import org.compiere.model.MPeriod;
import org.compiere.model.MProjectIssue;
import org.compiere.model.MRequisition;
import org.compiere.model.MTable;
import org.compiere.model.ModelValidationEngine;
import org.compiere.model.ModelValidator;
import org.compiere.model.PO;
import org.compiere.model.X_M_Production;
import org.compiere.process.DocumentEngine;
import org.compiere.util.AdempiereUserError;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
@ -122,42 +110,10 @@ import org.compiere.util.Trx;
public abstract class Doc
{
/** AD_Table_ID's of documents */
public static int[] documentsTableID = new int[] {
MInvoice.Table_ID, // C_Invoice
MAllocationHdr.Table_ID, // C_Allocation
MCash.Table_ID, // C_Cash
MBankStatement.Table_ID, // C_BankStatement
MOrder.Table_ID, // C_Order
MPayment.Table_ID, // C_Payment
MInOut.Table_ID, // M_InOut
MInventory.Table_ID, // M_Inventory
MMovement.Table_ID, // M_Movement
X_M_Production.Table_ID, // M_Production
MJournal.Table_ID, // GL_Journal
MMatchInv.Table_ID, // M_MatchInv
MMatchPO.Table_ID, // M_MatchPO
MProjectIssue.Table_ID, // C_ProjectIssue
MRequisition.Table_ID, // M_Requisition
};
private static int[] documentsTableID = null;
/** Table Names of documents */
public static String[] documentsTableName = new String[] {
MInvoice.Table_Name, // C_Invoice
MAllocationHdr.Table_Name, // C_Allocation
MCash.Table_Name, // C_Cash
MBankStatement.Table_Name, // C_BankStatement
MOrder.Table_Name, // C_Order
MPayment.Table_Name, // C_Payment
MInOut.Table_Name, // M_InOut
MInventory.Table_Name, // M_Inventory
MMovement.Table_Name, // M_Movement
X_M_Production.Table_Name, // M_Production
MJournal.Table_Name, // GL_Journal
MMatchInv.Table_Name, // M_MatchInv
MMatchPO.Table_Name, // M_MatchPO
MProjectIssue.Table_Name, // C_ProjectIssue
MRequisition.Table_Name, // M_Requisition
};
private static String[] documentsTableName = null;
/**************************************************************************
* Document Types
@ -251,11 +207,11 @@ public abstract class Doc
public static Doc get (MAcctSchema[] ass, int AD_Table_ID, int Record_ID, String trxName)
{
String TableName = null;
for (int i = 0; i < documentsTableID.length; i++)
for (int i = 0; i < getDocumentsTableID().length; i++)
{
if (documentsTableID[i] == AD_Table_ID)
if (getDocumentsTableID()[i] == AD_Table_ID)
{
TableName = documentsTableName[i];
TableName = getDocumentsTableName()[i];
break;
}
}
@ -303,40 +259,77 @@ public abstract class Doc
* @param rs ResultSet
* @param trxName transaction name
* @return Document
* @throws AdempiereUserError
*/
public static Doc get (MAcctSchema[] ass, int AD_Table_ID, ResultSet rs, String trxName)
public static Doc get (MAcctSchema[] ass, int AD_Table_ID, ResultSet rs, String trxName) throws AdempiereUserError
{
Doc doc = null;
if (AD_Table_ID == MInvoice.Table_ID)
doc = new Doc_Invoice (ass, rs, trxName);
else if (AD_Table_ID == MAllocationHdr.Table_ID)
doc = new Doc_Allocation (ass, rs, trxName);
else if (AD_Table_ID == MCash.Table_ID)
doc = new Doc_Cash (ass, rs, trxName);
else if (AD_Table_ID == MBankStatement.Table_ID)
doc = new Doc_Bank (ass, rs, trxName);
else if (AD_Table_ID == MOrder.Table_ID)
doc = new Doc_Order (ass, rs, trxName);
else if (AD_Table_ID == MPayment.Table_ID)
doc = new Doc_Payment (ass, rs, trxName);
else if (AD_Table_ID == MInOut.Table_ID)
doc = new Doc_InOut (ass, rs, trxName);
else if (AD_Table_ID == MInventory.Table_ID)
doc = new Doc_Inventory (ass, rs, trxName);
else if (AD_Table_ID == MMovement.Table_ID)
doc = new Doc_Movement (ass, rs, trxName);
else if (AD_Table_ID == X_M_Production.Table_ID)
doc = new Doc_Production (ass, rs, trxName);
else if (AD_Table_ID == MJournal.Table_ID)
doc = new Doc_GLJournal (ass, rs, trxName);
else if (AD_Table_ID == MMatchInv.Table_ID)
doc = new Doc_MatchInv (ass, rs, trxName);
else if (AD_Table_ID == MMatchPO.Table_ID)
doc = new Doc_MatchPO (ass, rs, trxName);
else if (AD_Table_ID == MProjectIssue.Table_ID)
doc = new Doc_ProjectIssue (ass, rs, trxName);
else if (AD_Table_ID == MRequisition.Table_ID)
doc = new Doc_Requisition (ass, rs, trxName);
/* Classname of the Doc class follows this convention:
* if the prefix (letters before the first underscore _) is 1 character, then the class is Doc_TableWithoutPrefixWithoutUnderscores
* otherwise Doc_WholeTableWithoutUnderscores
* i.e. following this query
SELECT t.ad_table_id, tablename,
CASE
WHEN instr(tablename, '_') = 2
THEN 'Doc_' || substr(tablename, 3)
WHEN instr(tablename, '_') > 2
THEN 'Doc_' ||
ELSE ''
REPLACE
(
tablename, '_', ''
)
END AS classname
FROM ad_table t, ad_column C
WHERE t.ad_table_id = C.ad_table_id AND
C.columnname = 'Posted' AND
isview = 'N'
ORDER BY 1
* This is:
* 224 GL_Journal Doc_GLJournal
* 259 C_Order Doc_Order
* 318 C_Invoice Doc_Invoice
* 319 M_InOut Doc_InOut
* 321 M_Inventory Doc_Inventory
* 323 M_Movement Doc_Movement
* 325 M_Production Doc_Production
* 335 C_Payment Doc_Payment
* 392 C_BankStatement Doc_BankStatement
* 407 C_Cash Doc_Cash
* 472 M_MatchInv Doc_MatchInv
* 473 M_MatchPO Doc_MatchPO
* 623 C_ProjectIssue Doc_ProjectIssue
* 702 M_Requisition Doc_Requisition
* 735 C_AllocationHdr Doc_AllocationHdr
* 53027 PP_Order Doc_PPOrder
* 53035 PP_Cost_Collector Doc_PPCostCollector
* 53037 DD_Order Doc_DDOrder
* 53092 HR_Process Doc_HRProcess
*/
String tableName = MTable.getTableName(Env.getCtx(), AD_Table_ID);
String packageName = "org.compiere.acct";
String className = null;
int firstUnderscore = tableName.indexOf("_");
if (firstUnderscore == 1)
className = packageName + ".Doc_" + tableName.substring(2).replaceAll("_", "");
else
className = packageName + ".Doc_" + tableName.replaceAll("_", "");
try
{
Class<?> cClass = Class.forName(className);
Constructor<?> cnstr = cClass.getConstructor(new Class[] {MAcctSchema[].class, ResultSet.class, String.class});
doc = (Doc) cnstr.newInstance(ass, rs, trxName);
}
catch (Exception e)
{
s_log.log(Level.SEVERE, "Doc Class invalid: " + className + " (" + e.toString() + ")");
throw new AdempiereUserError("Doc Class invalid: " + className + " (" + e.toString() + ")");
}
if (doc == null)
s_log.log(Level.SEVERE, "Unknown AD_Table_ID=" + AD_Table_ID);
return doc;
@ -2242,4 +2235,59 @@ public abstract class Doc
return m_fact;
}
/*
* Array of tables with Post column
*/
public static int[] getDocumentsTableID() {
fillDocumentsTableArrays();
return documentsTableID;
}
public static String[] getDocumentsTableName() {
fillDocumentsTableArrays();
return documentsTableName;
}
private static void fillDocumentsTableArrays() {
if (documentsTableID == null) {
String sql = "SELECT t.AD_Table_ID, t.TableName " +
"FROM AD_Table t, AD_Column c " +
"WHERE t.AD_Table_ID=c.AD_Table_ID AND " +
"c.ColumnName='Posted' AND " +
"IsView='N' " +
"ORDER BY t.AD_Table_ID";
ArrayList<Integer> tableIDs = new ArrayList<Integer>();
ArrayList<String> tableNames = new ArrayList<String>();
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement(sql, null);
rs = pstmt.executeQuery();
while (rs.next())
{
tableIDs.add(rs.getInt(1));
tableNames.add(rs.getString(2));
}
}
catch (SQLException e)
{
throw new DBException(e, sql);
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
// Convert to array
documentsTableID = new int[tableIDs.size()];
documentsTableName = new String[tableIDs.size()];
for (int i = 0; i < documentsTableID.length; i++)
{
documentsTableID[i] = tableIDs.get(i);
documentsTableName[i] = tableNames.get(i);
}
}
}
} // Doc

View File

@ -63,7 +63,7 @@ public class Doc_Allocation extends Doc
* @param rs record
* @param trxName trx
*/
protected Doc_Allocation (MAcctSchema[] ass, ResultSet rs, String trxName)
public Doc_Allocation (MAcctSchema[] ass, ResultSet rs, String trxName)
{
super (ass, MAllocationHdr.class, rs, DOCTYPE_Allocation, trxName);
} // Doc_Allocation

View File

@ -52,7 +52,7 @@ public class Doc_Bank extends Doc
* @param rs record
* @param trxName trx
*/
protected Doc_Bank (MAcctSchema[] ass, ResultSet rs, String trxName)
public Doc_Bank (MAcctSchema[] ass, ResultSet rs, String trxName)
{
super (ass, MBankStatement.class, rs, DOCTYPE_BankStatement, trxName);
} // Doc_Bank

View File

@ -44,7 +44,7 @@ public class Doc_Cash extends Doc
* @param rs record
* @param trxName trx
*/
protected Doc_Cash (MAcctSchema[] ass, ResultSet rs, String trxName)
public Doc_Cash (MAcctSchema[] ass, ResultSet rs, String trxName)
{
super(ass, MCash.class, rs, DOCTYPE_CashJournal, trxName);
} // Doc_Cash

View File

@ -44,7 +44,7 @@ public class Doc_GLJournal extends Doc
* @param rs record
* @param trxName trx
*/
protected Doc_GLJournal (MAcctSchema[] ass, ResultSet rs, String trxName)
public Doc_GLJournal (MAcctSchema[] ass, ResultSet rs, String trxName)
{
super(ass, MJournal.class, rs, null, trxName);
} // Doc_GL_Journal

View File

@ -57,7 +57,7 @@ public class Doc_Invoice extends Doc
* @param rs record
* @param trxName trx
*/
protected Doc_Invoice(MAcctSchema[] ass, ResultSet rs, String trxName)
public Doc_Invoice(MAcctSchema[] ass, ResultSet rs, String trxName)
{
super (ass, MInvoice.class, rs, null, trxName);
} // Doc_Invoice

View File

@ -55,7 +55,7 @@ public class Doc_MatchInv extends Doc
* @param rs record
* @param trxName trx
*/
protected Doc_MatchInv (MAcctSchema[] ass, ResultSet rs, String trxName)
public Doc_MatchInv (MAcctSchema[] ass, ResultSet rs, String trxName)
{
super(ass, MMatchInv.class, rs, DOCTYPE_MatMatchInv, trxName);
} // Doc_MatchInv

View File

@ -54,7 +54,7 @@ public class Doc_MatchPO extends Doc
* @param rs record
* @param trxName trx
*/
protected Doc_MatchPO (MAcctSchema[] ass, ResultSet rs, String trxName)
public Doc_MatchPO (MAcctSchema[] ass, ResultSet rs, String trxName)
{
super(ass, MMatchPO.class, rs, DOCTYPE_MatMatchPO, trxName);
} // Doc_MatchPO

View File

@ -53,7 +53,7 @@ public class Doc_Order extends Doc
* @param rs record
* @param trxName trx
*/
protected Doc_Order (MAcctSchema[] ass, ResultSet rs, String trxName)
public Doc_Order (MAcctSchema[] ass, ResultSet rs, String trxName)
{
super (ass, MOrder.class, rs, null, trxName);
} // Doc_Order

View File

@ -45,7 +45,7 @@ public class Doc_Payment extends Doc
* @param rs record
* @param trxName trx
*/
protected Doc_Payment (MAcctSchema[] ass, ResultSet rs, String trxName)
public Doc_Payment (MAcctSchema[] ass, ResultSet rs, String trxName)
{
super (ass, MPayment.class, rs, null, trxName);
} // Doc_Payment

View File

@ -47,7 +47,7 @@ public class Doc_Requisition extends Doc
* @param rs record
* @param trxName trx
*/
protected Doc_Requisition (MAcctSchema[] ass, ResultSet rs, String trxName)
public Doc_Requisition (MAcctSchema[] ass, ResultSet rs, String trxName)
{
super (ass, MRequisition.class, rs, DOCTYPE_PurchaseRequisition, trxName);
} // Doc_Requisition

View File

@ -794,7 +794,11 @@ public class MWFActivity extends X_AD_WF_Activity implements Runnable
//end vpj-cd e-evolution 03/08/2005 PostgreSQL
if (m_postImmediate != null)
try {
postImmediate();
} catch (Exception e) {
log.warning("Error posting document: " + e.toString());
} // ignore any error in this posting
}
catch (Exception e)
{

View File

@ -88,10 +88,10 @@ public class AcctProcessor extends AdempiereServer
*/
private void postSession()
{
for (int i = 0; i < Doc.documentsTableID.length; i++)
for (int i = 0; i < Doc.getDocumentsTableID().length; i++)
{
int AD_Table_ID = Doc.documentsTableID[i];
String TableName = Doc.documentsTableName[i];
int AD_Table_ID = Doc.getDocumentsTableID()[i];
String TableName = Doc.getDocumentsTableName()[i];
// Post only special documents
if (m_model.getAD_Table_ID() != 0
&& m_model.getAD_Table_ID() != AD_Table_ID)