Example usage for org.springframework.jdbc.core RowCallbackHandler RowCallbackHandler

List of usage examples for org.springframework.jdbc.core RowCallbackHandler RowCallbackHandler

Introduction

In this page you can find the example usage for org.springframework.jdbc.core RowCallbackHandler RowCallbackHandler.

Prototype

RowCallbackHandler

Source Link

Usage

From source file:com.px100systems.data.plugin.persistence.jdbc.Storage.java

protected Map<String, Long> loadMaxIds() {
    final Map<String, Long> result = new HashMap<String, Long>();
    connection.getJdbc().query("SELECT MAX(id), generator_name FROM " + table + " GROUP BY generator_name",
            new RowCallbackHandler() {
                @Override/*ww  w . ja v a2 s .c  om*/
                public void processRow(ResultSet rs) throws SQLException {
                    result.put(rs.getString("generator_name"), rs.getLong(1));
                }
            });
    return result;
}

From source file:io.lavagna.service.CardRepository.java

public Map<String, Integer> findCardsIds(List<String> cards) {

    List<Object[]> param = new ArrayList<>(cards.size());
    for (String card : cards) {
        String[] splitted = StringUtils.split(card, '-');
        if (splitted.length > 1) {
            try {
                Integer cardSequenceNumber = Integer.valueOf(splitted[splitted.length - 1], 10);
                String boardShortName = StringUtils.join(ArrayUtils.subarray(splitted, 0, splitted.length - 1),
                        '-');
                param.add(new Object[] { boardShortName, cardSequenceNumber });

            } catch (NumberFormatException nfe) {
                // skip
            }// w w  w . j av  a  2 s.  c  o  m
        }
    }

    if (param.isEmpty()) {
        return Collections.emptyMap();
    }

    final Map<String, Integer> res = new HashMap<>();
    MapSqlParameterSource paramS = new MapSqlParameterSource("projShortNameAndCardSeq", param);
    jdbc.query(queries.findCardsIs(), paramS, new RowCallbackHandler() {
        @Override
        public void processRow(ResultSet rs) throws SQLException {
            res.put(rs.getString("CARD_IDENTIFIER"), rs.getInt("CARD_ID"));
        }
    });

    return res;
}

From source file:com.cnd.greencube.server.dao.jdbc.JdbcDAO.java

@SuppressWarnings("rawtypes")
public List<?> queryForObject(String sql, Object[] params, final Class clazz) {
    final List result = new ArrayList();
    getJdbcTemplate().query(sql, params, new RowCallbackHandler() {
        @SuppressWarnings("unchecked")
        public void processRow(ResultSet rs) throws SQLException {
            Object obj = fetchRowObject(rs, clazz);
            if (obj != null) {
                result.add(obj);/*from w  w  w . j av a 2s. com*/
            }
        }
    });
    return result;
}

From source file:eionet.meta.dao.mysql.DataElementDAOImpl.java

/**
 * finds non-Common elements.//  w  w  w .  ja  v a2 s  . c  o  m
 *
 * @param filter
 *            search filter
 * @return list of data elements
 */
private List<DataElement> executeNonCommonElementQuery(final DataElementsFilter filter) {
    Map<String, Object> params = new HashMap<String, Object>();
    StringBuilder sql = new StringBuilder();

    sql.append("select de.DATAELEM_ID, de.IDENTIFIER, de.SHORT_NAME, ds.REG_STATUS, de.DATE, de.TYPE, ");
    sql.append(
            "t.SHORT_NAME as tableName, ds.IDENTIFIER as datasetName, ds.IDENTIFIER, ds.DATASET_ID, t.IDENTIFIER, ");
    sql.append("t.TABLE_ID, de.WORKING_USER, de.WORKING_COPY, a.VALUE AS NAME ");
    sql.append("from DATAELEM de ");
    sql.append("left join TBL2ELEM t2e on (de.DATAELEM_ID = t2e.DATAELEM_ID) ");
    sql.append("LEFT JOIN (ATTRIBUTE a, M_ATTRIBUTE ma) ");
    sql.append(
            "ON (de.DATAELEM_ID=a.DATAELEM_ID AND a.PARENT_TYPE='E' AND ma.M_ATTRIBUTE_ID=a.M_ATTRIBUTE_ID ");
    sql.append("and ma.SHORT_NAME='Name') ");
    sql.append("left join DS_TABLE t on (t2e.TABLE_ID = t.TABLE_ID) ");
    sql.append("left join DST2TBL d2t on (t.TABLE_ID = d2t.TABLE_ID) ");
    sql.append("left join DATASET ds on (d2t.DATASET_ID = ds.DATASET_ID) ");
    sql.append("where ");
    sql.append("de.PARENT_NS is not null ");
    sql.append("and ds.DELETED is null ");
    sql.append("and ds.WORKING_COPY = 'N' ");
    // Filter parameters
    if (StringUtils.isNotEmpty(filter.getDataSet())) {
        sql.append("and ds.IDENTIFIER = :dataSet ");
        params.put("dataSet", filter.getDataSet());
    }
    if (StringUtils.isNotEmpty(filter.getType())) {
        sql.append("and de.TYPE = :type ");
        params.put("type", filter.getType());
    }
    if (StringUtils.isNotEmpty(filter.getShortName())) {
        sql.append("and de.SHORT_NAME like :shortName ");
        params.put("shortName", "%" + filter.getShortName() + "%");
    }
    if (StringUtils.isNotEmpty(filter.getIdentifier())) {
        sql.append("and de.IDENTIFIER like :identifier ");
        params.put("identifier", "%" + filter.getIdentifier() + "%");
    }
    // attributes
    for (int i = 0; i < filter.getAttributes().size(); i++) {
        Attribute a = filter.getAttributes().get(i);
        String idKey = "attrId" + i;
        String valueKey = "attrValue" + i;
        if (StringUtils.isNotEmpty(a.getValue())) {
            sql.append("and ");
            sql.append("de.DATAELEM_ID in ( ");
            sql.append("select a.DATAELEM_ID from ATTRIBUTE a WHERE ");
            sql.append("a.M_ATTRIBUTE_ID = :" + idKey + " AND a.VALUE like :" + valueKey
                    + " AND a.PARENT_TYPE = :parentType ");
            sql.append(") ");
        }
        params.put(idKey, a.getId());
        String value = "%" + a.getValue() + "%";
        params.put(valueKey, value);
        params.put("parentType", DElemAttribute.ParentType.ELEMENT.toString());
    }

    sql.append(
            "order by ds.IDENTIFIER asc, ds.DATASET_ID desc, t.IDENTIFIER asc, t.TABLE_ID desc, de.IDENTIFIER asc, ")
            .append("de.DATAELEM_ID desc");

    // LOGGER.debug("SQL: " + sql.toString());

    final List<DataElement> dataElements = new ArrayList<DataElement>();

    getNamedParameterJdbcTemplate().query(sql.toString(), params, new RowCallbackHandler() {
        DataElement de;
        String curDstIdf;
        String curDstID;

        @Override
        public void processRow(ResultSet rs) throws SQLException {

            String dstID = rs.getString("ds.DATASET_ID");
            String dstIdf = rs.getString("ds.IDENTIFIER");
            if (dstID == null || dstIdf == null) {
                return;
            }

            String tblID = rs.getString("t.TABLE_ID");
            String tblIdf = rs.getString("t.IDENTIFIER");
            // skip non-existing tables, ie trash from some erroneous situation
            if (tblID == null || tblIdf == null) {
                return;
            }

            // int elmID = rs.getInt("de.DATAELEM_ID");
            String elmIdf = rs.getString("de.IDENTIFIER");
            // skip non-existing elements, ie trash from some erroneous situation
            if (elmIdf == null) {
                return;
            }

            // the following if block skips elements from non-latest DATASETS
            if (curDstIdf == null || !curDstIdf.equals(dstIdf)) {
                curDstID = dstID;
                curDstIdf = dstIdf;
            } else if (!filter.isIncludeHistoricVersions()) {
                if (!curDstID.equals(dstID)) {
                    return;
                }
            }

            de = new DataElement();
            de.setId(rs.getInt("de.DATAELEM_ID"));
            de.setShortName(rs.getString("de.SHORT_NAME"));
            de.setStatus(rs.getString("ds.REG_STATUS"));
            de.setType(rs.getString("de.TYPE"));
            de.setModified(new Date(rs.getLong("de.DATE")));
            de.setTableName(rs.getString("tableName"));
            de.setDataSetName(rs.getString("datasetName"));
            de.setWorkingUser(rs.getString("de.WORKING_USER"));

            de.setIdentifier(rs.getString("de.IDENTIFIER"));
            de.setName(rs.getString("NAME"));
            dataElements.add(de);
        }
    });

    return dataElements;
}

From source file:com.cnd.greencube.server.dao.jdbc.JdbcDAO.java

@SuppressWarnings("rawtypes")
public List<?> queryForObject(String sql, final Class clazz) {
    final List result = new ArrayList();
    getJdbcTemplate().query(sql, new RowCallbackHandler() {
        @SuppressWarnings("unchecked")
        public void processRow(ResultSet rs) throws SQLException {
            Object obj = fetchRowObject(rs, clazz);
            if (obj != null) {
                result.add(obj);//from ww  w. jav  a 2  s . com
            }
        }
    });
    return result;
}

From source file:org.geowebcache.diskquota.jdbc.JDBCQuotaStore.java

public void accept(final TileSetVisitor visitor) {
    String getTileSet = dialect.getTileSetsQuery(schema);
    final TileSetRowMapper tileSetMapper = new TileSetRowMapper();
    jt.getJdbcOperations().query(getTileSet, new RowCallbackHandler() {

        public void processRow(ResultSet rs) throws SQLException {
            TileSet tileSet = tileSetMapper.mapRow(rs, 0);
            if (!GLOBAL_QUOTA_NAME.equals(tileSet.getId())) {
                visitor.visit(tileSet, JDBCQuotaStore.this);
            }//from ww  w  .j  av  a  2  s.c o m
        }
    });
}

From source file:io.lavagna.service.CardDataRepository.java

public void outputFileContent(String digest, final OutputStream out) throws IOException {
    LOG.debug("get file digest : {} ", digest);
    SqlParameterSource param = new MapSqlParameterSource("digest", digest);

    jdbc.query(queries.fileContent(), param, new RowCallbackHandler() {
        @Override/*from  w w  w  . ja va2 s  .co  m*/
        public void processRow(ResultSet rs) throws SQLException {
            try (InputStream is = rs.getBinaryStream("CONTENT")) {
                StreamUtils.copy(is, out);
            } catch (IOException e) {
                throw new IllegalStateException("Error while copying data", e);
            }
        }
    });
}

From source file:io.lavagna.service.CardLabelRepository.java

/**
 * Return a mapping//from   ww w. java2  s  . c om
 *
 * {labelListValue : {labelId : labelListValueId}}
 *
 * @param labelListValues
 * @return
 */
public Map<String, Map<Integer, Integer>> findLabelListValueMapping(List<String> labelListValues) {

    if (labelListValues.isEmpty()) {
        return Collections.emptyMap();
    }

    final Map<String, Map<Integer, Integer>> res = new HashMap<>();
    jdbc.query(queries.findLabelListValueMapping(), new MapSqlParameterSource("values", labelListValues),
            new RowCallbackHandler() {
                @Override
                public void processRow(ResultSet rs) throws SQLException {
                    String name = rs.getString("CARD_LABEL_LIST_VALUE");
                    if (!res.containsKey(name)) {
                        res.put(name, new HashMap<Integer, Integer>());
                    }
                    res.get(name).put(rs.getInt("CARD_LABEL_ID_FK"), rs.getInt("CARD_LABEL_LIST_VALUE_ID"));
                }
            });
    return res;
}

From source file:com.cnd.greencube.server.dao.jdbc.JdbcDAO.java

@SuppressWarnings("rawtypes")
public List<?> queryForObject(String sql, String ctql, final Class clazz, PageInfo pageInfo) throws Exception {
    if (pageInfo == null)
        queryForObject(sql, clazz);//ww w .  j  a v  a2 s .c  o  m

    final List result = new ArrayList();

    int num = getCount(ctql);
    pageInfo.setRowCount(num);
    String pageLimit = null;
    if (num < 15) {
        pageLimit = "";
    } else {
        pageLimit = " limit " + (pageInfo.getCurrentPageIndex() - 1) * pageInfo.getPageSize() + ","
                + pageInfo.getPageSize();
    }
    sql = sql + pageLimit;

    getJdbcTemplate().query(sql, new RowCallbackHandler() {
        @SuppressWarnings("unchecked")
        public void processRow(ResultSet rs) throws SQLException {
            Object obj = fetchRowObject(rs, clazz);
            if (obj != null) {
                result.add(obj);
            }
        }
    });
    return result;
}