Search in sources :

Example 1 with EmptyEntityEventProcessor

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

the class DefaultEntityEventManagerIntergationTest method testSaveStates.

@Test
@Transactional
public void testSaveStates() {
    MockOwner mockOwner = new MockOwner();
    // 
    IdmEntityEventDto entityEventDto = manager.prepareEvent(mockOwner, null);
    entityEventDto.setResult(new OperationResultDto.Builder(OperationState.RUNNING).build());
    entityEventDto.setEventType(CoreEventType.CREATE.name());
    entityEventDto.setPriority(PriorityType.HIGH);
    entityEventDto = manager.saveEvent(entityEventDto);
    // 
    List<IdmEntityStateDto> states = manager.saveStates(manager.toEvent(entityEventDto), null, null);
    Assert.assertEquals(1, states.size());
    Assert.assertTrue(states.stream().anyMatch(s -> s.getResult().getState() == OperationState.EXECUTED));
    // 
    states = manager.saveStates(manager.toEvent(entityEventDto), states, new DefaultEventResult.Builder<>(manager.toEvent(entityEventDto), new EmptyEntityEventProcessor<>()).build());
    // 
    Assert.assertEquals(1, states.size());
    Assert.assertTrue(states.stream().anyMatch(s -> s.getResult().getState() == OperationState.EXECUTED));
    // 
    EventResult<?> result = new DefaultEventResult.Builder<>(manager.toEvent(entityEventDto), new EmptyEntityEventProcessor<>()).setResults(Lists.newArrayList(new OperationResult.Builder(OperationState.EXCEPTION).build(), new OperationResult.Builder(OperationState.CANCELED).build())).build();
    // 
    states = manager.saveStates(manager.toEvent(entityEventDto), null, result);
    // 
    Assert.assertEquals(2, states.size());
    Assert.assertTrue(states.stream().anyMatch(s -> s.getResult().getState() == OperationState.EXCEPTION));
    Assert.assertTrue(states.stream().anyMatch(s -> s.getResult().getState() == OperationState.CANCELED));
    // 
    result = new DefaultEventResult.Builder<>(manager.toEvent(entityEventDto), new EmptyEntityEventProcessor<>()).setResults(Lists.newArrayList(new OperationResult.Builder(OperationState.BLOCKED).build())).build();
    states = manager.saveStates(manager.toEvent(entityEventDto), states, result);
    // 
    Assert.assertEquals(1, states.size());
    Assert.assertTrue(states.stream().anyMatch(s -> s.getResult().getState() == OperationState.BLOCKED));
}
Also used : IdmEntityStateDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityStateDto) DefaultEventContext(eu.bcvsolutions.idm.core.api.event.DefaultEventContext) IdmEntityEventDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto) AcceptedContent(eu.bcvsolutions.idm.core.event.AcceptedContent) IdmIdentityRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityRoleDto) EntityGenerateValuesProcessor(eu.bcvsolutions.idm.core.model.event.processor.EntityGenerateValuesProcessor) Autowired(org.springframework.beans.factory.annotation.Autowired) 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) MockDto(eu.bcvsolutions.idm.core.event.domain.MockDto) TransactionContextHolder(eu.bcvsolutions.idm.core.api.domain.TransactionContextHolder) IdentityEventType(eu.bcvsolutions.idm.core.model.event.IdentityEvent.IdentityEventType) ObserveDtoProcessor(eu.bcvsolutions.idm.core.model.event.processor.ObserveDtoProcessor) TestEntityEventProcessorConfiguration(eu.bcvsolutions.idm.core.event.TestEntityEventProcessorConfiguration) EntityEventProcessorFilter(eu.bcvsolutions.idm.core.api.dto.filter.EntityEventProcessorFilter) IdmRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleRequestDto) IdentityContractEventType(eu.bcvsolutions.idm.core.model.event.IdentityContractEvent.IdentityContractEventType) PageRequest(org.springframework.data.domain.PageRequest) IdmEntityStateFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmEntityStateFilter) UUID(java.util.UUID) 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) NeverEndingProcessor(eu.bcvsolutions.idm.core.model.event.processor.NeverEndingProcessor) EventType(eu.bcvsolutions.idm.core.api.event.EventType) IdentityContractEvent(eu.bcvsolutions.idm.core.model.event.IdentityContractEvent) IdentityEvent(eu.bcvsolutions.idm.core.model.event.IdentityEvent) EntityEventProcessorDto(eu.bcvsolutions.idm.core.api.dto.EntityEventProcessorDto) IdmIdentityService(eu.bcvsolutions.idm.core.api.service.IdmIdentityService) RoleRequestEventType(eu.bcvsolutions.idm.core.model.event.RoleRequestEvent.RoleRequestEventType) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) TestContentTwo(eu.bcvsolutions.idm.core.event.TestContentTwo) EntityEventProcessor(eu.bcvsolutions.idm.core.api.event.EntityEventProcessor) MockOwner(eu.bcvsolutions.idm.core.event.domain.MockOwner) ArrayList(java.util.ArrayList) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) EntityStateManager(eu.bcvsolutions.idm.core.api.service.EntityStateManager) Lists(com.google.common.collect.Lists) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) Qualifier(org.springframework.beans.factory.annotation.Qualifier) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) DefaultEventResult(eu.bcvsolutions.idm.core.api.event.DefaultEventResult) EventResult(eu.bcvsolutions.idm.core.api.event.EventResult) IdentityRoleEventType(eu.bcvsolutions.idm.core.model.event.IdentityRoleEvent.IdentityRoleEventType) IdmIdentityContractDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto) EntityEvent(eu.bcvsolutions.idm.core.api.event.EntityEvent) Before(org.junit.Before) IdmIdentityRoleService(eu.bcvsolutions.idm.core.api.service.IdmIdentityRoleService) ConditionalContent(eu.bcvsolutions.idm.core.event.ConditionalContent) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) TestIdentityNotifyProcessor(eu.bcvsolutions.idm.core.model.event.processor.TestIdentityNotifyProcessor) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) IdmEntityEventFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmEntityEventFilter) Assert.assertTrue(org.junit.Assert.assertTrue) OperationState(eu.bcvsolutions.idm.core.api.domain.OperationState) Test(org.junit.Test) ApplicationContext(org.springframework.context.ApplicationContext) EventContext(eu.bcvsolutions.idm.core.api.event.EventContext) EmptyEntityEventProcessor(eu.bcvsolutions.idm.core.api.event.EmptyEntityEventProcessor) IdmEntityEventService(eu.bcvsolutions.idm.core.api.service.IdmEntityEventService) IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) CoreResultCode(eu.bcvsolutions.idm.core.api.domain.CoreResultCode) CoreEventType(eu.bcvsolutions.idm.core.api.event.CoreEvent.CoreEventType) TestContent(eu.bcvsolutions.idm.core.event.TestContent) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) Transactional(org.springframework.transaction.annotation.Transactional) MockOwner(eu.bcvsolutions.idm.core.event.domain.MockOwner) EmptyEntityEventProcessor(eu.bcvsolutions.idm.core.api.event.EmptyEntityEventProcessor) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) DefaultEventResult(eu.bcvsolutions.idm.core.api.event.DefaultEventResult) IdmEntityEventDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with EmptyEntityEventProcessor

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

the class DefaultEntityEventManager method process.

@Override
@Transactional
@SuppressWarnings("unchecked")
public <E extends Serializable> EventContext<E> process(EntityEvent<E> event, EntityEvent<?> parentEvent) {
    Assert.notNull(event, "Event is required for processing.");
    Serializable content = event.getContent();
    // 
    LOG.info("Publishing event [{}]", event);
    // 
    // continue suspended event
    event.getContext().setSuspended(false);
    // 
    if (parentEvent != null) {
        event.setParentId(parentEvent.getId());
        event.setRootId(parentEvent.getRootId() == null ? parentEvent.getId() : parentEvent.getRootId());
        if (parentEvent.getPriority() != null && (event.getPriority() == null || event.getPriority().getPriority() < parentEvent.getPriority().getPriority())) {
            // parent has higher priority ... execute with the same priority as parent
            event.setPriority(parentEvent.getPriority());
        }
        // parent event type can be preset manually
        if (StringUtils.isEmpty(event.getParentType())) {
            event.setParentType(parentEvent.getType().name());
        }
        // propagate properties from parent to child event.
        // properties need for internal event processing are ignored (see {@link EntityEvent} properties)
        propagateProperties(event, parentEvent);
    }
    // read previous (original) dto source - usable in "check modification" processors
    if (event.getOriginalSource() == null && (content instanceof AbstractDto)) {
        // original source could be set externally
        AbstractDto contentDto = (AbstractDto) content;
        // works only for dto modification
        if (contentDto.getId() != null && lookupService.getDtoLookup(contentDto.getClass()) != null) {
            event.setOriginalSource((E) lookupService.lookupDto(contentDto.getClass(), contentDto.getId()));
        }
    }
    // event is persisted automatically, when parent event is persisted
    try {
        if (content instanceof BaseDto && event.getId() == null && event.getParentId() != null && lookupService.getEntityClass(((BaseDto) content).getClass()) != null) {
            // entity event can be persisted into queue only
            BaseDto dto = (BaseDto) content;
            if (dto.getId() == null) {
                // prepare id for new content - event is persisted before entity is persisted.
                dto.setId(UUID.randomUUID());
            }
            // 
            IdmEntityEventDto preparedEvent = toDto(dto, (EntityEvent<AbstractDto>) event);
            // RUNNING => prevent to start by async task
            preparedEvent.setResult(new OperationResultDto.Builder(OperationState.RUNNING).build());
            preparedEvent.setRootId(event.getRootId() == null ? event.getParentId() : event.getRootId());
            preparedEvent = entityEventService.save(preparedEvent);
            event.setId(preparedEvent.getId());
            // 
            // prepared event is be executed
            CoreEvent<IdmEntityEventDto> executeEvent = new CoreEvent<>(EntityEventType.EXECUTE, preparedEvent);
            publisher.publishEvent(executeEvent);
            // 
            // fill original event result
            E processedContent = (E) preparedEvent.getContent();
            if (processedContent != null) {
                event.setContent(processedContent);
            }
            event.getContext().addResult(new DefaultEventResult<E>(event, new EmptyEntityEventProcessor<E>()));
            // 
            return completeEvent(event);
        } else {
            publisher.publishEvent(event);
            // 
            return completeEvent(event);
        }
    } catch (Exception ex) {
        completeEvent(event);
        // 
        throw ex;
    }
}
Also used : Serializable(java.io.Serializable) CoreEvent(eu.bcvsolutions.idm.core.api.event.CoreEvent) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) EmptyEntityEventProcessor(eu.bcvsolutions.idm.core.api.event.EmptyEntityEventProcessor) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) EventContentDeletedException(eu.bcvsolutions.idm.core.api.exception.EventContentDeletedException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IdmEntityEventDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

AbstractDto (eu.bcvsolutions.idm.core.api.dto.AbstractDto)2 BaseDto (eu.bcvsolutions.idm.core.api.dto.BaseDto)2 IdmEntityEventDto (eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto)2 CoreEvent (eu.bcvsolutions.idm.core.api.event.CoreEvent)2 EmptyEntityEventProcessor (eu.bcvsolutions.idm.core.api.event.EmptyEntityEventProcessor)2 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)2 Serializable (java.io.Serializable)2 Transactional (org.springframework.transaction.annotation.Transactional)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Lists (com.google.common.collect.Lists)1 EventConfiguration (eu.bcvsolutions.idm.core.api.config.domain.EventConfiguration)1 CoreResultCode (eu.bcvsolutions.idm.core.api.domain.CoreResultCode)1 OperationState (eu.bcvsolutions.idm.core.api.domain.OperationState)1 PriorityType (eu.bcvsolutions.idm.core.api.domain.PriorityType)1 TransactionContextHolder (eu.bcvsolutions.idm.core.api.domain.TransactionContextHolder)1 EntityEventProcessorDto (eu.bcvsolutions.idm.core.api.dto.EntityEventProcessorDto)1 IdmEntityStateDto (eu.bcvsolutions.idm.core.api.dto.IdmEntityStateDto)1 IdmIdentityContractDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto)1 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)1 IdmIdentityRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityRoleDto)1