Search in sources :

Example 21 with SqlRowSet

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

the class IdentityPopulator method execute.

// -------------------------------------------------------------------------
// Execute
// -------------------------------------------------------------------------
@Override
public void execute() throws Exception {
    for (String table : tables) {
        try {
            log.debug("Checking table: " + table);
            int count = 0;
            SqlRowSet resultSet = jdbcTemplate.queryForRowSet("SELECT * from " + table + " WHERE uid IS NULL");
            while (resultSet.next()) {
                ++count;
                String idColumn = getIdColumn(table);
                int id = resultSet.getInt(idColumn);
                String sql = "update " + table + " set uid = '" + CodeGenerator.generateUid() + "' where " + idColumn + " = " + id;
                jdbcTemplate.update(sql);
            }
            if (count > 0) {
                log.info(count + " uids set on " + table);
            }
            count = 0;
            resultSet = jdbcTemplate.queryForRowSet("SELECT * from " + table + " WHERE lastUpdated IS NULL");
            String timestamp = DateUtils.getLongDateString();
            while (resultSet.next()) {
                ++count;
                String idColumn = getIdColumn(table);
                int id = resultSet.getInt(idColumn);
                String sql = "update " + table + " set lastupdated = '" + timestamp + "' where " + idColumn + " = " + id;
                jdbcTemplate.update(sql);
            }
            if (count > 0) {
                log.info(count + " lastupdated set on " + table);
            }
            count = 0;
            resultSet = jdbcTemplate.queryForRowSet("SELECT * from " + table + " WHERE created IS NULL");
            while (resultSet.next()) {
                ++count;
                String idColumn = getIdColumn(table);
                int id = resultSet.getInt(idColumn);
                String sql = "update " + table + " set created = '" + timestamp + "' where " + idColumn + " = " + id;
                jdbcTemplate.update(sql);
            }
            if (count > 0) {
                log.info(count + " created timestamps set on " + table);
            }
        } catch (// Log and continue
        Exception ex) {
            log.error("Problem updating: " + table + ", id column: " + getIdColumn(table), ex);
        }
    }
    log.debug("Identifiable properties updated");
    log.debug("Organisation unit uuids updated");
    updatePasswordLastUpdated();
    log.debug("UserCredential passwordLastUpdated updated");
}
Also used : SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet)

Example 22 with SqlRowSet

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

the class HibernateDataValueStore method getDataElementOperandValues.

@Override
public MapMapMap<Period, String, DimensionalItemObject, Double> getDataElementOperandValues(Collection<DataElementOperand> dataElementOperands, Collection<Period> periods, OrganisationUnit orgUnit) {
    MapMapMap<Period, String, DimensionalItemObject, Double> result = new MapMapMap<>();
    Collection<Integer> periodIdList = IdentifiableObjectUtils.getIdentifiers(periods);
    SetMap<DataElement, DataElementOperand> deosByDataElement = getDeosByDataElement(dataElementOperands);
    if (periods.size() == 0 || dataElementOperands.size() == 0) {
        return result;
    }
    String sql = "select dv.dataelementid, coc.uid, dv.attributeoptioncomboid, dv.periodid, " + "sum( cast( dv.value as " + statementBuilder.getDoubleColumnType() + " ) ) as value " + "from datavalue dv " + "join organisationunit o on o.organisationunitid = dv.sourceid " + "join categoryoptioncombo coc on coc.categoryoptioncomboid = dv.categoryoptioncomboid " + "where o.path like '" + orgUnit.getPath() + "%' " + "and dv.periodid in (" + TextUtils.getCommaDelimitedString(periodIdList) + ") " + "and dv.value is not null " + "and dv.deleted is false " + "and ( ";
    String snippit = "";
    for (DataElement dataElement : deosByDataElement.keySet()) {
        sql += snippit + "( dv.dataelementid = " + dataElement.getId() + getDisaggRestriction(deosByDataElement.get(dataElement)) + " ) ";
        snippit = "or ";
    }
    sql += ") group by dv.dataelementid, coc.uid, dv.attributeoptioncomboid, dv.periodid";
    SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql);
    Map<Integer, DataElement> dataElementsById = IdentifiableObjectUtils.getIdentifierMap(deosByDataElement.keySet());
    Map<Integer, Period> periodsById = IdentifiableObjectUtils.getIdentifierMap(periods);
    while (rowSet.next()) {
        Integer dataElementId = rowSet.getInt(1);
        String categoryOptionComboUid = rowSet.getString(2);
        Integer periodId = rowSet.getInt(4);
        Double value = rowSet.getDouble(5);
        DataElement dataElement = dataElementsById.get(dataElementId);
        Period period = periodsById.get(periodId);
        Set<DataElementOperand> deos = deosByDataElement.get(dataElement);
        for (DataElementOperand deo : deos) {
            if (deo.getCategoryOptionCombo() == null || deo.getCategoryOptionCombo().getUid() == categoryOptionComboUid) {
                Double existingValue = result.getValue(period, categoryOptionComboUid, deo);
                if (existingValue != null) {
                    value += existingValue;
                }
                result.putEntry(period, categoryOptionComboUid, deo, value);
            }
        }
    }
    return result;
}
Also used : SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) Period(org.hisp.dhis.period.Period) MapMapMap(org.hisp.dhis.common.MapMapMap) DataElement(org.hisp.dhis.dataelement.DataElement) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject)

Example 23 with SqlRowSet

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

the class HibernateDataValueStore method getDataValueMapByAttributeCombo.

@Override
public MapMap<String, DimensionalItemObject, Double> getDataValueMapByAttributeCombo(SetMap<String, DataElementOperand> dataElementOperandsToGet, Date date, OrganisationUnit source, Collection<PeriodType> periodTypes, DataElementCategoryOptionCombo attributeCombo, Set<CategoryOptionGroup> cogDimensionConstraints, Set<DataElementCategoryOption> coDimensionConstraints, MapMap<String, DataElementOperand, Date> lastUpdatedMap) {
    MapMap<String, DimensionalItemObject, Double> map = new MapMap<>();
    if (dataElementOperandsToGet.isEmpty() || periodTypes.isEmpty() || (cogDimensionConstraints != null && cogDimensionConstraints.isEmpty()) || (coDimensionConstraints != null && coDimensionConstraints.isEmpty())) {
        return map;
    }
    String joinCo = coDimensionConstraints == null && cogDimensionConstraints == null ? StringUtils.EMPTY : "join categoryoptioncombos_categoryoptions c_c on dv.attributeoptioncomboid = c_c.categoryoptioncomboid ";
    String joinCog = cogDimensionConstraints == null ? StringUtils.EMPTY : "join categoryoptiongroupmembers cogm on c_c.categoryoptionid = cogm.categoryoptionid ";
    String whereCo = coDimensionConstraints == null ? StringUtils.EMPTY : "and c_c.categoryoptionid in (" + TextUtils.getCommaDelimitedString(getIdentifiers(coDimensionConstraints)) + ") ";
    String whereCog = cogDimensionConstraints == null ? StringUtils.EMPTY : "and cogm.categoryoptiongroupid in (" + TextUtils.getCommaDelimitedString(getIdentifiers(cogDimensionConstraints)) + ") ";
    String whereCombo = attributeCombo == null ? StringUtils.EMPTY : "and dv.attributeoptioncomboid = " + attributeCombo.getId() + " ";
    String sql = "select de.uid, coc.uid, aoc.uid, dv.value, dv.lastupdated, p.startdate, p.enddate " + "from datavalue dv " + "inner join dataelement de on dv.dataelementid = de.dataelementid " + "inner join categoryoptioncombo coc on dv.categoryoptioncomboid = coc.categoryoptioncomboid " + "inner join categoryoptioncombo aoc on dv.attributeoptioncomboid = aoc.categoryoptioncomboid " + "inner join period p on p.periodid = dv.periodid " + joinCo + joinCog + "where de.uid in (" + TextUtils.getQuotedCommaDelimitedString(dataElementOperandsToGet.keySet()) + ") " + "and dv.sourceid = " + source.getId() + " " + "and p.startdate <= '" + DateUtils.getMediumDateString(date) + "' " + "and p.enddate >= '" + DateUtils.getMediumDateString(date) + "' " + "and p.periodtypeid in (" + TextUtils.getCommaDelimitedString(getIds(periodTypes)) + ") " + "and dv.deleted is false " + whereCo + whereCog + whereCombo;
    SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql);
    MapMap<String, DataElementOperand, Long> checkForDuplicates = new MapMap<>();
    int rowCount = 0;
    while (rowSet.next()) {
        rowCount++;
        String dataElement = rowSet.getString(1);
        String categoryOptionCombo = rowSet.getString(2);
        String attributeOptionCombo = rowSet.getString(3);
        Double value = MathUtils.parseDouble(rowSet.getString(4));
        Date lastUpdated = rowSet.getDate(5);
        Date periodStartDate = rowSet.getDate(6);
        Date periodEndDate = rowSet.getDate(7);
        long periodInterval = periodEndDate.getTime() - periodStartDate.getTime();
        if (value != null) {
            Set<DataElementOperand> deos = dataElementOperandsToGet.get(dataElement);
            for (DataElementOperand deo : deos) {
                if (deo.getCategoryOptionCombo() == null || deo.getCategoryOptionCombo().getUid().equals(categoryOptionCombo)) {
                    Double existingValue = map.getValue(attributeOptionCombo, deo);
                    Long existingPeriodInterval = checkForDuplicates.getValue(attributeOptionCombo, deo);
                    if (existingPeriodInterval != null) {
                        if (existingPeriodInterval < periodInterval) {
                            // Do not overwrite the previous value if for a shorter interval
                            continue;
                        } else if (existingPeriodInterval > periodInterval) {
                            // Overwrite previous value if for a longer interval
                            existingValue = null;
                            if (lastUpdatedMap != null) {
                                lastUpdatedMap.putEntry(attributeOptionCombo, deo, lastUpdated);
                            }
                        }
                    }
                    if (existingValue != null) {
                        value += existingValue;
                    }
                    map.putEntry(attributeOptionCombo, deo, value);
                    if (lastUpdatedMap != null && lastUpdated != null) {
                        Date existingLastUpdated = lastUpdatedMap.getValue(attributeOptionCombo, deo);
                        if (existingLastUpdated == null || lastUpdated.after(existingLastUpdated)) {
                            lastUpdatedMap.putEntry(attributeOptionCombo, deo, lastUpdated);
                        }
                    }
                    checkForDuplicates.putEntry(attributeOptionCombo, deo, periodInterval);
                }
            }
        }
    }
    log.trace("getDataValueMapByAttributeCombo: " + rowCount + " rows into " + map.size() + " map entries from \"" + sql + "\"");
    return map;
}
Also used : SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) Date(java.util.Date) MapMapMap(org.hisp.dhis.common.MapMapMap) MapMap(org.hisp.dhis.common.MapMap) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject)

Example 24 with SqlRowSet

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

the class HibernateSqlViewStore method populateSqlViewGrid.

@Override
public void populateSqlViewGrid(Grid grid, String sql) {
    SqlRowSet rs = jdbcTemplate.queryForRowSet(sql);
    log.info("Get view SQL: " + sql);
    grid.addHeaders(rs);
    grid.addRows(rs);
}
Also used : SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet)

Example 25 with SqlRowSet

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

the class AnalyticalObjectEmbeddedDimensionUpgrader method upgradeGrupSetDimensions.

@SuppressWarnings("unchecked")
private void upgradeGrupSetDimensions(String favorite, String dimension, String item, Class<? extends AnalyticalObject> favoriteClazz, Class<? extends DimensionalObject> dimensionClass, Class<? extends DimensionalItemObject> itemClass, BiConsumer<BaseDimensionalEmbeddedObject, AnalyticalObject> consumer) {
    String groupSetSqlPattern = "select distinct d.{favorite}id, gsm.{dimension}id " + "from {favorite}_{item}s d " + "inner join {dimension}members gsm on d.{item}id=gsm.{item}id";
    String groupSetSql = TextUtils.replace(groupSetSqlPattern, "{favorite}", favorite, "{dimension}", dimension, "{item}", item);
    log.debug(String.format("Group set SQL: %s", groupSetSql));
    String groupSqlPattern = "select d.{item}id " + "from {favorite}_{item}s d " + "inner join {dimension}members gsm on d.{item}id=gsm.{item}id " + "where d.{favorite}id={favoriteId} " + "and gsm.{dimension}id={dimensionId} " + "order by d.sort_order";
    SqlRowSet groupSetRs = jdbcTemplate.queryForRowSet(groupSetSql);
    while (groupSetRs.next()) {
        int favoriteId = groupSetRs.getInt(1);
        int dimensionId = groupSetRs.getInt(2);
        AnalyticalObject analyticalObject = idObjectManager.get(favoriteClazz, favoriteId);
        DimensionalObject groupSet = idObjectManager.get(dimensionClass, dimensionId);
        Assert.notNull(analyticalObject, String.format("Analytical object not found: %s, class: %s", favoriteId, favoriteClazz));
        Assert.notNull(groupSet, String.format("Group set not found: %s, class: %s", dimensionId, dimensionClass));
        String groupSql = TextUtils.replace(groupSqlPattern, "{favorite}", favorite, "{dimension}", dimension, "{item}", item, "{favoriteId}", String.valueOf(favoriteId), "{dimensionId}", String.valueOf(dimensionId));
        log.debug(String.format("Group SQL: %s", groupSql));
        SqlRowSet groupRs = jdbcTemplate.queryForRowSet(groupSql);
        List<Integer> groupIds = new ArrayList<>();
        while (groupRs.next()) {
            groupIds.add(groupRs.getInt(1));
        }
        List<DimensionalItemObject> groups = (List<DimensionalItemObject>) idObjectManager.getById(itemClass, groupIds);
        Assert.notNull(groups, "Groups cannot be null");
        BaseDimensionalEmbeddedObject embeddedDimension = new BaseDimensionalEmbeddedObject(groupSet, groups);
        consumer.accept(embeddedDimension, analyticalObject);
        idObjectManager.update(analyticalObject);
        log.info(String.format("Added %s group set dimension: %s with groups: %d for favorite: %s", favorite, groupSet.getUid(), groups.size(), analyticalObject.getUid()));
    }
    String dropSql = TextUtils.replace("drop table {favorite}_{item}s", "{favorite}", favorite, "{item}", item);
    jdbcTemplate.update(dropSql);
    log.info(String.format("Dropped table, update done for favorite: %s and dimension: %s", favorite, dimension));
}
Also used : SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) DimensionalObject(org.hisp.dhis.common.DimensionalObject)

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