Search in sources :

Example 26 with SqlRowSet

use of org.springframework.jdbc.support.rowset.SqlRowSet in project dhis2-core by dhis2.

the class HibernateTrackedEntityInstanceStore method getTrackedEntityInstancesGrid.

@Override
public List<Map<String, String>> getTrackedEntityInstancesGrid(TrackedEntityInstanceQueryParams params) {
    SqlHelper hlp = new SqlHelper();
    // ---------------------------------------------------------------------
    // Select clause
    // ---------------------------------------------------------------------
    String sql = "select tei.uid as " + TRACKED_ENTITY_INSTANCE_ID + ", " + "tei.created as " + CREATED_ID + ", " + "tei.lastupdated as " + LAST_UPDATED_ID + ", " + "ou.uid as " + ORG_UNIT_ID + ", " + "ou.name as " + ORG_UNIT_NAME + ", " + "te.uid as " + TRACKED_ENTITY_ID + ", " + (params.isIncludeDeleted() ? "tei.deleted as " + DELETED + ", " : "") + "tei.inactive as " + INACTIVE_ID + ", ";
    for (QueryItem item : params.getAttributes()) {
        String col = statementBuilder.columnQuote(item.getItemId());
        sql += item.isNumeric() ? "CAST( " + col + ".value AS NUMERIC ) as " : col + ".value as ";
        sql += col + ", ";
    }
    sql = removeLastComma(sql) + " ";
    // ---------------------------------------------------------------------
    // From and where clause
    // ---------------------------------------------------------------------
    sql += getFromWhereClause(params, hlp);
    // ---------------------------------------------------------------------
    // Order clause
    // ---------------------------------------------------------------------
    sql += getOrderClause(params);
    if (params.isPaging()) {
        sql += " limit " + params.getPageSizeWithDefault() + " offset " + params.getOffset();
    }
    // ---------------------------------------------------------------------
    // Query
    // ---------------------------------------------------------------------
    log.info("Query: " + sql);
    SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql);
    log.debug("Tracked entity instance query SQL: " + sql);
    List<Map<String, String>> list = new ArrayList<>();
    while (rowSet.next()) {
        final Map<String, String> map = new HashMap<>();
        map.put(TRACKED_ENTITY_INSTANCE_ID, rowSet.getString(TRACKED_ENTITY_INSTANCE_ID));
        map.put(CREATED_ID, rowSet.getString(CREATED_ID));
        map.put(LAST_UPDATED_ID, rowSet.getString(LAST_UPDATED_ID));
        map.put(ORG_UNIT_ID, rowSet.getString(ORG_UNIT_ID));
        map.put(ORG_UNIT_NAME, rowSet.getString(ORG_UNIT_NAME));
        map.put(TRACKED_ENTITY_ID, rowSet.getString(TRACKED_ENTITY_ID));
        map.put(INACTIVE_ID, rowSet.getString(INACTIVE_ID));
        if (params.isIncludeDeleted()) {
            map.put(DELETED, rowSet.getString(DELETED));
        }
        for (QueryItem item : params.getAttributes()) {
            map.put(item.getItemId(), rowSet.getString(item.getItemId()));
        }
        list.add(map);
    }
    return list;
}
Also used : SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) QueryItem(org.hisp.dhis.common.QueryItem) SqlHelper(org.hisp.dhis.commons.util.SqlHelper) DateUtils.getMediumDateString(org.hisp.dhis.system.util.DateUtils.getMediumDateString)

Aggregations

SqlRowSet (org.springframework.jdbc.support.rowset.SqlRowSet)26 DateUtils.getMediumDateString (org.hisp.dhis.system.util.DateUtils.getMediumDateString)9 ArrayList (java.util.ArrayList)6 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)4 HashMap (java.util.HashMap)3 List (java.util.List)3 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)3 HashSet (java.util.HashSet)2 DimensionalObject (org.hisp.dhis.common.DimensionalObject)2 IdSchemes (org.hisp.dhis.common.IdSchemes)2 MapMapMap (org.hisp.dhis.common.MapMapMap)2 QueryItem (org.hisp.dhis.common.QueryItem)2 SqlHelper (org.hisp.dhis.commons.util.SqlHelper)2 TextUtils.getCommaDelimitedString (org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString)2 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)2 ProgramIndicator (org.hisp.dhis.program.ProgramIndicator)2 ProgramType (org.hisp.dhis.program.ProgramType)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 IOException (java.io.IOException)1 Timestamp (java.sql.Timestamp)1