IDEMPIERE-2354 Model Validator registered multiple times
This commit is contained in:
parent
a297d16c4f
commit
42d684fa99
|
@ -25,10 +25,13 @@
|
||||||
* *
|
* *
|
||||||
* Sponsors: *
|
* Sponsors: *
|
||||||
* - e-Evolution (http://www.e-evolution.com) *
|
* - e-Evolution (http://www.e-evolution.com) *
|
||||||
***********************************************************************/
|
**********************************************************************/
|
||||||
|
|
||||||
package org.adempiere.model;
|
package org.adempiere.model;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Hashtable;
|
||||||
|
import java.util.List;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import org.adempiere.process.rpl.exp.ExportHelper;
|
import org.adempiere.process.rpl.exp.ExportHelper;
|
||||||
|
@ -39,6 +42,7 @@ import org.compiere.model.MTable;
|
||||||
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.Query;
|
||||||
import org.compiere.model.X_AD_ReplicationDocument;
|
import org.compiere.model.X_AD_ReplicationDocument;
|
||||||
import org.compiere.model.X_AD_ReplicationTable;
|
import org.compiere.model.X_AD_ReplicationTable;
|
||||||
import org.compiere.util.CLogger;
|
import org.compiere.util.CLogger;
|
||||||
|
@ -61,151 +65,186 @@ import org.compiere.util.Env;
|
||||||
*
|
*
|
||||||
* @version $Id$
|
* @version $Id$
|
||||||
*/
|
*/
|
||||||
public class ExportModelValidator implements ModelValidator
|
public class ExportModelValidator implements ModelValidator {
|
||||||
{
|
/** Context variable which says if replication is enabled */
|
||||||
/** Context variable which says if replication is enabled */
|
|
||||||
public static final String CTX_IsReplicationEnabled = "#IsReplicationEnabled";
|
public static final String CTX_IsReplicationEnabled = "#IsReplicationEnabled";
|
||||||
|
|
||||||
/** Logger */
|
/** Logger */
|
||||||
private static CLogger log = CLogger.getCLogger(ExportModelValidator.class);
|
private static CLogger log = CLogger.getCLogger(ExportModelValidator.class);
|
||||||
|
|
||||||
/** Client */
|
/** Client */
|
||||||
private int m_AD_Client_ID = -1;
|
private int m_AD_Client_ID = -1;
|
||||||
|
|
||||||
/** Organization */
|
|
||||||
private int m_AD_Org_ID = -1;
|
|
||||||
|
|
||||||
/** Role */
|
|
||||||
private int m_AD_Role_ID = -1;
|
|
||||||
|
|
||||||
/** User */
|
|
||||||
private int m_AD_User_ID = -1;
|
|
||||||
|
|
||||||
/** Replication Strategy **/
|
|
||||||
private int m_AD_ReplicationStrategy_ID = -1;
|
|
||||||
|
|
||||||
/** ModelValidationEngine engine **/
|
/** ModelValidationEngine engine **/
|
||||||
ModelValidationEngine m_engine = null;
|
List<String> modelTables = new ArrayList<String>();
|
||||||
|
List<String> docTables = new ArrayList<String>();
|
||||||
/** Export Helper */
|
|
||||||
ExportHelper expHelper = null;
|
/* The Export Helpers per each replication strategy */
|
||||||
|
Hashtable<Integer, ExportHelper> helpers = new Hashtable<Integer, ExportHelper>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
* The class is instantiated when logging in and client is selected/known
|
* The class is instantiated when logging in and client is selected/known
|
||||||
*/
|
*/
|
||||||
public ExportModelValidator ()
|
public ExportModelValidator () {
|
||||||
{
|
|
||||||
super ();
|
super ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize Validation
|
* Initialize Validation
|
||||||
* @param engine validation engine
|
* @param engine validation engine
|
||||||
* @param client client
|
* @param client client
|
||||||
*/
|
*/
|
||||||
public void initialize (ModelValidationEngine engine, MClient client)
|
public void initialize (ModelValidationEngine engine, MClient client) {
|
||||||
{
|
if (client != null) {
|
||||||
m_engine = engine;
|
|
||||||
if (client != null)
|
|
||||||
{
|
|
||||||
m_AD_Client_ID = client.getAD_Client_ID();
|
m_AD_Client_ID = client.getAD_Client_ID();
|
||||||
if (log.isLoggable(Level.INFO)) log.info(client.toString());
|
if (log.isLoggable(Level.INFO)) log.info(client.toString());
|
||||||
}
|
loadReplicationStrategy(engine);
|
||||||
else
|
} else {
|
||||||
{
|
log.warning("Export Model Validator cannot be used as a global validator, it needs to be defined in a per-client (tenant) basis");
|
||||||
log.warning("Export Model Validator cannot be used as a global validator, it needs to be defined in a per-client (tenant) basis");
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public void loadReplicationStrategy(ModelValidationEngine engine) {
|
||||||
* Model Change of a monitored Table.
|
MClient client = MClient.get(Env.getCtx(), m_AD_Client_ID);
|
||||||
* Called after PO.beforeSave/PO.beforeDelete
|
String where = "AD_ReplicationStrategy_ID IN (" +
|
||||||
* @param po persistent object
|
"SELECT AD_ReplicationStrategy_ID FROM AD_Client WHERE AD_Client_ID=? " +
|
||||||
* @param type TYPE_
|
"UNION " +
|
||||||
* @return error message or null
|
"SELECT AD_ReplicationStrategy_ID FROM AD_Org WHERE AD_Client_ID=?)";
|
||||||
* @exception Exception if the recipient wishes the change to be not accept.
|
List<MReplicationStrategy> rss = new Query(Env.getCtx(), MReplicationStrategy.Table_Name, where, null)
|
||||||
*/
|
.setOnlyActiveRecords(true)
|
||||||
public String modelChange (PO po, int type) throws Exception
|
.setParameters(m_AD_Client_ID, m_AD_Client_ID)
|
||||||
{
|
.list();
|
||||||
//String Mode = "Table";
|
for (MReplicationStrategy rplStrategy : rss) {
|
||||||
if (log.isLoggable(Level.INFO)) log.info("po.get_TableName() = " + po.get_TableName());
|
ExportHelper expClientHelper = new ExportHelper(client, rplStrategy);
|
||||||
if (expHelper != null) {
|
helpers.put(rplStrategy.getAD_ReplicationStrategy_ID(), expClientHelper);
|
||||||
if ( type == TYPE_AFTER_CHANGE
|
// Add Tables
|
||||||
|| type == TYPE_AFTER_NEW
|
// We want to be informed when records in Replication tables are created/updated/deleted!
|
||||||
|| type == TYPE_BEFORE_DELETE) // After Change or After New
|
for (X_AD_ReplicationTable rplTable : rplStrategy.getReplicationTables()) {
|
||||||
{
|
if ( X_AD_ReplicationTable.REPLICATIONTYPE_Merge.equals(rplTable.getReplicationType())
|
||||||
X_AD_ReplicationTable replicationTable = MReplicationStrategy.getReplicationTable(
|
|| X_AD_ReplicationTable.REPLICATIONTYPE_Broadcast.equals(rplTable.getReplicationType())
|
||||||
po.getCtx(), m_AD_ReplicationStrategy_ID, po.get_Table_ID());
|
|| X_AD_ReplicationTable.REPLICATIONTYPE_Reference.equals(rplTable.getReplicationType()))
|
||||||
if (replicationTable != null)
|
{
|
||||||
{
|
String tableName = MTable.getTableName(client.getCtx(), rplTable.getAD_Table_ID());
|
||||||
expHelper.exportRecord(
|
if (! modelTables.contains(tableName)) {
|
||||||
po,
|
engine.addModelChange(tableName, this);
|
||||||
MReplicationStrategy.REPLICATION_TABLE,
|
modelTables.add(tableName);
|
||||||
replicationTable.getReplicationType(),
|
}
|
||||||
type);
|
}
|
||||||
}
|
}
|
||||||
}
|
// Add Documents
|
||||||
|
// We want to be informed when Replication documents are created/updated/deleted!
|
||||||
|
for (X_AD_ReplicationDocument rplDocument : rplStrategy.getReplicationDocuments()) {
|
||||||
|
if ( X_AD_ReplicationDocument.REPLICATIONTYPE_Merge.equals(rplDocument.getReplicationType())
|
||||||
|
|| X_AD_ReplicationDocument.REPLICATIONTYPE_Reference.equals(rplDocument.getReplicationType()))
|
||||||
|
{
|
||||||
|
String tableName = MTable.getTableName(client.getCtx(), rplDocument.getAD_Table_ID());
|
||||||
|
if (! docTables.contains(tableName)) {
|
||||||
|
engine.addDocValidate(tableName, this);
|
||||||
|
docTables.add(tableName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Model Change of a monitored Table.
|
||||||
|
* Called after PO.beforeSave/PO.beforeDelete
|
||||||
|
* @param po persistent object
|
||||||
|
* @param type TYPE_
|
||||||
|
* @return error message or null
|
||||||
|
* @exception Exception if the recipient wishes the change to be not accept.
|
||||||
|
*/
|
||||||
|
public String modelChange (PO po, int type) throws Exception {
|
||||||
|
if (log.isLoggable(Level.INFO)) log.info("po.get_TableName() = " + po.get_TableName());
|
||||||
|
int rsID = getReplicationStrategy(po);
|
||||||
|
if (rsID > 0) {
|
||||||
|
ExportHelper expHelper = helpers.get(rsID);
|
||||||
|
if (expHelper != null) {
|
||||||
|
if ( type == TYPE_AFTER_CHANGE
|
||||||
|
|| type == TYPE_AFTER_NEW
|
||||||
|
|| type == TYPE_BEFORE_DELETE) {
|
||||||
|
X_AD_ReplicationTable replicationTable = MReplicationStrategy.getReplicationTable(
|
||||||
|
po.getCtx(), rsID, po.get_Table_ID());
|
||||||
|
if (replicationTable != null) {
|
||||||
|
expHelper.exportRecord(
|
||||||
|
po,
|
||||||
|
MReplicationStrategy.REPLICATION_TABLE,
|
||||||
|
replicationTable.getReplicationType(),
|
||||||
|
type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate Document.
|
* Validate Document.
|
||||||
* Called as first step of DocAction.prepareIt
|
* Called as first step of DocAction.prepareIt
|
||||||
* when you called addDocValidate for the table.
|
* when you called addDocValidate for the table.
|
||||||
* @param po persistent object
|
* @param po persistent object
|
||||||
* @param type see TIMING_ constants
|
* @param type see TIMING_ constants
|
||||||
* @return error message or null
|
* @return error message or null
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public String docValidate (PO po, int type)
|
public String docValidate (PO po, int type) {
|
||||||
{
|
|
||||||
if (log.isLoggable(Level.INFO)) log.info("Replicate the Document = " + po.get_TableName() + " with Type = " + type);
|
if (log.isLoggable(Level.INFO)) log.info("Replicate the Document = " + po.get_TableName() + " with Type = " + type);
|
||||||
String result = null;
|
String result = null;
|
||||||
if (expHelper != null) {
|
int rsID = getReplicationStrategy(po);
|
||||||
try {
|
if (rsID > 0) {
|
||||||
if ( type == TIMING_AFTER_COMPLETE
|
ExportHelper expHelper = helpers.get(rsID);
|
||||||
|| type == TIMING_AFTER_CLOSE
|
if (expHelper != null) {
|
||||||
|| type == TIMING_AFTER_REVERSECORRECT
|
try {
|
||||||
|| type == TIMING_AFTER_VOID
|
if ( type == TIMING_AFTER_COMPLETE
|
||||||
|| type == TIMING_AFTER_REACTIVATE
|
|| type == TIMING_AFTER_CLOSE
|
||||||
//|| type == TIMING_AFTER_PREPARE
|
|| type == TIMING_AFTER_REVERSECORRECT
|
||||||
)
|
|| type == TIMING_AFTER_VOID
|
||||||
{
|
|| type == TIMING_AFTER_REACTIVATE
|
||||||
X_AD_ReplicationDocument replicationDocument = null;
|
//|| type == TIMING_AFTER_PREPARE
|
||||||
int C_DocType_ID = po.get_ValueAsInt("C_DocType_ID");
|
) {
|
||||||
if (C_DocType_ID > 0)
|
X_AD_ReplicationDocument replicationDocument = null;
|
||||||
{
|
int C_DocType_ID = po.get_ValueAsInt("C_DocType_ID");
|
||||||
replicationDocument = MReplicationStrategy.getReplicationDocument(
|
if (C_DocType_ID > 0) {
|
||||||
po.getCtx(), m_AD_ReplicationStrategy_ID, po.get_Table_ID(), C_DocType_ID);
|
replicationDocument = MReplicationStrategy.getReplicationDocument(
|
||||||
}
|
po.getCtx(), rsID, po.get_Table_ID(), C_DocType_ID);
|
||||||
else
|
} else {
|
||||||
{
|
replicationDocument = MReplicationStrategy.getReplicationDocument(
|
||||||
replicationDocument = MReplicationStrategy.getReplicationDocument(
|
po.getCtx(), rsID, po.get_Table_ID());
|
||||||
po.getCtx(), m_AD_ReplicationStrategy_ID, po.get_Table_ID());
|
}
|
||||||
}
|
|
||||||
|
if (replicationDocument != null) {
|
||||||
|
expHelper.exportRecord(
|
||||||
if (replicationDocument != null) {
|
po,
|
||||||
expHelper.exportRecord(
|
MReplicationStrategy.REPLICATION_DOCUMENT,
|
||||||
po,
|
replicationDocument.getReplicationType(),
|
||||||
MReplicationStrategy.REPLICATION_DOCUMENT,
|
type);
|
||||||
replicationDocument.getReplicationType(),
|
}
|
||||||
type);
|
}
|
||||||
}
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
result = e.toString();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
result = e.toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int getReplicationStrategy(PO po) {
|
||||||
|
int rsID = -1;
|
||||||
|
int orgID = po.getAD_Org_ID();
|
||||||
|
if (orgID > 0) {
|
||||||
|
rsID = MOrg.get(po.getCtx(), orgID).getAD_ReplicationStrategy_ID();
|
||||||
|
}
|
||||||
|
if (rsID <= 0) {
|
||||||
|
int clientID = po.getAD_Client_ID();
|
||||||
|
rsID = MClient.get(Env.getCtx(), clientID).getAD_ReplicationStrategy_ID();
|
||||||
|
}
|
||||||
|
return rsID;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User Login.
|
* User Login.
|
||||||
* Called when preferences are set
|
* Called when preferences are set
|
||||||
|
@ -214,91 +253,26 @@ public class ExportModelValidator implements ModelValidator
|
||||||
* @param AD_User_ID user
|
* @param AD_User_ID user
|
||||||
* @return error message or null
|
* @return error message or null
|
||||||
*/
|
*/
|
||||||
public String login (int AD_Org_ID, int AD_Role_ID, int AD_User_ID)
|
public String login (int AD_Org_ID, int AD_Role_ID, int AD_User_ID) {
|
||||||
{
|
Env.setContext(Env.getCtx(), CTX_IsReplicationEnabled, true);
|
||||||
Env.setContext(Env.getCtx(), CTX_IsReplicationEnabled, true);
|
|
||||||
m_AD_Org_ID = AD_Org_ID;
|
|
||||||
m_AD_Role_ID = AD_Role_ID;
|
|
||||||
m_AD_User_ID = AD_User_ID;
|
|
||||||
|
|
||||||
if (log.isLoggable(Level.INFO)){
|
|
||||||
log.info("AD_Org_ID =" + m_AD_Org_ID);
|
|
||||||
log.info("AD_Role_ID =" + m_AD_Role_ID);
|
|
||||||
log.info("AD_User_ID =" + m_AD_User_ID);}
|
|
||||||
loadReplicationStrategy();
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Client to be monitored
|
* Get Client to be monitored
|
||||||
* @return AD_Client_ID client
|
* @return AD_Client_ID client
|
||||||
*/
|
*/
|
||||||
public int getAD_Client_ID()
|
public int getAD_Client_ID() {
|
||||||
{
|
|
||||||
return m_AD_Client_ID;
|
return m_AD_Client_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadReplicationStrategy()
|
|
||||||
{
|
|
||||||
MClient client = MClient.get(Env.getCtx(), m_AD_Client_ID);
|
|
||||||
MReplicationStrategy rplStrategy = null;
|
|
||||||
|
|
||||||
m_AD_ReplicationStrategy_ID = MOrg.get(client.getCtx(),m_AD_Org_ID).getAD_ReplicationStrategy_ID();
|
|
||||||
|
|
||||||
if(m_AD_ReplicationStrategy_ID <= 0)
|
|
||||||
{
|
|
||||||
m_AD_ReplicationStrategy_ID = client.getAD_ReplicationStrategy_ID();
|
|
||||||
if (log.isLoggable(Level.INFO)) log.info("client.getAD_ReplicationStrategy_ID() = " + m_AD_ReplicationStrategy_ID);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_AD_ReplicationStrategy_ID > 0) {
|
|
||||||
rplStrategy = new MReplicationStrategy(client.getCtx(), m_AD_ReplicationStrategy_ID, null);
|
|
||||||
if(!rplStrategy.isActive())
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
expHelper = new ExportHelper(client, rplStrategy);
|
|
||||||
}
|
|
||||||
// Add Tables
|
|
||||||
// We want to be informed when records in Replication tables are created/updated/deleted!
|
|
||||||
//engine.addModelChange(MBPartner.Table_Name, this);
|
|
||||||
//engine.addModelChange(MOrder.Table_Name, this);
|
|
||||||
//engine.addModelChange(MOrderLine.Table_Name, this);
|
|
||||||
if (rplStrategy != null) {
|
|
||||||
|
|
||||||
for (X_AD_ReplicationTable rplTable : rplStrategy.getReplicationTables()) {
|
|
||||||
if (X_AD_ReplicationTable.REPLICATIONTYPE_Merge.equals(rplTable.getReplicationType())
|
|
||||||
|| X_AD_ReplicationTable.REPLICATIONTYPE_Broadcast.equals(rplTable.getReplicationType())
|
|
||||||
|| X_AD_ReplicationTable.REPLICATIONTYPE_Reference.equals(rplTable.getReplicationType()))
|
|
||||||
{
|
|
||||||
String tableName = MTable.getTableName(client.getCtx(), rplTable.getAD_Table_ID());
|
|
||||||
m_engine.addModelChange(tableName, this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Add Documents
|
|
||||||
// We want to be informed when Replication documents are created/updated/deleted!
|
|
||||||
if (rplStrategy != null) {
|
|
||||||
for (X_AD_ReplicationDocument rplDocument : rplStrategy.getReplicationDocuments()) {
|
|
||||||
if (X_AD_ReplicationDocument.REPLICATIONTYPE_Merge.equals(rplDocument.getReplicationType())
|
|
||||||
|| X_AD_ReplicationDocument.REPLICATIONTYPE_Reference.equals(rplDocument.getReplicationType()))
|
|
||||||
{
|
|
||||||
String tableName = MTable.getTableName(client.getCtx(), rplDocument.getAD_Table_ID());
|
|
||||||
m_engine.addDocValidate(tableName, this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* String Representation
|
* String Representation
|
||||||
* @return info
|
* @return info
|
||||||
*/
|
*/
|
||||||
public String toString ()
|
public String toString () {
|
||||||
{
|
|
||||||
StringBuilder sb = new StringBuilder (ExportModelValidator.class.getName());
|
StringBuilder sb = new StringBuilder (ExportModelValidator.class.getName());
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue