Search in sources :

Example 11 with SortProperty

use of de.symeda.sormas.api.utils.SortProperty in project SORMAS-Project by hzi-braunschweig.

the class ActionService method getActionList.

public List<Action> getActionList(ActionCriteria actionCriteria, Integer first, Integer max, List<SortProperty> sortProperties) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Action> cq = cb.createQuery(getElementClass());
    Root<Action> action = cq.from(getElementClass());
    ActionQueryContext queryContext = new ActionQueryContext(cb, cq, action);
    // Add filters
    Predicate filter = null;
    if (actionCriteria == null || !actionCriteria.hasContextCriteria()) {
        filter = createUserFilter(queryContext);
    }
    if (actionCriteria != null) {
        Predicate criteriaFilter = buildCriteriaFilter(actionCriteria, queryContext);
        filter = CriteriaBuilderHelper.and(cb, filter, criteriaFilter);
    }
    if (filter != null) {
        cq.where(filter);
    }
    if (sortProperties != null && !sortProperties.isEmpty()) {
        List<Order> order = new ArrayList<>(sortProperties.size());
        for (SortProperty sortProperty : sortProperties) {
            Expression<?> expression;
            switch(sortProperty.propertyName) {
                case ActionDto.UUID:
                case ActionDto.ACTION_STATUS:
                case ActionDto.ACTION_MEASURE:
                case ActionDto.ACTION_CONTEXT:
                case ActionDto.CHANGE_DATE:
                case ActionDto.CREATION_DATE:
                case ActionDto.DATE:
                case ActionDto.PRIORITY:
                    expression = action.get(sortProperty.propertyName);
                    break;
                case ActionDto.TITLE:
                    expression = cb.lower(action.get(sortProperty.propertyName));
                    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(action.get(Action.DATE)));
    }
    return QueryHelper.getResultList(em, cq, first, max);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) SortProperty(de.symeda.sormas.api.utils.SortProperty) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate)

Example 12 with SortProperty

use of de.symeda.sormas.api.utils.SortProperty in project SORMAS-Project by hzi-braunschweig.

the class ActionService method getEventActionIndexList.

public List<EventActionIndexDto> getEventActionIndexList(EventCriteria criteria, Integer first, Integer max, List<SortProperty> sortProperties) {
    final CriteriaBuilder cb = em.getCriteriaBuilder();
    final CriteriaQuery<Object[]> cq = cb.createQuery(Object[].class);
    final Root<Action> action = cq.from(getElementClass());
    final ActionQueryContext queryContext = new ActionQueryContext(cb, cq, action);
    final ActionJoins actionJoins = queryContext.getJoins();
    Join<Action, User> lastModifiedBy = actionJoins.getLastModifiedBy();
    Join<Action, User> creatorUser = actionJoins.getCreator();
    Join<Action, Event> event = actionJoins.getEvent();
    Join<Event, User> eventReportingUser = actionJoins.getEventJoins().getReportingUser();
    Join<Event, User> eventResponsibleUser = actionJoins.getEventJoins().getResponsibleUser();
    // Add filters
    Predicate filter = eventService.createUserFilter(new EventQueryContext(cb, cq, actionJoins.getEventJoins()));
    if (criteria != null) {
        Predicate criteriaFilter = buildEventCriteriaFilter(criteria, queryContext);
        filter = CriteriaBuilderHelper.and(cb, filter, criteriaFilter);
    }
    if (filter != null) {
        cq.where(filter);
    }
    cq.multiselect(event.get(Event.UUID), event.get(Event.EVENT_TITLE), event.get(Event.DISEASE), event.get(Event.DISEASE_VARIANT), event.get(Event.DISEASE_DETAILS), event.get(Event.EVENT_IDENTIFICATION_SOURCE), event.get(Event.START_DATE), event.get(Event.END_DATE), event.get(Event.EVENT_STATUS), event.get(Event.RISK_LEVEL), event.get(Event.EVENT_INVESTIGATION_STATUS), event.get(Event.EVENT_MANAGEMENT_STATUS), eventReportingUser.get(User.UUID), eventReportingUser.get(User.FIRST_NAME), eventReportingUser.get(User.LAST_NAME), eventResponsibleUser.get(User.UUID), eventResponsibleUser.get(User.FIRST_NAME), eventResponsibleUser.get(User.LAST_NAME), action.get(Action.ACTION_MEASURE), event.get(Event.EVOLUTION_DATE), action.get(Action.TITLE), action.get(Action.CREATION_DATE), action.get(Action.CHANGE_DATE), action.get(Action.DATE), action.get(Action.ACTION_STATUS), action.get(Action.PRIORITY), lastModifiedBy.get(User.UUID), lastModifiedBy.get(User.FIRST_NAME), lastModifiedBy.get(User.LAST_NAME), creatorUser.get(User.UUID), creatorUser.get(User.FIRST_NAME), creatorUser.get(User.LAST_NAME), event.get(Event.CHANGE_DATE));
    cq.distinct(true);
    if (sortProperties != null && !sortProperties.isEmpty()) {
        List<Order> order = new ArrayList<>(sortProperties.size());
        for (SortProperty sortProperty : sortProperties) {
            Expression<?> expression;
            switch(sortProperty.propertyName) {
                case EventActionIndexDto.EVENT_UUID:
                    expression = event.get(Event.UUID);
                    break;
                case EventActionIndexDto.EVENT_TITLE:
                    expression = cb.lower(event.get(Event.EVENT_TITLE));
                    break;
                case EventActionIndexDto.EVENT_DISEASE:
                    expression = event.get(Event.DISEASE);
                    break;
                case EventActionIndexDto.EVENT_DISEASE_VARIANT:
                    expression = event.get(Event.DISEASE_VARIANT);
                    break;
                case EventActionIndexDto.EVENT_IDENTIFICATION_SOURCE:
                    expression = event.get(Event.EVENT_IDENTIFICATION_SOURCE);
                    break;
                case EventActionIndexDto.EVENT_START_DATE:
                    expression = event.get(Event.START_DATE);
                    break;
                case EventActionIndexDto.EVENT_END_DATE:
                    expression = event.get(Event.END_DATE);
                    break;
                case EventActionIndexDto.EVENT_STATUS:
                    expression = event.get(Event.EVENT_STATUS);
                    break;
                case EventActionIndexDto.EVENT_INVESTIGATION_STATUS:
                    expression = event.get(Event.EVENT_INVESTIGATION_STATUS);
                    break;
                case EventActionIndexDto.EVENT_MANAGEMENT_STATUS:
                    expression = event.get(Event.EVENT_MANAGEMENT_STATUS);
                    break;
                case EventActionIndexDto.EVENT_EVOLUTION_DATE:
                    expression = event.get(Event.EVOLUTION_DATE);
                    break;
                case EventActionIndexDto.EVENT_RISK_LEVEL:
                    expression = event.get(Event.RISK_LEVEL);
                    break;
                case EventActionIndexDto.EVENT_REPORTING_USER:
                    expression = event.get(Event.REPORTING_USER);
                    break;
                case EventActionIndexDto.EVENT_RESPONSIBLE_USER:
                    expression = event.get(Event.RESPONSIBLE_USER);
                    break;
                case EventActionIndexDto.ACTION_CHANGE_DATE:
                    expression = action.get(Action.CHANGE_DATE);
                    break;
                case EventActionIndexDto.ACTION_CREATION_DATE:
                    expression = action.get(Action.CREATION_DATE);
                    break;
                case EventActionIndexDto.ACTION_DATE:
                    expression = action.get(Action.DATE);
                    break;
                case EventActionIndexDto.ACTION_PRIORITY:
                    expression = action.get(Action.PRIORITY);
                    break;
                case EventActionIndexDto.ACTION_STATUS:
                    expression = action.get(Action.ACTION_STATUS);
                    break;
                case EventActionIndexDto.ACTION_TITLE:
                    expression = cb.lower(action.get(Action.TITLE));
                    break;
                case EventActionIndexDto.ACTION_LAST_MODIFIED_BY:
                    expression = cb.selectCase().when(cb.isNotNull(action.get(Action.LAST_MODIFIED_BY)), cb.lower(action.get(Action.LAST_MODIFIED_BY).get(User.LAST_NAME))).otherwise(cb.lower(action.get(Action.CREATOR_USER).get(User.LAST_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(event.get(Event.CHANGE_DATE)));
    }
    @SuppressWarnings("unchecked") List<EventActionIndexDto> result = createQuery(cq, first, max).unwrap(org.hibernate.query.Query.class).setResultTransformer(new EventActionIndexDtoReasultTransformer()).getResultList();
    return result;
}
Also used : EventActionIndexDto(de.symeda.sormas.api.event.EventActionIndexDto) User(de.symeda.sormas.backend.user.User) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate) SortProperty(de.symeda.sormas.api.utils.SortProperty) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) EventQueryContext(de.symeda.sormas.backend.event.EventQueryContext) Event(de.symeda.sormas.backend.event.Event)

Example 13 with SortProperty

use of de.symeda.sormas.api.utils.SortProperty in project SORMAS-Project by hzi-braunschweig.

the class PersonFacadeEjbTest method testCountAndGetIndexListWithAssociations.

/**
 * Test all {@link PersonAssociation} variants if they work. Also serves to review the generated SQL.
 */
@Test
public void testCountAndGetIndexListWithAssociations() {
    PersonFacade cut = getPersonFacade();
    Integer offset = null;
    Integer limit = null;
    List<SortProperty> sortProperties = null;
    UserDto user = creator.createUser(rdcf.region.getUuid(), null, null, null, "Surv", "Sup", UserRole.SURVEILLANCE_SUPERVISOR);
    loginWith(user);
    // 1a. Test for all available PersonAssociations
    for (PersonAssociation pa : PersonAssociation.values()) {
        PersonCriteria criteria = new PersonCriteria().personAssociation(pa);
        assertThat("Failed for testing association on count: " + pa.name(), cut.count(criteria), equalTo(0L));
        assertThat(criteria.getPersonAssociation(), equalTo(pa));
        assertThat("Failed for testing association on getIndexList: " + pa.name(), cut.getIndexList(criteria, offset, limit, sortProperties), is(empty()));
        assertThat(criteria.getPersonAssociation(), equalTo(pa));
    }
    // 1b. Test that calling with "null" as criteria also works
    assertThat(cut.count(null), equalTo(0L));
    assertThat(cut.getIndexList(null, offset, limit, sortProperties), is(empty()));
    // 2. Test paging windows
    final PersonDto person1 = creator.createPerson("James", "Smith", Sex.MALE, 1920, 1, 1);
    final CaseDataDto case1 = creator.createCase(user.toReference(), person1.toReference(), Disease.EVD, CaseClassification.PROBABLE, InvestigationStatus.PENDING, new Date(), rdcf);
    final PersonDto person2 = creator.createPerson("Maria", "Garcia", Sex.FEMALE, 1920, 1, 1);
    final ContactDto contact2 = creator.createContact(user.toReference(), user.toReference(), person2.toReference(), null, new Date(), new Date(), Disease.EVD, rdcf);
    // 2a. count
    assertThat(cut.count(new PersonCriteria().personAssociation(PersonAssociation.ALL)), equalTo(2L));
    assertThat(cut.count(new PersonCriteria().personAssociation(PersonAssociation.CASE)), equalTo(1L));
    assertThat(cut.count(new PersonCriteria().personAssociation(PersonAssociation.CONTACT)), equalTo(1L));
    // 2b. getIndexList with all persons in the paging window
    assertPersonsFound(case1, contact2, cut, offset, limit, sortProperties);
    offset = 0;
    limit = 2;
    assertPersonsFound(case1, contact2, cut, offset, limit, sortProperties);
    offset = 0;
    limit = 1;
    assertPersonsFound(case1, contact2, cut, offset, limit, sortProperties);
    // 2c. getIndexList [PersonAssociation.ALL] with only the contact person in the paging window (default sorting by changeDate)
    offset = 1;
    limit = 2;
    assertPersonsFound(null, null, Arrays.asList(contact2.getPerson()), cut, offset, limit, sortProperties);
}
Also used : SortProperty(de.symeda.sormas.api.utils.SortProperty) CaseDataDto(de.symeda.sormas.api.caze.CaseDataDto) PersonDto(de.symeda.sormas.api.person.PersonDto) JournalPersonDto(de.symeda.sormas.api.person.JournalPersonDto) PersonFacade(de.symeda.sormas.api.person.PersonFacade) UserDto(de.symeda.sormas.api.user.UserDto) PersonCriteria(de.symeda.sormas.api.person.PersonCriteria) ContactDto(de.symeda.sormas.api.contact.ContactDto) PersonAssociation(de.symeda.sormas.api.person.PersonAssociation) Date(java.util.Date) AbstractBeanTest(de.symeda.sormas.backend.AbstractBeanTest) Test(org.junit.Test)

Example 14 with SortProperty

use of de.symeda.sormas.api.utils.SortProperty in project SORMAS-Project by hzi-braunschweig.

the class SampleFacadeEjbTest method testGetIndexListForCaseConvertedFromContact.

@Test
public void testGetIndexListForCaseConvertedFromContact() {
    RDCFEntities rdcf = creator.createRDCFEntities("Region", "District", "Community", "Facility");
    UserDto user = creator.createUser(rdcf.region.getUuid(), rdcf.district.getUuid(), rdcf.facility.getUuid(), "Surv", "Sup", UserRole.SURVEILLANCE_SUPERVISOR);
    PersonDto cazePerson = creator.createPerson("Case", "Person1");
    CaseDataDto caze = creator.createCase(user.toReference(), cazePerson.toReference(), Disease.EVD, CaseClassification.PROBABLE, InvestigationStatus.PENDING, new Date(), rdcf);
    PersonDto contactPerson = creator.createPerson("Contact", "Person2");
    ContactDto contact = creator.createContact(user.toReference(), contactPerson.toReference(), caze);
    SampleDto cazeSample = creator.createSample(caze.toReference(), user.toReference(), rdcf.facility);
    cazeSample.setSampleDateTime(DateHelper.subtractDays(new Date(), 5));
    getSampleFacade().saveSample(cazeSample);
    SampleDto sample = creator.createSample(contact.toReference(), DateHelper.subtractDays(new Date(), 4), new Date(), user.toReference(), SampleMaterial.BLOOD, rdcf.facility);
    SampleDto referredSample = creator.createSample(contact.toReference(), DateHelper.subtractDays(new Date(), 3), new Date(), user.toReference(), SampleMaterial.BLOOD, rdcf.facility);
    sample.setReferredTo(referredSample.toReference());
    creator.createAdditionalTest(sample.toReference());
    CaseDataDto caseDataDto = CaseDataDto.buildFromContact(contact);
    caseDataDto.setResponsibleRegion(new RegionReferenceDto(rdcf.region.getUuid(), null, null));
    caseDataDto.setResponsibleDistrict(new DistrictReferenceDto(rdcf.district.getUuid(), null, null));
    caseDataDto.setFacilityType(rdcf.facility.getType());
    caseDataDto.setHealthFacility(new FacilityReferenceDto(rdcf.facility.getUuid(), null, null));
    caseDataDto.setReportingUser(user.toReference());
    CaseDataDto caseConvertedFromContact = getCaseFacade().save(caseDataDto);
    getCaseFacade().setSampleAssociations(contact.toReference(), caseConvertedFromContact.toReference());
    final SampleCriteria samplesConnectedToConvertedCaseCriteria = new SampleCriteria().caze(caseConvertedFromContact.toReference());
    assertEquals(2, getSampleFacade().count(samplesConnectedToConvertedCaseCriteria));
    final ArrayList<SortProperty> sortProperties = new ArrayList<>();
    sortProperties.add(new SortProperty(SampleIndexDto.SAMPLE_DATE_TIME));
    sortProperties.add(new SortProperty(SampleIndexDto.ASSOCIATED_CONTACT));
    final List<SampleIndexDto> samplesOfConvertedCase = getSampleFacade().getIndexList(samplesConnectedToConvertedCaseCriteria, 0, 100, sortProperties);
    assertEquals(2, samplesOfConvertedCase.size());
    final SampleIndexDto sample11 = samplesOfConvertedCase.get(0);
    Assert.assertEquals(sample.getUuid(), sample11.getUuid());
    Assert.assertEquals(caseConvertedFromContact.getUuid(), sample11.getAssociatedCase().getUuid());
    Assert.assertEquals(contact.getUuid(), sample11.getAssociatedContact().getUuid());
    final SampleIndexDto sample12 = samplesOfConvertedCase.get(1);
    Assert.assertEquals(referredSample.getUuid(), sample12.getUuid());
    Assert.assertEquals(contact.getUuid(), sample12.getAssociatedContact().getUuid());
    Assert.assertEquals(caseConvertedFromContact.getUuid(), sample11.getAssociatedCase().getUuid());
}
Also used : CaseDataDto(de.symeda.sormas.api.caze.CaseDataDto) FacilityReferenceDto(de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto) PersonDto(de.symeda.sormas.api.person.PersonDto) SampleCriteria(de.symeda.sormas.api.sample.SampleCriteria) UserDto(de.symeda.sormas.api.user.UserDto) ArrayList(java.util.ArrayList) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) Date(java.util.Date) RDCFEntities(de.symeda.sormas.backend.TestDataCreator.RDCFEntities) SortProperty(de.symeda.sormas.api.utils.SortProperty) RegionReferenceDto(de.symeda.sormas.api.infrastructure.region.RegionReferenceDto) ContactDto(de.symeda.sormas.api.contact.ContactDto) SampleDto(de.symeda.sormas.api.sample.SampleDto) SampleIndexDto(de.symeda.sormas.api.sample.SampleIndexDto) AbstractBeanTest(de.symeda.sormas.backend.AbstractBeanTest) Test(org.junit.Test)

Example 15 with SortProperty

use of de.symeda.sormas.api.utils.SortProperty in project SORMAS-Project by hzi-braunschweig.

the class FacilityFacadeEjbTest method testGetIndexListMappedSorting.

@Test
public void testGetIndexListMappedSorting() {
    FacilityCriteria facilityCriteria = new FacilityCriteria();
    // 0. No sortProperties
    List<FacilityIndexDto> result = getFacilityFacade().getIndexList(facilityCriteria, null, null, new ArrayList<>());
    assertThat(result, is(empty()));
    List<SortProperty> allSortProperties = new ArrayList<>();
    allSortProperties.add(new SortProperty(FacilityDto.NAME));
    allSortProperties.add(new SortProperty(FacilityDto.TYPE));
    allSortProperties.add(new SortProperty(FacilityDto.REGION));
    allSortProperties.add(new SortProperty(FacilityDto.DISTRICT));
    allSortProperties.add(new SortProperty(FacilityDto.COMMUNITY));
    allSortProperties.add(new SortProperty(FacilityDto.CITY));
    allSortProperties.add(new SortProperty(FacilityDto.LATITUDE));
    allSortProperties.add(new SortProperty(FacilityDto.LONGITUDE));
    allSortProperties.add(new SortProperty(FacilityDto.EXTERNAL_ID));
    // 1. Sort by every property
    for (SortProperty sortProperty : allSortProperties) {
        getFacilityFacade().getIndexList(facilityCriteria, null, null, Collections.singletonList(sortProperty));
        assertThat(sortProperty.toString(), result, is(empty()));
    }
    // 2. Sort by all properties at once
    getFacilityFacade().getIndexList(facilityCriteria, null, null, allSortProperties);
    assertThat(result, is(empty()));
}
Also used : SortProperty(de.symeda.sormas.api.utils.SortProperty) FacilityIndexDto(de.symeda.sormas.api.infrastructure.facility.FacilityIndexDto) ArrayList(java.util.ArrayList) FacilityCriteria(de.symeda.sormas.api.infrastructure.facility.FacilityCriteria) Test(org.junit.Test) AbstractBeanTest(de.symeda.sormas.backend.AbstractBeanTest)

Aggregations

SortProperty (de.symeda.sormas.api.utils.SortProperty)73 ArrayList (java.util.ArrayList)42 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)37 Order (javax.persistence.criteria.Order)37 Predicate (javax.persistence.criteria.Predicate)34 Date (java.util.Date)14 District (de.symeda.sormas.backend.infrastructure.district.District)13 Region (de.symeda.sormas.backend.infrastructure.region.Region)10 Pseudonymizer (de.symeda.sormas.backend.util.Pseudonymizer)10 PersonDto (de.symeda.sormas.api.person.PersonDto)9 Location (de.symeda.sormas.backend.location.Location)8 AbstractDomainObject (de.symeda.sormas.backend.common.AbstractDomainObject)7 Person (de.symeda.sormas.backend.person.Person)7 User (de.symeda.sormas.backend.user.User)7 Collectors (java.util.stream.Collectors)6 I18nProperties (de.symeda.sormas.api.i18n.I18nProperties)5 UserDto (de.symeda.sormas.api.user.UserDto)5 AbstractBeanTest (de.symeda.sormas.backend.AbstractBeanTest)5 Case (de.symeda.sormas.backend.caze.Case)5 Selection (javax.persistence.criteria.Selection)5