Example usage for org.springframework.jdbc.core JdbcTemplate queryForObject

List of usage examples for org.springframework.jdbc.core JdbcTemplate queryForObject

Introduction

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

Prototype

@Override
    @Nullable
    public <T> T queryForObject(String sql, Class<T> requiredType) throws DataAccessException 

Source Link

Usage

From source file:org.lexevs.dao.database.ibatis.codingscheme.IbatisCodingSchemeDaoTest.java

@Test
@Transactional/*w w w  .j  av a 2 s.c  o m*/
public void testInsertCodingScheme() throws SQLException {
    CodingScheme cs = new CodingScheme();

    final Timestamp effectiveDate = new Timestamp(1l);
    final Timestamp expirationDate = new Timestamp(2l);

    cs.setCodingSchemeName("csName");
    cs.setCodingSchemeURI("uri");
    cs.setRepresentsVersion("1.2");
    cs.setFormalName("csFormalName");
    cs.setDefaultLanguage("lang");
    cs.setApproxNumConcepts(22l);

    EntityDescription ed = new EntityDescription();
    ed.setContent("cs Description");
    cs.setEntityDescription(ed);

    Text copyright = new Text();
    copyright.setContent("cs Copyright");
    cs.setCopyright(copyright);

    cs.setIsActive(false);

    cs.setOwner("cs owner");

    cs.setStatus("testing");

    cs.setEffectiveDate(effectiveDate);
    cs.setExpirationDate(expirationDate);

    EntryState es = new EntryState();
    es.setChangeType(ChangeType.REMOVE);
    es.setRelativeOrder(22l);
    cs.setEntryState(es);

    String id = ibatisCodingSchemeDao.insertCodingScheme(cs, null, true);

    JdbcTemplate template = new JdbcTemplate(this.getDataSource());
    final String[] keys = (String[]) template.queryForObject("Select * from CodingScheme", new RowMapper() {

        public Object mapRow(ResultSet rs, int arg1) throws SQLException {
            String csId = rs.getString(1);
            assertTrue(rs.getString(2).equals("csName"));
            assertTrue(rs.getString(3).equals("uri"));
            assertTrue(rs.getString(4).equals("1.2"));
            assertTrue(rs.getString(5).equals("csFormalName"));
            assertTrue(rs.getString(6).equals("lang"));
            assertTrue(rs.getLong(7) == 22l);
            assertTrue(rs.getString(8).equals("cs Description"));
            assertTrue(rs.getString(9).equals("cs Copyright"));
            assertEquals("0", rs.getString(10));
            assertTrue(rs.getString(11).equals("cs owner"));
            assertTrue(rs.getString(12).equals("testing"));
            assertTrue(rs.getTimestamp(13).equals(effectiveDate));
            assertTrue(rs.getTimestamp(14).equals(expirationDate));

            //TODO: Test with a Release GUID
            //assertTrue(rs.getString(15).equals("someReleaseGuid"));

            String csEntryState = rs.getString(16);

            String[] keys = new String[] { csEntryState, csId };
            return keys;
        }
    });

    assertEquals(id, keys[1]);

    template.queryForObject("Select * from EntryState", new RowMapper() {

        public Object mapRow(ResultSet rs, int arg1) throws SQLException {
            assertTrue(rs.getString(1) + " - " + keys[0], rs.getString(1).equals(keys[0]));
            assertTrue(rs.getString(2) + " - " + keys[1], rs.getString(2).equals(keys[1]));
            assertEquals(rs.getString(3), "codingScheme");
            assertEquals(rs.getString(4), ChangeType.REMOVE.toString());
            assertEquals(rs.getLong(5), 22l);

            //TODO: Test with a Revision GUID
            //TODO: Test with a Previous Revision GUID
            //TODO: Test with a Previous EntryState GUID

            return null;
        }
    });
}

From source file:org.lexevs.dao.database.ibatis.codingscheme.IbatisCodingSchemeDaoTest.java

/**
 * Test insert uri map./* ww  w.ja  va  2 s.c om*/
 * 
 * @throws SQLException the SQL exception
 */
@Test
public void testInsertURIMap() throws SQLException {
    JdbcTemplate template = new JdbcTemplate(this.getDataSource());

    CodingScheme codingScheme = new CodingScheme();
    codingScheme.setCodingSchemeName("name");
    codingScheme.setCodingSchemeURI("uri");
    codingScheme.setRepresentsVersion("version");
    final String csKey = ibatisCodingSchemeDao.insertCodingScheme(codingScheme, null, true);

    SupportedCodingScheme cs = new SupportedCodingScheme();
    cs.setContent("supported cs");
    cs.setIsImported(true);
    cs.setLocalId("cs");
    cs.setUri("uri://test");

    ibatisCodingSchemeDao.insertURIMap(csKey, cs);

    template.queryForObject("Select * from csSupportedAttrib", new RowMapper() {

        public Object mapRow(ResultSet rs, int arg1) throws SQLException {

            assertNotNull(rs.getString(1));
            assertEquals(rs.getString(2), csKey);
            assertEquals(rs.getString(3), SQLTableConstants.TBLCOLVAL_SUPPTAG_CODINGSCHEME);
            assertEquals(rs.getString(4), "cs");
            assertEquals(rs.getString(5), "uri://test");
            assertEquals(rs.getString(6), "supported cs");
            assertEquals(true, rs.getBoolean(10));

            return true;
        }
    });
}

From source file:org.lexevs.dao.database.ibatis.codingscheme.IbatisCodingSchemeDaoTest.java

@Test
public void testInsertOrUpdateURIMap() throws SQLException {
    JdbcTemplate template = new JdbcTemplate(this.getDataSource());

    CodingScheme codingScheme = new CodingScheme();
    codingScheme.setCodingSchemeName("name");
    codingScheme.setCodingSchemeURI("uri");
    codingScheme.setRepresentsVersion("version");
    final String csKey = ibatisCodingSchemeDao.insertCodingScheme(codingScheme, null, true);

    SupportedCodingScheme cs = new SupportedCodingScheme();
    cs.setContent("supported cs");
    cs.setIsImported(true);//w  ww.  j ava2s .c om
    cs.setLocalId("cs");
    cs.setUri("uri://test");

    ibatisCodingSchemeDao.insertOrUpdateURIMap(csKey, cs);

    template.queryForObject("Select * from csSupportedAttrib", new RowMapper() {

        public Object mapRow(ResultSet rs, int arg1) throws SQLException {

            assertNotNull(rs.getString(1));
            assertEquals(rs.getString(2), csKey);
            assertEquals(rs.getString(3), SQLTableConstants.TBLCOLVAL_SUPPTAG_CODINGSCHEME);
            assertEquals(rs.getString(4), "cs");
            assertEquals(rs.getString(5), "uri://test");
            assertEquals(rs.getString(6), "supported cs");
            assertEquals(rs.getBoolean(10), true);

            return true;
        }
    });
}

From source file:org.lexevs.dao.database.ibatis.codingscheme.IbatisCodingSchemeDaoTest.java

/**
 * Test insert coding scheme source.//from  w w w. j a v  a2  s.  c om
 * 
 * @throws SQLException the SQL exception
 */
@Test
@Transactional
public void testInsertCodingSchemeSource() throws SQLException {
    CodingScheme codingScheme = new CodingScheme();
    codingScheme.setCodingSchemeName("name");
    codingScheme.setCodingSchemeURI("uri");
    codingScheme.setRepresentsVersion("version");
    final String csKey = ibatisCodingSchemeDao.insertCodingScheme(codingScheme, null, true);

    Source source = new Source();
    source.setContent("a source");
    source.setSubRef("sub ref");
    source.setRole("source role");

    ibatisCodingSchemeDao.insertCodingSchemeSource(csKey, source);

    JdbcTemplate template = new JdbcTemplate(this.getDataSource());

    template.queryForObject("Select * from csMultiAttrib", new RowMapper() {

        public Object mapRow(ResultSet rs, int arg1) throws SQLException {

            assertNotNull(rs.getString(1));
            assertEquals(rs.getString(2), csKey);
            assertEquals(rs.getString(3), SQLTableConstants.TBLCOLVAL_SOURCE);
            assertEquals(rs.getString(4), "a source");
            assertEquals(rs.getString(5), "sub ref");
            assertEquals(rs.getString(6), "source role");

            return true;
        }
    });
}

From source file:org.lexevs.dao.database.ibatis.codingscheme.IbatisCodingSchemeDaoTest.java

@Test
public void testUpdateCodingSchemeSource() throws SQLException {
    JdbcTemplate template = new JdbcTemplate(this.getDataSource());
    template.execute(/*from   w  ww.java  2 s  . c  om*/
            "Insert into codingScheme (codingSchemeGuid, codingSchemeName, codingSchemeUri, representsVersion) "
                    + "values ('1', 'csname', 'csuri', 'csversion')");

    template.execute(
            "Insert into csmultiattrib " + "values ('1', '1', 'source', 'a source', 'subRef', 'role', null)");

    Source source = new Source();
    source.setContent("a source");
    source.setSubRef("updatedSubRef");
    source.setRole("updated role");

    ibatisCodingSchemeDao.insertOrUpdateCodingSchemeSource("1", source);

    assertEquals(1, template.queryForInt("select count(*) from csmultiattrib"));

    template.queryForObject("Select * from csmultiattrib", new RowMapper() {

        public Object mapRow(ResultSet rs, int arg1) throws SQLException {

            assertEquals(rs.getString(4), "a source");
            assertEquals(rs.getString(5), "updatedSubRef");
            assertEquals(rs.getString(6), "updated role");

            return true;
        }
    });
}

From source file:org.lexevs.dao.database.ibatis.codingscheme.IbatisCodingSchemeDaoTest.java

/**
 * Test insert coding scheme local name.
 * // w  w w .  j  a v a  2  s . co  m
 * @throws SQLException the SQL exception
 */
@Test
@Transactional
public void testInsertCodingSchemeLocalName() throws SQLException {
    CodingScheme codingScheme = new CodingScheme();
    codingScheme.setCodingSchemeName("name");
    codingScheme.setCodingSchemeURI("uri");
    codingScheme.setRepresentsVersion("version");
    final String csKey = ibatisCodingSchemeDao.insertCodingScheme(codingScheme, null, true);

    final String localName = "LocalName";

    ibatisCodingSchemeDao.insertCodingSchemeLocalName(csKey, localName);

    JdbcTemplate template = new JdbcTemplate(this.getDataSource());

    template.queryForObject("Select * from csMultiAttrib", new RowMapper() {

        public Object mapRow(ResultSet rs, int arg1) throws SQLException {

            assertNotNull(rs.getString(1));
            assertEquals(rs.getString(2), csKey);
            assertEquals(rs.getString(3), SQLTableConstants.TBLCOLVAL_LOCALNAME);
            assertEquals(rs.getString(4), localName);

            return true;
        }
    });
}

From source file:org.lexevs.dao.database.ibatis.codingscheme.IbatisCodingSchemeDaoTest.java

/**
 * Test distinct namespaces./*from   w  w w .  java 2  s  . c om*/
 */
@Test
@Transactional
public void testUpdateUriMap() {
    JdbcTemplate template = new JdbcTemplate(this.getDataSource());

    template.execute(
            "Insert into codingScheme (codingSchemeGuid, codingSchemeName, codingSchemeUri, representsVersion) "
                    + "values ('1', 'csname', 'csuri', 'csversion')");

    template.execute("Insert into cssupportedattrib "
            + "values ('1', '1', 'CodingScheme', 'id', 'uri', null, null, null, null, null, null, null, null, null, null, null)");

    SupportedCodingScheme scs = new SupportedCodingScheme();
    scs.setIsImported(true);
    scs.setLocalId("id");
    scs.setUri("changedUri");

    ibatisCodingSchemeDao.insertOrUpdateURIMap("1", scs);

    assertEquals(1, template.queryForInt("Select count(*) from cssupportedattrib"));

    template.queryForObject("Select * from cssupportedattrib", new RowMapper() {

        public Object mapRow(ResultSet rs, int arg1) throws SQLException {

            assertEquals("changedUri", rs.getString(5));
            assertTrue(rs.getBoolean(10));

            return true;
        }
    });
}

From source file:org.projectforge.business.meb.MebDao.java

/**
 * Called by MebCache to get the number of recent entries for the given users. For administrative users the number of
 * unassigned entries (without owner) is added.
 * /*from  www. j a v  a2  s.co  m*/
 * @param userId
 * @return Number of recent (and unassigned) MEB entries.
 */
int internalGetRecentMEBEntries(final Integer userId) {
    final JdbcTemplate jdbc = new JdbcTemplate(dataSource);
    try {
        int counter = jdbc.queryForObject(
                "SELECT COUNT(*) FROM t_meb_entry where owner_fk=" + userId + " and status='RECENT'",
                Integer.class);
        if (accessChecker.isLoggedInUserMemberOfAdminGroup() == true) {
            counter += jdbc.queryForObject("SELECT COUNT(*) FROM t_meb_entry where owner_fk is null",
                    Integer.class);
        }
        return counter;
    } catch (final Exception ex) {
        log.error(ex.getMessage(), ex);
        return 0;
    }
}

From source file:org.springframework.batch.item.database.IbatisPagingItemReaderAsyncTests.java

@Before
public void init() {
    Assert.notNull(dataSource);//from w w w .jav  a 2  s  .  c om
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    maxId = jdbcTemplate.queryForObject("SELECT MAX(ID) from T_FOOS", Integer.class);
    for (int i = ITEM_COUNT; i > maxId; i--) {
        jdbcTemplate.update("INSERT into T_FOOS (ID,NAME,VALUE) values (?, ?, ?)", i, "foo" + i, i);
    }
    assertEquals(ITEM_COUNT, JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_FOOS"));
}

From source file:org.springframework.batch.item.database.JdbcPagingItemReaderAsyncTests.java

@Before
public void init() {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    Integer tempMaxId = jdbcTemplate.queryForObject("SELECT MAX(ID) from T_FOOS", Integer.class);
    maxId = tempMaxId != null ? tempMaxId : 0;
    for (int i = ITEM_COUNT; i > maxId; i--) {
        jdbcTemplate.update("INSERT into T_FOOS (ID,NAME,VALUE) values (?, ?, ?)", i, "foo" + i, i);
    }//from w w  w.  j  av a  2s. c o  m
    assertEquals(ITEM_COUNT, JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_FOOS"));
}