Search in sources :

Example 1 with SampleJurisdictionFlagsDto

use of de.symeda.sormas.api.sample.SampleJurisdictionFlagsDto in project SORMAS-Project by hzi-braunschweig.

the class SampleFacadeEjb method pseudonymizeDto.

private void pseudonymizeDto(Sample source, SampleDto dto, Pseudonymizer pseudonymizer) {
    if (dto != null) {
        final SampleJurisdictionFlagsDto sampleJurisdictionFlagsDto = sampleService.inJurisdictionOrOwned(source);
        User currentUser = userService.getCurrentUser();
        pseudonymizer.pseudonymizeDto(SampleDto.class, dto, sampleJurisdictionFlagsDto.getInJurisdiction(), s -> {
            pseudonymizer.pseudonymizeUser(source.getReportingUser(), currentUser, s::setReportingUser);
            pseudonymizeAssociatedObjects(s.getAssociatedCase(), s.getAssociatedContact(), s.getAssociatedEventParticipant(), pseudonymizer, sampleJurisdictionFlagsDto);
        });
    }
}
Also used : SampleJurisdictionFlagsDto(de.symeda.sormas.api.sample.SampleJurisdictionFlagsDto) User(de.symeda.sormas.backend.user.User) StringUtils(org.apache.commons.lang3.StringUtils) Captions(de.symeda.sormas.api.i18n.Captions) I18nProperties(de.symeda.sormas.api.i18n.I18nProperties) Stateless(javax.ejb.Stateless) Collectors(java.util.stream.Collectors) MessageContents(de.symeda.sormas.backend.common.messaging.MessageContents) DeletionDetails(de.symeda.sormas.api.common.DeletionDetails) ModelConstants(de.symeda.sormas.backend.util.ModelConstants) Validations(de.symeda.sormas.api.i18n.Validations) Strings(de.symeda.sormas.api.i18n.Strings) Collections(java.util.Collections)

Example 2 with SampleJurisdictionFlagsDto

use of de.symeda.sormas.api.sample.SampleJurisdictionFlagsDto in project SORMAS-Project by hzi-braunschweig.

the class SampleService method getIndexList.

public List<SampleIndexDto> getIndexList(SampleCriteria sampleCriteria, Integer first, Integer max, List<SortProperty> sortProperties) {
    final CriteriaBuilder cb = em.getCriteriaBuilder();
    final CriteriaQuery<SampleIndexDto> cq = cb.createQuery(SampleIndexDto.class);
    final Root<Sample> sample = cq.from(Sample.class);
    SampleQueryContext sampleQueryContext = new SampleQueryContext(cb, cq, sample);
    SampleJoins joins = sampleQueryContext.getJoins();
    final Join<Sample, Case> caze = joins.getCaze();
    final Join<Case, District> caseDistrict = joins.getCaseDistrict();
    final Join<Sample, Contact> contact = joins.getContact();
    final Join<Contact, District> contactDistrict = joins.getContactDistrict();
    final Join<Case, District> contactCaseDistrict = joins.getContactCaseDistrict();
    final Join<EventParticipant, Event> event = joins.getEvent();
    final Join<Location, District> eventDistrict = joins.getEventDistrict();
    Expression<Object> diseaseSelect = cb.selectCase().when(cb.isNotNull(caze), caze.get(Case.DISEASE)).otherwise(cb.selectCase().when(cb.isNotNull(contact), contact.get(Contact.DISEASE)).otherwise(event.get(Event.DISEASE)));
    Expression<Object> diseaseDetailsSelect = cb.selectCase().when(cb.isNotNull(caze), caze.get(Case.DISEASE_DETAILS)).otherwise(cb.selectCase().when(cb.isNotNull(contact), contact.get(Contact.DISEASE_DETAILS)).otherwise(event.get(Event.DISEASE_DETAILS)));
    Expression<Object> districtSelect = cb.selectCase().when(cb.isNotNull(caseDistrict), caseDistrict.get(District.NAME)).otherwise(cb.selectCase().when(cb.isNotNull(contactDistrict), contactDistrict.get(District.NAME)).otherwise(cb.selectCase().when(cb.isNotNull(contactCaseDistrict), contactCaseDistrict.get(District.NAME)).otherwise(eventDistrict.get(District.NAME))));
    cq.distinct(true);
    List<Selection<?>> selections = new ArrayList<>(Arrays.asList(sample.get(Sample.UUID), caze.get(Case.EPID_NUMBER), sample.get(Sample.LAB_SAMPLE_ID), sample.get(Sample.SAMPLE_DATE_TIME), sample.get(Sample.SHIPPED), sample.get(Sample.SHIPMENT_DATE), sample.get(Sample.RECEIVED), sample.get(Sample.RECEIVED_DATE), sample.get(Sample.SAMPLE_MATERIAL), sample.get(Sample.SAMPLE_PURPOSE), sample.get(Sample.SPECIMEN_CONDITION), joins.getLab().get(Facility.NAME), joins.getReferredSample().get(Sample.UUID), sample.get(Sample.SAMPLING_REASON), sample.get(Sample.SAMPLING_REASON_DETAILS), caze.get(Case.UUID), joins.getCasePerson().get(Person.FIRST_NAME), joins.getCasePerson().get(Person.LAST_NAME), joins.getContact().get(Contact.UUID), joins.getContactPerson().get(Person.FIRST_NAME), joins.getContactPerson().get(Person.LAST_NAME), joins.getEventParticipant().get(EventParticipant.UUID), joins.getEventParticipantPerson().get(Person.FIRST_NAME), joins.getEventParticipantPerson().get(Person.LAST_NAME), diseaseSelect, diseaseDetailsSelect, sample.get(Sample.PATHOGEN_TEST_RESULT), sample.get(Sample.ADDITIONAL_TESTING_REQUESTED), cb.isNotEmpty(sample.get(Sample.ADDITIONAL_TESTS)), districtSelect, joins.getLab().get(Facility.UUID)));
    // Tests count subquery
    Subquery<Long> testCountSq = cq.subquery(Long.class);
    Root<PathogenTest> testCountRoot = testCountSq.from(PathogenTest.class);
    testCountSq.where(cb.equal(testCountRoot.get(PathogenTest.SAMPLE), sample), cb.isFalse(testCountRoot.get(PathogenTest.DELETED)));
    testCountSq.select(cb.countDistinct(testCountRoot.get(PathogenTest.ID)));
    selections.add(testCountSq.getSelection());
    selections.addAll(getJurisdictionSelections(sampleQueryContext));
    cq.multiselect(selections);
    Predicate filter = createUserFilter(sampleQueryContext, sampleCriteria);
    if (sampleCriteria != null) {
        Predicate criteriaFilter = buildCriteriaFilter(sampleCriteria, sampleQueryContext);
        filter = CriteriaBuilderHelper.and(cb, filter, criteriaFilter);
    }
    if (filter != null) {
        cq.where(filter);
    }
    if (sortProperties != null && sortProperties.size() > 0) {
        List<Order> order = new ArrayList<>(sortProperties.size());
        for (SortProperty sortProperty : sortProperties) {
            Expression<?> expression;
            switch(sortProperty.propertyName) {
                case SampleIndexDto.UUID:
                case SampleIndexDto.LAB_SAMPLE_ID:
                case SampleIndexDto.SHIPPED:
                case SampleIndexDto.RECEIVED:
                case SampleIndexDto.REFERRED:
                case SampleIndexDto.SAMPLE_DATE_TIME:
                case SampleIndexDto.SHIPMENT_DATE:
                case SampleIndexDto.RECEIVED_DATE:
                case SampleIndexDto.SAMPLE_MATERIAL:
                case SampleIndexDto.SAMPLE_PURPOSE:
                case SampleIndexDto.PATHOGEN_TEST_RESULT:
                case SampleIndexDto.ADDITIONAL_TESTING_STATUS:
                    expression = sample.get(sortProperty.propertyName);
                    break;
                case SampleIndexDto.DISEASE:
                    expression = diseaseSelect;
                    break;
                case SampleIndexDto.EPID_NUMBER:
                    expression = caze.get(Case.EPID_NUMBER);
                    break;
                case SampleIndexDto.ASSOCIATED_CASE:
                    expression = joins.getCasePerson().get(Person.LAST_NAME);
                    order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
                    expression = joins.getCasePerson().get(Person.FIRST_NAME);
                    break;
                case SampleIndexDto.ASSOCIATED_CONTACT:
                    expression = joins.getContactPerson().get(Person.LAST_NAME);
                    order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
                    expression = joins.getContactPerson().get(Person.FIRST_NAME);
                    break;
                case SampleIndexDto.ASSOCIATED_EVENT_PARTICIPANT:
                    expression = joins.getEventParticipantPerson().get(Person.LAST_NAME);
                    order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
                    expression = joins.getEventParticipantPerson().get(Person.FIRST_NAME);
                    break;
                case SampleIndexDto.DISTRICT:
                    expression = districtSelect;
                    break;
                case SampleIndexDto.LAB:
                    expression = joins.getLab().get(Facility.NAME);
                    break;
                default:
                    throw new IllegalArgumentException(sortProperty.propertyName);
            }
            order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
        }
        cq.orderBy(order);
    } else {
        cq.orderBy(cb.desc(sample.get(Sample.SAMPLE_DATE_TIME)));
    }
    List<SampleIndexDto> samples = QueryHelper.getResultList(em, cq, first, max);
    if (!samples.isEmpty()) {
        CriteriaQuery<Object[]> testCq = cb.createQuery(Object[].class);
        Root<PathogenTest> testRoot = testCq.from(PathogenTest.class);
        Expression<String> sampleIdExpr = testRoot.get(PathogenTest.SAMPLE).get(Sample.UUID);
        Path<Long> testType = testRoot.get(PathogenTest.TEST_TYPE);
        Path<Date> cqValue = testRoot.get(PathogenTest.CQ_VALUE);
        testCq.select(cb.array(testType, cqValue, sampleIdExpr));
        testCq.where(cb.isFalse(testRoot.get(PathogenTest.DELETED)), sampleIdExpr.in(samples.stream().map(SampleIndexDto::getUuid).collect(Collectors.toList())));
        testCq.orderBy(cb.desc(testRoot.get(PathogenTest.CHANGE_DATE)));
        List<Object[]> testList = em.createQuery(testCq).getResultList();
        Map<String, Object[]> tests = testList.stream().filter(distinctByKey(pathogenTest -> pathogenTest[2])).collect(Collectors.toMap(pathogenTest -> pathogenTest[2].toString(), Function.identity()));
        for (SampleIndexDto indexDto : samples) {
            Optional.ofNullable(tests.get(indexDto.getUuid())).ifPresent(test -> {
                indexDto.setTypeOfLastTest((PathogenTestType) test[0]);
                indexDto.setLastTestCqValue((Float) test[1]);
            });
        }
    }
    Pseudonymizer pseudonymizer = Pseudonymizer.getDefault(userService::hasRight, I18nProperties.getCaption(Captions.inaccessibleValue));
    Pseudonymizer emptyValuePseudonymizer = Pseudonymizer.getDefault(userService::hasRight);
    pseudonymizer.pseudonymizeDtoCollection(SampleIndexDto.class, samples, s -> s.getSampleJurisdictionFlagsDto().getInJurisdiction(), (s, ignored) -> {
        final SampleJurisdictionFlagsDto sampleJurisdictionFlagsDto = s.getSampleJurisdictionFlagsDto();
        if (s.getAssociatedCase() != null) {
            emptyValuePseudonymizer.pseudonymizeDto(CaseReferenceDto.class, s.getAssociatedCase(), sampleJurisdictionFlagsDto.getCaseInJurisdiction(), null);
        }
        ContactReferenceDto associatedContact = s.getAssociatedContact();
        if (associatedContact != null) {
            emptyValuePseudonymizer.pseudonymizeDto(ContactReferenceDto.PersonName.class, associatedContact.getContactName(), sampleJurisdictionFlagsDto.getContactInJurisdiction(), null);
            if (associatedContact.getCaseName() != null) {
                pseudonymizer.pseudonymizeDto(ContactReferenceDto.PersonName.class, associatedContact.getCaseName(), sampleJurisdictionFlagsDto.getContactCaseInJurisdiction(), null);
            }
        }
        if (s.getAssociatedEventParticipant() != null) {
            emptyValuePseudonymizer.pseudonymizeDto(EventParticipantReferenceDto.class, s.getAssociatedEventParticipant(), sampleJurisdictionFlagsDto.getEvenParticipantInJurisdiction(), null);
        }
    }, true);
    return samples;
}
Also used : Arrays(java.util.Arrays) Join(javax.persistence.criteria.Join) AbstractDomainObject(de.symeda.sormas.backend.common.AbstractDomainObject) Date(java.util.Date) I18nProperties(de.symeda.sormas.api.i18n.I18nProperties) TransactionAttributeType(javax.ejb.TransactionAttributeType) Facility(de.symeda.sormas.backend.infrastructure.facility.Facility) Predicate(javax.persistence.criteria.Predicate) Map(java.util.Map) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) JoinType(javax.persistence.criteria.JoinType) From(javax.persistence.criteria.From) Path(javax.persistence.criteria.Path) EventParticipantReferenceDto(de.symeda.sormas.api.event.EventParticipantReferenceDto) Contact(de.symeda.sormas.backend.contact.Contact) Stateless(javax.ejb.Stateless) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) CaseService(de.symeda.sormas.backend.caze.CaseService) Person(de.symeda.sormas.backend.person.Person) Timestamp(java.sql.Timestamp) Collection(java.util.Collection) LabMessageService(de.symeda.sormas.backend.labmessage.LabMessageService) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Region(de.symeda.sormas.backend.infrastructure.region.Region) District(de.symeda.sormas.backend.infrastructure.district.District) Instant(java.time.Instant) Pseudonymizer(de.symeda.sormas.backend.util.Pseudonymizer) Collectors(java.util.stream.Collectors) ContactQueryContext(de.symeda.sormas.backend.contact.ContactQueryContext) EventParticipantQueryContext(de.symeda.sormas.backend.event.EventParticipantQueryContext) List(java.util.List) UserService(de.symeda.sormas.backend.user.UserService) User(de.symeda.sormas.backend.user.User) PathogenTestResultType(de.symeda.sormas.api.sample.PathogenTestResultType) JurisdictionHelper(de.symeda.sormas.backend.util.JurisdictionHelper) SormasToSormasShareInfoFacadeEjbLocal(de.symeda.sormas.backend.sormastosormas.share.shareinfo.SormasToSormasShareInfoFacadeEjb.SormasToSormasShareInfoFacadeEjbLocal) Order(javax.persistence.criteria.Order) DeletableAdo(de.symeda.sormas.backend.common.DeletableAdo) Optional(java.util.Optional) JurisdictionLevel(de.symeda.sormas.api.user.JurisdictionLevel) Location(de.symeda.sormas.backend.location.Location) SampleAssociationType(de.symeda.sormas.api.sample.SampleAssociationType) SampleCriteria(de.symeda.sormas.api.sample.SampleCriteria) EventParticipant(de.symeda.sormas.backend.event.EventParticipant) QueryHelper(de.symeda.sormas.backend.util.QueryHelper) SormasToSormasShareInfoService(de.symeda.sormas.backend.sormastosormas.share.shareinfo.SormasToSormasShareInfoService) DateHelper(de.symeda.sormas.api.utils.DateHelper) SampleListEntryDtoResultTransformer(de.symeda.sormas.backend.sample.transformers.SampleListEntryDtoResultTransformer) Selection(javax.persistence.criteria.Selection) HashMap(java.util.HashMap) EntityRelevanceStatus(de.symeda.sormas.api.EntityRelevanceStatus) CaseFacadeEjb(de.symeda.sormas.backend.caze.CaseFacadeEjb) Function(java.util.function.Function) EventParticipantService(de.symeda.sormas.backend.event.EventParticipantService) ArrayList(java.util.ArrayList) CaseReferenceDto(de.symeda.sormas.api.caze.CaseReferenceDto) PathogenTestType(de.symeda.sormas.api.sample.PathogenTestType) Case(de.symeda.sormas.backend.caze.Case) DeletionDetails(de.symeda.sormas.api.common.DeletionDetails) SampleReferenceDto(de.symeda.sormas.api.sample.SampleReferenceDto) CriteriaUpdate(javax.persistence.criteria.CriteriaUpdate) TransactionAttribute(javax.ejb.TransactionAttribute) IterableHelper(de.symeda.sormas.backend.util.IterableHelper) SampleListEntryDto(de.symeda.sormas.api.sample.SampleListEntryDto) LocalBean(javax.ejb.LocalBean) CaseQueryContext(de.symeda.sormas.backend.caze.CaseQueryContext) SampleIndexDto(de.symeda.sormas.api.sample.SampleIndexDto) Expression(javax.persistence.criteria.Expression) CriteriaBuilderHelper(de.symeda.sormas.backend.common.CriteriaBuilderHelper) SampleJurisdictionFlagsDto(de.symeda.sormas.api.sample.SampleJurisdictionFlagsDto) AbstractDeletableAdoService(de.symeda.sormas.backend.common.AbstractDeletableAdoService) EJB(javax.ejb.EJB) Root(javax.persistence.criteria.Root) DataHelper(de.symeda.sormas.api.utils.DataHelper) SortProperty(de.symeda.sormas.api.utils.SortProperty) Captions(de.symeda.sormas.api.i18n.Captions) Event(de.symeda.sormas.backend.event.Event) ContactService(de.symeda.sormas.backend.contact.ContactService) SormasToSormasShareInfo(de.symeda.sormas.backend.sormastosormas.share.shareinfo.SormasToSormasShareInfo) Subquery(javax.persistence.criteria.Subquery) ContactReferenceDto(de.symeda.sormas.api.contact.ContactReferenceDto) Collections(java.util.Collections) Pseudonymizer(de.symeda.sormas.backend.util.Pseudonymizer) Selection(javax.persistence.criteria.Selection) ArrayList(java.util.ArrayList) Case(de.symeda.sormas.backend.caze.Case) Predicate(javax.persistence.criteria.Predicate) SampleJurisdictionFlagsDto(de.symeda.sormas.api.sample.SampleJurisdictionFlagsDto) SortProperty(de.symeda.sormas.api.utils.SortProperty) SampleIndexDto(de.symeda.sormas.api.sample.SampleIndexDto) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) Date(java.util.Date) Contact(de.symeda.sormas.backend.contact.Contact) ContactReferenceDto(de.symeda.sormas.api.contact.ContactReferenceDto) Event(de.symeda.sormas.backend.event.Event) AbstractDomainObject(de.symeda.sormas.backend.common.AbstractDomainObject) District(de.symeda.sormas.backend.infrastructure.district.District) EventParticipant(de.symeda.sormas.backend.event.EventParticipant) Location(de.symeda.sormas.backend.location.Location)

Example 3 with SampleJurisdictionFlagsDto

use of de.symeda.sormas.api.sample.SampleJurisdictionFlagsDto in project SORMAS-Project by hzi-braunschweig.

the class SampleService method inJurisdictionOrOwned.

public SampleJurisdictionFlagsDto inJurisdictionOrOwned(Sample sample) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<SampleJurisdictionFlagsDto> cq = cb.createQuery(SampleJurisdictionFlagsDto.class);
    Root<Sample> root = cq.from(Sample.class);
    cq.multiselect(getJurisdictionSelections(new SampleQueryContext(cb, cq, root)));
    cq.where(cb.equal(root.get(Sample.UUID), sample.getUuid()));
    return em.createQuery(cq).getSingleResult();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) SampleJurisdictionFlagsDto(de.symeda.sormas.api.sample.SampleJurisdictionFlagsDto)

Aggregations

SampleJurisdictionFlagsDto (de.symeda.sormas.api.sample.SampleJurisdictionFlagsDto)3 DeletionDetails (de.symeda.sormas.api.common.DeletionDetails)2 Captions (de.symeda.sormas.api.i18n.Captions)2 I18nProperties (de.symeda.sormas.api.i18n.I18nProperties)2 User (de.symeda.sormas.backend.user.User)2 Collections (java.util.Collections)2 Collectors (java.util.stream.Collectors)2 Stateless (javax.ejb.Stateless)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 EntityRelevanceStatus (de.symeda.sormas.api.EntityRelevanceStatus)1 CaseReferenceDto (de.symeda.sormas.api.caze.CaseReferenceDto)1 ContactReferenceDto (de.symeda.sormas.api.contact.ContactReferenceDto)1 EventParticipantReferenceDto (de.symeda.sormas.api.event.EventParticipantReferenceDto)1 Strings (de.symeda.sormas.api.i18n.Strings)1 Validations (de.symeda.sormas.api.i18n.Validations)1 PathogenTestResultType (de.symeda.sormas.api.sample.PathogenTestResultType)1 PathogenTestType (de.symeda.sormas.api.sample.PathogenTestType)1 SampleAssociationType (de.symeda.sormas.api.sample.SampleAssociationType)1 SampleCriteria (de.symeda.sormas.api.sample.SampleCriteria)1 SampleIndexDto (de.symeda.sormas.api.sample.SampleIndexDto)1