List of usage examples for org.springframework.jdbc.core ConnectionCallback ConnectionCallback
ConnectionCallback
From source file:org.opennms.ng.services.capsd.JdbcCapsdDbSyncer.java
/** * <p>syncManagementState</p> *///from w w w. j a v a 2 s . co m @Override public void syncManagementState() { m_jdbcTemplate.execute(new ConnectionCallback<Object>() { @Override public Object doInConnection(Connection con) throws SQLException, DataAccessException { syncManagementState(con); return null; } }); }
From source file:annis.dao.SpringAnnisDao.java
@Transactional(readOnly = true) @Override// w w w.j av a 2 s. co m public boolean find(final QueryData queryData, final OutputStream out) { prepareTransaction(queryData); Boolean finished = getJdbcTemplate().execute(new ConnectionCallback<Boolean>() { @Override public Boolean doInConnection(Connection con) throws SQLException, DataAccessException { try (Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);) { String sql = findSqlGenerator.toSql(queryData); PrintWriter w; try (ResultSet rs = stmt.executeQuery(sql)) { w = new PrintWriter(new OutputStreamWriter(out, "UTF-8")); ResultSetTypedIterator<Match> itMatches = new ResultSetTypedIterator<>(rs, findSqlGenerator); int i = 1; while (itMatches.hasNext()) { // write single match to output stream Match m = itMatches.next(); w.print(m.toString()); w.print("\n"); // flush after every 10th item if (i % 10 == 0) { w.flush(); } i++; } // end for each match } w.flush(); return true; } catch (UnsupportedEncodingException ex) { log.error("Your system is not able to handle UTF-8 but ANNIS really needs this charset", ex); } return false; } }); return finished; }
From source file:annis.dao.SpringAnnisDao.java
@Transactional(readOnly = true) @Override//from w ww.ja v a 2 s.co m public void matrix(final QueryData queryData, final boolean outputCsv, final OutputStream out) { prepareTransaction(queryData); getJdbcTemplate().execute(new ConnectionCallback<Boolean>() { @Override public Boolean doInConnection(Connection con) throws SQLException, DataAccessException { try (Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt.executeQuery(matrixSqlGenerator.toSql(queryData));) { AnnotatedMatchIterator itMatches = new AnnotatedMatchIterator(rs, matrixSqlGenerator.getSpanExtractor()); // write the header to the output stream PrintWriter w = new PrintWriter(new OutputStreamWriter(out, "UTF-8")); if (outputCsv) { SortedMap<Integer, SortedSet<String>> columnsByNodePos = CSVHelper .exportCSVHeader(itMatches, w); w.flush(); // go back to the beginning and print the actual data itMatches.reset(); CSVHelper.exportCSVData(itMatches, columnsByNodePos, w); } else { SortedMap<Integer, SortedSet<String>> columnsByNodePos = WekaHelper .exportArffHeader(itMatches, w); w.flush(); // go back to the beginning and print the actual data itMatches.reset(); WekaHelper.exportArffData(itMatches, columnsByNodePos, w); } w.flush(); } catch (UnsupportedEncodingException ex) { log.error("Your system is not able to handle UTF-8 but ANNIS really needs this charset", ex); } return true; } }); }
From source file:architecture.common.spring.jdbc.core.ExtendedJdbcTemplate.java
public Object executeScript(final boolean stopOnError, final Reader reader) { return execute(new ConnectionCallback<Object>() { public Object doInConnection(Connection connection) throws SQLException, DataAccessException { try { return runScript(connection, stopOnError, reader); } catch (IOException e) { return null; }//from w ww . j ava 2 s .c o m } }); }
From source file:org.opennms.ng.services.capsd.JdbcCapsdDbSyncer.java
/** * <p>syncSnmpPrimaryState</p> */// w w w . j a v a 2 s.c o m @Override public void syncSnmpPrimaryState() { m_jdbcTemplate.execute(new ConnectionCallback<Object>() { @Override public Object doInConnection(Connection con) throws SQLException, DataAccessException { syncSnmpPrimaryState(con); return null; } }); }
From source file:org.opennms.ng.services.capsd.JdbcCapsdDbSyncer.java
/** {@inheritDoc} */ @Override/* w ww. j a v a 2 s . c om*/ public boolean isInterfaceInDB(final InetAddress ifAddress) { return m_jdbcTemplate.execute(new ConnectionCallback<Boolean>() { @Override public Boolean doInConnection(Connection con) throws SQLException, DataAccessException { return isInterfaceInDB(con, ifAddress) ? Boolean.TRUE : Boolean.FALSE; } }).booleanValue(); }
From source file:org.apereo.portal.i18n.RDBMLocaleStore.java
@Override public Locale[] getUserLocales(final IPerson person) { return jdbcOperations.execute(new ConnectionCallback<Locale[]>() { @Override//from w w w .j a va 2 s. co m public Locale[] doInConnection(Connection con) throws SQLException, DataAccessException { final List<Locale> localeList = new ArrayList<Locale>(); final String query = "SELECT * FROM UP_USER_LOCALE WHERE USER_ID=? ORDER BY PRIORITY"; final PreparedStatement pstmt = con.prepareStatement(query); try { pstmt.clearParameters(); pstmt.setInt(1, person.getID()); logger.debug(query); final ResultSet rs = pstmt.executeQuery(); try { while (rs.next()) { final String localeString = rs.getString("LOCALE"); final Locale locale = LocaleManager.parseLocale(localeString); localeList.add(locale); } } finally { rs.close(); } } finally { pstmt.close(); } return localeList.toArray(new Locale[localeList.size()]); } }); }
From source file:org.apereo.portal.i18n.RDBMLocaleStore.java
@Override public void updateUserLocales(final IPerson person, final Locale[] locales) { this.transactionOperations.execute(new TransactionCallback<Object>() { @Override// w w w. j av a 2 s .co m public Object doInTransaction(TransactionStatus status) { return jdbcOperations.execute(new ConnectionCallback<Object>() { @Override public Object doInConnection(Connection con) throws SQLException, DataAccessException { // Delete the existing list of locales final String delete = "DELETE FROM UP_USER_LOCALE WHERE USER_ID=?"; PreparedStatement pstmt = con.prepareStatement(delete); try { pstmt.clearParameters(); pstmt.setInt(1, person.getID()); logger.debug(delete); pstmt.executeUpdate(); } finally { pstmt.close(); } // Insert the new list of locales final String insert = "INSERT INTO UP_USER_LOCALE VALUES (?, ?, ?)"; pstmt = con.prepareStatement(insert); try { for (int i = 0; i < locales.length; i++) { pstmt.clearParameters(); pstmt.setInt(1, person.getID()); pstmt.setString(2, locales[i].toString()); pstmt.setInt(3, i); logger.debug(insert); pstmt.executeUpdate(); } } finally { pstmt.close(); } return null; } }); } }); }
From source file:org.apereo.portal.RDBMUserIdentityStore.java
/** * Gets the PortalUser data store object for the specified user name. * * @param userName The user's name//from w w w.j av a 2s . c om * @return A PortalUser object or null if the user doesn't exist. * @throws Exception */ protected PortalUser getPortalUser(final String userName) throws Exception { return jdbcOperations.execute(new ConnectionCallback<PortalUser>() { @Override public PortalUser doInConnection(Connection con) throws SQLException, DataAccessException { PortalUser portalUser = null; PreparedStatement pstmt = null; try { String query = "SELECT USER_ID, USER_DFLT_USR_ID FROM UP_USER WHERE USER_NAME=?"; pstmt = con.prepareStatement(query); pstmt.setString(1, userName); ResultSet rs = null; try { if (log.isDebugEnabled()) log.debug("RDBMUserIdentityStore::getPortalUID(userName=" + userName + "): " + query); rs = pstmt.executeQuery(); if (rs.next()) { portalUser = new PortalUser(); portalUser.setUserId(rs.getInt("USER_ID")); portalUser.setUserName(userName); portalUser.setDefaultUserId(rs.getInt("USER_DFLT_USR_ID")); } } finally { try { rs.close(); } catch (Exception e) { } } } finally { try { pstmt.close(); } catch (Exception e) { } } return portalUser; } }); }
From source file:org.apereo.portal.RDBMUserIdentityStore.java
/** * Gets the TemplateUser data store object for the specified template user name. * * @param templateUserName The template user's name * @return A TemplateUser object or null if the user doesn't exist. * @throws Exception//from w w w .j a va 2 s .c o m */ protected TemplateUser getTemplateUser(final String templateUserName) throws Exception { return jdbcOperations.execute(new ConnectionCallback<TemplateUser>() { @Override public TemplateUser doInConnection(Connection con) throws SQLException, DataAccessException { TemplateUser templateUser = null; PreparedStatement pstmt = null; try { String query = "SELECT USER_ID, USER_DFLT_LAY_ID FROM UP_USER WHERE USER_NAME=?"; pstmt = con.prepareStatement(query); pstmt.setString(1, templateUserName); ResultSet rs = null; try { if (log.isDebugEnabled()) log.debug("RDBMUserIdentityStore::getTemplateUser(templateUserName=" + templateUserName + "): " + query); rs = pstmt.executeQuery(); if (rs.next()) { templateUser = new TemplateUser(); templateUser.setUserName(templateUserName); templateUser.setUserId(rs.getInt("USER_ID")); templateUser.setDefaultLayoutId(rs.getInt("USER_DFLT_LAY_ID")); } else { if (!templateUserName.equals(defaultTemplateUserName)) { try { templateUser = getTemplateUser(defaultTemplateUserName); } catch (Exception e) { throw new SQLException(e); } } } } finally { try { rs.close(); } catch (Exception e) { } } } finally { try { pstmt.close(); } catch (Exception e) { } } return templateUser; } }); }