Search in sources :

Example 36 with ListGrid

use of org.hisp.dhis.system.grid.ListGrid in project dhis2-core by dhis2.

the class AbstractEventService method getEventsGrid.

@Transactional(readOnly = true)
@Override
public Grid getEventsGrid(EventSearchParams params) {
    User user = currentUserService.getCurrentUser();
    if (params.getProgramStage() == null || params.getProgramStage().getProgram() == null) {
        throw new IllegalQueryException("Program stage can not be null");
    }
    if (params.getProgramStage().getProgramStageDataElements() == null) {
        throw new IllegalQueryException("Program stage should have at least one data element");
    }
    List<OrganisationUnit> organisationUnits = getOrganisationUnits(params, user);
    params.handleCurrentUserSelectionMode(user);
    if (params.isIncludeAllDataElements()) {
        for (ProgramStageDataElement pde : params.getProgramStage().getProgramStageDataElements()) {
            QueryItem qi = new QueryItem(pde.getDataElement(), pde.getDataElement().getLegendSet(), pde.getDataElement().getValueType(), pde.getDataElement().getAggregationType(), pde.getDataElement().hasOptionSet() ? pde.getDataElement().getOptionSet() : null);
            params.getDataElements().add(qi);
        }
    } else {
        if (params.getDataElements().isEmpty()) {
            for (ProgramStageDataElement pde : params.getProgramStage().getProgramStageDataElements()) {
                if (pde.getDisplayInReports()) {
                    QueryItem qi = new QueryItem(pde.getDataElement(), pde.getDataElement().getLegendSet(), pde.getDataElement().getValueType(), pde.getDataElement().getAggregationType(), pde.getDataElement().hasOptionSet() ? pde.getDataElement().getOptionSet() : null);
                    params.getDataElements().add(qi);
                }
            }
        }
    }
    // ---------------------------------------------------------------------
    // Grid headers
    // ---------------------------------------------------------------------
    Grid grid = new ListGrid();
    for (String col : STATIC_EVENT_COLUMNS) {
        grid.addHeader(new GridHeader(col, col));
    }
    for (QueryItem item : params.getDataElements()) {
        grid.addHeader(new GridHeader(item.getItem().getUid(), item.getItem().getName()));
    }
    List<Map<String, String>> events = eventStore.getEventsGrid(params, organisationUnits);
    for (Map<String, String> event : events) {
        grid.addRow();
        for (String col : STATIC_EVENT_COLUMNS) {
            grid.addValue(event.get(col));
        }
        for (QueryItem item : params.getDataElements()) {
            grid.addValue(event.get(item.getItemId()));
        }
    }
    Map<String, Object> metaData = new HashMap<>();
    if (params.isPaging()) {
        final Pager pager;
        if (params.isTotalPages()) {
            int count = eventStore.getEventCount(params, organisationUnits);
            pager = new Pager(params.getPageWithDefault(), count, params.getPageSizeWithDefault());
        } else {
            pager = handleLastPageFlag(params, grid);
        }
        metaData.put(PAGER_META_KEY, pager);
    }
    grid.setMetaData(metaData);
    return grid;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) QueryItem(org.hisp.dhis.common.QueryItem) User(org.hisp.dhis.user.User) HashMap(java.util.HashMap) ListGrid(org.hisp.dhis.system.grid.ListGrid) Grid(org.hisp.dhis.common.Grid) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) DateUtils.getMediumDateString(org.hisp.dhis.util.DateUtils.getMediumDateString) ListGrid(org.hisp.dhis.system.grid.ListGrid) GridHeader(org.hisp.dhis.common.GridHeader) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) SlimPager(org.hisp.dhis.common.SlimPager) Pager(org.hisp.dhis.common.Pager) Map(java.util.Map) HashMap(java.util.HashMap) Collections.emptyMap(java.util.Collections.emptyMap) CachingMap(org.hisp.dhis.commons.collection.CachingMap) Transactional(org.springframework.transaction.annotation.Transactional)

Example 37 with ListGrid

use of org.hisp.dhis.system.grid.ListGrid in project dhis2-core by dhis2.

the class DataAggregator method getAggregatedDataValueGrid.

/**
 * Returns a grid with aggregated data.
 *
 * @param params the {@link DataQueryParams}.
 * @return a grid with aggregated data.
 */
public Grid getAggregatedDataValueGrid(DataQueryParams params) {
    params = preHandleQuery(params);
    // ---------------------------------------------------------------------
    // Headers
    // ---------------------------------------------------------------------
    Grid grid = new ListGrid();
    headerHandler.addHeaders(params, grid);
    // ---------------------------------------------------------------------
    // Data
    // ---------------------------------------------------------------------
    dataHandler.addIndicatorValues(params, grid);
    dataHandler.addDataElementValues(params, grid);
    dataHandler.addDataElementOperandValues(params, grid);
    dataHandler.addReportingRates(params, grid);
    dataHandler.addProgramDataElementAttributeIndicatorValues(params, grid);
    dataHandler.addDynamicDimensionValues(params, grid);
    dataHandler.addValidationResultValues(params, grid);
    dataHandler.addPerformanceMetrics(params, grid);
    // ---------------------------------------------------------------------
    // Meta-data
    // ---------------------------------------------------------------------
    metaDataHandler.addMetaData(params, grid);
    metaDataHandler.handleDataValueSet(params, grid);
    metaDataHandler.applyIdScheme(params, grid);
    postHandleGrid(params, grid);
    return grid;
}
Also used : ListGrid(org.hisp.dhis.system.grid.ListGrid) Grid(org.hisp.dhis.common.Grid) ListGrid(org.hisp.dhis.system.grid.ListGrid)

Example 38 with ListGrid

use of org.hisp.dhis.system.grid.ListGrid in project dhis2-core by dhis2.

the class EnrollmentAnalyticsManagerTest method verifyWithProgramIndicatorAndRelationshipTypeBothSidesTei2.

@Test
void verifyWithProgramIndicatorAndRelationshipTypeBothSidesTei2() {
    Date startDate = getDate(2015, 1, 1);
    Date endDate = getDate(2017, 4, 8);
    Program programB = createProgram('B');
    String piSubquery = "distinct psi";
    ProgramIndicator programIndicatorA = createProgramIndicator('A', programB, "", "");
    RelationshipType relationshipTypeA = createRelationshipType();
    EventQueryParams.Builder params = new EventQueryParams.Builder(createRequestParams(programIndicatorA, relationshipTypeA)).withStartDate(startDate).withEndDate(endDate);
    when(programIndicatorService.getAnalyticsSql("", programIndicatorA, getDate(2000, 1, 1), getDate(2017, 4, 8), "subax")).thenReturn(piSubquery);
    subject.getEnrollments(params.build(), new ListGrid(), 100);
    verify(jdbcTemplate).queryForRowSet(sql.capture());
    String expected = "ax.\"monthly\",ax.\"ou\",(SELECT avg (" + piSubquery + ") FROM analytics_event_" + programB.getUid().toLowerCase() + " as subax WHERE  " + "subax.tei in (select tei.uid from trackedentityinstance tei " + "LEFT JOIN relationshipitem ri on tei.trackedentityinstanceid = ri.trackedentityinstanceid  " + "LEFT JOIN relationship r on r.from_relationshipitemid = ri.relationshipitemid " + "LEFT JOIN relationshipitem ri2 on r.to_relationshipitemid = ri2.relationshipitemid " + "LEFT JOIN relationshiptype rty on rty.relationshiptypeid = r.relationshiptypeid " + "LEFT JOIN trackedentityinstance tei on tei.trackedentityinstanceid = ri2.trackedentityinstanceid " + "WHERE rty.relationshiptypeid = " + relationshipTypeA.getId() + " AND tei.uid = ax.tei )) as \"" + programIndicatorA.getUid() + "\"  " + "from analytics_enrollment_" + programA.getUid() + " as ax where enrollmentdate >= '2015-01-01' and enrollmentdate < '2017-04-09' and (uidlevel1 = 'ouabcdefghA' ) limit 101";
    assertSql(sql.getValue(), expected);
}
Also used : EventQueryParams(org.hisp.dhis.analytics.event.EventQueryParams) DhisConvenienceTest.createProgram(org.hisp.dhis.DhisConvenienceTest.createProgram) Program(org.hisp.dhis.program.Program) PostgreSQLStatementBuilder(org.hisp.dhis.jdbc.statementbuilder.PostgreSQLStatementBuilder) DefaultProgramIndicatorSubqueryBuilder(org.hisp.dhis.analytics.event.data.programindicator.DefaultProgramIndicatorSubqueryBuilder) StatementBuilder(org.hisp.dhis.jdbc.StatementBuilder) RelationshipType(org.hisp.dhis.relationship.RelationshipType) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DhisConvenienceTest.createProgramIndicator(org.hisp.dhis.DhisConvenienceTest.createProgramIndicator) ProgramIndicator(org.hisp.dhis.program.ProgramIndicator) ListGrid(org.hisp.dhis.system.grid.ListGrid) Date(java.util.Date) DhisConvenienceTest.getDate(org.hisp.dhis.DhisConvenienceTest.getDate) Test(org.junit.jupiter.api.Test)

Example 39 with ListGrid

use of org.hisp.dhis.system.grid.ListGrid in project dhis2-core by dhis2.

the class EnrollmentAnalyticsManagerTest method verifyWithProgramStageAndTextualDataElementAndFilter.

@Test
void verifyWithProgramStageAndTextualDataElementAndFilter() {
    EventQueryParams params = createRequestParamsWithFilter(programStage, ValueType.TEXT);
    subject.getEnrollments(params, new ListGrid(), 10000);
    verify(jdbcTemplate).queryForRowSet(sql.capture());
    String subSelect = "(select \"fWIAEtYVEGk\" from analytics_event_" + programA.getUid() + " where analytics_event_" + programA.getUid() + ".pi = ax.pi and \"fWIAEtYVEGk\" is not null and ps = '" + programStage.getUid() + "' order by executiondate desc limit 1 )";
    String expected = "ax.\"monthly\",ax.\"ou\"," + subSelect + "  from " + getTable(programA.getUid()) + " as ax where ax.\"monthly\" in ('2000Q1') and (uidlevel1 = 'ouabcdefghA' ) " + "and ps = '" + programStage.getUid() + "' and " + subSelect + " > '10' limit 10001";
    assertSql(sql.getValue(), expected);
}
Also used : EventQueryParams(org.hisp.dhis.analytics.event.EventQueryParams) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ListGrid(org.hisp.dhis.system.grid.ListGrid) Test(org.junit.jupiter.api.Test)

Example 40 with ListGrid

use of org.hisp.dhis.system.grid.ListGrid in project dhis2-core by dhis2.

the class EnrollmentAnalyticsManagerTest method verifyWithProgramIndicatorAndRelationshipTypeDifferentConstraint.

@Test
void verifyWithProgramIndicatorAndRelationshipTypeDifferentConstraint() {
    Date startDate = getDate(2015, 1, 1);
    Date endDate = getDate(2017, 4, 8);
    String piSubquery = "distinct psi";
    ProgramIndicator programIndicatorA = createProgramIndicator('A', programA, "", "");
    RelationshipType relationshipTypeA = createRelationshipType(RelationshipEntity.PROGRAM_INSTANCE);
    EventQueryParams.Builder params = new EventQueryParams.Builder(createRequestParams(programIndicatorA, relationshipTypeA)).withStartDate(startDate).withEndDate(endDate);
    when(programIndicatorService.getAnalyticsSql("", programIndicatorA, getDate(2000, 1, 1), getDate(2017, 4, 8), "subax")).thenReturn(piSubquery);
    subject.getEnrollments(params.build(), new ListGrid(), 100);
    verify(jdbcTemplate).queryForRowSet(sql.capture());
    String expected = "ax.\"monthly\",ax.\"ou\",(SELECT avg (" + piSubquery + ") FROM analytics_event_" + programA.getUid().toLowerCase() + " as subax WHERE " + " subax.tei in (select tei.uid from trackedentityinstance tei LEFT JOIN relationshipitem ri on tei.trackedentityinstanceid = ri.trackedentityinstanceid  " + "LEFT JOIN relationship r on r.from_relationshipitemid = ri.relationshipitemid " + "LEFT JOIN relationshipitem ri2 on r.to_relationshipitemid = ri2.relationshipitemid " + "LEFT JOIN relationshiptype rty on rty.relationshiptypeid = r.relationshiptypeid " + "LEFT JOIN programinstance pi on pi.programinstanceid = ri2.programinstanceid WHERE rty.relationshiptypeid " + "= " + relationshipTypeA.getId() + " AND pi.uid = ax.pi ))" + " as \"" + programIndicatorA.getUid() + "\"  " + "from analytics_enrollment_" + programA.getUid() + " as ax where enrollmentdate >= '2015-01-01' and enrollmentdate < '2017-04-09' and (uidlevel1 = 'ouabcdefghA' ) limit 101";
    assertSql(sql.getValue(), expected);
}
Also used : EventQueryParams(org.hisp.dhis.analytics.event.EventQueryParams) PostgreSQLStatementBuilder(org.hisp.dhis.jdbc.statementbuilder.PostgreSQLStatementBuilder) DefaultProgramIndicatorSubqueryBuilder(org.hisp.dhis.analytics.event.data.programindicator.DefaultProgramIndicatorSubqueryBuilder) StatementBuilder(org.hisp.dhis.jdbc.StatementBuilder) RelationshipType(org.hisp.dhis.relationship.RelationshipType) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DhisConvenienceTest.createProgramIndicator(org.hisp.dhis.DhisConvenienceTest.createProgramIndicator) ProgramIndicator(org.hisp.dhis.program.ProgramIndicator) ListGrid(org.hisp.dhis.system.grid.ListGrid) Date(java.util.Date) DhisConvenienceTest.getDate(org.hisp.dhis.DhisConvenienceTest.getDate) Test(org.junit.jupiter.api.Test)

Aggregations

ListGrid (org.hisp.dhis.system.grid.ListGrid)58 Grid (org.hisp.dhis.common.Grid)41 GridHeader (org.hisp.dhis.common.GridHeader)28 Test (org.junit.jupiter.api.Test)21 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)14 EventQueryParams (org.hisp.dhis.analytics.event.EventQueryParams)11 ArrayList (java.util.ArrayList)10 DimensionalObject (org.hisp.dhis.common.DimensionalObject)10 HashMap (java.util.HashMap)9 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)9 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 DataElement (org.hisp.dhis.dataelement.DataElement)8 List (java.util.List)7 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)7 Test (org.junit.Test)7 ProgramIndicator (org.hisp.dhis.program.ProgramIndicator)6 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)5 DataValueSet (org.hisp.dhis.dxf2.datavalueset.DataValueSet)5 I18nFormat (org.hisp.dhis.i18n.I18nFormat)5