Search in sources :

Example 1 with EventType

use of eu.bcvsolutions.idm.core.api.event.EventType in project CzechIdMng by bcvsolutions.

the class DefaultEntityEventManagerIntergationTest method testConfigPropertyEventTypeOverwrite.

@Test
public void testConfigPropertyEventTypeOverwrite() {
    String eventTypeName = System.nanoTime() + "_test_type";
    EventType type = (EventType) () -> eventTypeName;
    EntityEvent<TestContentTwo> event = new CoreEvent<>(type, new TestContentTwo());
    EventContext<TestContentTwo> context = entityEventManager.process(event);
    assertEquals(0, context.getResults().size());
    String configPropName = testTwoEntityEventProcessorOne.getConfigurationPropertyName(EntityEventProcessor.PROPERTY_EVENT_TYPES);
    configurationService.setValue(configPropName, eventTypeName);
    EntityEvent<TestContentTwo> event2 = new CoreEvent<>(type, new TestContentTwo());
    EventContext<TestContentTwo> context2 = entityEventManager.process(event2);
    assertEquals(2, context2.getResults().size());
}
Also used : IdentityEventType(eu.bcvsolutions.idm.core.model.event.IdentityEvent.IdentityEventType) CoreEventType(eu.bcvsolutions.idm.core.api.event.CoreEvent.CoreEventType) EventType(eu.bcvsolutions.idm.core.api.event.EventType) CoreEvent(eu.bcvsolutions.idm.core.api.event.CoreEvent) TestContentTwo(eu.bcvsolutions.idm.core.event.TestContentTwo) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 2 with EventType

use of eu.bcvsolutions.idm.core.api.event.EventType in project CzechIdMng by bcvsolutions.

the class AbstractEventableDtoController method saveDto.

/**
 * {@inheritDoc}
 *
 * Calls save method by event publishing with higher priority.
 *
 * @param dto
 * @return
 */
@Override
public DTO saveDto(DTO dto, BasePermission... permission) {
    Assert.notNull(dto, "DTO is required");
    // UI actions has higher priority
    EventType eventType = getService().isNew(dto) ? CoreEventType.CREATE : CoreEventType.UPDATE;
    Map<String, Serializable> properties = new HashMap<>();
    properties.put(EntityEventManager.EVENT_PROPERTY_PRIORITY, PriorityType.HIGH);
    CoreEvent<DTO> event = new CoreEvent<DTO>(eventType, validateDto(dto), properties);
    // 
    return getService().publish(event, permission).getContent();
}
Also used : Serializable(java.io.Serializable) CoreEventType(eu.bcvsolutions.idm.core.api.event.CoreEvent.CoreEventType) EventType(eu.bcvsolutions.idm.core.api.event.EventType) HashMap(java.util.HashMap) CoreEvent(eu.bcvsolutions.idm.core.api.event.CoreEvent)

Example 3 with EventType

use of eu.bcvsolutions.idm.core.api.event.EventType in project CzechIdMng by bcvsolutions.

the class DefaultEntityEventManager method putToQueue.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void putToQueue(IdmEntityEventDto entityEvent) {
    if (entityEvent.getPriority() == PriorityType.IMMEDIATE) {
        LOG.trace("Event type [{}] for owner with id [{}] will be executed synchronously.", entityEvent.getEventType(), entityEvent.getOwnerId());
        executeEvent(entityEvent);
        return;
    }
    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);
        executeEvent(entityEvent);
        return;
    }
    // 
    // get enabled processors
    final EntityEvent<Identifiable> event = toEvent(entityEvent);
    List<EntityEventProcessor> registeredProcessors = context.getBeansOfType(EntityEventProcessor.class).values().stream().filter(enabledEvaluator::isEnabled).filter(processor -> !processor.isDisabled()).filter(processor -> processor.supports(event)).filter(processor -> processor.conditional(event)).sorted(new AnnotationAwareOrderComparator()).collect(Collectors.toList());
    if (registeredProcessors.isEmpty()) {
        LOG.debug("Event type [{}] for owner with id [{}] will not be executed, no enabled processor is registered.", entityEvent.getEventType(), entityEvent.getOwnerId());
        return;
    }
    // 
    // evaluate event priority by registered processors
    PriorityType priority = evaluatePriority(event, registeredProcessors);
    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());
        executeEvent(entityEvent);
        return;
    }
    // 
    // 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
    entityEventService.save(entityEvent);
}
Also used : Auditable(eu.bcvsolutions.idm.core.api.domain.Auditable) DefaultEventContext(eu.bcvsolutions.idm.core.api.event.DefaultEventContext) IdmEntityEventDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto) NotificationManager(eu.bcvsolutions.idm.core.notification.api.service.NotificationManager) 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) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) Map(java.util.Map) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Sort(org.springframework.data.domain.Sort) Objects(com.google.common.base.Objects) IdmEntityStateService(eu.bcvsolutions.idm.core.api.service.IdmEntityStateService) EntityEventProcessorFilter(eu.bcvsolutions.idm.core.api.dto.filter.EntityEventProcessorFilter) NotificationLevel(eu.bcvsolutions.idm.core.notification.api.domain.NotificationLevel) ImmutableMap(com.google.common.collect.ImmutableMap) AbstractEntity(eu.bcvsolutions.idm.core.api.entity.AbstractEntity) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PageRequest(org.springframework.data.domain.PageRequest) IdmEntityStateFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmEntityStateFilter) UUID(java.util.UUID) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) PriorityType(eu.bcvsolutions.idm.core.api.domain.PriorityType) Serializable(java.io.Serializable) IdmEntityStateDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityStateDto) List(java.util.List) EventConfiguration(eu.bcvsolutions.idm.core.api.config.domain.EventConfiguration) CreatedComparator(eu.bcvsolutions.idm.core.api.domain.comparator.CreatedComparator) Entry(java.util.Map.Entry) Identifiable(eu.bcvsolutions.idm.core.api.domain.Identifiable) EventType(eu.bcvsolutions.idm.core.api.event.EventType) IdmEntityEventRepository(eu.bcvsolutions.idm.core.model.repository.IdmEntityEventRepository) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) EntityEventProcessorDto(eu.bcvsolutions.idm.core.api.dto.EntityEventProcessorDto) AnnotationAwareOrderComparator(org.springframework.core.annotation.AnnotationAwareOrderComparator) EntityEventProcessor(eu.bcvsolutions.idm.core.api.event.EntityEventProcessor) Scheduled(org.springframework.scheduling.annotation.Scheduled) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) Lists(com.google.common.collect.Lists) LookupService(eu.bcvsolutions.idm.core.api.service.LookupService) Propagation(org.springframework.transaction.annotation.Propagation) CoreModuleDescriptor(eu.bcvsolutions.idm.core.CoreModuleDescriptor) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) EntityUtils(eu.bcvsolutions.idm.core.api.utils.EntityUtils) EventResult(eu.bcvsolutions.idm.core.api.event.EventResult) Direction(org.springframework.data.domain.Sort.Direction) EntityEvent(eu.bcvsolutions.idm.core.api.event.EntityEvent) EntityEventType(eu.bcvsolutions.idm.core.api.event.EntityEventEvent.EntityEventType) SchedulerConfiguration(eu.bcvsolutions.idm.core.scheduler.api.config.SchedulerConfiguration) DateTime(org.joda.time.DateTime) OperationState(eu.bcvsolutions.idm.core.api.domain.OperationState) EnabledEvaluator(eu.bcvsolutions.idm.core.security.api.service.EnabledEvaluator) ApplicationContext(org.springframework.context.ApplicationContext) EventContext(eu.bcvsolutions.idm.core.api.event.EventContext) AsyncEntityEventProcessor(eu.bcvsolutions.idm.core.api.event.AsyncEntityEventProcessor) ConfigurationMap(eu.bcvsolutions.idm.core.api.domain.ConfigurationMap) IdmEntityEventService(eu.bcvsolutions.idm.core.api.service.IdmEntityEventService) CoreResultCode(eu.bcvsolutions.idm.core.api.domain.CoreResultCode) CoreEventType(eu.bcvsolutions.idm.core.api.event.CoreEvent.CoreEventType) IdmMessageDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto) EventContentDeletedException(eu.bcvsolutions.idm.core.api.exception.EventContentDeletedException) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) EntityEventManager(eu.bcvsolutions.idm.core.api.service.EntityEventManager) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) PriorityType(eu.bcvsolutions.idm.core.api.domain.PriorityType) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) AnnotationAwareOrderComparator(org.springframework.core.annotation.AnnotationAwareOrderComparator) Identifiable(eu.bcvsolutions.idm.core.api.domain.Identifiable) LinkedHashMap(java.util.LinkedHashMap) EntityEventProcessor(eu.bcvsolutions.idm.core.api.event.EntityEventProcessor) AsyncEntityEventProcessor(eu.bcvsolutions.idm.core.api.event.AsyncEntityEventProcessor)

Example 4 with EventType

use of eu.bcvsolutions.idm.core.api.event.EventType in project CzechIdMng by bcvsolutions.

the class DefaultEntityEventManager method toEvent.

@Override
public EntityEvent<Identifiable> toEvent(IdmEntityEventDto entityEvent) {
    Identifiable content = null;
    // only if type and id is the same as owner can be used
    if (entityEvent.getContent() != null && entityEvent.getContent().getClass().getCanonicalName().equals(entityEvent.getOwnerType()) && entityEvent.getContent().getId().equals(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(EVENT_PROPERTY_EVENT_ID, entityEvent.getId());
    eventProperties.put(EVENT_PROPERTY_PRIORITY, entityEvent.getPriority());
    eventProperties.put(EVENT_PROPERTY_EXECUTE_DATE, entityEvent.getExecuteDate());
    eventProperties.put(EVENT_PROPERTY_PARENT_EVENT_TYPE, entityEvent.getParentEventType());
    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);
    resurectedEvent.setOriginalSource(entityEvent.getOriginalSource());
    // 
    return resurectedEvent;
}
Also used : Serializable(java.io.Serializable) EventType(eu.bcvsolutions.idm.core.api.event.EventType) EntityEventType(eu.bcvsolutions.idm.core.api.event.EntityEventEvent.EntityEventType) CoreEventType(eu.bcvsolutions.idm.core.api.event.CoreEvent.CoreEventType) 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 5 with EventType

use of eu.bcvsolutions.idm.core.api.event.EventType in project CzechIdMng by bcvsolutions.

the class DefaultEntityEventManager method init.

/**
 * Cancel all previously ran events
 */
@Override
public void init() {
    LOG.info("Cancel unprocessed events - event was interrupt during instance restart");
    // 
    String instanceId = configurationService.getInstanceId();
    entityEventService.findByState(instanceId, OperationState.RUNNING).forEach(event -> {
        LOG.info("Cancel unprocessed event [{}] - event was interrupt during instance [{}] restart", event.getId(), instanceId);
        // 
        // cancel event
        ResultModel resultModel = new DefaultResultModel(CoreResultCode.EVENT_CANCELED_BY_RESTART, ImmutableMap.of("eventId", event.getId(), "eventType", event.getEventType(), "ownerId", String.valueOf(event.getOwnerId()), "instanceId", event.getInstanceId()));
        OperationResultDto result = new OperationResultDto.Builder(OperationState.CANCELED).setModel(resultModel).build();
        event.setResult(result);
        entityEventService.saveInternal(event);
        // 
        // cancel event states
        IdmEntityStateFilter filter = new IdmEntityStateFilter();
        filter.setEventId(event.getId());
        entityStateService.find(filter, null).getContent().stream().filter(state -> {
            return OperationState.RUNNING == state.getResult().getState();
        }).forEach(state -> {
            event.setResult(result);
            entityStateService.save(state);
        });
    });
}
Also used : Auditable(eu.bcvsolutions.idm.core.api.domain.Auditable) DefaultEventContext(eu.bcvsolutions.idm.core.api.event.DefaultEventContext) IdmEntityEventDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto) NotificationManager(eu.bcvsolutions.idm.core.notification.api.service.NotificationManager) 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) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) Map(java.util.Map) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Sort(org.springframework.data.domain.Sort) Objects(com.google.common.base.Objects) IdmEntityStateService(eu.bcvsolutions.idm.core.api.service.IdmEntityStateService) EntityEventProcessorFilter(eu.bcvsolutions.idm.core.api.dto.filter.EntityEventProcessorFilter) NotificationLevel(eu.bcvsolutions.idm.core.notification.api.domain.NotificationLevel) ImmutableMap(com.google.common.collect.ImmutableMap) AbstractEntity(eu.bcvsolutions.idm.core.api.entity.AbstractEntity) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PageRequest(org.springframework.data.domain.PageRequest) IdmEntityStateFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmEntityStateFilter) UUID(java.util.UUID) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) PriorityType(eu.bcvsolutions.idm.core.api.domain.PriorityType) Serializable(java.io.Serializable) IdmEntityStateDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityStateDto) List(java.util.List) EventConfiguration(eu.bcvsolutions.idm.core.api.config.domain.EventConfiguration) CreatedComparator(eu.bcvsolutions.idm.core.api.domain.comparator.CreatedComparator) Entry(java.util.Map.Entry) Identifiable(eu.bcvsolutions.idm.core.api.domain.Identifiable) EventType(eu.bcvsolutions.idm.core.api.event.EventType) IdmEntityEventRepository(eu.bcvsolutions.idm.core.model.repository.IdmEntityEventRepository) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) EntityEventProcessorDto(eu.bcvsolutions.idm.core.api.dto.EntityEventProcessorDto) AnnotationAwareOrderComparator(org.springframework.core.annotation.AnnotationAwareOrderComparator) EntityEventProcessor(eu.bcvsolutions.idm.core.api.event.EntityEventProcessor) Scheduled(org.springframework.scheduling.annotation.Scheduled) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) Lists(com.google.common.collect.Lists) LookupService(eu.bcvsolutions.idm.core.api.service.LookupService) Propagation(org.springframework.transaction.annotation.Propagation) CoreModuleDescriptor(eu.bcvsolutions.idm.core.CoreModuleDescriptor) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) EntityUtils(eu.bcvsolutions.idm.core.api.utils.EntityUtils) EventResult(eu.bcvsolutions.idm.core.api.event.EventResult) Direction(org.springframework.data.domain.Sort.Direction) EntityEvent(eu.bcvsolutions.idm.core.api.event.EntityEvent) EntityEventType(eu.bcvsolutions.idm.core.api.event.EntityEventEvent.EntityEventType) SchedulerConfiguration(eu.bcvsolutions.idm.core.scheduler.api.config.SchedulerConfiguration) DateTime(org.joda.time.DateTime) OperationState(eu.bcvsolutions.idm.core.api.domain.OperationState) EnabledEvaluator(eu.bcvsolutions.idm.core.security.api.service.EnabledEvaluator) ApplicationContext(org.springframework.context.ApplicationContext) EventContext(eu.bcvsolutions.idm.core.api.event.EventContext) AsyncEntityEventProcessor(eu.bcvsolutions.idm.core.api.event.AsyncEntityEventProcessor) ConfigurationMap(eu.bcvsolutions.idm.core.api.domain.ConfigurationMap) IdmEntityEventService(eu.bcvsolutions.idm.core.api.service.IdmEntityEventService) CoreResultCode(eu.bcvsolutions.idm.core.api.domain.CoreResultCode) CoreEventType(eu.bcvsolutions.idm.core.api.event.CoreEvent.CoreEventType) IdmMessageDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto) EventContentDeletedException(eu.bcvsolutions.idm.core.api.exception.EventContentDeletedException) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) EntityEventManager(eu.bcvsolutions.idm.core.api.service.EntityEventManager) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) IdmEntityStateFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmEntityStateFilter)

Aggregations

CoreEvent (eu.bcvsolutions.idm.core.api.event.CoreEvent)5 CoreEventType (eu.bcvsolutions.idm.core.api.event.CoreEvent.CoreEventType)5 EventType (eu.bcvsolutions.idm.core.api.event.EventType)5 Serializable (java.io.Serializable)4 Identifiable (eu.bcvsolutions.idm.core.api.domain.Identifiable)3 DefaultEventContext (eu.bcvsolutions.idm.core.api.event.DefaultEventContext)3 EntityEventType (eu.bcvsolutions.idm.core.api.event.EntityEventEvent.EntityEventType)3 EventContentDeletedException (eu.bcvsolutions.idm.core.api.exception.EventContentDeletedException)3 Objects (com.google.common.base.Objects)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Lists (com.google.common.collect.Lists)2 CoreModuleDescriptor (eu.bcvsolutions.idm.core.CoreModuleDescriptor)2 EventConfiguration (eu.bcvsolutions.idm.core.api.config.domain.EventConfiguration)2 Auditable (eu.bcvsolutions.idm.core.api.domain.Auditable)2 ConfigurationMap (eu.bcvsolutions.idm.core.api.domain.ConfigurationMap)2 CoreResultCode (eu.bcvsolutions.idm.core.api.domain.CoreResultCode)2 OperationState (eu.bcvsolutions.idm.core.api.domain.OperationState)2 PriorityType (eu.bcvsolutions.idm.core.api.domain.PriorityType)2 CreatedComparator (eu.bcvsolutions.idm.core.api.domain.comparator.CreatedComparator)2 AbstractDto (eu.bcvsolutions.idm.core.api.dto.AbstractDto)2