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

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

Introduction

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

Prototype

ResultSetExtractor

Source Link

Usage

From source file:org.LexGrid.lexevs.metabrowser.impl.MetaBrowserServiceImpl.java

/**
 * Builds the relationship tab results.//from  w  ww.java2  s.  c om
 * 
 * @param rs the rs
 * @param map the map
 * @param direction the direction
 * 
 * @return the map< string, list< relationship tab results>>
 * 
 * @throws SQLException the SQL exception
 */
@SuppressWarnings({ "unchecked" })
private Map<String, List<RelationshipTabResults>> buildRelationshipTabResults(String sql, String cui,
        final Map<String, List<RelationshipTabResults>> map, final Direction direction) throws SQLException {

    return (Map<String, List<RelationshipTabResults>>) this.getJdbcTemplate().query(sql, new String[] { cui },
            new ResultSetExtractor() {

                public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
                    while (rs.next()) {
                        RelationshipTabResults result = new RelationshipTabResults();

                        String rel = rs.getString(REL_COL);
                        if (direction.equals(Direction.TARGETOF)) {
                            rel = reverseRel(rel);
                        }

                        String targetConceptCode = null;
                        if (direction.equals(Direction.TARGETOF)) {
                            targetConceptCode = rs.getString("sourceEntityCode");
                        } else {
                            targetConceptCode = rs.getString("targetEntityCode");
                        }

                        String entityDescription = rs.getString(ENTITY_DESCRIPTION);
                        String sourceQualValue = rs.getString(SOURCE_QUAL_COL);

                        String relaQualValue = rs.getString(RELA_QUAL_COL);
                        if (direction.equals(Direction.TARGETOF)) {
                            relaQualValue = reverseRela(relaQualValue);
                        }

                        result.setCui(targetConceptCode);
                        result.setName(entityDescription);
                        result.setRel(rel);
                        result.setSource(sourceQualValue);
                        result.setRela(relaQualValue);

                        map.get(rel).add(result);
                    }
                    return map;
                }
            });
}

From source file:org.ohmage.query.impl.UserCampaignQueries.java

public Map<String, Set<Campaign.Role>> getCampaignAndRolesForUser(final String username)
        throws DataAccessException {

    try {//from w  w  w. j  a v a  2s . c o m
        return getJdbcTemplate().query(SQL_GET_CAMPAIGNS_AND_ROLES_FOR_USER, new Object[] { username },
                new ResultSetExtractor<Map<String, Set<Campaign.Role>>>() {
                    /**
                     * Extracts the data combining the roles into the same
                     * set. 
                     */
                    @Override
                    public Map<String, Set<Role>> extractData(final ResultSet rs)
                            throws SQLException, org.springframework.dao.DataAccessException {

                        Map<String, Set<Campaign.Role>> result = new HashMap<String, Set<Campaign.Role>>();

                        while (rs.next()) {
                            Set<Campaign.Role> roles = result.get(rs.getString("urn"));

                            if (roles == null) {
                                roles = new HashSet<Campaign.Role>();
                                result.put(rs.getString("urn"), roles);
                            }

                            try {
                                roles.add(Campaign.Role.getValue(rs.getString("role")));
                            } catch (IllegalArgumentException e) {
                                throw new SQLException("The role is not a valid role.", e);
                            }
                        }

                        return result;
                    }
                });
    } catch (org.springframework.dao.DataAccessException e) {
        throw new DataAccessException("Error executing SQL '" + SQL_GET_CAMPAIGNS_AND_ROLES_FOR_USER
                + "' with parameter: " + username, e);
    }
}

From source file:com.hygenics.parser.getDAOTemplate.java

public ResultSetExtractor<Map<String, String>> getExtractor(final String column, final String idcolumn,
        final boolean unique) {
    return (new ResultSetExtractor<Map<String, String>>() {

        @Override//from   www.ja  v a 2  s . c  om
        public Map<String, String> extractData(ResultSet rs) throws SQLException, DataAccessException {
            // TODO Auto-generated method stub
            Map<String, String> results = new HashMap<String, String>();

            while (rs.next()) {
                if (unique == false) {
                    results.put(rs.getString(column), rs.getString(idcolumn).replaceAll("\"", ""));
                } else {
                    String id = rs.getString(column);
                    id += "UQUQ" + uniqueid + "UQUQ";
                    results.put(id, rs.getString(idcolumn));
                    uniqueid++;
                }
            }
            rs.close();
            return results;
        }

    });
}

From source file:nz.geek.caffe.spring.hdb.HANAExceptionMappingTest.java

/**
 *//*from  ww  w.  j  a va 2  s .com*/
@Test
public void testInvalidResultSetColumnColumnStore() {
    this.jdbcTemplate.execute("INSERT INTO TEST_PARENT_COL VALUES (1, 'test')");

    try {
        this.jdbcTemplate.query(new PreparedStatementCreator() {

            public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                return con.prepareStatement("SELECT STR_ FROM TEST_PARENT_COL");
            }
        }, new ResultSetExtractor<String>() {

            public String extractData(final ResultSet rs) throws SQLException, DataAccessException {

                return rs.getString("STR__");
            }
        });
        Assert.fail("query should have failed");
    } catch (final InvalidResultSetAccessException e) {
        // expected
    }
}

From source file:ar.com.zauber.commons.gis.street.impl.SQLStreetsDAO.java

/** @see StreetsDAO#getStreets(String) */
public final List<Result> getStreets(final String text) {
    final List<Result> results = new ArrayList<Result>();

    final String q = "%" + executeFilters(escapeForLike(text, '+')) + "%";
    final List<Object> args = new ArrayList<Object>(4);
    args.add(q);/* w w w .  ja  va 2s. c o m*/
    template.query("select * from geocode_street(?)", args.toArray(), new ResultSetExtractor() {
        public Object extractData(final ResultSet rset) throws SQLException, DataAccessException {
            while (rset.next()) {
                try {
                    results.add(new StreetResult(rset.getString("nomoficial"),
                            (Point) wktReader.read(rset.getString("middle")), "Buenos Aires", "AR"));
                } catch (ParseException e) {
                    throw new DataRetrievalFailureException("parsing feature geom");
                }
            }
            return null;
        }
    });

    return results;
}

From source file:nz.geek.caffe.spring.hdb.HANAExceptionMappingTest.java

/**
 *///from w  ww .  j ava 2  s . c  o  m
@Test
public void testInvalidResultSetColumnColumnStore2() {
    this.jdbcTemplate.execute("INSERT INTO TEST_PARENT_COL VALUES (1, 'test')");

    try {
        this.jdbcTemplate.query(new PreparedStatementCreator() {

            public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                return con.prepareStatement("SELECT STR_ FROM TEST_PARENT_COL");
            }
        }, new ResultSetExtractor<String>() {

            public String extractData(final ResultSet rs) throws SQLException, DataAccessException {

                return rs.getString(7);
            }
        });
        Assert.fail("query should have failed");
    } catch (final InvalidResultSetAccessException e) {
        // expected
    }
}

From source file:gov.nih.nci.ncicb.cadsr.common.persistence.dao.jdbc.JDBCValueDomainDAO.java

public Map<String, ValueMeaning> getValueMeanings(Collection<String> vmIds) {
    if (vmIds == null || vmIds.size() < 1) {
        return new HashMap<String, ValueMeaning>();
    }//ww w  . j  a v a 2 s.c om
    String qry = "select * from VALUE_MEANINGS_VIEW where VM_IDSEQ in ( ";
    for (String vmId : vmIds) {
        qry += "'" + vmId + "',";
    }

    if (qry.lastIndexOf(",") == -1) {
        qry += "'')";
    } else {
        qry = qry.substring(0, qry.length() - 1) + ")";
    }

    JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSource());
    Map<String, ValueMeaning> vmMap = (Map<String, ValueMeaning>) jdbcTemplate.query(qry,
            new ResultSetExtractor() {
                public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
                    Map<String, ValueMeaning> vmMap = new HashMap<String, ValueMeaning>();
                    while (rs.next()) {
                        ValueMeaning vm = new ValueMeaningTransferObject();
                        vm.setPreferredName(rs.getString(1));
                        vm.setPreferredDefinition(rs.getString(2));
                        String vmIdSeq = rs.getString(11);
                        vm.setLongName(rs.getString(14));
                        vm.setDefinitions(getDefinitions(vmIdSeq));
                        vm.setDesignations(getDesignations(vmIdSeq, null));

                        vmMap.put(vmIdSeq, vm);
                    }
                    return vmMap;
                }
            });

    return vmMap;
}

From source file:com.hygenics.parser.getDAOTemplate.java

/**
 * Extractor for ArrayList//from www . j  ava  2 s .  c o  m
 * 
 * @param column
 * @return
 */
private ResultSetExtractor<ArrayList<String>> getArrayResultSetExtractor(final String column) {
    return (new ResultSetExtractor<ArrayList<String>>() {

        @Override
        public ArrayList<String> extractData(ResultSet rs) throws SQLException, DataAccessException {
            // TODO Auto-generated method stub
            ArrayList<String> results = new ArrayList<String>();

            while (rs.next()) {
                results.add(rs.getString(column));
            }

            rs.close();
            return results;
        }

    });
}

From source file:nz.geek.caffe.spring.hdb.HANAExceptionMappingTest.java

/**
 *//* ww  w .  ja  v a 2  s  . c o  m*/
@Test
public void testInvalidResultSetColumnRowStore() {
    this.jdbcTemplate.execute("INSERT INTO TEST_PARENT_ROW VALUES (1, 'test')");

    try {
        this.jdbcTemplate.query(new PreparedStatementCreator() {

            public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                return con.prepareStatement("SELECT STR_ FROM TEST_PARENT_ROW");
            }
        }, new ResultSetExtractor<String>() {

            public String extractData(final ResultSet rs) throws SQLException, DataAccessException {

                return rs.getString("STR__");
            }
        });
        Assert.fail("query should have failed");
    } catch (final InvalidResultSetAccessException e) {
        // expected
    }
}

From source file:com.hygenics.parser.getDAOTemplate.java

public ResultSetExtractor<ArrayList<String>> getJsonwithQuotesExtractor() {
    return (new ResultSetExtractor<ArrayList<String>>() {

        @Override/*from  w w  w  .j  a va2  s.c o  m*/
        public ArrayList<String> extractData(ResultSet rs) throws SQLException, DataAccessException {
            // TODO Auto-generated method stub
            int j = 0;
            ArrayList<String> results = new ArrayList<String>();
            JsonObject obj = null;
            int columns = 0;

            while (rs.next()) {
                if (columns == 0) {
                    columns = rs.getMetaData().getColumnCount();
                    if (columns == 0) {
                        return results;
                    }
                }

                obj = null;
                obj = new JsonObject();
                for (int i = 1; i <= columns; i++) {
                    obj.add(rs.getMetaData().getColumnName(i),
                            StringEscapeUtils.escapeJson(rs.getString(i).replaceAll("\t|\r|\n|\r\n|", "")));
                }
                if (obj != null) {
                    results.add(obj.toString());
                }

                j++;
            }

            if (rs.getFetchSize() <= j) {
                rs.close();
            }

            return results;
        }

    });
}