Search in sources :

Example 36 with CoreEvent

use of eu.bcvsolutions.idm.core.api.event.CoreEvent 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)

Example 37 with CoreEvent

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

the class DefaultFormServiceIntegrationTest method testPropagateHighEventPriority.

@Test
public void testPropagateHighEventPriority() {
    try {
        getHelper().setConfigurationValue(EventConfiguration.PROPERTY_EVENT_ASYNCHRONOUS_ENABLED, true);
        Assert.assertTrue(eventConfiguration.isAsynchronous());
        Assert.assertTrue(entityEventManager.isAsynchronous());
        // 
        FormableDto owner = getHelper().createIdentity((GuardedString) null);
        getHelper().waitForResult(null, 1, 1);
        // 
        // create definition one
        IdmFormAttributeDto attributeDefinitionOne = new IdmFormAttributeDto();
        attributeDefinitionOne.setCode(getHelper().createName());
        attributeDefinitionOne.setName(attributeDefinitionOne.getCode());
        attributeDefinitionOne.setPersistentType(PersistentType.SHORTTEXT);
        IdmFormDefinitionDto formDefinitionOne = formService.createDefinition(IdmIdentity.class.getCanonicalName(), getHelper().createName(), Lists.newArrayList(attributeDefinitionOne));
        attributeDefinitionOne = formDefinitionOne.getMappedAttributeByCode(attributeDefinitionOne.getCode());
        // 
        IdmFormValueDto value1 = new IdmFormValueDto(attributeDefinitionOne);
        value1.setValue(FORM_VALUE_ONE);
        IdmFormInstanceDto formInstance = new IdmFormInstanceDto(owner, formDefinitionOne, Lists.newArrayList(value1));
        // prepare event envelope
        CoreEvent<IdmFormInstanceDto> event = new CoreEvent<IdmFormInstanceDto>(CoreEventType.UPDATE, formInstance);
        // FE - high event priority
        event.setPriority(PriorityType.HIGH);
        // publish event for save form instance
        formService.publish(event);
        // 
        Assert.assertEquals(FORM_VALUE_ONE, formService.getValues(owner, formDefinitionOne).get(0).getShortTextValue());
        // 
        IdmEntityEventFilter filter = new IdmEntityEventFilter();
        filter.setOwnerId(owner.getId());
        filter.setEventType(IdentityEventType.NOTIFY.name());
        // 
        List<IdmEntityEventDto> events = entityEventService.find(filter, PageRequest.of(0, 1, new Sort(Direction.DESC, IdmEntityEvent_.created.getName()))).getContent();
        // 
        Assert.assertFalse(events.isEmpty());
        Assert.assertEquals(PriorityType.HIGH, events.get(0).getPriority());
    } finally {
        getHelper().setConfigurationValue(EventConfiguration.PROPERTY_EVENT_ASYNCHRONOUS_ENABLED, false);
        Assert.assertFalse(eventConfiguration.isAsynchronous());
    }
}
Also used : IdmFormInstanceDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto) FormableDto(eu.bcvsolutions.idm.core.api.dto.FormableDto) IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) CoreEvent(eu.bcvsolutions.idm.core.api.event.CoreEvent) Sort(org.springframework.data.domain.Sort) IdmFormDefinitionDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto) IdmFormValueDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormValueDto) IdmEntityEventFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmEntityEventFilter) IdmIdentity(eu.bcvsolutions.idm.core.model.entity.IdmIdentity) IdmEntityEventDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto) DefaultAttachmentManagerIntegrationTest(eu.bcvsolutions.idm.core.ecm.service.impl.DefaultAttachmentManagerIntegrationTest) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 38 with CoreEvent

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

the class DefaultIdmRoleServiceUnitTest method testUpdateFailureCodeChangeWithEnvironment.

@Test(expected = ResultCodeException.class)
public void testUpdateFailureCodeChangeWithEnvironment() {
    IdmRoleDto role = new IdmRoleDto();
    role.setCode("code");
    role.setBaseCode("code");
    role.setEnvironment("env");
    // 
    IdmRoleDto roleUpdate = new IdmRoleDto();
    roleUpdate.setCode("code2");
    roleUpdate.setBaseCode("code");
    role.setEnvironment("env2");
    // 
    RoleCodeEnvironmentProcessor roleCodeEnvironmentProcessor = new RoleCodeEnvironmentProcessor(service);
    CoreEvent<IdmRoleDto> updateEvent = new CoreEvent<IdmRoleDto>(CoreEventType.CREATE, roleUpdate);
    updateEvent.setOriginalSource(role);
    roleCodeEnvironmentProcessor.process(updateEvent);
}
Also used : IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) CoreEvent(eu.bcvsolutions.idm.core.api.event.CoreEvent) RoleCodeEnvironmentProcessor(eu.bcvsolutions.idm.core.model.event.processor.role.RoleCodeEnvironmentProcessor) Test(org.junit.Test) AbstractUnitTest(eu.bcvsolutions.idm.test.api.AbstractUnitTest)

Example 39 with CoreEvent

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

the class DefaultIdmRoleServiceUnitTest method testChangeRoleEnvironment.

@Test
public void testChangeRoleEnvironment() {
    when(roleConfiguration.getCodeEnvironmentSeperator()).thenReturn(RoleConfiguration.DEFAULT_CODE_ENVIRONMENT_SEPARATOR);
    // 
    String code = "code";
    String env = "env";
    IdmRoleDto role = new IdmRoleDto();
    role.setBaseCode(code);
    role.setEnvironment(env);
    // 
    RoleCodeEnvironmentProcessor roleCodeEnvironmentProcessor = new RoleCodeEnvironmentProcessor(service);
    IdmRoleDto result = roleCodeEnvironmentProcessor.process(new CoreEvent<IdmRoleDto>(CoreEventType.CREATE, role)).getEvent().getContent();
    // 
    Assert.assertEquals(service.getCodeWithEnvironment(role), result.getCode());
    // 
    role.setBaseCode(code);
    role.setEnvironment(env);
    role.setCode(service.getCodeWithEnvironment(role));
    // 
    result = roleCodeEnvironmentProcessor.process(new CoreEvent<IdmRoleDto>(CoreEventType.CREATE, role)).getEvent().getContent();
    // 
    Assert.assertEquals(service.getCodeWithEnvironment(role), result.getCode());
    // 
    // after environment change
    IdmRoleDto roleUpdate = new IdmRoleDto();
    roleUpdate.setBaseCode(code);
    String envUpdate = "envU";
    roleUpdate.setEnvironment(envUpdate);
    // 
    CoreEvent<IdmRoleDto> updateEvent = new CoreEvent<IdmRoleDto>(CoreEventType.CREATE, roleUpdate);
    updateEvent.setOriginalSource(result);
    IdmRoleDto resultUpdate = roleCodeEnvironmentProcessor.process(updateEvent).getEvent().getContent();
    // 
    Assert.assertEquals(service.getCodeWithEnvironment(roleUpdate), resultUpdate.getCode());
    Assert.assertEquals(code + RoleConfiguration.DEFAULT_CODE_ENVIRONMENT_SEPARATOR + envUpdate, resultUpdate.getCode());
}
Also used : IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) CoreEvent(eu.bcvsolutions.idm.core.api.event.CoreEvent) RoleCodeEnvironmentProcessor(eu.bcvsolutions.idm.core.model.event.processor.role.RoleCodeEnvironmentProcessor) Test(org.junit.Test) AbstractUnitTest(eu.bcvsolutions.idm.test.api.AbstractUnitTest)

Example 40 with CoreEvent

use of eu.bcvsolutions.idm.core.api.event.CoreEvent 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.");
    // 
    if (this.isRequestModeEnabled()) {
        throw new ResultCodeException(CoreResultCode.REQUEST_CUD_OPERATIONS_NOT_ALLOWED, ImmutableMap.of("controller", this.getClass().getSimpleName()));
    }
    // UI actions has higher priority
    EventType eventType = getService().isNew(dto) ? CoreEventType.CREATE : CoreEventType.UPDATE;
    CoreEvent<DTO> event = new CoreEvent<DTO>(eventType, validateDto(dto));
    event.setPriority(PriorityType.HIGH);
    // 
    return getService().publish(event, permission).getContent();
}
Also used : 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) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException)

Aggregations

CoreEvent (eu.bcvsolutions.idm.core.api.event.CoreEvent)46 Test (org.junit.Test)27 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)17 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)11 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)10 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)9 UUID (java.util.UUID)9 Transactional (org.springframework.transaction.annotation.Transactional)8 BaseDto (eu.bcvsolutions.idm.core.api.dto.BaseDto)7 IdmEntityEventDto (eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto)7 CoreEventType (eu.bcvsolutions.idm.core.api.event.CoreEvent.CoreEventType)7 IdmFormInstanceDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto)7 AbstractUnitTest (eu.bcvsolutions.idm.test.api.AbstractUnitTest)7 Serializable (java.io.Serializable)7 RoleCodeEnvironmentProcessor (eu.bcvsolutions.idm.core.model.event.processor.role.RoleCodeEnvironmentProcessor)6 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)6 IdmFormDefinitionDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto)5 TestContent (eu.bcvsolutions.idm.core.event.TestContent)5 ArrayList (java.util.ArrayList)5 IdmEntityEventFilter (eu.bcvsolutions.idm.core.api.dto.filter.IdmEntityEventFilter)4