Search in sources :

Example 1 with TextUtils.getCommaDelimitedString

use of org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString in project dhis2-core by dhis2.

the class JdbcDataAnalysisStore method getDeflatedDataValues.

private List<DeflatedDataValue> getDeflatedDataValues(DataElement dataElement, CategoryOptionCombo categoryOptionCombo, Collection<Period> periods, List<Long> organisationUnits, Map<Long, Integer> lowerBoundMap, Map<Long, Integer> upperBoundMap) {
    String periodIds = TextUtils.getCommaDelimitedString(getIdentifiers(periods));
    // @formatter:off
    String sql = "select dv.dataelementid, dv.periodid, dv.sourceid, " + "dv.categoryoptioncomboid, dv.attributeoptioncomboid, dv.value, dv.storedby, dv.lastupdated, " + "dv.created, dv.comment, dv.followup, ou.name as sourcename, " + "? as dataelementname, pt.name as periodtypename, pe.startdate, pe.enddate, " + "? as categoryoptioncomboname " + "from datavalue dv " + "inner join period pe on dv.periodid = pe.periodid " + "inner join periodtype pt on pe.periodtypeid = pt.periodtypeid " + "inner join organisationunit ou on dv.sourceid = ou.organisationunitid " + "where dv.dataelementid = " + dataElement.getId() + " " + "and dv.categoryoptioncomboid = " + categoryOptionCombo.getId() + " " + "and dv.periodid in (" + periodIds + ") and (";
    for (Long orgUnitUid : organisationUnits) {
        sql += "(dv.sourceid = " + orgUnitUid + " " + "and (cast( dv.value as " + statementBuilder.getDoubleColumnType() + " ) < " + lowerBoundMap.get(orgUnitUid) + " " + "or cast(dv.value as " + statementBuilder.getDoubleColumnType() + ") > " + upperBoundMap.get(orgUnitUid) + ")) or ";
    }
    sql = TextUtils.removeLastOr(sql) + ") ";
    sql += "and dv.deleted is false ";
    PreparedStatementSetter pss = (ps) -> {
        ps.setString(1, dataElement.getName());
        ps.setString(2, categoryOptionCombo.getName());
    };
    return jdbcTemplate.query(sql, pss, new DeflatedDataValueNameMinMaxRowMapper(lowerBoundMap, upperBoundMap));
}
Also used : IdentifiableObjectUtils.getIdentifiers(org.hisp.dhis.common.IdentifiableObjectUtils.getIdentifiers) Date(java.util.Date) PreparedStatementSetter(org.springframework.jdbc.core.PreparedStatementSetter) DataAnalysisStore(org.hisp.dhis.dataanalysis.DataAnalysisStore) DeflatedDataValue(org.hisp.dhis.datavalue.DeflatedDataValue) ArrayList(java.util.ArrayList) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataElement(org.hisp.dhis.dataelement.DataElement) PaginatedList(org.hisp.dhis.commons.collection.PaginatedList) Map(java.util.Map) Qualifier(org.springframework.beans.factory.annotation.Qualifier) SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) Repository(org.springframework.stereotype.Repository) TextUtils.getCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString) Period(org.hisp.dhis.period.Period) Collection(java.util.Collection) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) StatementBuilder(org.hisp.dhis.jdbc.StatementBuilder) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) DataAnalysisMeasures(org.hisp.dhis.dataanalysis.DataAnalysisMeasures) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) DeflatedDataValueNameMinMaxRowMapper(org.hisp.dhis.system.objectmapper.DeflatedDataValueNameMinMaxRowMapper) DateUtils(org.hisp.dhis.util.DateUtils) TextUtils(org.hisp.dhis.commons.util.TextUtils) PreparedStatementSetter(org.springframework.jdbc.core.PreparedStatementSetter) TextUtils.getCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString) DeflatedDataValueNameMinMaxRowMapper(org.hisp.dhis.system.objectmapper.DeflatedDataValueNameMinMaxRowMapper)

Example 2 with TextUtils.getCommaDelimitedString

use of org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString in project dhis2-core by dhis2.

the class JdbcDataAnalysisStore method getDataAnalysisMeasures.

@Override
public List<DataAnalysisMeasures> getDataAnalysisMeasures(DataElement dataElement, Collection<CategoryOptionCombo> categoryOptionCombos, Collection<String> parentPaths, Date from) {
    List<DataAnalysisMeasures> measures = new ArrayList<>();
    if (categoryOptionCombos.isEmpty() || parentPaths.isEmpty()) {
        return measures;
    }
    String catOptionComboIds = TextUtils.getCommaDelimitedString(getIdentifiers(categoryOptionCombos));
    String matchPaths = "(";
    for (String path : parentPaths) {
        matchPaths += "ou.path like '" + path + "%' or ";
    }
    matchPaths = TextUtils.removeLastOr(matchPaths) + ") ";
    String sql = "select dv.sourceid, dv.categoryoptioncomboid, " + "avg(cast(dv.value as " + statementBuilder.getDoubleColumnType() + ")) as average, " + "stddev_pop(cast(dv.value as " + statementBuilder.getDoubleColumnType() + ")) as standarddeviation " + "from datavalue dv " + "inner join organisationunit ou on ou.organisationunitid = dv.sourceid " + "inner join period pe on dv.periodid = pe.periodid " + "where dv.dataelementid = " + dataElement.getId() + " " + "and dv.categoryoptioncomboid in (" + catOptionComboIds + ") " + "and pe.startdate >= '" + DateUtils.getMediumDateString(from) + "' " + "and " + matchPaths + "and dv.deleted is false " + "group by dv.sourceid, dv.categoryoptioncomboid";
    SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql);
    while (rowSet.next()) {
        int orgUnitId = rowSet.getInt(1);
        int categoryOptionComboId = rowSet.getInt(2);
        double average = rowSet.getDouble(3);
        double stdDev = rowSet.getDouble(4);
        if (stdDev != 0.0) {
            measures.add(new DataAnalysisMeasures(orgUnitId, categoryOptionComboId, average, stdDev));
        }
    }
    return measures;
}
Also used : SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) ArrayList(java.util.ArrayList) TextUtils.getCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString) DataAnalysisMeasures(org.hisp.dhis.dataanalysis.DataAnalysisMeasures)

Example 3 with TextUtils.getCommaDelimitedString

use of org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString in project dhis2-core by dhis2.

the class JdbcDataAnalysisStore method getDeflatedDataValues.

private List<DeflatedDataValue> getDeflatedDataValues(DataElement dataElement, DataElementCategoryOptionCombo categoryOptionCombo, Collection<Period> periods, List<Integer> organisationUnits, Map<Integer, Integer> lowerBoundMap, Map<Integer, Integer> upperBoundMap) {
    String periodIds = TextUtils.getCommaDelimitedString(getIdentifiers(periods));
    String sql = "select dv.dataelementid, dv.periodid, dv.sourceid, dv.categoryoptioncomboid, dv.value, dv.storedby, dv.lastupdated, " + "dv.created, dv.comment, dv.followup, ou.name as sourcename, " + "'" + dataElement.getName() + "' as dataelementname, pt.name as periodtypename, pe.startdate, pe.enddate, " + "'" + categoryOptionCombo.getName() + "' as categoryoptioncomboname " + "from datavalue dv " + "join period pe on dv.periodid = pe.periodid " + "join periodtype pt on pe.periodtypeid = pt.periodtypeid " + "join organisationunit ou on dv.sourceid = ou.organisationunitid " + "where dv.dataelementid = " + dataElement.getId() + " " + "and dv.categoryoptioncomboid = " + categoryOptionCombo.getId() + " " + "and dv.periodid in (" + periodIds + ") and ( ";
    for (Integer orgUnitUid : organisationUnits) {
        sql += "( dv.sourceid = " + orgUnitUid + " " + "and ( cast( dv.value as " + statementBuilder.getDoubleColumnType() + " ) < " + lowerBoundMap.get(orgUnitUid) + " " + "or cast( dv.value as " + statementBuilder.getDoubleColumnType() + " ) > " + upperBoundMap.get(orgUnitUid) + " ) ) or ";
    }
    sql = TextUtils.removeLastOr(sql) + " ) ";
    sql += "and dv.deleted is false ";
    return jdbcTemplate.query(sql, new DeflatedDataValueNameMinMaxRowMapper(lowerBoundMap, upperBoundMap));
}
Also used : TextUtils.getCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString) DeflatedDataValueNameMinMaxRowMapper(org.hisp.dhis.system.objectmapper.DeflatedDataValueNameMinMaxRowMapper)

Aggregations

TextUtils.getCommaDelimitedString (org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString)3 ArrayList (java.util.ArrayList)2 DataAnalysisMeasures (org.hisp.dhis.dataanalysis.DataAnalysisMeasures)2 DeflatedDataValueNameMinMaxRowMapper (org.hisp.dhis.system.objectmapper.DeflatedDataValueNameMinMaxRowMapper)2 SqlRowSet (org.springframework.jdbc.support.rowset.SqlRowSet)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Collection (java.util.Collection)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 Slf4j (lombok.extern.slf4j.Slf4j)1 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)1 IdentifiableObjectUtils.getIdentifiers (org.hisp.dhis.common.IdentifiableObjectUtils.getIdentifiers)1 PaginatedList (org.hisp.dhis.commons.collection.PaginatedList)1 TextUtils (org.hisp.dhis.commons.util.TextUtils)1 DataAnalysisStore (org.hisp.dhis.dataanalysis.DataAnalysisStore)1 DataElement (org.hisp.dhis.dataelement.DataElement)1 DeflatedDataValue (org.hisp.dhis.datavalue.DeflatedDataValue)1 StatementBuilder (org.hisp.dhis.jdbc.StatementBuilder)1 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)1