Search in sources :

Example 91 with DataElement

use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.

the class JdbcEventAnalyticsTableManager method getDimensionColumns.

@Override
public List<AnalyticsTableColumn> getDimensionColumns(AnalyticsTable table) {
    final String dbl = statementBuilder.getDoubleColumnType();
    final String numericClause = " and value " + statementBuilder.getRegexpMatch() + " '" + NUMERIC_LENIENT_REGEXP + "'";
    final String dateClause = " and value " + statementBuilder.getRegexpMatch() + " '" + DATE_REGEXP + "'";
    //TODO dateClause regular expression
    List<AnalyticsTableColumn> columns = new ArrayList<>();
    if (table.getProgram().hasCategoryCombo()) {
        List<DataElementCategory> categories = table.getProgram().getCategoryCombo().getCategories();
        for (DataElementCategory category : categories) {
            if (category.isDataDimension()) {
                columns.add(new AnalyticsTableColumn(quote(category.getUid()), "character(11)", "acs." + quote(category.getUid()), category.getCreated()));
            }
        }
    }
    List<OrganisationUnitLevel> levels = organisationUnitService.getFilledOrganisationUnitLevels();
    List<OrganisationUnitGroupSet> orgUnitGroupSets = idObjectManager.getDataDimensionsNoAcl(OrganisationUnitGroupSet.class);
    List<CategoryOptionGroupSet> attributeCategoryOptionGroupSets = categoryService.getAttributeCategoryOptionGroupSetsNoAcl();
    for (OrganisationUnitLevel level : levels) {
        String column = quote(PREFIX_ORGUNITLEVEL + level.getLevel());
        columns.add(new AnalyticsTableColumn(column, "character(11)", "ous." + column, level.getCreated()));
    }
    for (OrganisationUnitGroupSet groupSet : orgUnitGroupSets) {
        columns.add(new AnalyticsTableColumn(quote(groupSet.getUid()), "character(11)", "ougs." + quote(groupSet.getUid()), groupSet.getCreated()));
    }
    for (CategoryOptionGroupSet groupSet : attributeCategoryOptionGroupSets) {
        columns.add(new AnalyticsTableColumn(quote(groupSet.getUid()), "character(11)", "acs." + quote(groupSet.getUid()), groupSet.getCreated()));
    }
    for (PeriodType periodType : PeriodType.getAvailablePeriodTypes()) {
        String column = quote(periodType.getName().toLowerCase());
        columns.add(new AnalyticsTableColumn(column, "character varying(15)", "dps." + column));
    }
    for (DataElement dataElement : table.getProgram().getDataElements()) {
        ValueType valueType = dataElement.getValueType();
        String dataType = getColumnType(valueType);
        String dataClause = dataElement.isNumericType() ? numericClause : dataElement.getValueType().isDate() ? dateClause : "";
        String select = getSelectClause(valueType);
        boolean skipIndex = NO_INDEX_VAL_TYPES.contains(dataElement.getValueType()) && !dataElement.hasOptionSet();
        String sql = "(select " + select + " from trackedentitydatavalue where programstageinstanceid=psi.programstageinstanceid " + "and dataelementid=" + dataElement.getId() + dataClause + ") as " + quote(dataElement.getUid());
        columns.add(new AnalyticsTableColumn(quote(dataElement.getUid()), dataType, sql, skipIndex));
    }
    for (DataElement dataElement : table.getProgram().getDataElementsWithLegendSet()) {
        for (LegendSet legendSet : dataElement.getLegendSets()) {
            String column = quote(dataElement.getUid() + PartitionUtils.SEP + legendSet.getUid());
            String select = getSelectClause(dataElement.getValueType());
            String sql = "(select l.uid from maplegend l " + "inner join trackedentitydatavalue dv on l.startvalue <= " + select + " " + "and l.endvalue > " + select + " " + "and l.maplegendsetid=" + legendSet.getId() + " " + "and dv.programstageinstanceid=psi.programstageinstanceid " + "and dv.dataelementid=" + dataElement.getId() + numericClause + ") as " + column;
            columns.add(new AnalyticsTableColumn(column, "character(11)", sql));
        }
    }
    for (TrackedEntityAttribute attribute : table.getProgram().getNonConfidentialTrackedEntityAttributes()) {
        String dataType = getColumnType(attribute.getValueType());
        String dataClause = attribute.isNumericType() ? numericClause : attribute.isDateType() ? dateClause : "";
        String select = getSelectClause(attribute.getValueType());
        boolean skipIndex = NO_INDEX_VAL_TYPES.contains(attribute.getValueType()) && !attribute.hasOptionSet();
        String sql = "(select " + select + " from trackedentityattributevalue where trackedentityinstanceid=pi.trackedentityinstanceid " + "and trackedentityattributeid=" + attribute.getId() + dataClause + ") as " + quote(attribute.getUid());
        columns.add(new AnalyticsTableColumn(quote(attribute.getUid()), dataType, sql, skipIndex));
    }
    for (TrackedEntityAttribute attribute : table.getProgram().getNonConfidentialTrackedEntityAttributesWithLegendSet()) {
        for (LegendSet legendSet : attribute.getLegendSets()) {
            String column = quote(attribute.getUid() + PartitionUtils.SEP + legendSet.getUid());
            String select = getSelectClause(attribute.getValueType());
            String sql = "(select l.uid from maplegend l " + "inner join trackedentityattributevalue av on l.startvalue <= " + select + " " + "and l.endvalue > " + select + " " + "and l.maplegendsetid=" + legendSet.getId() + " " + "and av.trackedentityinstanceid=pi.trackedentityinstanceid " + "and av.trackedentityattributeid=" + attribute.getId() + numericClause + ") as " + column;
            columns.add(new AnalyticsTableColumn(column, "character(11)", sql));
        }
    }
    AnalyticsTableColumn psi = new AnalyticsTableColumn(quote("psi"), "character(11) not null", "psi.uid");
    AnalyticsTableColumn pi = new AnalyticsTableColumn(quote("pi"), "character(11) not null", "pi.uid");
    AnalyticsTableColumn ps = new AnalyticsTableColumn(quote("ps"), "character(11) not null", "ps.uid");
    AnalyticsTableColumn ao = new AnalyticsTableColumn(quote("ao"), "character(11) not null", "ao.uid");
    AnalyticsTableColumn erd = new AnalyticsTableColumn(quote("enrollmentdate"), "timestamp", "pi.enrollmentdate");
    AnalyticsTableColumn id = new AnalyticsTableColumn(quote("incidentdate"), "timestamp", "pi.incidentdate");
    AnalyticsTableColumn ed = new AnalyticsTableColumn(quote("executiondate"), "timestamp", "psi.executiondate");
    AnalyticsTableColumn dd = new AnalyticsTableColumn(quote("duedate"), "timestamp", "psi.duedate");
    AnalyticsTableColumn cd = new AnalyticsTableColumn(quote("completeddate"), "timestamp", "psi.completeddate");
    AnalyticsTableColumn pes = new AnalyticsTableColumn(quote("pistatus"), "character(25)", "pi.status");
    AnalyticsTableColumn es = new AnalyticsTableColumn(quote("psistatus"), "character(25)", "psi.status");
    AnalyticsTableColumn longitude = new AnalyticsTableColumn(quote("longitude"), dbl, "psi.longitude");
    AnalyticsTableColumn latitude = new AnalyticsTableColumn(quote("latitude"), dbl, "psi.latitude");
    AnalyticsTableColumn ou = new AnalyticsTableColumn(quote("ou"), "character(11) not null", "ou.uid");
    AnalyticsTableColumn oun = new AnalyticsTableColumn(quote("ouname"), "character varying(230) not null", "ou.name");
    AnalyticsTableColumn ouc = new AnalyticsTableColumn(quote("oucode"), "character varying(50)", "ou.code");
    columns.addAll(Lists.newArrayList(psi, pi, ps, ao, erd, id, ed, dd, cd, pes, es, longitude, latitude, ou, oun, ouc));
    if (databaseInfo.isSpatialSupport()) {
        String alias = "(select ST_SetSRID(ST_MakePoint(psi.longitude, psi.latitude), 4326)) as geom";
        columns.add(new AnalyticsTableColumn(quote("geom"), "geometry(Point, 4326)", alias, false, "gist"));
    }
    if (table.hasProgram() && table.getProgram().isRegistration()) {
        columns.add(new AnalyticsTableColumn(quote("tei"), "character(11)", "tei.uid"));
    }
    return filterDimensionColumns(columns);
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) ValueType(org.hisp.dhis.common.ValueType) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) UniqueArrayList(org.hisp.dhis.commons.collection.UniqueArrayList) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) CategoryOptionGroupSet(org.hisp.dhis.dataelement.CategoryOptionGroupSet) LegendSet(org.hisp.dhis.legend.LegendSet) AnalyticsTableColumn(org.hisp.dhis.analytics.AnalyticsTableColumn) DataElement(org.hisp.dhis.dataelement.DataElement) OrganisationUnitLevel(org.hisp.dhis.organisationunit.OrganisationUnitLevel) OrganisationUnitGroupSet(org.hisp.dhis.organisationunit.OrganisationUnitGroupSet)

Example 92 with DataElement

use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.

the class AnalyticsUtilsTest method testGetDimensionalItemObjectMap.

@Test
public void testGetDimensionalItemObjectMap() {
    DataElement deA = createDataElement('A');
    Indicator inA = createIndicator('A', null);
    DataSet dsA = createDataSet('A');
    DimensionalObject dx = new BaseDimensionalObject(DimensionalObject.DATA_X_DIM_ID, DimensionType.DATA_X, DimensionalObjectUtils.getList(deA, inA, dsA));
    DataQueryParams params = DataQueryParams.newBuilder().addDimension(dx).withDisplayProperty(DisplayProperty.NAME).build();
    Map<String, DimensionalItemObject> map = AnalyticsUtils.getDimensionalItemObjectMap(params);
    assertEquals(map.get(deA.getDimensionItem()), deA);
    assertEquals(map.get(inA.getDimensionItem()), inA);
    assertEquals(map.get(dsA.getDimensionItem()), dsA);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DataSet(org.hisp.dhis.dataset.DataSet) Indicator(org.hisp.dhis.indicator.Indicator) ProgramIndicator(org.hisp.dhis.program.ProgramIndicator) DimensionalObject(org.hisp.dhis.common.DimensionalObject) Test(org.junit.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 93 with DataElement

use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.

the class AnalyticsUtilsTest method testGetDimensionItemNameMap.

@Test
public void testGetDimensionItemNameMap() {
    DataElement deA = createDataElement('A');
    Indicator inA = createIndicator('A', null);
    DataSet dsA = createDataSet('A');
    OrganisationUnit ouA = createOrganisationUnit('A');
    OrganisationUnit ouB = createOrganisationUnit('B');
    DimensionalObject dx = new BaseDimensionalObject(DimensionalObject.DATA_X_DIM_ID, DimensionType.DATA_X, DimensionalObjectUtils.getList(deA, inA, dsA));
    DimensionalObject ou = new BaseDimensionalObject(DimensionalObject.ORGUNIT_DIM_ID, DimensionType.ORGANISATION_UNIT, Lists.newArrayList(ouA, ouB));
    DataQueryParams params = DataQueryParams.newBuilder().addDimension(dx).addDimension(ou).withDisplayProperty(DisplayProperty.NAME).build();
    Map<String, String> map = AnalyticsUtils.getDimensionItemNameMap(params);
    assertEquals(map.get(deA.getDimensionItem()), deA.getDisplayName());
    assertEquals(map.get(inA.getDimensionItem()), inA.getDisplayName());
    assertEquals(map.get(dsA.getDimensionItem()), dsA.getDisplayName());
    assertEquals(map.get(ouA.getDimensionItem()), ouA.getDisplayName());
    assertEquals(map.get(ouB.getDimensionItem()), ouB.getDisplayName());
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataSet(org.hisp.dhis.dataset.DataSet) Indicator(org.hisp.dhis.indicator.Indicator) ProgramIndicator(org.hisp.dhis.program.ProgramIndicator) DimensionalObject(org.hisp.dhis.common.DimensionalObject) Test(org.junit.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 94 with DataElement

use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.

the class AnalyticsUtilsTest method testGetByDataDimensionType.

@Test
public void testGetByDataDimensionType() {
    Program prA = createProgram('A');
    DataElement deA = createDataElement('A', new DataElementCategoryCombo());
    DataElement deB = createDataElement('B', new DataElementCategoryCombo());
    ProgramDataElementDimensionItem pdeA = new ProgramDataElementDimensionItem(prA, deA);
    ProgramDataElementDimensionItem pdeB = new ProgramDataElementDimensionItem(prA, deB);
    ProgramIndicator piA = createProgramIndicator('A', prA, null, null);
    List<DimensionalItemObject> list = Lists.newArrayList(deA, deB, pdeA, pdeB, piA);
    assertEquals(Lists.newArrayList(deA, deB), AnalyticsUtils.getByDataDimensionItemType(DataDimensionItemType.DATA_ELEMENT, list));
    assertEquals(Lists.newArrayList(pdeA, pdeB), AnalyticsUtils.getByDataDimensionItemType(DataDimensionItemType.PROGRAM_DATA_ELEMENT, list));
    assertEquals(Lists.newArrayList(piA), AnalyticsUtils.getByDataDimensionItemType(DataDimensionItemType.PROGRAM_INDICATOR, list));
    assertEquals(Lists.newArrayList(), AnalyticsUtils.getByDataDimensionItemType(DataDimensionItemType.PROGRAM_ATTRIBUTE, list));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Program(org.hisp.dhis.program.Program) DataElementCategoryCombo(org.hisp.dhis.dataelement.DataElementCategoryCombo) ProgramDataElementDimensionItem(org.hisp.dhis.program.ProgramDataElementDimensionItem) ProgramIndicator(org.hisp.dhis.program.ProgramIndicator) Test(org.junit.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 95 with DataElement

use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.

the class ExpressionUpgrader method upgradeExpression.

private String upgradeExpression(String expression) {
    if (expression == null || expression.trim().isEmpty()) {
        return null;
    }
    boolean changes = false;
    StringBuffer sb = new StringBuffer();
    try {
        // -----------------------------------------------------------------
        // Constants
        // -----------------------------------------------------------------
        Matcher matcher = OLD_CONSTANT_PATTERN.matcher(expression);
        while (matcher.find()) {
            Constant constant = constantService.getConstant(Integer.parseInt(matcher.group(1)));
            String replacement = "C{" + constant.getUid() + "}";
            matcher.appendReplacement(sb, replacement);
            changes = true;
        }
        matcher.appendTail(sb);
        expression = sb.toString();
        // -----------------------------------------------------------------
        // Operands
        // -----------------------------------------------------------------
        matcher = OLD_OPERAND_PATTERN.matcher(expression);
        sb = new StringBuffer();
        while (matcher.find()) {
            DataElement de = dataElementService.getDataElement(Integer.parseInt(matcher.group(1)));
            String replacement = "#{" + de.getUid();
            if (matcher.groupCount() == 2 && matcher.group(2) != null && !matcher.group(2).trim().isEmpty()) {
                DataElementCategoryOptionCombo coc = categoryService.getDataElementCategoryOptionCombo(Integer.parseInt(matcher.group(2)));
                replacement += "." + coc.getUid();
            }
            replacement += "}";
            matcher.appendReplacement(sb, replacement);
            changes = true;
        }
        matcher.appendTail(sb);
        expression = sb.toString();
    } catch (Exception ex) {
        log.error("Failed to upgrade expression: " + expression);
        // Log and continue
        log.error(ex);
    }
    if (changes) {
        log.info("Upgraded expression: " + expression);
    }
    return changes ? expression : null;
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Matcher(java.util.regex.Matcher) Constant(org.hisp.dhis.constant.Constant) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Aggregations

DataElement (org.hisp.dhis.dataelement.DataElement)254 Test (org.junit.Test)137 DhisSpringTest (org.hisp.dhis.DhisSpringTest)99 User (org.hisp.dhis.user.User)51 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)47 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)44 DataSet (org.hisp.dhis.dataset.DataSet)39 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)36 ArrayList (java.util.ArrayList)31 List (java.util.List)31 ClassPathResource (org.springframework.core.io.ClassPathResource)26 HashSet (java.util.HashSet)23 Period (org.hisp.dhis.period.Period)20 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)19 DataElementGroup (org.hisp.dhis.dataelement.DataElementGroup)18 DataElementCategoryCombo (org.hisp.dhis.dataelement.DataElementCategoryCombo)16 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)15 DataValue (org.hisp.dhis.datavalue.DataValue)15 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)14 Matcher (java.util.regex.Matcher)13