Search in sources :

Example 21 with EventSearchParams

use of org.hisp.dhis.dxf2.events.event.EventSearchParams in project dhis2-core by dhis2.

the class JdbcEventStore method buildGridSql.

private String buildGridSql(EventSearchParams params, List<OrganisationUnit> organisationUnits) {
    SqlHelper hlp = new SqlHelper();
    // ---------------------------------------------------------------------
    // Select clause
    // ---------------------------------------------------------------------
    StringBuilder sqlBuilder = new StringBuilder().append("select ").append(COLUMNS_ALIAS_MAP.entrySet().stream().map(col -> col.getKey() + " as " + col.getValue()).collect(Collectors.joining(", "))).append(" , ");
    for (QueryItem item : params.getDataElementsAndFilters()) {
        final String col = item.getItemId();
        final String dataValueValueSql = "psi.eventdatavalues #>> '{" + col + ", value}'";
        String queryCol = item.isNumeric() ? castToNumber(dataValueValueSql) : dataValueValueSql;
        queryCol += " as " + col + ", ";
        sqlBuilder.append(queryCol);
    }
    String intermediateSql = sqlBuilder.toString();
    sqlBuilder = new StringBuilder().append(removeLastComma(intermediateSql)).append(" ");
    // ---------------------------------------------------------------------
    // From and where clause
    // ---------------------------------------------------------------------
    sqlBuilder.append(getFromWhereClause(params, hlp, organisationUnits));
    // ---------------------------------------------------------------------
    // Order clause
    // ---------------------------------------------------------------------
    sqlBuilder.append(getGridOrderQuery(params));
    // ---------------------------------------------------------------------
    // Paging clause
    // ---------------------------------------------------------------------
    sqlBuilder.append(getEventPagingQuery(params));
    return sqlBuilder.toString();
}
Also used : EventUtils.userInfoToJson(org.hisp.dhis.dxf2.events.event.EventUtils.userInfoToJson) WKTReader(org.locationtech.jts.io.WKTReader) UID(org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UID) CREATEDCLIENT(org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.CREATEDCLIENT) EventRow(org.hisp.dhis.dxf2.events.report.EventRow) DELETED(org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.DELETED) UPDATED(org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UPDATED) StringUtils(org.apache.commons.lang3.StringUtils) EVENT_CREATED_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_CREATED_ID) EVENT_EXECUTION_DATE_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_EXECUTION_DATE_ID) Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) EnrollmentStatus(org.hisp.dhis.dxf2.events.enrollment.EnrollmentStatus) STATIC_EVENT_COLUMNS(org.hisp.dhis.dxf2.events.event.AbstractEventService.STATIC_EVENT_COLUMNS) CREATED(org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.CREATED) Map(java.util.Map) SqlUtils.castToNumber(org.hisp.dhis.system.util.SqlUtils.castToNumber) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) SqlUtils.lower(org.hisp.dhis.system.util.SqlUtils.lower) SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) Repository(org.springframework.stereotype.Repository) EVENT_STATUS_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_STATUS_ID) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) JpaQueryUtils(org.hisp.dhis.query.JpaQueryUtils) DateUtils.getMediumDateString(org.hisp.dhis.util.DateUtils.getMediumDateString) Set(java.util.Set) STATUS(org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.STATUS) EXECUTION_DATE(org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.EXECUTION_DATE) PreparedStatement(java.sql.PreparedStatement) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) TextUtils.removeLastComma(org.hisp.dhis.commons.util.TextUtils.removeLastComma) Slf4j(lombok.extern.slf4j.Slf4j) ParseException(org.locationtech.jts.io.ParseException) ProgramType(org.hisp.dhis.program.ProgramType) QueryFilter(org.hisp.dhis.common.QueryFilter) EVENT_STORED_BY_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_STORED_BY_ID) Joiner(com.google.common.base.Joiner) QueryItem(org.hisp.dhis.common.QueryItem) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) EVENT_GEOMETRY(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_GEOMETRY) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) STOREDBY(org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.STOREDBY) SqlHelper(org.hisp.dhis.commons.util.SqlHelper) OrderParam(org.hisp.dhis.webapi.controller.event.mapper.OrderParam) EVENT_ENROLLMENT_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_ENROLLMENT_ID) IdSchemes(org.hisp.dhis.common.IdSchemes) QueryOperator(org.hisp.dhis.common.QueryOperator) ID(org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.ID) IOException(java.io.IOException) StatementBuilder(org.hisp.dhis.jdbc.StatementBuilder) ObjectUtils(org.hisp.dhis.util.ObjectUtils) GEOMETRY(org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.GEOMETRY) COMPLETEDBY(org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.COMPLETEDBY) DateUtils(org.hisp.dhis.util.DateUtils) TextUtils(org.hisp.dhis.commons.util.TextUtils) EVENT_PROGRAM_STAGE_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_PROGRAM_STAGE_ID) Date(java.util.Date) ValueType(org.hisp.dhis.common.ValueType) DateUtils.getDateAfterAddition(org.hisp.dhis.util.DateUtils.getDateAfterAddition) RequiredArgsConstructor(lombok.RequiredArgsConstructor) EVENT_LAST_UPDATED_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_LAST_UPDATED_ID) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) Gson(com.google.gson.Gson) SqlUtils.escapeSql(org.hisp.dhis.system.util.SqlUtils.escapeSql) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString) TypeReference(com.fasterxml.jackson.core.type.TypeReference) EVENT_PROGRAM_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_PROGRAM_ID) EventUtils.eventDataValuesToJson(org.hisp.dhis.dxf2.events.event.EventUtils.eventDataValuesToJson) EVENT_DUE_DATE_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_DUE_DATE_ID) ImmutableMap(com.google.common.collect.ImmutableMap) DateUtils.getLongGmtDateString(org.hisp.dhis.util.DateUtils.getLongGmtDateString) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) Timestamp(java.sql.Timestamp) Collection(java.util.Collection) UserInfoSnapshot(org.hisp.dhis.program.UserInfoSnapshot) EventStatus(org.hisp.dhis.event.EventStatus) Collectors(java.util.stream.Collectors) TextUtils.splitToArray(org.hisp.dhis.commons.util.TextUtils.splitToArray) List(java.util.List) CollectionUtils.isNotEmpty(org.apache.commons.collections4.CollectionUtils.isNotEmpty) Environment(org.springframework.core.env.Environment) AclService(org.hisp.dhis.security.acl.AclService) Optional(java.util.Optional) Geometry(org.locationtech.jts.geom.Geometry) BatchPreparedStatementSetterWithKeyHolder(org.hisp.dhis.jdbc.BatchPreparedStatementSetterWithKeyHolder) EVENT_ORG_UNIT_NAME(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_ORG_UNIT_NAME) EVENT_CREATED_BY_USER_INFO_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_CREATED_BY_USER_INFO_ID) EVENT_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_ID) DataAccessException(org.springframework.dao.DataAccessException) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) Program(org.hisp.dhis.program.Program) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) HashSet(java.util.HashSet) DataElement(org.hisp.dhis.dataelement.DataElement) PGobject(org.postgresql.util.PGobject) ImmutableList(com.google.common.collect.ImmutableList) UPDATEDCLIENT(org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UPDATEDCLIENT) Qualifier(org.springframework.beans.factory.annotation.Qualifier) User(org.hisp.dhis.user.User) EVENT_ORG_UNIT_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_ORG_UNIT_ID) EVENT_LAST_UPDATED_BY_USER_INFO_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_LAST_UPDATED_BY_USER_INFO_ID) EVENT_DELETED(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_DELETED) JdbcUtils(org.hisp.dhis.jdbc.JdbcUtils) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) EventUtils.jsonToUserInfo(org.hisp.dhis.dxf2.events.event.EventUtils.jsonToUserInfo) ProgramStage(org.hisp.dhis.program.ProgramStage) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ProgramStatus(org.hisp.dhis.program.ProgramStatus) Collectors.toList(java.util.stream.Collectors.toList) CollectionUtils(org.hisp.dhis.commons.collection.CollectionUtils) EVENT_ATTRIBUTE_OPTION_COMBO_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_ATTRIBUTE_OPTION_COMBO_ID) JsonEventDataValueSetBinaryType(org.hisp.dhis.hibernate.jsonb.type.JsonEventDataValueSetBinaryType) CurrentUserService(org.hisp.dhis.user.CurrentUserService) COMPLETEDDATE(org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.COMPLETEDDATE) CachingMap(org.hisp.dhis.commons.collection.CachingMap) DUE_DATE(org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.DUE_DATE) Comparator(java.util.Comparator) EVENT_COMPLETED_BY_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_COMPLETED_BY_ID) EVENT_COMPLETED_DATE_ID(org.hisp.dhis.dxf2.events.event.EventSearchParams.EVENT_COMPLETED_DATE_ID) IdScheme(org.hisp.dhis.common.IdScheme) QueryItem(org.hisp.dhis.common.QueryItem) SqlHelper(org.hisp.dhis.commons.util.SqlHelper) DateUtils.getMediumDateString(org.hisp.dhis.util.DateUtils.getMediumDateString) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString) DateUtils.getLongGmtDateString(org.hisp.dhis.util.DateUtils.getLongGmtDateString)

Example 22 with EventSearchParams

use of org.hisp.dhis.dxf2.events.event.EventSearchParams in project dhis2-core by dhis2.

the class EventXmlImportTest method testNoAccessEvent.

@Test
void testNoAccessEvent() throws IOException {
    InputStream is = createEventXmlInputStream();
    ImportSummaries importSummaries = eventService.addEventsXml(is, null);
    assertEquals(ImportStatus.SUCCESS, importSummaries.getStatus());
    // Get by admin
    Events events = eventService.getEvents(new EventSearchParams().setProgram(programA).setOrgUnitSelectionMode(OrganisationUnitSelectionMode.ACCESSIBLE));
    assertEquals(1, events.getEvents().size());
    // Get by user without access
    User user = createUser("A");
    userService.addUser(user);
    injectSecurityContext(user);
    events = eventService.getEvents(new EventSearchParams().setProgram(programA).setOrgUnitSelectionMode(OrganisationUnitSelectionMode.ACCESSIBLE));
    assertEquals(0, events.getEvents().size());
}
Also used : User(org.hisp.dhis.user.User) Events(org.hisp.dhis.dxf2.events.event.Events) InputStream(java.io.InputStream) EventSearchParams(org.hisp.dhis.dxf2.events.event.EventSearchParams) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 23 with EventSearchParams

use of org.hisp.dhis.dxf2.events.event.EventSearchParams in project dhis2-core by dhis2.

the class EventXmlImportTest method testGeometry.

@Test
void testGeometry() throws IOException {
    InputStream is = createEventXmlInputStream();
    ImportSummaries importSummaries = eventService.addEventsXml(is, null);
    assertEquals(ImportStatus.SUCCESS, importSummaries.getStatus());
    Events events = eventService.getEvents(new EventSearchParams().setProgram(programA).setOrgUnitSelectionMode(OrganisationUnitSelectionMode.ACCESSIBLE));
    assertEquals(1, events.getEvents().size());
    assertTrue(events.getEvents().stream().allMatch(e -> e.getGeometry().getGeometryType().equals("Point")));
}
Also used : EventService(org.hisp.dhis.dxf2.events.event.EventService) Date(java.util.Date) ValueType(org.hisp.dhis.common.ValueType) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) ClassPathResource(org.springframework.core.io.ClassPathResource) Autowired(org.springframework.beans.factory.annotation.Autowired) Program(org.hisp.dhis.program.Program) HashSet(java.util.HashSet) DataElement(org.hisp.dhis.dataelement.DataElement) EventSearchParams(org.hisp.dhis.dxf2.events.event.EventSearchParams) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) ProgramInstance(org.hisp.dhis.program.ProgramInstance) User(org.hisp.dhis.user.User) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) UserService(org.hisp.dhis.user.UserService) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) IOException(java.io.IOException) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) ProgramStageDataElementService(org.hisp.dhis.program.ProgramStageDataElementService) ProgramStage(org.hisp.dhis.program.ProgramStage) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Test(org.junit.jupiter.api.Test) ProgramStatus(org.hisp.dhis.program.ProgramStatus) FeatureType(org.hisp.dhis.organisationunit.FeatureType) Events(org.hisp.dhis.dxf2.events.event.Events) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ProgramType(org.hisp.dhis.program.ProgramType) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) InputStream(java.io.InputStream) Events(org.hisp.dhis.dxf2.events.event.Events) InputStream(java.io.InputStream) EventSearchParams(org.hisp.dhis.dxf2.events.event.EventSearchParams) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 24 with EventSearchParams

use of org.hisp.dhis.dxf2.events.event.EventSearchParams in project dhis2-core by dhis2.

the class RegistrationMultiEventsServiceTest method testDeleteEnrollmentWithEvents.

@Test
void testDeleteEnrollmentWithEvents() {
    Event event = createEvent(programA.getUid(), programStageA.getUid(), organisationUnitA.getUid(), trackedEntityInstanceMaleA.getTrackedEntityInstance(), dataElementA.getUid());
    eventService.addEvent(event, null, false);
    Enrollment enrollment = createEnrollment(programA.getUid(), trackedEntityInstanceMaleA.getTrackedEntityInstance());
    enrollment.setEvents(Lists.newArrayList(event));
    ImportSummary importSummary = enrollmentService.addEnrollment(enrollment, null, null);
    assertEquals(ImportStatus.SUCCESS, importSummary.getStatus());
    TrackedEntityInstance tei = trackedEntityInstanceService.getTrackedEntityInstance(maleA.getUid());
    Enrollment retrievedEnrlollment = enrollmentService.getEnrollment(tei.getEnrollments().get(0).getEnrollment());
    EventSearchParams params = new EventSearchParams();
    params.setProgram(programA);
    params.setOrgUnit(organisationUnitA);
    params.setOrgUnitSelectionMode(OrganisationUnitSelectionMode.SELECTED);
    Event retrievedEvent = enrollment.getEvents().get(0);
    assertNotNull(retrievedEnrlollment);
    assertNotNull(retrievedEvent);
    enrollmentService.deleteEnrollment(retrievedEnrlollment.getEnrollment());
    assertNull(enrollmentService.getEnrollment(tei.getEnrollments().get(0).getEnrollment()));
    assertEquals(1, eventService.getEvents(params).getEvents().size());
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) EventSearchParams(org.hisp.dhis.dxf2.events.event.EventSearchParams) Event(org.hisp.dhis.dxf2.events.event.Event) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 25 with EventSearchParams

use of org.hisp.dhis.dxf2.events.event.EventSearchParams in project dhis2-core by dhis2.

the class RegistrationSingleEventServiceTest method testSavingMultipleEventsShouldOnlyUpdate.

@Test
@Disabled
void testSavingMultipleEventsShouldOnlyUpdate() {
    Enrollment enrollment = createEnrollment(programA.getUid(), trackedEntityInstanceMaleA.getTrackedEntityInstance());
    ImportSummary importSummary = enrollmentService.addEnrollment(enrollment, null, null);
    assertEquals(ImportStatus.SUCCESS, importSummary.getStatus());
    Event event = createEvent(programA.getUid(), programStageA.getUid(), organisationUnitA.getUid(), trackedEntityInstanceMaleA.getTrackedEntityInstance());
    importSummary = eventService.addEvent(event, null, false);
    assertEquals(ImportStatus.SUCCESS, importSummary.getStatus());
    EventSearchParams params = new EventSearchParams();
    params.setProgram(programA);
    params.setOrgUnit(organisationUnitA);
    params.setOrgUnitSelectionMode(OrganisationUnitSelectionMode.SELECTED);
    assertEquals(1, eventService.getEvents(params).getEvents().size());
    event = createEvent(programA.getUid(), programStageA.getUid(), organisationUnitA.getUid(), trackedEntityInstanceMaleA.getTrackedEntityInstance());
    importSummary = eventService.addEvent(event, null, false);
    assertEquals(ImportStatus.SUCCESS, importSummary.getStatus());
    assertEquals(1, eventService.getEvents(params).getEvents().size());
    event = createEvent(programA.getUid(), programStageA.getUid(), organisationUnitA.getUid(), trackedEntityInstanceMaleA.getTrackedEntityInstance());
    importSummary = eventService.addEvent(event, null, false);
    assertEquals(ImportStatus.SUCCESS, importSummary.getStatus());
    assertEquals(1, eventService.getEvents(params).getEvents().size());
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) EventSearchParams(org.hisp.dhis.dxf2.events.event.EventSearchParams) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) Event(org.hisp.dhis.dxf2.events.event.Event) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

EventSearchParams (org.hisp.dhis.dxf2.events.event.EventSearchParams)28 Events (org.hisp.dhis.dxf2.events.event.Events)11 Test (org.junit.jupiter.api.Test)10 GetMapping (org.springframework.web.bind.annotation.GetMapping)10 Event (org.hisp.dhis.dxf2.events.event.Event)8 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)7 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)7 User (org.hisp.dhis.user.User)7 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)7 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)6 Program (org.hisp.dhis.program.Program)5 ProgramStage (org.hisp.dhis.program.ProgramStage)5 HashSet (java.util.HashSet)4 QueryItem (org.hisp.dhis.common.QueryItem)4 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)4 ImmutableList (com.google.common.collect.ImmutableList)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)3