IDEMPIERE-3924 Implement EventHandler for pre/after/post processes

This commit is contained in:
Carlos Ruiz 2019-03-20 22:24:29 -03:00
parent 160aa2a692
commit 6510c48125
4 changed files with 86 additions and 4 deletions

View File

@ -14,8 +14,10 @@
package org.adempiere.base.event;
import java.util.List;
import java.util.UUID;
import org.compiere.model.PO;
import org.compiere.process.ProcessInfo;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
@ -105,6 +107,21 @@ public abstract class AbstractEventHandler implements EventHandler {
registerEvent(topic, filter);
}
/**
* @param topic
* @param classOrUUID className or ProcessUUID
*/
protected void registerProcessEvent(String topic, String classOrUUID) {
String prop = "processUUID";
try {
UUID.fromString(classOrUUID);
} catch (Exception e) {
prop = "className";
}
String filter = "("+prop+"="+classOrUUID+")";
registerEvent(topic, filter);
}
/**
* @param event
* @return PO
@ -113,6 +130,14 @@ public abstract class AbstractEventHandler implements EventHandler {
return getEventProperty(event, IEventManager.EVENT_DATA);
}
/**
* @param event
* @return ProcessInfo
*/
protected ProcessInfo getProcessInfo(Event event) {
return getEventProperty(event, IEventManager.EVENT_DATA);
}
/**
*
* @param <T>

View File

@ -97,5 +97,12 @@ public interface IEventTopics {
public static final String BROADCAST_MESSAGE = "idempiere/broadcastMsg";
public static final String REQUEST_SEND_EMAIL = "idempiere/requestSendEMail";
}
/** Called before starting a process, after prepared */
public static final String BEFORE_PROCESS = "idempiere/beforeProcess";
/** Called after a process finishes, before commit */
public static final String AFTER_PROCESS = "idempiere/afterProcess";
/** Called after a process is committed */
public static final String POST_PROCESS = "idempiere/postProcess";
}

View File

@ -24,6 +24,7 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.adempiere.util.IProcessUI;
import org.compiere.model.MPInstance;
import org.compiere.model.MPInstancePara;
import org.compiere.model.MProcess;
@ -49,7 +50,7 @@ public class ProcessInfo implements Serializable
/**
*
*/
private static final long serialVersionUID = 2167823616151648814L;
private static final long serialVersionUID = -4600747909096993053L;
/**
* Constructor
@ -203,6 +204,8 @@ public class ProcessInfo implements Serializable
sb.append(",Transient=").append(m_TransientObject);
if (m_SerializableObject != null)
sb.append(",Serializable=").append(m_SerializableObject);
if (m_transactionName != null)
sb.append(",Trx=").append(m_transactionName);
sb.append(",Summary=").append(getSummary())
.append(",Log=").append(m_logs == null ? 0 : m_logs.size());
// .append(getLogInfo(false));
@ -918,5 +921,15 @@ public class ProcessInfo implements Serializable
return lastServerSession.getCreated();
}
private IProcessUI processUI;
public void setProcessUI(IProcessUI processUI) {
this.processUI = processUI;
}
public IProcessUI getProcessUI() {
return processUI;
}
} // ProcessInfo

View File

@ -27,6 +27,10 @@ import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import org.adempiere.base.event.EventManager;
import org.adempiere.base.event.EventProperty;
import org.adempiere.base.event.IEventManager;
import org.adempiere.base.event.IEventTopics;
import org.adempiere.util.IProcessUI;
import org.compiere.model.MPInstance;
import org.compiere.model.PO;
@ -35,6 +39,7 @@ import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import org.compiere.util.Trx;
import org.osgi.service.event.Event;
/**
* Server Process Template
@ -128,6 +133,8 @@ public abstract class SvrProcess implements ProcessCall
m_trx = Trx.get(Trx.createTrxName("SvrProcess"), true);
m_trx.setDisplayName(getClass().getName()+"_startProcess");
}
m_pi.setTransactionName(m_trx.getTrxName());
m_pi.setProcessUI(processUI);
//
ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
ClassLoader processLoader = getClass().getClassLoader();
@ -168,13 +175,17 @@ public abstract class SvrProcess implements ProcessCall
m_trx.rollback();
m_trx.close();
m_trx = null;
m_pi.setTransactionName(null);
}
unlock();
// outside transaction processing [ teo_sarca, 1646891 ]
postProcess(!m_pi.isError());
@SuppressWarnings("unused")
Event eventPP = sendProcessEvent(IEventTopics.POST_PROCESS);
Thread.currentThread().setContextClassLoader(contextLoader);
}
} finally {
@ -198,7 +209,24 @@ public abstract class SvrProcess implements ProcessCall
try
{
prepare();
msg = doIt();
// event before process
Event eventBP = sendProcessEvent(IEventTopics.BEFORE_PROCESS);
@SuppressWarnings("unchecked")
List<String> errorsBP = (List<String>) eventBP.getProperty(IEventManager.EVENT_ERROR_MESSAGES);
if (errorsBP != null && !errorsBP.isEmpty()) {
msg = "@Error@:" + errorsBP.get(0);
} else {
msg = doIt();
if (msg != null && ! msg.startsWith("@Error@")) {
Event eventAP = sendProcessEvent(IEventTopics.AFTER_PROCESS);
@SuppressWarnings("unchecked")
List<String> errorsAP = (List<String>) eventAP.getProperty(IEventManager.EVENT_ERROR_MESSAGES);
if (errorsAP != null && !errorsAP.isEmpty()) {
msg = "@Error@:" + errorsAP.get(0);
}
}
}
}
catch (Throwable e)
{
@ -227,6 +255,15 @@ public abstract class SvrProcess implements ProcessCall
return success;
} // process
private Event sendProcessEvent(String topic) {
Event event = EventManager.newEvent(topic,
new EventProperty(EventManager.EVENT_DATA, m_pi),
new EventProperty("processUUID", m_pi.getAD_Process_UU()),
new EventProperty("className", m_pi.getClassName()));
EventManager.getInstance().sendEvent(event);
return event;
}
/**
* Prepare - e.g., get Parameters.
* <code>