use of eu.bcvsolutions.idm.core.api.event.DefaultEventContext in project CzechIdMng by bcvsolutions.
the class DefaultEntityEventManagerIntergationTest method testStartEventInMiddle.
@Test
public void testStartEventInMiddle() {
DefaultEventContext<TestContent> initContext = new DefaultEventContext<>();
initContext.setProcessedOrder(2);
EntityEvent<TestContent> event = new CoreEvent<>(CoreEventType.CREATE, new TestContent(), null, initContext);
EventContext<TestContent> context = manager.process(event);
//
assertEquals(2, context.getResults().size());
assertEquals(4, context.getProcessedOrder().intValue());
assertEquals("4", context.getLastResult().getEvent().getContent().getText());
}
use of eu.bcvsolutions.idm.core.api.event.DefaultEventContext in project CzechIdMng by bcvsolutions.
the class DefaultEntityEventManager method toEvent.
@Override
public EntityEvent<? extends Identifiable> toEvent(IdmEntityEventDto entityEvent) {
Identifiable content = null;
// only if type and id is the same as owner can be used
if (entityEvent.getContent() != null && Objects.equal(getOwnerType(entityEvent.getContent().getClass()), entityEvent.getOwnerType()) && Objects.equal(entityEvent.getContent().getId(), entityEvent.getOwnerId())) {
content = entityEvent.getContent();
}
if (content == null) {
// content is not persisted - try to find actual entity
content = findOwner(entityEvent);
}
if (content == null) {
throw new EventContentDeletedException(entityEvent);
}
//
Map<String, Serializable> eventProperties = entityEvent.getProperties().toMap();
eventProperties.put(EntityEvent.EVENT_PROPERTY_EVENT_ID, entityEvent.getId());
eventProperties.put(EntityEvent.EVENT_PROPERTY_PRIORITY, entityEvent.getPriority());
eventProperties.put(EntityEvent.EVENT_PROPERTY_EXECUTE_DATE, entityEvent.getExecuteDate());
eventProperties.put(EntityEvent.EVENT_PROPERTY_PARENT_EVENT_TYPE, entityEvent.getParentEventType());
eventProperties.put(EntityEvent.EVENT_PROPERTY_PARENT_EVENT_ID, entityEvent.getParent());
eventProperties.put(EntityEvent.EVENT_PROPERTY_ROOT_EVENT_ID, entityEvent.getRootId());
eventProperties.put(EntityEvent.EVENT_PROPERTY_SUPER_OWNER_ID, entityEvent.getSuperOwnerId());
eventProperties.put(EntityEvent.EVENT_PROPERTY_TRANSACTION_ID, entityEvent.getTransactionId());
final String type = entityEvent.getEventType();
DefaultEventContext<Identifiable> initContext = new DefaultEventContext<>();
initContext.setProcessedOrder(entityEvent.getProcessedOrder());
EventType eventType = (EventType) () -> type;
EntityEvent<Identifiable> resurectedEvent = new CoreEvent<>(eventType, content, eventProperties, initContext);
//
// prevent to mix content and original source types between new and parent event
Identifiable originalSource = entityEvent.getOriginalSource();
if (originalSource != null && !originalSource.getClass().equals(content.getClass())) {
// preset original source by current content -> content is already persisted in NOFIFY event
resurectedEvent.setOriginalSource(content);
} else {
resurectedEvent.setOriginalSource(originalSource);
}
//
return resurectedEvent;
}
use of eu.bcvsolutions.idm.core.api.event.DefaultEventContext in project CzechIdMng by bcvsolutions.
the class DefaultEntityEventManager method putToQueue.
/**
* Try put event to queue - event is put into queue, only if it's not executed synchronously.
* If event is executed synchronously, then {@link EventContext} is returned, {@code null} is returned otherwise.
*
* @param entityEvent
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private EventContext<?> putToQueue(IdmEntityEventDto entityEvent) {
if (entityEvent.getPriority() == PriorityType.IMMEDIATE) {
LOG.trace("Event type [{}] for owner with id [{}] will be executed synchronously.", entityEvent.getEventType(), entityEvent.getOwnerId());
// we don't persist events and their states
return process(new CoreEvent<>(EntityEventType.EXECUTE, entityEvent));
}
if (!eventConfiguration.isAsynchronous()) {
LOG.trace("Event type [{}] for owner with id [{}] will be executed synchronously, asynchronous event processing [{}] is disabled.", entityEvent.getEventType(), entityEvent.getOwnerId(), EventConfiguration.PROPERTY_EVENT_ASYNCHRONOUS_ENABLED);
// synchronous processing
return process(new CoreEvent<>(EntityEventType.EXECUTE, entityEvent));
}
//
// get enabled processors, which listen given event (conditional is evaluated)
final EntityEvent<?> event = toEvent(entityEvent);
List<EntityEventProcessor> listenProcessors = getEnabledProcessors(event).stream().filter(processor -> processor.conditional(event)).collect(Collectors.toList());
if (listenProcessors.isEmpty()) {
LOG.debug("Event type [{}] for owner with id [{}] will not be executed, no enabled processor is registered.", entityEvent.getEventType(), entityEvent.getOwnerId());
// return empty context - nothing is processed
return new DefaultEventContext<>();
}
//
// evaluate event priority by registered processors
PriorityType priority = evaluatePriority(event, listenProcessors);
if (priority != null && priority.getPriority() < entityEvent.getPriority().getPriority()) {
entityEvent.setPriority(priority);
}
// registered processors voted about event will be processed synchronously
if (entityEvent.getPriority() == PriorityType.IMMEDIATE) {
LOG.trace("Event type [{}] for owner with id [{}] will be executed synchronously.", entityEvent.getEventType(), entityEvent.getOwnerId());
// synchronous processing
// we don't persist events and their states
process(new CoreEvent<>(EntityEventType.EXECUTE, entityEvent));
}
//
// TODO: send notification only when event fails
// notification - info about registered (asynchronous) processors
// Map<String, Object> parameters = new LinkedHashMap<>();
// parameters.put("eventType", entityEvent.getEventType());
// parameters.put("ownerId", entityEvent.getOwnerId());
// parameters.put("instanceId", entityEvent.getInstanceId());
// parameters.put("processors", registeredProcessors
// .stream()
// .map(DefaultEntityEventManager.this::toDto)
// .collect(Collectors.toList()));
// notificationManager.send(
// CoreModuleDescriptor.TOPIC_EVENT,
// new IdmMessageDto
// .Builder()
// .setLevel(NotificationLevel.INFO)
// .setModel(new DefaultResultModel(CoreResultCode.EVENT_ACCEPTED, parameters))
// .build());
//
// persist event - asynchronous processing
entityEvent = entityEventService.save(entityEvent);
addEventCache(entityEvent.getId(), entityEvent.getTransactionId());
// not processed - persisted into queue
return null;
}
Aggregations