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));
}
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;
}
}
Aggregations