Search in sources :

Example 1 with IdmEntityEventDto

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);
}
Also used : IdmEntityEventDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with IdmEntityEventDto

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;
}
Also used : DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ArrayList(java.util.ArrayList) DateTime(org.joda.time.DateTime) LinkedHashMap(java.util.LinkedHashMap) PageRequest(org.springframework.data.domain.PageRequest) CreatedComparator(eu.bcvsolutions.idm.core.api.domain.comparator.CreatedComparator) Sort(org.springframework.data.domain.Sort) UUID(java.util.UUID) IdmEntityEventDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto)

Example 3 with IdmEntityEventDto

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;
}
Also used : DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) IdmEntityEventDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto)

Example 4 with IdmEntityEventDto

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;
}
Also used : IdmEntityStateDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityStateDto) ArrayList(java.util.ArrayList) IdmEntityEventDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with IdmEntityEventDto

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);
}
Also used : IdmEntityEventDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto)

Aggregations

IdmEntityEventDto (eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto)35 Test (org.junit.Test)21 OperationResultDto (eu.bcvsolutions.idm.core.api.dto.OperationResultDto)15 Transactional (org.springframework.transaction.annotation.Transactional)13 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)12 UUID (java.util.UUID)12 ArrayList (java.util.ArrayList)11 DateTime (org.joda.time.DateTime)10 IdmEntityEventFilter (eu.bcvsolutions.idm.core.api.dto.filter.IdmEntityEventFilter)9 AbstractUnitTest (eu.bcvsolutions.idm.test.api.AbstractUnitTest)9 Identifiable (eu.bcvsolutions.idm.core.api.domain.Identifiable)5 DefaultEventResult (eu.bcvsolutions.idm.core.api.event.DefaultEventResult)5 IdmEntityStateDto (eu.bcvsolutions.idm.core.api.dto.IdmEntityStateDto)4 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)4 IdmIdentity (eu.bcvsolutions.idm.core.model.entity.IdmIdentity)4 PageRequest (org.springframework.data.domain.PageRequest)4 Lists (com.google.common.collect.Lists)3 PriorityType (eu.bcvsolutions.idm.core.api.domain.PriorityType)3 DefaultResultModel (eu.bcvsolutions.idm.core.api.dto.DefaultResultModel)3 MockOwner (eu.bcvsolutions.idm.core.event.domain.MockOwner)3