Search in sources :

Example 1 with CreatedComparator

use of eu.bcvsolutions.idm.core.api.domain.comparator.CreatedComparator 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)

Aggregations

CreatedComparator (eu.bcvsolutions.idm.core.api.domain.comparator.CreatedComparator)1 DefaultResultModel (eu.bcvsolutions.idm.core.api.dto.DefaultResultModel)1 IdmEntityEventDto (eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 UUID (java.util.UUID)1 DateTime (org.joda.time.DateTime)1 PageRequest (org.springframework.data.domain.PageRequest)1 Sort (org.springframework.data.domain.Sort)1