mirror of
https://github.com/hs-web/hsweb-framework.git
synced 2026-06-08 17:03:39 +08:00
增加EntitySavedEvent事件
This commit is contained in:
@@ -2,7 +2,6 @@ package org.hswebframework.web.crud.events;
|
||||
|
||||
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.hswebframework.ezorm.core.GlobalConfig;
|
||||
import org.hswebframework.ezorm.rdb.events.*;
|
||||
import org.hswebframework.ezorm.rdb.mapping.*;
|
||||
import org.hswebframework.ezorm.rdb.mapping.events.MappingContextKeys;
|
||||
@@ -21,6 +20,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
@SuppressWarnings("all")
|
||||
public class EntityEventListener implements EventListener {
|
||||
@@ -54,9 +54,17 @@ public class EntityEventListener implements EventListener {
|
||||
if (type == MappingEventTypes.insert_after) {
|
||||
boolean single = context.get(MappingContextKeys.type).map("single"::equals).orElse(false);
|
||||
if (single) {
|
||||
handleInsertSingle(mapping.getEntityType(), context);
|
||||
handleSingleOperation(mapping.getEntityType(), context, EntityCreatedEvent::new);
|
||||
} else {
|
||||
handleInsertBatch(mapping.getEntityType(), context);
|
||||
handleBatchOperation(mapping.getEntityType(), context, EntityCreatedEvent::new);
|
||||
}
|
||||
}
|
||||
if (type == MappingEventTypes.save_after) {
|
||||
boolean single = context.get(MappingContextKeys.type).map("single"::equals).orElse(false);
|
||||
if (single) {
|
||||
handleSingleOperation(mapping.getEntityType(), context, EntitySavedEvent::new);
|
||||
} else {
|
||||
handleBatchOperation(mapping.getEntityType(), context, EntitySavedEvent::new);
|
||||
}
|
||||
}
|
||||
if (type == MappingEventTypes.update_before) {
|
||||
@@ -175,22 +183,22 @@ public class EntityEventListener implements EventListener {
|
||||
|
||||
}
|
||||
|
||||
protected void handleInsertBatch(Class clazz, EventContext context) {
|
||||
protected void handleBatchOperation(Class clazz, EventContext context, BiFunction<List<?>, Class, Object> mapper) {
|
||||
|
||||
context.get(MappingContextKeys.instance)
|
||||
.filter(List.class::isInstance)
|
||||
.map(List.class::cast)
|
||||
.ifPresent(lst -> {
|
||||
eventPublisher.publishEvent(new GenericsPayloadApplicationEvent<>(this, new EntityCreatedEvent(lst, clazz), clazz));
|
||||
eventPublisher.publishEvent(new GenericsPayloadApplicationEvent<>(this, mapper.apply(lst, clazz), clazz));
|
||||
});
|
||||
}
|
||||
|
||||
protected void handleInsertSingle(Class clazz, EventContext context) {
|
||||
protected void handleSingleOperation(Class clazz, EventContext context, BiFunction<List<?>, Class, Object> mapper) {
|
||||
context.get(MappingContextKeys.instance)
|
||||
.filter(Entity.class::isInstance)
|
||||
.map(Entity.class::cast)
|
||||
.ifPresent(entity -> {
|
||||
eventPublisher.publishEvent(new GenericsPayloadApplicationEvent<>(this, new EntityCreatedEvent(Collections.singletonList(entity), clazz), clazz));
|
||||
eventPublisher.publishEvent(new GenericsPayloadApplicationEvent<>(this, mapper.apply(Collections.singletonList(entity), clazz), clazz));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.hswebframework.web.crud.events;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @see org.hswebframework.web.crud.annotation.EnableEntityEvent
|
||||
* @param <E>
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public class EntitySavedEvent<E> implements Serializable {
|
||||
|
||||
private List<E> entity;
|
||||
|
||||
private Class<E> entityType;
|
||||
}
|
||||
@@ -71,6 +71,14 @@ public class EntityEventListenerTest {
|
||||
|
||||
Assert.assertEquals(listener.deleted.getAndSet(0), 2);
|
||||
|
||||
reactiveRepository.save(EventTestEntity.of("test2", 1))
|
||||
.then()
|
||||
.as(StepVerifier::create)
|
||||
.expectComplete()
|
||||
.verify();
|
||||
|
||||
Assert.assertEquals(listener.saved.getAndSet(0), 1);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -15,6 +15,7 @@ public class TestEntityListener {
|
||||
|
||||
AtomicInteger modified = new AtomicInteger();
|
||||
|
||||
AtomicInteger saved = new AtomicInteger();
|
||||
|
||||
@EventListener
|
||||
public void handleCreated(EntityCreatedEvent<EventTestEntity> event) {
|
||||
@@ -33,4 +34,10 @@ public class TestEntityListener {
|
||||
System.out.println(event);
|
||||
modified.addAndGet(event.getAfter().size());
|
||||
}
|
||||
|
||||
@EventListener
|
||||
public void handleSave(EntitySavedEvent<EventTestEntity> event) {
|
||||
System.out.println(event);
|
||||
saved.addAndGet(event.getEntity().size());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user