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() 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]; int AD_Table_ID = Doc.getDocumentsTableID()[i];
String TableName = Doc.documentsTableName[i]; String TableName = Doc.getDocumentsTableName()[i];
// Post only special documents // Post only special documents
if (p_AD_Table_ID != 0 if (p_AD_Table_ID != 0
&& p_AD_Table_ID != AD_Table_ID) && p_AD_Table_ID != AD_Table_ID)

View File

@ -28,32 +28,20 @@ import java.util.Iterator;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level; import java.util.logging.Level;
import org.adempiere.exceptions.DBException;
import org.compiere.model.MAccount; import org.compiere.model.MAccount;
import org.compiere.model.MAcctSchema; 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.MClient;
import org.compiere.model.MConversionRate; import org.compiere.model.MConversionRate;
import org.compiere.model.MDocType; 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.MNote;
import org.compiere.model.MOrder;
import org.compiere.model.MPayment;
import org.compiere.model.MPeriod; import org.compiere.model.MPeriod;
import org.compiere.model.MProjectIssue; import org.compiere.model.MTable;
import org.compiere.model.MRequisition;
import org.compiere.model.ModelValidationEngine; import org.compiere.model.ModelValidationEngine;
import org.compiere.model.ModelValidator; import org.compiere.model.ModelValidator;
import org.compiere.model.PO; import org.compiere.model.PO;
import org.compiere.model.X_M_Production;
import org.compiere.process.DocumentEngine; import org.compiere.process.DocumentEngine;
import org.compiere.util.AdempiereUserError;
import org.compiere.util.CLogger; import org.compiere.util.CLogger;
import org.compiere.util.DB; import org.compiere.util.DB;
import org.compiere.util.Env; import org.compiere.util.Env;
@ -122,42 +110,10 @@ import org.compiere.util.Trx;
public abstract class Doc public abstract class Doc
{ {
/** AD_Table_ID's of documents */ /** AD_Table_ID's of documents */
public static int[] documentsTableID = new int[] { private static int[] documentsTableID = null;
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
};
/** Table Names of documents */ /** Table Names of documents */
public static String[] documentsTableName = new String[] { private static String[] documentsTableName = null;
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
};
/************************************************************************** /**************************************************************************
* Document Types * 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) public static Doc get (MAcctSchema[] ass, int AD_Table_ID, int Record_ID, String trxName)
{ {
String TableName = null; 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; break;
} }
} }
@ -303,40 +259,77 @@ public abstract class Doc
* @param rs ResultSet * @param rs ResultSet
* @param trxName transaction name * @param trxName transaction name
* @return Document * @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; Doc doc = null;
if (AD_Table_ID == MInvoice.Table_ID)
doc = new Doc_Invoice (ass, rs, trxName); /* Classname of the Doc class follows this convention:
else if (AD_Table_ID == MAllocationHdr.Table_ID) * if the prefix (letters before the first underscore _) is 1 character, then the class is Doc_TableWithoutPrefixWithoutUnderscores
doc = new Doc_Allocation (ass, rs, trxName); * otherwise Doc_WholeTableWithoutUnderscores
else if (AD_Table_ID == MCash.Table_ID) * i.e. following this query
doc = new Doc_Cash (ass, rs, trxName); SELECT t.ad_table_id, tablename,
else if (AD_Table_ID == MBankStatement.Table_ID) CASE
doc = new Doc_Bank (ass, rs, trxName); WHEN instr(tablename, '_') = 2
else if (AD_Table_ID == MOrder.Table_ID) THEN 'Doc_' || substr(tablename, 3)
doc = new Doc_Order (ass, rs, trxName); WHEN instr(tablename, '_') > 2
else if (AD_Table_ID == MPayment.Table_ID) THEN 'Doc_' ||
doc = new Doc_Payment (ass, rs, trxName); ELSE ''
else if (AD_Table_ID == MInOut.Table_ID) REPLACE
doc = new Doc_InOut (ass, rs, trxName); (
else if (AD_Table_ID == MInventory.Table_ID) tablename, '_', ''
doc = new Doc_Inventory (ass, rs, trxName); )
else if (AD_Table_ID == MMovement.Table_ID) END AS classname
doc = new Doc_Movement (ass, rs, trxName); FROM ad_table t, ad_column C
else if (AD_Table_ID == X_M_Production.Table_ID) WHERE t.ad_table_id = C.ad_table_id AND
doc = new Doc_Production (ass, rs, trxName); C.columnname = 'Posted' AND
else if (AD_Table_ID == MJournal.Table_ID) isview = 'N'
doc = new Doc_GLJournal (ass, rs, trxName); ORDER BY 1
else if (AD_Table_ID == MMatchInv.Table_ID) * This is:
doc = new Doc_MatchInv (ass, rs, trxName); * 224 GL_Journal Doc_GLJournal
else if (AD_Table_ID == MMatchPO.Table_ID) * 259 C_Order Doc_Order
doc = new Doc_MatchPO (ass, rs, trxName); * 318 C_Invoice Doc_Invoice
else if (AD_Table_ID == MProjectIssue.Table_ID) * 319 M_InOut Doc_InOut
doc = new Doc_ProjectIssue (ass, rs, trxName); * 321 M_Inventory Doc_Inventory
else if (AD_Table_ID == MRequisition.Table_ID) * 323 M_Movement Doc_Movement
doc = new Doc_Requisition (ass, rs, trxName); * 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) if (doc == null)
s_log.log(Level.SEVERE, "Unknown AD_Table_ID=" + AD_Table_ID); s_log.log(Level.SEVERE, "Unknown AD_Table_ID=" + AD_Table_ID);
return doc; return doc;
@ -2241,5 +2234,60 @@ public abstract class Doc
public ArrayList<Fact> getFacts() { public ArrayList<Fact> getFacts() {
return m_fact; 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 } // Doc

View File

@ -63,7 +63,7 @@ public class Doc_Allocation extends Doc
* @param rs record * @param rs record
* @param trxName trx * @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); super (ass, MAllocationHdr.class, rs, DOCTYPE_Allocation, trxName);
} // Doc_Allocation } // Doc_Allocation

View File

@ -52,7 +52,7 @@ public class Doc_Bank extends Doc
* @param rs record * @param rs record
* @param trxName trx * @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); super (ass, MBankStatement.class, rs, DOCTYPE_BankStatement, trxName);
} // Doc_Bank } // Doc_Bank

View File

@ -44,7 +44,7 @@ public class Doc_Cash extends Doc
* @param rs record * @param rs record
* @param trxName trx * @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); super(ass, MCash.class, rs, DOCTYPE_CashJournal, trxName);
} // Doc_Cash } // Doc_Cash

View File

@ -44,7 +44,7 @@ public class Doc_GLJournal extends Doc
* @param rs record * @param rs record
* @param trxName trx * @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); super(ass, MJournal.class, rs, null, trxName);
} // Doc_GL_Journal } // Doc_GL_Journal

View File

@ -57,7 +57,7 @@ public class Doc_Invoice extends Doc
* @param rs record * @param rs record
* @param trxName trx * @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); super (ass, MInvoice.class, rs, null, trxName);
} // Doc_Invoice } // Doc_Invoice

View File

@ -55,7 +55,7 @@ public class Doc_MatchInv extends Doc
* @param rs record * @param rs record
* @param trxName trx * @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); super(ass, MMatchInv.class, rs, DOCTYPE_MatMatchInv, trxName);
} // Doc_MatchInv } // Doc_MatchInv

View File

@ -54,7 +54,7 @@ public class Doc_MatchPO extends Doc
* @param rs record * @param rs record
* @param trxName trx * @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); super(ass, MMatchPO.class, rs, DOCTYPE_MatMatchPO, trxName);
} // Doc_MatchPO } // Doc_MatchPO

View File

@ -53,7 +53,7 @@ public class Doc_Order extends Doc
* @param rs record * @param rs record
* @param trxName trx * @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); super (ass, MOrder.class, rs, null, trxName);
} // Doc_Order } // Doc_Order

View File

@ -45,7 +45,7 @@ public class Doc_Payment extends Doc
* @param rs record * @param rs record
* @param trxName trx * @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); super (ass, MPayment.class, rs, null, trxName);
} // Doc_Payment } // Doc_Payment

View File

@ -47,7 +47,7 @@ public class Doc_Requisition extends Doc
* @param rs record * @param rs record
* @param trxName trx * @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); super (ass, MRequisition.class, rs, DOCTYPE_PurchaseRequisition, trxName);
} // Doc_Requisition } // 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 //end vpj-cd e-evolution 03/08/2005 PostgreSQL
if (m_postImmediate != null) if (m_postImmediate != null)
postImmediate(); try {
postImmediate();
} catch (Exception e) {
log.warning("Error posting document: " + e.toString());
} // ignore any error in this posting
} }
catch (Exception e) catch (Exception e)
{ {

View File

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