Search in sources :

Example 6 with User

use of de.symeda.sormas.backend.user.User in project SORMAS-Project by hzi-braunschweig.

the class SampleFacadeEjb method restorePseudonymizedDto.

private void restorePseudonymizedDto(SampleDto dto, Sample existingSample, SampleDto existingSampleDto) {
    if (existingSampleDto != null) {
        boolean inJurisdiction = sampleService.inJurisdictionOrOwned(existingSample).getInJurisdiction();
        User currentUser = userService.getCurrentUser();
        Pseudonymizer pseudonymizer = Pseudonymizer.getDefault(userService::hasRight);
        pseudonymizer.restoreUser(existingSample.getReportingUser(), currentUser, dto, dto::setReportingUser);
        pseudonymizer.restorePseudonymizedValues(SampleDto.class, dto, existingSampleDto, inJurisdiction);
    }
}
Also used : User(de.symeda.sormas.backend.user.User) Pseudonymizer(de.symeda.sormas.backend.util.Pseudonymizer)

Example 7 with User

use of de.symeda.sormas.backend.user.User in project SORMAS-Project by hzi-braunschweig.

the class SampleFacadeEjb method deleteSamples.

public List<String> deleteSamples(List<String> sampleUuids) {
    User user = userService.getCurrentUser();
    if (!userRoleConfigFacade.getEffectiveUserRights(user.getUserRoles().toArray(new UserRole[user.getUserRoles().size()])).contains(UserRight.SAMPLE_DELETE)) {
        throw new UnsupportedOperationException("User " + user.getUuid() + " is not allowed to delete samples.");
    }
    List<String> deletedSampleUuids = new ArrayList<>();
    List<Sample> samplesToBeDeleted = sampleService.getByUuids(sampleUuids);
    if (samplesToBeDeleted != null) {
        samplesToBeDeleted.forEach(sampleToBeDeleted -> {
            if (!sampleToBeDeleted.isDeleted()) {
                sampleService.delete(sampleToBeDeleted);
                deletedSampleUuids.add(sampleToBeDeleted.getUuid());
            }
        });
    }
    return deletedSampleUuids;
}
Also used : User(de.symeda.sormas.backend.user.User) UserRole(de.symeda.sormas.api.user.UserRole) ArrayList(java.util.ArrayList)

Example 8 with User

use of de.symeda.sormas.backend.user.User in project SORMAS-Project by hzi-braunschweig.

the class PersonService method getAllAfter.

@Override
public // todo refactor this to use the create user filter form persons
List<Person> getAllAfter(Date date, Integer batchSize, String lastSynchronizedUuid) {
    User user = getCurrentUser();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    // persons by LGA
    CriteriaQuery<Person> personsQuery = cb.createQuery(Person.class);
    Root<Person> personsRoot = personsQuery.from(Person.class);
    Join<Person, Location> address = personsRoot.join(Person.ADDRESS);
    Predicate lgaFilter = cb.equal(address.get(Location.DISTRICT), user.getDistrict());
    // date range
    if (date != null) {
        Predicate dateFilter = createChangeDateFilter(cb, personsRoot, DateHelper.toTimestampUpper(date), lastSynchronizedUuid);
        lgaFilter = cb.and(lgaFilter, dateFilter);
    }
    personsQuery.where(lgaFilter);
    List<Person> lgaResultList = getBatchedQueryResults(cb, personsQuery, personsRoot, batchSize);
    // persons by case
    CriteriaQuery<Person> casePersonsQuery = cb.createQuery(Person.class);
    Root<Case> casePersonsRoot = casePersonsQuery.from(Case.class);
    Join<Person, Person> casePersonsSelect = casePersonsRoot.join(Case.PERSON);
    casePersonsSelect.fetch(Person.ADDRESS);
    casePersonsQuery.select(casePersonsSelect);
    Predicate casePersonsFilter = caseService.createUserFilter(cb, casePersonsQuery, casePersonsRoot);
    // date range
    if (date != null) {
        Predicate dateFilter = createChangeDateFilter(cb, casePersonsSelect, DateHelper.toTimestampUpper(date), lastSynchronizedUuid);
        if (batchSize == null) {
            // include case change dates: When a case is relocated it may become available to another user and this will have to include the person as-well
            Predicate caseDateFilter = caseService.createChangeDateFilter(cb, casePersonsRoot, DateHelper.toTimestampUpper(date));
            dateFilter = cb.or(dateFilter, caseDateFilter);
        }
        if (casePersonsFilter != null) {
            casePersonsFilter = cb.and(casePersonsFilter, dateFilter);
        } else {
            casePersonsFilter = dateFilter;
        }
    }
    if (casePersonsFilter != null) {
        casePersonsQuery.where(casePersonsFilter);
    }
    casePersonsQuery.distinct(true);
    List<Person> casePersonsResultList = getBatchedQueryResults(cb, casePersonsQuery, casePersonsSelect, batchSize);
    // persons by contact
    CriteriaQuery<Person> contactPersonsQuery = cb.createQuery(Person.class);
    Root<Contact> contactPersonsRoot = contactPersonsQuery.from(Contact.class);
    Join<Person, Person> contactPersonsSelect = contactPersonsRoot.join(Contact.PERSON);
    contactPersonsSelect.fetch(Person.ADDRESS);
    contactPersonsQuery.select(contactPersonsSelect);
    Predicate contactPersonsFilter = contactService.createUserFilter(cb, contactPersonsQuery, contactPersonsRoot);
    // date range
    if (date != null) {
        Predicate dateFilter = createChangeDateFilter(cb, contactPersonsSelect, DateHelper.toTimestampUpper(date), lastSynchronizedUuid);
        if (batchSize == null) {
            Predicate contactDateFilter = contactService.createChangeDateFilter(cb, contactPersonsRoot, date);
            dateFilter = cb.or(dateFilter, contactDateFilter);
        }
        contactPersonsFilter = and(cb, contactPersonsFilter, dateFilter);
    }
    if (contactPersonsFilter != null) {
        contactPersonsQuery.where(contactPersonsFilter);
    }
    contactPersonsQuery.distinct(true);
    List<Person> contactPersonsResultList = getBatchedQueryResults(cb, contactPersonsQuery, contactPersonsSelect, batchSize);
    // persons by event participant
    CriteriaQuery<Person> eventPersonsQuery = cb.createQuery(Person.class);
    Root<EventParticipant> eventPersonsRoot = eventPersonsQuery.from(EventParticipant.class);
    Join<Person, Person> eventPersonsSelect = eventPersonsRoot.join(EventParticipant.PERSON);
    eventPersonsSelect.fetch(Person.ADDRESS);
    eventPersonsQuery.select(eventPersonsSelect);
    Predicate eventPersonsFilter = eventParticipantService.createUserFilter(cb, eventPersonsQuery, eventPersonsRoot);
    // date range
    if (date != null) {
        Predicate dateFilter = createChangeDateFilter(cb, eventPersonsSelect, DateHelper.toTimestampUpper(date), lastSynchronizedUuid);
        if (batchSize == null) {
            Predicate eventParticipantDateFilter = eventParticipantService.createChangeDateFilter(cb, eventPersonsRoot, DateHelper.toTimestampUpper(date));
            dateFilter = cb.or(dateFilter, eventParticipantDateFilter);
        }
        eventPersonsFilter = and(cb, eventPersonsFilter, dateFilter);
    }
    if (eventPersonsFilter != null) {
        eventPersonsQuery.where(eventPersonsFilter);
    }
    eventPersonsQuery.distinct(true);
    List<Person> eventPersonsResultList = getBatchedQueryResults(cb, eventPersonsQuery, eventPersonsSelect, batchSize);
    // persons by immunization
    List<Person> immunizationPersonsResultList = new ArrayList<>();
    if (!featureConfigurationFacade.isPropertyValueTrue(FeatureType.IMMUNIZATION_MANAGEMENT, FeatureTypeProperty.REDUCED)) {
        CriteriaQuery<Person> immunizationPersonsQuery = cb.createQuery(Person.class);
        Root<Immunization> immunizationPersonsRoot = immunizationPersonsQuery.from(Immunization.class);
        Join<Immunization, Person> immunizationPersonsSelect = immunizationPersonsRoot.join(Immunization.PERSON);
        immunizationPersonsSelect.fetch(Person.ADDRESS);
        immunizationPersonsQuery.select(immunizationPersonsSelect);
        Predicate immunizationPersonsFilter = immunizationService.createUserFilter(cb, immunizationPersonsQuery, immunizationPersonsRoot);
        // date range
        if (date != null) {
            Predicate dateFilter = createChangeDateFilter(cb, immunizationPersonsSelect, DateHelper.toTimestampUpper(date), lastSynchronizedUuid);
            if (batchSize == null) {
                Predicate immunizationDateFilter = immunizationService.createChangeDateFilter(cb, immunizationPersonsRoot, DateHelper.toTimestampUpper(date));
                dateFilter = cb.or(dateFilter, immunizationDateFilter);
            }
            immunizationPersonsFilter = and(cb, immunizationPersonsFilter, dateFilter);
        }
        if (immunizationPersonsFilter != null) {
            immunizationPersonsQuery.where(immunizationPersonsFilter);
        }
        immunizationPersonsQuery.distinct(true);
        immunizationPersonsResultList = getBatchedQueryResults(cb, immunizationPersonsQuery, immunizationPersonsSelect, batchSize);
    }
    List<Person> travelEntryPersonsResultList = new ArrayList<>();
    // if a batch size is given, this is a sync from the mobile app where travel entries are not relevant for now
    if (batchSize == null) {
        // persons by travel entries
        CriteriaQuery<Person> tepQuery = cb.createQuery(Person.class);
        Root<TravelEntry> tepRoot = tepQuery.from(TravelEntry.class);
        Join<TravelEntry, Person> tepSelect = tepRoot.join(TravelEntry.PERSON);
        tepSelect.fetch(Person.ADDRESS);
        tepQuery.select(tepSelect);
        Predicate tepFilter = travelEntryService.createUserFilter(cb, tepQuery, tepRoot);
        // date range
        if (date != null) {
            Predicate dateFilter = createChangeDateFilter(cb, tepSelect, DateHelper.toTimestampUpper(date));
            Predicate travelEntryDateFilter = travelEntryService.createChangeDateFilter(cb, tepRoot, DateHelper.toTimestampUpper(date));
            tepFilter = and(cb, tepFilter, cb.or(dateFilter, travelEntryDateFilter));
        }
        if (tepFilter != null) {
            tepQuery.where(tepFilter);
        }
        tepQuery.distinct(true);
        travelEntryPersonsResultList = em.createQuery(tepQuery).getResultList();
    }
    return Stream.of(lgaResultList, casePersonsResultList, contactPersonsResultList, eventPersonsResultList, immunizationPersonsResultList, travelEntryPersonsResultList).flatMap(List<Person>::stream).distinct().sorted(new ChangeDateUuidComparator<>()).limit(batchSize == null ? Long.MAX_VALUE : batchSize).collect(Collectors.toList());
}
Also used : TravelEntry(de.symeda.sormas.backend.travelentry.TravelEntry) User(de.symeda.sormas.backend.user.User) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate) Case(de.symeda.sormas.backend.caze.Case) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Immunization(de.symeda.sormas.backend.immunization.entity.Immunization) Contact(de.symeda.sormas.backend.contact.Contact) EventParticipant(de.symeda.sormas.backend.event.EventParticipant) Location(de.symeda.sormas.backend.location.Location)

Example 9 with User

use of de.symeda.sormas.backend.user.User in project SORMAS-Project by hzi-braunschweig.

the class WeeklyReportService method filterWeeklyReportUsers.

/**
 * Filters users analogous to reportingUsers in ::createUserFilter
 *
 * @see /sormas-backend/doc/UserDataAccess.md
 */
public Stream<User> filterWeeklyReportUsers(User user, Stream<User> usersStream) {
    if (user == null) {
        return usersStream;
    }
    final JurisdictionLevel jurisdictionLevel = user.getCalculatedJurisdictionLevel();
    // National users can access all reports in the system
    if (jurisdictionLevel == JurisdictionLevel.NATION && !UserRole.isPortHealthUser(user.getUserRoles())) {
        return usersStream;
    }
    // Whoever created the weekly report is allowed to access it
    java.util.function.Predicate<User> constraints = user::equals;
    // Supervisors see all reports from users in their region
    if (user.getRegion() != null && jurisdictionLevel == JurisdictionLevel.REGION) {
        constraints = constraints.or(u -> user.getRegion().equals(u.getRegion()));
    }
    // Officers see all reports from their assigned informants
    if (user.hasAnyUserRole(UserRole.SURVEILLANCE_OFFICER)) {
        constraints = constraints.or(u -> user.equals(u.getAssociatedOfficer()));
    }
    return usersStream.filter(constraints);
}
Also used : Join(javax.persistence.criteria.Join) QueryHelper(de.symeda.sormas.backend.util.QueryHelper) EpiWeek(de.symeda.sormas.api.utils.EpiWeek) DistrictService(de.symeda.sormas.backend.infrastructure.district.DistrictService) WeeklyReportCriteria(de.symeda.sormas.api.report.WeeklyReportCriteria) Facility(de.symeda.sormas.backend.infrastructure.facility.Facility) Predicate(javax.persistence.criteria.Predicate) AdoServiceWithUserFilter(de.symeda.sormas.backend.common.AdoServiceWithUserFilter) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) JoinType(javax.persistence.criteria.JoinType) LocalBean(javax.ejb.LocalBean) UserRole(de.symeda.sormas.api.user.UserRole) CriteriaBuilderHelper(de.symeda.sormas.backend.common.CriteriaBuilderHelper) From(javax.persistence.criteria.From) EJB(javax.ejb.EJB) Path(javax.persistence.criteria.Path) Root(javax.persistence.criteria.Root) Stateless(javax.ejb.Stateless) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Region(de.symeda.sormas.backend.infrastructure.region.Region) RegionService(de.symeda.sormas.backend.infrastructure.region.RegionService) List(java.util.List) UserService(de.symeda.sormas.backend.user.UserService) Stream(java.util.stream.Stream) User(de.symeda.sormas.backend.user.User) Optional(java.util.Optional) JurisdictionLevel(de.symeda.sormas.api.user.JurisdictionLevel) User(de.symeda.sormas.backend.user.User) JurisdictionLevel(de.symeda.sormas.api.user.JurisdictionLevel)

Example 10 with User

use of de.symeda.sormas.backend.user.User in project SORMAS-Project by hzi-braunschweig.

the class SampleService method createUserFilter.

@SuppressWarnings("rawtypes")
public Predicate createUserFilter(CriteriaQuery cq, CriteriaBuilder cb, SampleJoins joins, SampleCriteria criteria) {
    Predicate filter = createUserFilterWithoutAssociations(cb, joins);
    User currentUser = getCurrentUser();
    final JurisdictionLevel jurisdictionLevel = currentUser.getCalculatedJurisdictionLevel();
    if (jurisdictionLevel == JurisdictionLevel.LABORATORY || jurisdictionLevel == JurisdictionLevel.EXTERNAL_LABORATORY) {
        return filter;
    }
    if (criteria != null && criteria.getSampleAssociationType() != null && criteria.getSampleAssociationType() != SampleAssociationType.ALL) {
        final SampleAssociationType sampleAssociationType = criteria.getSampleAssociationType();
        if (sampleAssociationType == SampleAssociationType.CASE) {
            filter = CriteriaBuilderHelper.or(cb, filter, caseService.createUserFilter(cb, cq, joins.getCaze(), null));
        } else if (sampleAssociationType == SampleAssociationType.CONTACT) {
            filter = CriteriaBuilderHelper.or(cb, filter, contactService.createUserFilterForJoin(cb, cq, joins.getContact()));
        } else if (sampleAssociationType == SampleAssociationType.EVENT_PARTICIPANT) {
            filter = CriteriaBuilderHelper.or(cb, filter, eventParticipantService.createUserFilterForJoin(cb, cq, joins.getEventParticipant()));
        }
    } else if (currentUser.getLimitedDisease() != null) {
        filter = CriteriaBuilderHelper.and(cb, filter, CriteriaBuilderHelper.or(cb, caseService.createUserFilter(cb, cq, joins.getCaze(), null), contactService.createUserFilterForJoin(cb, cq, joins.getContact()), eventParticipantService.createUserFilterForJoin(cb, cq, joins.getEventParticipant())));
    } else {
        filter = CriteriaBuilderHelper.or(cb, filter, caseService.createUserFilter(cb, cq, joins.getCaze(), null), contactService.createUserFilterForJoin(cb, cq, joins.getContact()), eventParticipantService.createUserFilterForJoin(cb, cq, joins.getEventParticipant()));
    }
    return filter;
}
Also used : User(de.symeda.sormas.backend.user.User) JurisdictionLevel(de.symeda.sormas.api.user.JurisdictionLevel) Predicate(javax.persistence.criteria.Predicate) SampleAssociationType(de.symeda.sormas.api.sample.SampleAssociationType)

Aggregations

User (de.symeda.sormas.backend.user.User)138 Predicate (javax.persistence.criteria.Predicate)61 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)51 List (java.util.List)48 Region (de.symeda.sormas.backend.infrastructure.region.Region)43 Collections (java.util.Collections)42 ArrayList (java.util.ArrayList)40 DataHelper (de.symeda.sormas.api.utils.DataHelper)38 Date (java.util.Date)38 Stateless (javax.ejb.Stateless)38 EJB (javax.ejb.EJB)37 LocalBean (javax.ejb.LocalBean)37 District (de.symeda.sormas.backend.infrastructure.district.District)36 Collectors (java.util.stream.Collectors)36 UserRole (de.symeda.sormas.api.user.UserRole)34 UserService (de.symeda.sormas.backend.user.UserService)33 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)33 Case (de.symeda.sormas.backend.caze.Case)32 Root (javax.persistence.criteria.Root)32 Disease (de.symeda.sormas.api.Disease)31