use of eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto in project CzechIdMng by bcvsolutions.
the class DefaultEntityEventManager method saveResult.
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public IdmEntityEventDto saveResult(UUID eventId, OperationResultDto result) {
Assert.notNull(eventId);
Assert.notNull(result);
IdmEntityEventDto entityEvent = entityEventService.get(eventId);
Assert.notNull(entityEvent);
//
entityEvent.setResult(result);
return entityEventService.save(entityEvent);
}
use of eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto in project CzechIdMng by bcvsolutions.
the class DefaultEntityEventManager method getCreatedEvents.
/**
* Called from scheduler - concurrency is prevented.
* Returns events to process sorted by priority 7 / 3 (high / normal).
* Immediate priority is executed synchronously.
* Cancel duplicate events (same type, owner and props) - last event is returned
*
* @param instanceId
* @return
*/
protected List<IdmEntityEventDto> getCreatedEvents(String instanceId) {
Assert.notNull(instanceId);
//
// load created events - high priority
DateTime executeDate = new DateTime();
Page<IdmEntityEventDto> highEvents = entityEventService.findToExecute(instanceId, executeDate, PriorityType.HIGH, new PageRequest(0, 100, new Sort(Direction.ASC, Auditable.PROPERTY_CREATED)));
// load created events - low priority
Page<IdmEntityEventDto> normalEvents = entityEventService.findToExecute(instanceId, executeDate, PriorityType.NORMAL, new PageRequest(0, 100, new Sort(Direction.ASC, Auditable.PROPERTY_CREATED)));
// merge events
List<IdmEntityEventDto> events = new ArrayList<>();
events.addAll(highEvents.getContent());
events.addAll(normalEvents.getContent());
// sort by created date
events.sort(new CreatedComparator());
//
// cancel duplicates - by owner => properties has to be the same
// execute the first event for each owner only - preserve events order
Map<UUID, IdmEntityEventDto> distinctEvents = new LinkedHashMap<>();
events.forEach(event -> {
if (!distinctEvents.containsKey(event.getOwnerId())) {
// the first event
distinctEvents.put(event.getOwnerId(), event);
} else {
// cancel duplicate older event
IdmEntityEventDto olderEvent = distinctEvents.get(event.getOwnerId());
if (isDuplicate(olderEvent, event)) {
// try to set higher priority
if (olderEvent.getPriority() == PriorityType.HIGH) {
event.setPriority(PriorityType.HIGH);
}
distinctEvents.put(event.getOwnerId(), event);
//
LOG.debug(new DefaultResultModel(CoreResultCode.EVENT_DUPLICATE_CANCELED, ImmutableMap.of("eventId", olderEvent.getId(), "eventType", String.valueOf(olderEvent.getEventType()), "ownerId", String.valueOf(olderEvent.getOwnerId()), "instanceId", String.valueOf(olderEvent.getInstanceId()), "neverEventId", event.getId())).toString());
//
if (entityEventRepository.countByParentId(olderEvent.getId()) == 0) {
entityEventService.delete(olderEvent);
}
}
}
});
//
// sort by priority
events = distinctEvents.values().stream().sorted((o1, o2) -> {
return Integer.compare(o1.getPriority().getPriority(), o2.getPriority().getPriority());
}).collect(Collectors.toList());
int normalCount = events.stream().filter(e -> e.getPriority() == PriorityType.NORMAL).collect(Collectors.toList()).size();
int highMaximum = normalCount > 30 ? 70 : (100 - normalCount);
// evaluate priority => high 70 / low 30
int highCounter = 0;
List<IdmEntityEventDto> prioritizedEvents = new ArrayList<>();
for (IdmEntityEventDto event : events) {
if (event.getPriority() == PriorityType.HIGH) {
if (highCounter < highMaximum) {
prioritizedEvents.add(event);
highCounter++;
}
} else {
// normal priority remains only
if (prioritizedEvents.size() >= 100) {
break;
}
prioritizedEvents.add(event);
}
}
//
return prioritizedEvents;
}
use of eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto in project CzechIdMng by bcvsolutions.
the class DefaultEntityEventManager method createEvent.
private IdmEntityEventDto createEvent(Class<? extends Identifiable> ownerType, UUID ownerId, EntityEvent<? extends Identifiable> originalEvent) {
Assert.notNull(ownerType);
Assert.notNull(ownerId, "Change can be published after entity id is assigned at least.");
//
IdmEntityEventDto savedEvent = new IdmEntityEventDto();
savedEvent.setOwnerId(ownerId);
savedEvent.setOwnerType(getOwnerType(ownerType));
savedEvent.setResult(new OperationResultDto.Builder(OperationState.CREATED).build());
savedEvent.setInstanceId(eventConfiguration.getAsynchronousInstanceId());
//
if (originalEvent != null) {
savedEvent.setEventType(originalEvent.getType().name());
savedEvent.getProperties().putAll(originalEvent.getProperties());
savedEvent.setParent(EntityUtils.toUuid(originalEvent.getProperties().get(EVENT_PROPERTY_EVENT_ID)));
savedEvent.setExecuteDate((DateTime) originalEvent.getProperties().get(EVENT_PROPERTY_EXECUTE_DATE));
savedEvent.setPriority((PriorityType) originalEvent.getProperties().get(EVENT_PROPERTY_PRIORITY));
savedEvent.setParentEventType(originalEvent.getType().name());
savedEvent.setContent(originalEvent.getContent());
savedEvent.setOriginalSource(originalEvent.getOriginalSource());
savedEvent.setClosed(originalEvent.isClosed());
if (savedEvent.isClosed()) {
savedEvent.setResult(new OperationResultDto.Builder(OperationState.EXECUTED).setModel(new DefaultResultModel(CoreResultCode.EVENT_ALREADY_CLOSED)).build());
}
savedEvent.setSuspended(originalEvent.isSuspended());
} else {
// notify as default event type
savedEvent.setEventType(CoreEventType.NOTIFY.name());
}
if (savedEvent.getPriority() == null) {
savedEvent.setPriority(PriorityType.NORMAL);
}
//
return savedEvent;
}
use of eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto in project CzechIdMng by bcvsolutions.
the class DefaultEntityEventManager method saveStates.
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public <E extends Serializable> List<IdmEntityStateDto> saveStates(EntityEvent<E> event, List<IdmEntityStateDto> previousStates, EventResult<E> result) {
IdmEntityEventDto entityEvent = getEvent(event);
List<IdmEntityStateDto> results = new ArrayList<>();
if (entityEvent == null) {
return results;
}
// simple drop - we don't need to find and update results, we'll create new ones
if (previousStates != null && !previousStates.isEmpty()) {
previousStates.forEach(state -> {
entityStateService.delete(state);
});
}
//
if (result == null) {
IdmEntityStateDto state = new IdmEntityStateDto(entityEvent);
// default result without model
state.setResult(new OperationResultDto.Builder(OperationState.EXECUTED).build());
results.add(entityStateService.save(state));
return results;
}
if (result.getResults().isEmpty()) {
results.add(entityStateService.save(createState(entityEvent, result, new OperationResultDto.Builder(OperationState.EXECUTED).build())));
return results;
}
result.getResults().forEach(opeartionResult -> {
results.add(entityStateService.save(createState(entityEvent, result, opeartionResult.toDto())));
});
//
return results;
}
use of eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto in project CzechIdMng by bcvsolutions.
the class DefaultEntityEventManager method changedEntity.
@Override
public void changedEntity(Class<? extends Identifiable> ownerType, UUID ownerId, EntityEvent<? extends Identifiable> originalEvent) {
IdmEntityEventDto event = createEvent(ownerType, ownerId, originalEvent);
event.setEventType(CoreEventType.NOTIFY.name());
//
putToQueue(event);
}
Aggregations