1. 介绍
业务事件允许你进行一些逻辑处理,在一个对象进行保存,修改或者删除的时候。就像数据库里面的触发器一样,但是它的优点是,你可以通过java代码来实现你想要的功能,方便调试与实现。比如你想在生成销售订单的时候自动生成一个单据号,可以利用业务事件来处理。具体参考如下这个类:
SetDocumentNoHandler
2. 简单示例
我们先来一个简单示例,如下代码所示:
public class GreetingEventHandler extends EntityPersistenceEventObserver {
private static Entity[] entities = { ModelProvider.getInstance().getEntity(Greeting.ENTITY_NAME) };
protected Logger logger = Logger.getLogger(this.getClass());
@Override
protected Entity[] getObservedEntities() {
return entities;
}
public void onUpdate(@Observes EntityUpdateEvent event) {
if (!isValidEvent(event)) {
return;
}
logger.info("Greeting " + event.getTargetInstance().getId() + " is being updated");
}
public void onSave(@Observes EntityNewEvent event) {
if (!isValidEvent(event)) {
return;
}
logger.info("Greeting " + ((Greeting) event.getTargetInstance()).getName()
+ " is being created");
}
public void onDelete(@Observes EntityDeleteEvent event) {
if (!isValidEvent(event)) {
return;
}
logger.info("Greeting " + event.getTargetInstance().getId() + " is being deleted");
}
}
需要注意如下几点:
1. 继承于
EntityPersistenceEventObserver 类,帮助过滤出正确的事件(因为所有的保存,修改,删除事件都会发送过来,我们需要刷新出自己感兴趣的事件)
2. 方法名不重要,比如里面的onDelete方法,主要的是那个参数的声明@Observes EntityDeleteEvent event, Weld框架利用这个参数声明来判定哪些事件应该发送给这个方法。
3. 任何一个实体更新,保存,修改都会发送事件到这个eventHandler的对应方法,每一个方法头部,都需要通过方法isValidEvent事件来判断这个事件是否为自己感兴趣的。
4. 我们通过org.apache.log4j.Logger类来记录日志。
3. 简单展示
当你将这个类加入你的模块,重启tomcat的时候,进入称呼这个窗口:
http://localhost:8080/openbravo/?tabId=282
你将在控制台看到你记录的日志。
4. 更复杂一点示例
假如我们要在更新的时候判断title字段是否有一点,如果没有,在末尾加上一点。在保存的时候,增加一个子表翻译。如下代码所示:
public void onUpdate(@Observes
EntityUpdateEvent event) {
if (!isValidEvent(event)) {
return;
}
final Greeting greeting = (Greeting) event.getTargetInstance();
final String title = greeting.getTitle();
if (title != null && !title.endsWith(".")) {
final Entity greetingEntity = ModelProvider.getInstance().getEntity(Greeting.ENTITY_NAME);
final Property greetingTitleProperty = greetingEntity.getProperty(Greeting.PROPERTY_TITLE);
// note use setCurrentState and not setters on the Greeting object directly
event.setCurrentState(greetingTitleProperty, title + ".");
}
System.err.println("Greeting " + event.getTargetInstance().getId() + " is being updated");
}
public void onSave(@Observes
EntityNewEvent event) {
if (!isValidEvent(event)) {
return;
}
final Entity greetingEntity = ModelProvider.getInstance().getEntity(Greeting.ENTITY_NAME);
final Greeting greeting = (Greeting) event.getTargetInstance();
// now also add the dot to the title
final String title = greeting.getTitle();
if (title != null && !title.endsWith(".")) {
final Property greetingTitleProperty = greetingEntity.getProperty(Greeting.PROPERTY_TITLE);
// note use setCurrentState and not setters on the Greeting object directly
event.setCurrentState(greetingTitleProperty, title + ".");
}
final GreetingTrl greetingTrl = OBProvider.getInstance().get(GreetingTrl.class);
// set relevant translation properties
greetingTrl.setGreeting(greeting);
// 171 is dutch, choose any other language..
greetingTrl.setLanguage(OBDal.getInstance().get(Language.class, "171"));
// note we can call getters on the targetInstance, but not setters!
greetingTrl.setName(greeting.getName());
greetingTrl.setTitle(greeting.getTitle());
greetingTrl.setTranslation(false);
// and add the greetingTrl to the greeting
// we don't use event.setCurrentState as we get the list and add to it
// get the trl property for the greeting entity
final Property greetingTrlProperty = greetingEntity
.getProperty(Greeting.PROPERTY_GREETINGTRLLIST);
@SuppressWarnings("unchecked")
final List<Object> greetingTrls = (List<Object>) event.getCurrentState(greetingTrlProperty);
greetingTrls.add(greetingTrl);
// don't need to save the greetingTrl, it is saved as the child of the greeting
// OBDal.getInstance().save(greetingTrl);
System.err.println("Greeting " + ((Greeting) event.getTargetInstance()).getName()
+ " is being created");
}
这里需要注意几点:
1. 获取字段名通过新建一个空实体,如下代码所示
final Entity greetingEntity = ModelProvider.getInstance().getEntity(Greeting.ENTITY_NAME);
final Property greetingTitleProperty = greetingEntity.getProperty(Greeting.PROPERTY_TITLE);
2. 修改一个保存前实体某一个字段的值,不能用set方法,必须如下所示:
event.setCurrentState(greetingTitleProperty, title + ".");
3. 新建一个需要持久化的实体,不能用new关键字,必须如下所示:
final GreetingTrl greetingTrl = OBProvider.getInstance().get(GreetingTrl.class);
4. 新建的子实体不需要自己调用代码进行持久化,会随着主实体持久化而持久化。
5. 中断处理
假如我们想在保存前,判断一些条件,如果不满足,中断保存过程,我们可以抛出异常,如下代码所示:
public void onUpdate(@Observes
EntityUpdateEvent event) {
if (!isValidEvent(event)) {
return;
}
final OBSA_Orderline_Assign olineAssign = (OBSA_Orderline_Assign) event.getTargetInstance();
if (olineAssign.getProductWithStorage().getProduct() != olineAssign.getSalesOrderLine()
.getProduct()) {
String language = OBContext.getOBContext().getLanguage().getLanguage();
ConnectionProvider conn = new DalConnectionProvider(false);
throw new OBException(Utility.messageBD(conn, "OBSA_ErrorProduct", language));
}
}