Search in sources :

Example 1 with RepeatableStageParams

use of org.hisp.dhis.common.RepeatableStageParams in project dhis2-core by dhis2.

the class RepeatableStageParamsHelper method getRepeatableStageParams.

/**
 * @param dimension
 * @return RepeatableStageParams
 * @throws InvalidRepeatableStageParamsException
 */
public static RepeatableStageParams getRepeatableStageParams(String dimension) throws InvalidRepeatableStageParamsException {
    Pattern pattern = Arrays.stream(PS_PARAMS_PATTERN_LIST).filter(p -> p.matcher(dimension).find()).findFirst().orElse(Pattern.compile(""));
    final Matcher matcher = pattern.matcher(dimension);
    List<String> tokens;
    switch(pattern.toString()) {
        case PS_ASTERISK_REGEX:
            return getRepeatableStageParams(0, Integer.MAX_VALUE);
        case PS_INDEX_COUNT_REGEX:
            tokens = getMatchedRepeatableStageParamTokens(matcher, 2);
            return getRepeatableStageParams(Integer.parseInt(tokens.get(0)), Integer.parseInt(tokens.get(1)));
        case PS_INDEX_REGEX:
            return getRepeatableStageParams(Integer.parseInt(getDefaultIndex(matcher)), 1);
        case PS_INDEX_COUNT_START_DATE_END_DATE_REGEX:
            tokens = getMatchedRepeatableStageParamTokens(matcher, 4);
            return getRepeatableStageParams(Integer.parseInt(tokens.get(0)), Integer.parseInt(tokens.get(1)), DateUtils.parseDate(tokens.get(2)), DateUtils.parseDate(tokens.get(3)));
        case PS_INDEX_COUNT_RELATIVE_PERIOD_REGEX:
            tokens = getMatchedRepeatableStageParamTokens(matcher, 3);
            return getRepeatableStageParams(Integer.parseInt(tokens.get(0)), Integer.parseInt(tokens.get(1)), getRelativePeriods(tokens.get(2)));
        case PS_START_DATE_END_DATE_REGEX:
            tokens = getMatchedRepeatableStageParamTokens(matcher, 2);
            return getRepeatableStageParams(DateUtils.parseDate(tokens.get(0)), DateUtils.parseDate(tokens.get(1).trim()));
        case PS_RELATIVE_PERIOD_REGEX:
            tokens = getMatchedRepeatableStageParamTokens(matcher, 1);
            return getRepeatableStageParams(getRelativePeriods(tokens.get(0)));
        default:
            return new RepeatableStageParams();
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) RepeatableStageParams(org.hisp.dhis.common.RepeatableStageParams)

Example 2 with RepeatableStageParams

use of org.hisp.dhis.common.RepeatableStageParams in project dhis2-core by dhis2.

the class AbstractAnalyticsService method getGrid.

protected Grid getGrid(EventQueryParams params) {
    // ---------------------------------------------------------------------
    // Decide access, add constraints and validate
    // ---------------------------------------------------------------------
    securityManager.decideAccessEventQuery(params);
    params = securityManager.withUserConstraints(params);
    queryValidator.validate(params);
    // keywords as well as their periods are removed in the next step,
    // params object is modified
    List<DimensionItemKeywords.Keyword> periodKeywords = params.getDimensions().stream().map(DimensionalObject::getDimensionItemKeywords).filter(dimensionItemKeywords -> dimensionItemKeywords != null && !dimensionItemKeywords.isEmpty()).flatMap(dk -> dk.getKeywords().stream()).collect(Collectors.toList());
    params = new EventQueryParams.Builder(params).withStartEndDatesForPeriods().build();
    // ---------------------------------------------------------------------
    // Headers
    // ---------------------------------------------------------------------
    Grid grid = createGridWithHeaders(params);
    for (DimensionalObject dimension : params.getDimensions()) {
        grid.addHeader(new GridHeader(dimension.getDimension(), dimension.getDimensionDisplayName(), ValueType.TEXT, false, true));
    }
    for (QueryItem item : params.getItems()) {
        /**
         * Special case: If the request contains an item of Org Unit value
         * type and the item UID is linked to coordinates (coordinateField),
         * then create an Header of ValueType COORDINATE and type "Point"
         */
        if (item.getValueType() == ValueType.ORGANISATION_UNIT && params.getCoordinateField().equals(item.getItem().getUid())) {
            grid.addHeader(new GridHeader(item.getItem().getUid(), item.getItem().getDisplayProperty(params.getDisplayProperty()), COORDINATE, false, true, item.getOptionSet(), item.getLegendSet()));
        } else if (hasNonDefaultRepeatableProgramStageOffset(item)) {
            String column = item.getItem().getDisplayProperty(params.getDisplayProperty());
            RepeatableStageParams repeatableStageParams = item.getRepeatableStageParams();
            String name = repeatableStageParams.getDimension();
            grid.addHeader(new GridHeader(name, column, repeatableStageParams.simpleStageValueExpected() ? item.getValueType() : ValueType.REFERENCE, false, true, item.getOptionSet(), item.getLegendSet(), item.getProgramStage().getUid(), item.getRepeatableStageParams()));
        } else {
            final String uid = getItemUid(item);
            final String column = item.getItem().getDisplayProperty(params.getDisplayProperty());
            grid.addHeader(new GridHeader(uid, column, item.getValueType(), false, true, item.getOptionSet(), item.getLegendSet()));
        }
    }
    // ---------------------------------------------------------------------
    // Data
    // ---------------------------------------------------------------------
    long count = 0;
    if (!params.isSkipData() || params.analyzeOnly()) {
        count = addEventData(grid, params);
    }
    // ---------------------------------------------------------------------
    // Meta-data
    // ---------------------------------------------------------------------
    addMetadata(params, periodKeywords, grid);
    if (params.hasDataIdScheme()) {
        substituteData(grid);
    }
    if (params.isPaging()) {
        Pager pager = new Pager(params.getPageWithDefault(), count, params.getPageSizeWithDefault());
        grid.getMetaData().put(PAGER.getKey(), pager);
    }
    maybeApplyHeaders(params, grid);
    return grid;
}
Also used : DIMENSIONS(org.hisp.dhis.analytics.AnalyticsMetaDataKey.DIMENSIONS) ORGUNIT_DIM_ID(org.hisp.dhis.common.DimensionalObject.ORGUNIT_DIM_ID) Arrays(java.util.Arrays) QueryItem(org.hisp.dhis.common.QueryItem) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ValueType(org.hisp.dhis.common.ValueType) StringUtils.joinWith(org.apache.commons.lang3.StringUtils.joinWith) HashMap(java.util.HashMap) AnalyticsSecurityManager(org.hisp.dhis.analytics.AnalyticsSecurityManager) ArrayList(java.util.ArrayList) RepeatableStageParams(org.hisp.dhis.common.RepeatableStageParams) ORG_UNIT_HIERARCHY(org.hisp.dhis.analytics.AnalyticsMetaDataKey.ORG_UNIT_HIERARCHY) Lists(com.google.common.collect.Lists) Calendar(org.hisp.dhis.calendar.Calendar) GridHeader(org.hisp.dhis.common.GridHeader) Map(java.util.Map) DimensionalObjectUtils.getDimensionalItemIds(org.hisp.dhis.common.DimensionalObjectUtils.getDimensionalItemIds) User(org.hisp.dhis.user.User) ORG_UNIT_NAME_HIERARCHY(org.hisp.dhis.analytics.AnalyticsMetaDataKey.ORG_UNIT_NAME_HIERARCHY) DimensionItemKeywords(org.hisp.dhis.common.DimensionItemKeywords) PAGER(org.hisp.dhis.analytics.AnalyticsMetaDataKey.PAGER) COORDINATE(org.hisp.dhis.common.ValueType.COORDINATE) OrganisationUnit.getParentNameGraphMap(org.hisp.dhis.organisationunit.OrganisationUnit.getParentNameGraphMap) EventQueryParams(org.hisp.dhis.analytics.event.EventQueryParams) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) PERIOD_DIM_ID(org.hisp.dhis.common.DimensionalObject.PERIOD_DIM_ID) Collection(java.util.Collection) EventQueryValidator(org.hisp.dhis.analytics.event.EventQueryValidator) Pager(org.hisp.dhis.common.Pager) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Grid(org.hisp.dhis.common.Grid) Collectors(java.util.stream.Collectors) AnalyticsUtils(org.hisp.dhis.analytics.util.AnalyticsUtils) MetadataItem(org.hisp.dhis.common.MetadataItem) Objects(java.util.Objects) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) OrganisationUnit.getParentGraphMap(org.hisp.dhis.organisationunit.OrganisationUnit.getParentGraphMap) List(java.util.List) Option(org.hisp.dhis.option.Option) IdentifiableObjectUtils.getUids(org.hisp.dhis.common.IdentifiableObjectUtils.getUids) DimensionalObject(org.hisp.dhis.common.DimensionalObject) PeriodType(org.hisp.dhis.period.PeriodType) IdentifiableObjectUtils.getLocalPeriodIdentifiers(org.hisp.dhis.common.IdentifiableObjectUtils.getLocalPeriodIdentifiers) ITEMS(org.hisp.dhis.analytics.AnalyticsMetaDataKey.ITEMS) DimensionalObjectUtils.asTypedList(org.hisp.dhis.common.DimensionalObjectUtils.asTypedList) IdScheme(org.hisp.dhis.common.IdScheme) EventQueryParams(org.hisp.dhis.analytics.event.EventQueryParams) QueryItem(org.hisp.dhis.common.QueryItem) Grid(org.hisp.dhis.common.Grid) RepeatableStageParams(org.hisp.dhis.common.RepeatableStageParams) DimensionalObject(org.hisp.dhis.common.DimensionalObject) GridHeader(org.hisp.dhis.common.GridHeader) Pager(org.hisp.dhis.common.Pager)

Example 3 with RepeatableStageParams

use of org.hisp.dhis.common.RepeatableStageParams in project dhis2-core by dhis2.

the class EnrollmentAnalyticsManagerTest method verifyGetColumnOfTypeCoordinateAndWithProgramStagesAndParamsWithReferenceTypeValue.

@Test
void verifyGetColumnOfTypeCoordinateAndWithProgramStagesAndParamsWithReferenceTypeValue() {
    // Given
    DimensionalItemObject dio = new BaseDimensionalItemObject(dataElementA.getUid());
    QueryItem item = new QueryItem(dio);
    item.setValueType(ValueType.COORDINATE);
    item.setProgramStage(programStageWithRepeatableParams);
    item.setProgram(programB);
    RepeatableStageParams repeatableStageParams = new RepeatableStageParams();
    repeatableStageParams.setStartIndex(0);
    repeatableStageParams.setCount(100);
    repeatableStageParams.setStartDate(DateUtils.parseDate("2022-01-01"));
    repeatableStageParams.setEndDate(DateUtils.parseDate("2022-01-31"));
    item.setRepeatableStageParams(repeatableStageParams);
    // When
    String columnSql = subject.getColumn(item);
    // Then
    assertThat(columnSql, is("(select json_agg(t1) from (select \"" + dataElementA.getUid() + "\", incidentdate, duedate, executiondate  from analytics_event_" + programB.getUid() + " where analytics_event_" + programB.getUid() + ".pi = ax.pi and \"" + dataElementA.getUid() + "\" is not null and ps = '" + programStageWithRepeatableParams.getUid() + "' and executiondate >= '2022-01-01'  and executiondate <= '2022-01-31' order by executiondate desc LIMIT 100 ) as t1)"));
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseDimensionalItemObject(org.hisp.dhis.common.BaseDimensionalItemObject) RepeatableStageParams(org.hisp.dhis.common.RepeatableStageParams) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BaseDimensionalItemObject(org.hisp.dhis.common.BaseDimensionalItemObject) Test(org.junit.jupiter.api.Test)

Example 4 with RepeatableStageParams

use of org.hisp.dhis.common.RepeatableStageParams in project dhis2-core by dhis2.

the class GridTest method setUp.

@BeforeEach
void setUp() {
    gridA = new ListGrid();
    gridB = new ListGrid();
    headerA = new GridHeader("ColA", "colA", ValueType.TEXT, false, true, null, null, "programStage", new RepeatableStageParams());
    headerB = new GridHeader("ColB", "colB", ValueType.TEXT, false, true);
    headerC = new GridHeader("ColC", "colC", ValueType.TEXT, true, false);
    gridA.addHeader(headerA);
    gridA.addHeader(headerB);
    gridA.addHeader(headerC);
    gridA.addRow();
    gridA.addValue(11);
    gridA.addValue(12);
    gridA.addValue(13);
    gridA.addRow();
    gridA.addValue(21);
    gridA.addValue(22);
    gridA.addValue(23);
    gridA.addRow();
    gridA.addValue(31);
    gridA.addValue(32);
    gridA.addValue(33);
    gridA.addRow();
    gridA.addValue(41);
    gridA.addValue(42);
    gridA.addValue(43);
    gridB.addRow();
    gridB.addValue(11);
    gridB.addValue(12);
    gridB.addValue(13);
}
Also used : RepeatableStageParams(org.hisp.dhis.common.RepeatableStageParams) GridHeader(org.hisp.dhis.common.GridHeader) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with RepeatableStageParams

use of org.hisp.dhis.common.RepeatableStageParams in project dhis2-core by dhis2.

the class EnrollmentAnalyticsManagerTest method verifyGetColumnOfTypeCoordinateAndWithProgramStagesAndParamsWithNumberTypeValue.

@Test
void verifyGetColumnOfTypeCoordinateAndWithProgramStagesAndParamsWithNumberTypeValue() {
    // Given
    DimensionalItemObject dio = new BaseDimensionalItemObject(dataElementA.getUid());
    QueryItem item = new QueryItem(dio);
    item.setValueType(ValueType.COORDINATE);
    item.setProgramStage(programStageWithRepeatableParams);
    item.setProgram(programB);
    RepeatableStageParams repeatableStageParams = new RepeatableStageParams();
    repeatableStageParams.setStartIndex(0);
    repeatableStageParams.setCount(1);
    item.setRepeatableStageParams(repeatableStageParams);
    // When
    String columnSql = subject.getColumn(item);
    // Then
    assertThat(columnSql, is("(select \"" + dataElementA.getUid() + "\" from analytics_event_" + programB.getUid() + " where analytics_event_" + programB.getUid() + ".pi = ax.pi and \"" + dataElementA.getUid() + "\" is not null and ps = '" + programStageWithRepeatableParams.getUid() + "' order by executiondate desc limit 1 )"));
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseDimensionalItemObject(org.hisp.dhis.common.BaseDimensionalItemObject) RepeatableStageParams(org.hisp.dhis.common.RepeatableStageParams) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BaseDimensionalItemObject(org.hisp.dhis.common.BaseDimensionalItemObject) Test(org.junit.jupiter.api.Test)

Aggregations

RepeatableStageParams (org.hisp.dhis.common.RepeatableStageParams)9 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)3 QueryItem (org.hisp.dhis.common.QueryItem)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 BaseDimensionalItemObject (org.hisp.dhis.common.BaseDimensionalItemObject)2 GridHeader (org.hisp.dhis.common.GridHeader)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Lists (com.google.common.collect.Lists)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 StringUtils.joinWith (org.apache.commons.lang3.StringUtils.joinWith)1 DIMENSIONS (org.hisp.dhis.analytics.AnalyticsMetaDataKey.DIMENSIONS)1