Search in sources :

Example 1 with EventGroupReferenceDto

use of de.symeda.sormas.api.event.EventGroupReferenceDto in project SORMAS-Project by hzi-braunschweig.

the class EventGroupFacadeEjb method notifyModificationOfEventGroup.

private void notifyModificationOfEventGroup(EventGroupReferenceDto eventGroupReference, List<EventReferenceDto> impactedEventReferences, NotificationType notificationType, MessageSubject subject, String contentTemplate) {
    EventGroup eventGroup = eventGroupService.getByUuid(eventGroupReference.getUuid());
    if (eventGroup == null) {
        return;
    }
    User currentUser = userService.getCurrentUser();
    try {
        notificationService.sendNotifications(notificationType, subject, () -> {
            final Set<String> allRemainingEventUuids = getEventReferencesByEventGroupUuid(eventGroupReference.getUuid()).stream().map(EventReferenceDto::getUuid).collect(Collectors.toSet());
            final Set<String> impactedEventUuids = impactedEventReferences.stream().map(EventReferenceDto::getUuid).collect(Collectors.toSet());
            final Map<String, User> responsibleUserByEventUuid = userService.getResponsibleUsersByEventUuids(new ArrayList<>(Sets.union(allRemainingEventUuids, impactedEventUuids)));
            final Map<String, User> responsibleUserByRemainingEventUuid = Maps.filterKeys(responsibleUserByEventUuid, allRemainingEventUuids::contains);
            final Map<String, User> responsibleUserByImpactedEventUuid = Maps.filterKeys(responsibleUserByEventUuid, impactedEventUuids::contains);
            final String message;
            if (impactedEventReferences.isEmpty()) {
                message = String.format(I18nProperties.getString(contentTemplate), eventGroup.getName(), DataHelper.getShortUuid(eventGroup.getUuid()), buildCaptionForUserInNotification(currentUser), buildEventGroupSummaryForNotification(responsibleUserByRemainingEventUuid));
            } else {
                message = String.format(I18nProperties.getString(contentTemplate), stringifyEventsWithResponsibleUser(responsibleUserByImpactedEventUuid, ", ", ""), eventGroup.getName(), DataHelper.getShortUuid(eventGroup.getUuid()), buildCaptionForUserInNotification(currentUser), buildEventGroupSummaryForNotification(responsibleUserByRemainingEventUuid));
            }
            return responsibleUserByEventUuid.values().stream().collect(Collectors.toMap(Function.identity(), (u) -> message));
        });
    } catch (NotificationDeliveryFailedException e) {
        logger.error("NotificationDeliveryFailedException when trying to notify event responsible user about a modification on an EventGroup.");
    }
}
Also used : DtoHelper(de.symeda.sormas.backend.util.DtoHelper) Join(javax.persistence.criteria.Join) RolesAllowed(javax.annotation.security.RolesAllowed) AbstractDomainObject(de.symeda.sormas.backend.common.AbstractDomainObject) I18nProperties(de.symeda.sormas.api.i18n.I18nProperties) LoggerFactory(org.slf4j.LoggerFactory) EventGroupDto(de.symeda.sormas.api.event.EventGroupDto) StringUtils(org.apache.commons.lang3.StringUtils) Valid(javax.validation.Valid) Page(de.symeda.sormas.api.common.Page) Predicate(javax.persistence.criteria.Predicate) Map(java.util.Map) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) JoinType(javax.persistence.criteria.JoinType) NotificationService(de.symeda.sormas.backend.common.NotificationService) EventGroupFacade(de.symeda.sormas.api.event.EventGroupFacade) Stateless(javax.ejb.Stateless) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) NotificationDeliveryFailedException(de.symeda.sormas.backend.common.messaging.NotificationDeliveryFailedException) Set(java.util.Set) Region(de.symeda.sormas.backend.infrastructure.region.Region) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) MessageContents(de.symeda.sormas.backend.common.messaging.MessageContents) UserService(de.symeda.sormas.backend.user.UserService) User(de.symeda.sormas.backend.user.User) Order(javax.persistence.criteria.Order) JurisdictionLevel(de.symeda.sormas.api.user.JurisdictionLevel) Location(de.symeda.sormas.backend.location.Location) RegionReferenceDto(de.symeda.sormas.api.infrastructure.region.RegionReferenceDto) EventGroupIndexDto(de.symeda.sormas.api.event.EventGroupIndexDto) QueryHelper(de.symeda.sormas.backend.util.QueryHelper) HashMap(java.util.HashMap) EventGroupCriteria(de.symeda.sormas.api.event.EventGroupCriteria) EventGroupReferenceDto(de.symeda.sormas.api.event.EventGroupReferenceDto) Function(java.util.function.Function) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) IterableHelper(de.symeda.sormas.backend.util.IterableHelper) LocalBean(javax.ejb.LocalBean) Expression(javax.persistence.criteria.Expression) CriteriaBuilderHelper(de.symeda.sormas.backend.common.CriteriaBuilderHelper) EJB(javax.ejb.EJB) Root(javax.persistence.criteria.Root) ModelConstants(de.symeda.sormas.backend.util.ModelConstants) Logger(org.slf4j.Logger) DataHelper(de.symeda.sormas.api.utils.DataHelper) MessageSubject(de.symeda.sormas.backend.common.messaging.MessageSubject) EntityManager(javax.persistence.EntityManager) PersistenceContext(javax.persistence.PersistenceContext) SortProperty(de.symeda.sormas.api.utils.SortProperty) Maps(com.google.common.collect.Maps) UserRight(de.symeda.sormas.api.user.UserRight) NotificationType(de.symeda.sormas.api.user.NotificationType) EventReferenceDto(de.symeda.sormas.api.event.EventReferenceDto) Subquery(javax.persistence.criteria.Subquery) Strings(de.symeda.sormas.api.i18n.Strings) Collections(java.util.Collections) User(de.symeda.sormas.backend.user.User) NotificationDeliveryFailedException(de.symeda.sormas.backend.common.messaging.NotificationDeliveryFailedException)

Example 2 with EventGroupReferenceDto

use of de.symeda.sormas.api.event.EventGroupReferenceDto in project SORMAS-Project by hzi-braunschweig.

the class EventImportFacadeEjb method buildEntities.

private ImportLineResultDto<EventImportEntities> buildEntities(String[] values, String[] entityClasses, String[][] entityPropertyPaths, boolean ignoreEmptyEntries, EventImportEntities entities) {
    final UserReferenceDto currentUserRef = userService.getCurrentUser().toReference();
    final List<EventParticipantDto> eventParticipants = entities.getEventParticipants();
    final List<EventGroupReferenceDto> eventGroupReferences = entities.getEventGroupReferences();
    final MutableBoolean currentEventParticipantHasEntries = new MutableBoolean(false);
    final Mutable<String> firstEventParticipantColumnName = new MutableObject<>(null);
    final ImportLineResultDto<EventImportEntities> result = insertRowIntoData(values, entityClasses, entityPropertyPaths, ignoreEmptyEntries, (cellData) -> {
        try {
            // participant if they don't have any entries
            if (String.join(".", cellData.getEntityPropertyPath()).equals(firstEventParticipantColumnName.getValue())) {
                if (eventParticipants.size() > 0 && currentEventParticipantHasEntries.isFalse()) {
                    eventParticipants.remove(eventParticipants.size() - 1);
                    currentEventParticipantHasEntries.setTrue();
                }
            }
            EventDto event = entities.getEvent();
            if (DataHelper.equal(cellData.getEntityClass(), DataHelper.getHumanClassName(EventParticipantDto.class)) || DataHelper.equal(cellData.getEntityClass(), DataHelper.getHumanClassName(PersonDto.class)) || (DataHelper.equal(cellData.getEntityClass(), DataHelper.getHumanClassName(LocationDto.class)) && eventParticipants.size() > 0)) {
                // to the list if the first column of a new participant has been reached and insert the entry of the cell into the participant
                if (firstEventParticipantColumnName.getValue() == null) {
                    firstEventParticipantColumnName.setValue(String.join(".", cellData.getEntityPropertyPath()));
                }
                if (String.join(".", cellData.getEntityPropertyPath()).equals(firstEventParticipantColumnName.getValue())) {
                    currentEventParticipantHasEntries.setFalse();
                    EventParticipantDto eventParticipantDto = EventParticipantDto.build(new EventReferenceDto(event.getUuid()), currentUserRef);
                    eventParticipantDto.setPerson(PersonDto.buildImportEntity());
                    eventParticipants.add(eventParticipantDto);
                }
                if (!StringUtils.isEmpty(cellData.getValue())) {
                    currentEventParticipantHasEntries.setTrue();
                    insertColumnEntryIntoEventParticipantData(eventParticipants.get(eventParticipants.size() - 1), cellData.getValue(), cellData.getEntityPropertyPath());
                }
            } else if (DataHelper.equal(cellData.getEntityClass(), DataHelper.getHumanClassName(EventGroupReferenceDto.class))) {
                eventGroupReferences.add(new EventGroupReferenceDto(cellData.getValue()));
            } else if (!StringUtils.isEmpty(cellData.getValue())) {
                // If the cell entry is not empty, try to insert it into the current event
                insertColumnEntryIntoData(event, cellData.getValue(), cellData.getEntityPropertyPath());
            }
        } catch (ImportErrorException | InvalidColumnException e) {
            return e;
        }
        return null;
    });
    // Remove the eventParticipant if empty
    if (eventParticipants.size() > 0 && currentEventParticipantHasEntries.isFalse()) {
        eventParticipants.remove(eventParticipants.size() - 1);
    }
    return result;
}
Also used : ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) PersonDto(de.symeda.sormas.api.person.PersonDto) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) EventGroupReferenceDto(de.symeda.sormas.api.event.EventGroupReferenceDto) EventDto(de.symeda.sormas.api.event.EventDto) EventParticipantDto(de.symeda.sormas.api.event.EventParticipantDto) UserReferenceDto(de.symeda.sormas.api.user.UserReferenceDto) EventReferenceDto(de.symeda.sormas.api.event.EventReferenceDto) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) EventImportEntities(de.symeda.sormas.api.event.eventimport.EventImportEntities) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Example 3 with EventGroupReferenceDto

use of de.symeda.sormas.api.event.EventGroupReferenceDto in project SORMAS-Project by hzi-braunschweig.

the class EventImportFacadeEjb method saveImportedEntities.

@Override
public ImportLineResultDto<EventImportEntities> saveImportedEntities(@Valid EventImportEntities entities) {
    EventDto event = entities.getEvent();
    List<EventParticipantDto> eventParticipants = entities.getEventParticipants();
    List<EventGroupReferenceDto> eventGroupReferences = entities.getEventGroupReferences();
    try {
        event = eventFacade.save(event);
        for (EventParticipantDto eventParticipant : eventParticipants) {
            PersonDto existingPerson = personFacade.getPersonByUuid(eventParticipant.getPerson().getUuid());
            // So no need to persist an already existing person
            if (existingPerson == null) {
                personFacade.savePerson(eventParticipant.getPerson());
            }
            eventParticipantFacade.save(eventParticipant);
        }
        eventGroupFacade.linkEventToGroups(event.toReference(), eventGroupReferences);
        return ImportLineResultDto.successResult();
    } catch (ValidationRuntimeException e) {
        return ImportLineResultDto.errorResult(e.getMessage());
    }
}
Also used : PersonDto(de.symeda.sormas.api.person.PersonDto) EventDto(de.symeda.sormas.api.event.EventDto) EventGroupReferenceDto(de.symeda.sormas.api.event.EventGroupReferenceDto) EventParticipantDto(de.symeda.sormas.api.event.EventParticipantDto) ValidationRuntimeException(de.symeda.sormas.api.utils.ValidationRuntimeException)

Example 4 with EventGroupReferenceDto

use of de.symeda.sormas.api.event.EventGroupReferenceDto in project SORMAS-Project by hzi-braunschweig.

the class EventGroupFacadeEjb method unlinkEventGroup.

@Override
@RolesAllowed(UserRight._EVENTGROUP_LINK)
public void unlinkEventGroup(EventReferenceDto eventReference, EventGroupReferenceDto eventGroupReference) {
    User currentUser = userService.getCurrentUser();
    Event event = eventService.getByUuid(eventReference.getUuid());
    final JurisdictionLevel jurisdictionLevel = currentUser.getJurisdictionLevel();
    if (jurisdictionLevel != JurisdictionLevel.NATION) {
        Region region = event.getEventLocation().getRegion();
        if (!userService.hasRegion(new RegionReferenceDto(region.getUuid()))) {
            throw new UnsupportedOperationException("User " + currentUser.getUuid() + " is not allowed to unlink events from another region to an event group.");
        }
    }
    // Check that the event group is not already unlinked to this event
    if (event.getEventGroups() == null || event.getEventGroups().stream().noneMatch(group -> group.getUuid().equals(eventGroupReference.getUuid()))) {
        return;
    }
    List<EventGroup> groups = new ArrayList<>();
    for (EventGroup eventGroup : event.getEventGroups()) {
        if (eventGroup.getUuid().equals(eventGroupReference.getUuid())) {
            continue;
        }
        groups.add(eventGroup);
    }
    event.setEventGroups(groups);
    eventService.ensurePersisted(event);
}
Also used : DtoHelper(de.symeda.sormas.backend.util.DtoHelper) Join(javax.persistence.criteria.Join) RolesAllowed(javax.annotation.security.RolesAllowed) AbstractDomainObject(de.symeda.sormas.backend.common.AbstractDomainObject) I18nProperties(de.symeda.sormas.api.i18n.I18nProperties) LoggerFactory(org.slf4j.LoggerFactory) EventGroupDto(de.symeda.sormas.api.event.EventGroupDto) StringUtils(org.apache.commons.lang3.StringUtils) Valid(javax.validation.Valid) Page(de.symeda.sormas.api.common.Page) Predicate(javax.persistence.criteria.Predicate) Map(java.util.Map) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) JoinType(javax.persistence.criteria.JoinType) NotificationService(de.symeda.sormas.backend.common.NotificationService) EventGroupFacade(de.symeda.sormas.api.event.EventGroupFacade) Stateless(javax.ejb.Stateless) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) NotificationDeliveryFailedException(de.symeda.sormas.backend.common.messaging.NotificationDeliveryFailedException) Set(java.util.Set) Region(de.symeda.sormas.backend.infrastructure.region.Region) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) MessageContents(de.symeda.sormas.backend.common.messaging.MessageContents) UserService(de.symeda.sormas.backend.user.UserService) User(de.symeda.sormas.backend.user.User) Order(javax.persistence.criteria.Order) JurisdictionLevel(de.symeda.sormas.api.user.JurisdictionLevel) Location(de.symeda.sormas.backend.location.Location) RegionReferenceDto(de.symeda.sormas.api.infrastructure.region.RegionReferenceDto) EventGroupIndexDto(de.symeda.sormas.api.event.EventGroupIndexDto) QueryHelper(de.symeda.sormas.backend.util.QueryHelper) HashMap(java.util.HashMap) EventGroupCriteria(de.symeda.sormas.api.event.EventGroupCriteria) EventGroupReferenceDto(de.symeda.sormas.api.event.EventGroupReferenceDto) Function(java.util.function.Function) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) IterableHelper(de.symeda.sormas.backend.util.IterableHelper) LocalBean(javax.ejb.LocalBean) Expression(javax.persistence.criteria.Expression) CriteriaBuilderHelper(de.symeda.sormas.backend.common.CriteriaBuilderHelper) EJB(javax.ejb.EJB) Root(javax.persistence.criteria.Root) ModelConstants(de.symeda.sormas.backend.util.ModelConstants) Logger(org.slf4j.Logger) DataHelper(de.symeda.sormas.api.utils.DataHelper) MessageSubject(de.symeda.sormas.backend.common.messaging.MessageSubject) EntityManager(javax.persistence.EntityManager) PersistenceContext(javax.persistence.PersistenceContext) SortProperty(de.symeda.sormas.api.utils.SortProperty) Maps(com.google.common.collect.Maps) UserRight(de.symeda.sormas.api.user.UserRight) NotificationType(de.symeda.sormas.api.user.NotificationType) EventReferenceDto(de.symeda.sormas.api.event.EventReferenceDto) Subquery(javax.persistence.criteria.Subquery) Strings(de.symeda.sormas.api.i18n.Strings) Collections(java.util.Collections) User(de.symeda.sormas.backend.user.User) RegionReferenceDto(de.symeda.sormas.api.infrastructure.region.RegionReferenceDto) JurisdictionLevel(de.symeda.sormas.api.user.JurisdictionLevel) ArrayList(java.util.ArrayList) Region(de.symeda.sormas.backend.infrastructure.region.Region) RolesAllowed(javax.annotation.security.RolesAllowed)

Example 5 with EventGroupReferenceDto

use of de.symeda.sormas.api.event.EventGroupReferenceDto in project SORMAS-Project by hzi-braunschweig.

the class EventGroupFacadeEjb method getCommonEventGroupsByEvents.

@Override
public List<EventGroupReferenceDto> getCommonEventGroupsByEvents(List<EventReferenceDto> eventReferences) {
    Map<String, Set<String>> eventGroupsByEvent = new HashMap<>();
    IterableHelper.executeBatched(eventReferences, ModelConstants.PARAMETER_LIMIT, batchedEventReferences -> {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Object[]> cq = cb.createQuery(Object[].class);
        Root<EventGroup> from = cq.from(EventGroup.class);
        Join<EventGroup, Event> eventJoin = from.join(EventGroup.EVENTS, JoinType.INNER);
        Set<String> eventUuids = batchedEventReferences.stream().map(EventReferenceDto::getUuid).collect(Collectors.toSet());
        cq.where(eventJoin.get(Event.UUID).in(eventUuids));
        cq.multiselect(eventJoin.get(Event.UUID), from.get(EventGroup.UUID));
        eventGroupsByEvent.putAll(em.createQuery(cq).getResultList().stream().collect(Collectors.groupingBy(row -> (String) row[0], Collectors.mapping(row -> (String) row[1], Collectors.toSet()))));
    });
    if (eventGroupsByEvent.isEmpty()) {
        return Collections.emptyList();
    }
    Set<String> commonEventGroupUuids = eventGroupsByEvent.values().stream().reduce(Sets::intersection).orElseGet(Collections::emptySet);
    return commonEventGroupUuids.stream().map(EventGroupReferenceDto::new).collect(Collectors.toList());
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) DtoHelper(de.symeda.sormas.backend.util.DtoHelper) Join(javax.persistence.criteria.Join) RolesAllowed(javax.annotation.security.RolesAllowed) AbstractDomainObject(de.symeda.sormas.backend.common.AbstractDomainObject) I18nProperties(de.symeda.sormas.api.i18n.I18nProperties) LoggerFactory(org.slf4j.LoggerFactory) EventGroupDto(de.symeda.sormas.api.event.EventGroupDto) StringUtils(org.apache.commons.lang3.StringUtils) Valid(javax.validation.Valid) Page(de.symeda.sormas.api.common.Page) Predicate(javax.persistence.criteria.Predicate) Map(java.util.Map) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) JoinType(javax.persistence.criteria.JoinType) NotificationService(de.symeda.sormas.backend.common.NotificationService) EventGroupFacade(de.symeda.sormas.api.event.EventGroupFacade) Stateless(javax.ejb.Stateless) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) NotificationDeliveryFailedException(de.symeda.sormas.backend.common.messaging.NotificationDeliveryFailedException) Set(java.util.Set) Region(de.symeda.sormas.backend.infrastructure.region.Region) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) MessageContents(de.symeda.sormas.backend.common.messaging.MessageContents) UserService(de.symeda.sormas.backend.user.UserService) User(de.symeda.sormas.backend.user.User) Order(javax.persistence.criteria.Order) JurisdictionLevel(de.symeda.sormas.api.user.JurisdictionLevel) Location(de.symeda.sormas.backend.location.Location) RegionReferenceDto(de.symeda.sormas.api.infrastructure.region.RegionReferenceDto) EventGroupIndexDto(de.symeda.sormas.api.event.EventGroupIndexDto) QueryHelper(de.symeda.sormas.backend.util.QueryHelper) HashMap(java.util.HashMap) EventGroupCriteria(de.symeda.sormas.api.event.EventGroupCriteria) EventGroupReferenceDto(de.symeda.sormas.api.event.EventGroupReferenceDto) Function(java.util.function.Function) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) IterableHelper(de.symeda.sormas.backend.util.IterableHelper) LocalBean(javax.ejb.LocalBean) Expression(javax.persistence.criteria.Expression) CriteriaBuilderHelper(de.symeda.sormas.backend.common.CriteriaBuilderHelper) EJB(javax.ejb.EJB) Root(javax.persistence.criteria.Root) ModelConstants(de.symeda.sormas.backend.util.ModelConstants) Logger(org.slf4j.Logger) DataHelper(de.symeda.sormas.api.utils.DataHelper) MessageSubject(de.symeda.sormas.backend.common.messaging.MessageSubject) EntityManager(javax.persistence.EntityManager) PersistenceContext(javax.persistence.PersistenceContext) SortProperty(de.symeda.sormas.api.utils.SortProperty) Maps(com.google.common.collect.Maps) UserRight(de.symeda.sormas.api.user.UserRight) NotificationType(de.symeda.sormas.api.user.NotificationType) EventReferenceDto(de.symeda.sormas.api.event.EventReferenceDto) Subquery(javax.persistence.criteria.Subquery) Strings(de.symeda.sormas.api.i18n.Strings) Collections(java.util.Collections) Set(java.util.Set) HashMap(java.util.HashMap) AbstractDomainObject(de.symeda.sormas.backend.common.AbstractDomainObject) Collections(java.util.Collections)

Aggregations

EventGroupReferenceDto (de.symeda.sormas.api.event.EventGroupReferenceDto)7 AbstractDomainObject (de.symeda.sormas.backend.common.AbstractDomainObject)5 Region (de.symeda.sormas.backend.infrastructure.region.Region)5 Location (de.symeda.sormas.backend.location.Location)5 User (de.symeda.sormas.backend.user.User)5 EventReferenceDto (de.symeda.sormas.api.event.EventReferenceDto)4 SortProperty (de.symeda.sormas.api.utils.SortProperty)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 Predicate (javax.persistence.criteria.Predicate)4 Maps (com.google.common.collect.Maps)3 Sets (com.google.common.collect.Sets)3 Page (de.symeda.sormas.api.common.Page)3 EventGroupCriteria (de.symeda.sormas.api.event.EventGroupCriteria)3 EventGroupDto (de.symeda.sormas.api.event.EventGroupDto)3 EventGroupFacade (de.symeda.sormas.api.event.EventGroupFacade)3 EventGroupIndexDto (de.symeda.sormas.api.event.EventGroupIndexDto)3 I18nProperties (de.symeda.sormas.api.i18n.I18nProperties)3 Strings (de.symeda.sormas.api.i18n.Strings)3