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