Search in sources :

Example 1 with DefaultEventContext

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());
}
Also used : CoreEvent(eu.bcvsolutions.idm.core.api.event.CoreEvent) DefaultEventContext(eu.bcvsolutions.idm.core.api.event.DefaultEventContext) TestContent(eu.bcvsolutions.idm.core.event.TestContent) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 2 with DefaultEventContext

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;
}
Also used : Serializable(java.io.Serializable) LongRunningTaskEventType(eu.bcvsolutions.idm.core.scheduler.api.event.LongRunningTaskEvent.LongRunningTaskEventType) EventType(eu.bcvsolutions.idm.core.api.event.EventType) CoreEventType(eu.bcvsolutions.idm.core.api.event.CoreEvent.CoreEventType) EntityEventType(eu.bcvsolutions.idm.core.api.event.EntityEventEvent.EntityEventType) CoreEvent(eu.bcvsolutions.idm.core.api.event.CoreEvent) DefaultEventContext(eu.bcvsolutions.idm.core.api.event.DefaultEventContext) EventContentDeletedException(eu.bcvsolutions.idm.core.api.exception.EventContentDeletedException) Identifiable(eu.bcvsolutions.idm.core.api.domain.Identifiable)

Example 3 with DefaultEventContext

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;
}
Also used : Auditable(eu.bcvsolutions.idm.core.api.domain.Auditable) DefaultEventContext(eu.bcvsolutions.idm.core.api.event.DefaultEventContext) DtoUtils(eu.bcvsolutions.idm.core.api.utils.DtoUtils) IdmEntityEventDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto) ZonedDateTime(java.time.ZonedDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) ConfigurationService(eu.bcvsolutions.idm.core.api.service.ConfigurationService) StringUtils(org.apache.commons.lang3.StringUtils) SecurityService(eu.bcvsolutions.idm.core.security.api.service.SecurityService) CoreEvent(eu.bcvsolutions.idm.core.api.event.CoreEvent) Map(java.util.Map) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) LongRunningTaskEventType(eu.bcvsolutions.idm.core.scheduler.api.event.LongRunningTaskEvent.LongRunningTaskEventType) Sort(org.springframework.data.domain.Sort) EntityEventProcessorFilter(eu.bcvsolutions.idm.core.api.dto.filter.EntityEventProcessorFilter) Set(java.util.Set) PageRequest(org.springframework.data.domain.PageRequest) Page(org.springframework.data.domain.Page) Serializable(java.io.Serializable) CreatedComparator(eu.bcvsolutions.idm.core.api.domain.comparator.CreatedComparator) EventType(eu.bcvsolutions.idm.core.api.event.EventType) Lazy(org.springframework.context.annotation.Lazy) AnnotationAwareOrderComparator(org.springframework.core.annotation.AnnotationAwareOrderComparator) Callable(java.util.concurrent.Callable) ValueWrapper(eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper) EntityEventProcessor(eu.bcvsolutions.idm.core.api.event.EntityEventProcessor) Scheduled(org.springframework.scheduling.annotation.Scheduled) TransactionContext(eu.bcvsolutions.idm.core.api.domain.TransactionContext) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) Lists(com.google.common.collect.Lists) LookupService(eu.bcvsolutions.idm.core.api.service.LookupService) IdmAuthorityUtils(eu.bcvsolutions.idm.core.security.api.utils.IdmAuthorityUtils) DefaultEventResult(eu.bcvsolutions.idm.core.api.event.DefaultEventResult) EventResult(eu.bcvsolutions.idm.core.api.event.EventResult) SchedulerConfiguration(eu.bcvsolutions.idm.core.scheduler.api.config.SchedulerConfiguration) IdmCacheManager(eu.bcvsolutions.idm.core.api.service.IdmCacheManager) OperationState(eu.bcvsolutions.idm.core.api.domain.OperationState) LongRunningTaskExecutor(eu.bcvsolutions.idm.core.scheduler.api.service.LongRunningTaskExecutor) EventContext(eu.bcvsolutions.idm.core.api.event.EventContext) HttpStatus(org.springframework.http.HttpStatus) EmptyEntityEventProcessor(eu.bcvsolutions.idm.core.api.event.EmptyEntityEventProcessor) IdmEntityEventService(eu.bcvsolutions.idm.core.api.service.IdmEntityEventService) IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) CoreResultCode(eu.bcvsolutions.idm.core.api.domain.CoreResultCode) CoreEventType(eu.bcvsolutions.idm.core.api.event.CoreEvent.CoreEventType) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) EventContentDeletedException(eu.bcvsolutions.idm.core.api.exception.EventContentDeletedException) LongRunningTaskEvent(eu.bcvsolutions.idm.core.scheduler.api.event.LongRunningTaskEvent) EntityEventManager(eu.bcvsolutions.idm.core.api.service.EntityEventManager) AutowireHelper(eu.bcvsolutions.idm.core.api.utils.AutowireHelper) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) BasePermission(eu.bcvsolutions.idm.core.security.api.domain.BasePermission) TransactionContextHolder(eu.bcvsolutions.idm.core.api.domain.TransactionContextHolder) Pageable(org.springframework.data.domain.Pageable) Objects(com.google.common.base.Objects) ImmutableMap(com.google.common.collect.ImmutableMap) AbstractEntity(eu.bcvsolutions.idm.core.api.entity.AbstractEntity) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IdmEntityStateFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmEntityStateFilter) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) PriorityType(eu.bcvsolutions.idm.core.api.domain.PriorityType) IdmEntityStateDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityStateDto) List(java.util.List) EventConfiguration(eu.bcvsolutions.idm.core.api.config.domain.EventConfiguration) ExceptionProcessable(eu.bcvsolutions.idm.core.security.api.service.ExceptionProcessable) ExceptionUtils(eu.bcvsolutions.idm.core.api.utils.ExceptionUtils) Entry(java.util.Map.Entry) Identifiable(eu.bcvsolutions.idm.core.api.domain.Identifiable) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) EntityEventProcessorDto(eu.bcvsolutions.idm.core.api.dto.EntityEventProcessorDto) IdmJwtAuthentication(eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication) HashSet(java.util.HashSet) ModelMapper(org.modelmapper.ModelMapper) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) EntityStateManager(eu.bcvsolutions.idm.core.api.service.EntityStateManager) ReadDtoService(eu.bcvsolutions.idm.core.api.service.ReadDtoService) Propagation(org.springframework.transaction.annotation.Propagation) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) Direction(org.springframework.data.domain.Sort.Direction) EntityEvent(eu.bcvsolutions.idm.core.api.event.EntityEvent) EntityEventLock(eu.bcvsolutions.idm.core.api.event.EntityEventLock) EntityEventType(eu.bcvsolutions.idm.core.api.event.EntityEventEvent.EntityEventType) TransactionalEventListener(org.springframework.transaction.event.TransactionalEventListener) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) IdmEntityEventFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmEntityEventFilter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) EnabledEvaluator(eu.bcvsolutions.idm.core.security.api.service.EnabledEvaluator) ApplicationContext(org.springframework.context.ApplicationContext) AsyncEntityEventProcessor(eu.bcvsolutions.idm.core.api.event.AsyncEntityEventProcessor) ConfigurationMap(eu.bcvsolutions.idm.core.api.domain.ConfigurationMap) Comparator(java.util.Comparator) Collections(java.util.Collections) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) PriorityType(eu.bcvsolutions.idm.core.api.domain.PriorityType) EntityEventProcessor(eu.bcvsolutions.idm.core.api.event.EntityEventProcessor) EmptyEntityEventProcessor(eu.bcvsolutions.idm.core.api.event.EmptyEntityEventProcessor) AsyncEntityEventProcessor(eu.bcvsolutions.idm.core.api.event.AsyncEntityEventProcessor) DefaultEventContext(eu.bcvsolutions.idm.core.api.event.DefaultEventContext)

Aggregations

CoreEvent (eu.bcvsolutions.idm.core.api.event.CoreEvent)3 DefaultEventContext (eu.bcvsolutions.idm.core.api.event.DefaultEventContext)3 Identifiable (eu.bcvsolutions.idm.core.api.domain.Identifiable)2 CoreEventType (eu.bcvsolutions.idm.core.api.event.CoreEvent.CoreEventType)2 EntityEventType (eu.bcvsolutions.idm.core.api.event.EntityEventEvent.EntityEventType)2 EventType (eu.bcvsolutions.idm.core.api.event.EventType)2 EventContentDeletedException (eu.bcvsolutions.idm.core.api.exception.EventContentDeletedException)2 LongRunningTaskEventType (eu.bcvsolutions.idm.core.scheduler.api.event.LongRunningTaskEvent.LongRunningTaskEventType)2 Serializable (java.io.Serializable)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Objects (com.google.common.base.Objects)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 ValueWrapper (eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper)1 EventConfiguration (eu.bcvsolutions.idm.core.api.config.domain.EventConfiguration)1 Auditable (eu.bcvsolutions.idm.core.api.domain.Auditable)1 ConfigurationMap (eu.bcvsolutions.idm.core.api.domain.ConfigurationMap)1 CoreResultCode (eu.bcvsolutions.idm.core.api.domain.CoreResultCode)1 OperationState (eu.bcvsolutions.idm.core.api.domain.OperationState)1