Search in sources :

Example 1 with ExportHelper

use of org.adempiere.process.rpl.exp.ExportHelper in project adempiere by adempiere.

the class ImportHelper method importXMLDocument.

/**
	 * Import XML Document
	 * @param result
	 * @param documentToBeImported
	 * @param trxName
	 * @throws Exception
	 * @throws SQLException
	 * @throws XPathExpressionException
	 */
public void importXMLDocument(StringBuffer result, Document documentToBeImported, String trxName) throws Exception, SQLException, XPathExpressionException {
    Element rootElement = documentToBeImported.getDocumentElement();
    // Find which Export format to Load...
    String AD_Client_Value = null;
    AD_Client_Value = rootElement.getAttribute("AD_Client_Value");
    log.info("AD_Client_Value = " + AD_Client_Value);
    if (AD_Client_Value == null || Util.isEmpty(AD_Client_Value)) {
        throw new Exception(Msg.getMsg(ctx, "XMLClientValueMandatory"));
    }
    String version = null;
    version = rootElement.getAttribute("Version");
    log.info("Version = " + version);
    if (version == null || Util.isEmpty(version)) {
        throw new Exception(Msg.getMsg(ctx, "XMLVersionAttributeMandatory"));
    }
    ///Getting Attributes.
    int ReplicationMode = new Integer(rootElement.getAttribute("ReplicationMode"));
    String ReplicationType = rootElement.getAttribute("ReplicationType");
    int ReplicationEvent = new Integer(rootElement.getAttribute("ReplicationEvent"));
    MClient client = null;
    client = getAD_ClientByValue(ctx, AD_Client_Value, trxName);
    if (client == null) {
        throw new Exception(Msg.getMsg(ctx, "XMLClientNotFound"));
    }
    log.info("XML ROOT AD_Client = " + client.toString());
    String EXP_Format_Value = null;
    EXP_Format_Value = rootElement.getNodeName();
    log.info("EXP_Format_Value = " + EXP_Format_Value);
    MEXPFormat expFormat = null;
    expFormat = MEXPFormat.getFormatByValueAD_Client_IDAndVersion(ctx, EXP_Format_Value, client.getAD_Client_ID(), version, trxName);
    if (expFormat == null || expFormat.getEXP_Format_ID() == 0) {
        // Fall back to SYSTEM Client.
        // Try to search Export format defined for SYSTEM Client!!!
        MClient systemClient = null;
        systemClient = MClient.get(ctx, 0);
        if (systemClient == null) {
            throw new Exception(Msg.getMsg(ctx, "XMLClientNotFound"));
        }
        log.info("SYSTEM Client = " + systemClient.toString());
        expFormat = MEXPFormat.getFormatByValueAD_Client_IDAndVersion(ctx, EXP_Format_Value, systemClient.getAD_Client_ID(), version, trxName);
    }
    if (expFormat == null || expFormat.getEXP_Format_ID() == 0) {
        throw new Exception(Msg.getMsg(ctx, "EXPFormatNotFound"));
    }
    log.info("expFormat = " + expFormat.toString());
    isChanged = false;
    PO po = importElement(ctx, result, rootElement, expFormat, ReplicationType, trxName);
    if (ModelValidator.TYPE_BEFORE_DELETE == ReplicationEvent || ModelValidator.TYPE_BEFORE_DELETE_REPLICATION == ReplicationEvent || ModelValidator.TYPE_DELETE == ReplicationEvent)
        ;
    else if (!po.is_Changed() && !isChanged) {
        log.info("Object not changed = " + po.toString());
        return;
    }
    if (po != null) {
        Env.setContext(po.getCtx(), "#AD_Client_ID", po.getAD_Client_ID());
        if (MReplicationStrategy.REPLICATION_TABLE == ReplicationMode) {
            // Here must invoke other method else we get cycle...
            if (ModelValidator.TYPE_BEFORE_DELETE == ReplicationEvent || ModelValidator.TYPE_BEFORE_DELETE_REPLICATION == ReplicationEvent || ModelValidator.TYPE_DELETE == ReplicationEvent) {
                po.deleteEx(true);
            } else {
                if (X_AD_ReplicationTable.REPLICATIONTYPE_Broadcast.equals(ReplicationType)) {
                    MReplicationStrategy rplStrategy = new MReplicationStrategy(client.getCtx(), client.getAD_ReplicationStrategy_ID(), po.get_TrxName());
                    ExportHelper expHelper = new ExportHelper(client, rplStrategy);
                    expHelper.exportRecord(po, MReplicationStrategy.REPLICATION_TABLE, X_AD_ReplicationTable.REPLICATIONTYPE_Merge, ModelValidator.TYPE_AFTER_CHANGE);
                    po.saveReplica(true);
                } else if (X_AD_ReplicationTable.REPLICATIONTYPE_Merge.equals(ReplicationType) || X_AD_ReplicationTable.REPLICATIONTYPE_Reference.equals(ReplicationType)) {
                    po.saveReplica(true);
                } else /*else if (X_AD_ReplicationTable.REPLICATIONTYPE_Reference.equals(ReplicationType))
					{
						//Do nothing??	
					}*/
                if (X_AD_ReplicationTable.REPLICATIONTYPE_Local.equals(ReplicationType)) {
                //Do nothing??	
                } else {
                    // Replication Type is not one of the possible values...ERROR
                    throw new Exception(Msg.getMsg(ctx, "EXPReplicationTypeNonValidType"));
                }
            }
        } else if (MReplicationStrategy.REPLICATION_DOCUMENT == ReplicationMode && X_AD_ReplicationDocument.REPLICATIONTYPE_Merge.equals(ReplicationType) && po instanceof DocAction) {
            DocAction document = (DocAction) po;
            String action = document.getDocAction();
            String status = document.getDocStatus();
            log.info("Document:" + document.toString() + " DocStauts:" + status + " DocAction:" + action);
            if (ModelValidator.TIMING_AFTER_REVERSECORRECT == ReplicationEvent) {
                if (status.equals(DocAction.STATUS_Reversed) && action.equals(DocAction.ACTION_None)) {
                    po.saveEx();
                    return;
                }
            }
            if ((action.equals(DocAction.ACTION_Complete) && status.equals(DocAction.STATUS_InProgress)) || (action.equals(DocAction.ACTION_Close) && status.equals(DocAction.STATUS_Completed))) {
                if (!document.processIt(action)) {
                    log.info("PO.toString() = can not " + po.get_Value("DocAction"));
                }
                po.saveEx();
            } else {
                po.saveEx();
                return;
            }
        }
    }
    result.append("Save Successful ;");
}
Also used : MEXPFormat(org.compiere.model.MEXPFormat) DocAction(org.compiere.process.DocAction) Element(org.w3c.dom.Element) ExportHelper(org.adempiere.process.rpl.exp.ExportHelper) MReplicationStrategy(org.compiere.model.MReplicationStrategy) XPathExpressionException(javax.xml.xpath.XPathExpressionException) SQLException(java.sql.SQLException) ParseException(java.text.ParseException) AdempiereException(org.adempiere.exceptions.AdempiereException) MClient(org.compiere.model.MClient) PO(org.compiere.model.PO)

Example 2 with ExportHelper

use of org.adempiere.process.rpl.exp.ExportHelper in project adempiere by adempiere.

the class ExportModelValidator method loadReplicationStrategy.

public void loadReplicationStrategy(Properties ctx) {
    MClient m_client = MClient.get(Env.getCtx(), clientId);
    /*replicationStrategyId = MRole.get(m_client.getCtx(), roleId).get_ValueAsInt("AD_ReplicationStrategy_ID");
		if(replicationStrategyId <= 0)
		{
			replicationStrategyId = MOrg.get(m_client.getCtx(), orgId).getAD_ReplicationStrategy_ID();
		}

		if(replicationStrategyId <= 0)
		{
			replicationStrategyId =  m_client.getAD_ReplicationStrategy_ID();
			log.info("client.getAD_ReplicationStrategy_ID() = " + replicationStrategyId);
		}
		
		if (replicationStrategyId > 0) {
			replicationStrategy = new MReplicationStrategy(m_client.getCtx(), replicationStrategyId, null);
			if(!replicationStrategy.isActive())
			{	
				return;
			}
			exportHelper = new ExportHelper(m_client, replicationStrategy);
		}*/
    // 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);
    MReplicationStrategy.getByOrgAndRole(ctx, orgId, roleId, null).stream().filter(replicationStrategy -> replicationStrategy != null).forEach(replicationStrategy -> {
        for (X_AD_ReplicationTable rplTable : replicationStrategy.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(replicationStrategy.getCtx(), rplTable.getAD_Table_ID());
                modelValidationEngine.addModelChange(tableName, this);
            }
        }
        for (X_AD_ReplicationDocument rplDocument : replicationStrategy.getReplicationDocuments()) {
            if (X_AD_ReplicationDocument.REPLICATIONTYPE_Merge.equals(rplDocument.getReplicationType()) || X_AD_ReplicationDocument.REPLICATIONTYPE_Reference.equals(rplDocument.getReplicationType())) {
                String tableName = MTable.getTableName(replicationStrategy.getCtx(), rplDocument.getAD_Table_ID());
                modelValidationEngine.addDocValidate(tableName, this);
            }
        }
    });
}
Also used : CLogger(org.compiere.util.CLogger) Properties(java.util.Properties) MClient(org.compiere.model.MClient) X_AD_ReplicationTable(org.compiere.model.X_AD_ReplicationTable) ExportHelper(org.adempiere.process.rpl.exp.ExportHelper) Env(org.compiere.util.Env) X_AD_ReplicationDocument(org.compiere.model.X_AD_ReplicationDocument) MReplicationStrategy(org.compiere.model.MReplicationStrategy) ModelValidator(org.compiere.model.ModelValidator) MReplicationTable(org.compiere.model.MReplicationTable) MTable(org.compiere.model.MTable) PO(org.compiere.model.PO) ModelValidationEngine(org.compiere.model.ModelValidationEngine) X_AD_ReplicationTable(org.compiere.model.X_AD_ReplicationTable) MClient(org.compiere.model.MClient) X_AD_ReplicationDocument(org.compiere.model.X_AD_ReplicationDocument)

Example 3 with ExportHelper

use of org.adempiere.process.rpl.exp.ExportHelper in project adempiere by adempiere.

the class ExportModelValidator method docValidate.

/**
	 *	Validate Document.
	 *	Called as first step of DocAction.prepareIt 
     *	when you called addDocValidate for the table.
	 *	@param po persistent object
	 *	@param type see TIMING_ constants
     *	@return error message or null
	 * @throws Exception 
	 */
public String docValidate(PO po, int type) {
    log.info("Replicate the Document = " + po.get_TableName() + " with Type = " + type);
    String result = null;
    if (exportHelper != null) {
        try {
            if (type == TIMING_AFTER_COMPLETE || type == TIMING_AFTER_CLOSE || type == TIMING_AFTER_REVERSECORRECT || type == TIMING_AFTER_VOID || type == TIMING_AFTER_REACTIVATE) //|| type == TIMING_AFTER_PREPARE
            {
                MReplicationStrategy.getByOrgAndRole(po.getCtx(), orgId, roleId, po.get_TrxName()).stream().forEach(replicationStrategy -> {
                    X_AD_ReplicationDocument replicationDocument = null;
                    int C_DocType_ID = po.get_ValueAsInt("C_DocType_ID");
                    if (C_DocType_ID > 0) {
                        replicationDocument = MReplicationStrategy.getReplicationDocument(po.getCtx(), replicationStrategy.get_ID(), po.get_Table_ID(), C_DocType_ID);
                    } else {
                        replicationDocument = MReplicationStrategy.getReplicationDocument(po.getCtx(), replicationStrategy.get_ID(), po.get_Table_ID());
                    }
                    if (replicationDocument != null) {
                        exportHelper = new ExportHelper(client, replicationStrategy);
                        try {
                            exportHelper.exportRecord(po, MReplicationStrategy.REPLICATION_DOCUMENT, replicationDocument.getReplicationType(), type);
                        } catch (Exception exeption) {
                        }
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = e.toString();
        }
    }
    return result;
}
Also used : ExportHelper(org.adempiere.process.rpl.exp.ExportHelper) X_AD_ReplicationDocument(org.compiere.model.X_AD_ReplicationDocument)

Aggregations

ExportHelper (org.adempiere.process.rpl.exp.ExportHelper)3 MClient (org.compiere.model.MClient)2 MReplicationStrategy (org.compiere.model.MReplicationStrategy)2 PO (org.compiere.model.PO)2 X_AD_ReplicationDocument (org.compiere.model.X_AD_ReplicationDocument)2 SQLException (java.sql.SQLException)1 ParseException (java.text.ParseException)1 Properties (java.util.Properties)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 AdempiereException (org.adempiere.exceptions.AdempiereException)1 MEXPFormat (org.compiere.model.MEXPFormat)1 MReplicationTable (org.compiere.model.MReplicationTable)1 MTable (org.compiere.model.MTable)1 ModelValidationEngine (org.compiere.model.ModelValidationEngine)1 ModelValidator (org.compiere.model.ModelValidator)1 X_AD_ReplicationTable (org.compiere.model.X_AD_ReplicationTable)1 DocAction (org.compiere.process.DocAction)1 CLogger (org.compiere.util.CLogger)1 Env (org.compiere.util.Env)1 Element (org.w3c.dom.Element)1