BATCH-1891: Migrate usage of deprecated classes

This commit is contained in:
Chris Schaefer
2012-09-16 23:30:12 -04:00
committed by Dave Syer
parent b1246f99b1
commit d0e0334674
58 changed files with 282 additions and 253 deletions

View File

@@ -29,16 +29,15 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcOperations;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.util.Assert;
/**
* <p>{@link ItemWriter} that uses the batching features from
* {@link SimpleJdbcTemplate} to execute a batch of statements for all items
* {@link NamedParameterJdbcTemplate} to execute a batch of statements for all items
* provided.</p>
*
* The user must provide an SQL query and a special callback in the for of either
@@ -62,7 +61,7 @@ public class JdbcBatchItemWriter<T> implements ItemWriter<T>, InitializingBean {
protected static final Log logger = LogFactory.getLog(JdbcBatchItemWriter.class);
private SimpleJdbcOperations simpleJdbcTemplate;
private NamedParameterJdbcOperations namedParameterJdbcTemplate;
private ItemPreparedStatementSetter<T> itemPreparedStatementSetter;
@@ -119,17 +118,17 @@ public class JdbcBatchItemWriter<T> implements ItemWriter<T>, InitializingBean {
* @param dataSource
*/
public void setDataSource(DataSource dataSource) {
if (simpleJdbcTemplate == null) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
if (namedParameterJdbcTemplate == null) {
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
}
/**
* Public setter for the {@link JdbcOperations}.
* @param simpleJdbcTemplate the {@link JdbcOperations} to set
* Public setter for the {@link NamedParameterJdbcOperations}.
* @param namedParameterJdbcTemplate the {@link NamedParameterJdbcOperations} to set
*/
public void setSimpleJdbcTemplate(SimpleJdbcOperations simpleJdbcTemplate) {
this.simpleJdbcTemplate = simpleJdbcTemplate;
public void setJdbcTemplate(NamedParameterJdbcOperations namedParameterJdbcTemplate) {
this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
}
/**
@@ -137,7 +136,7 @@ public class JdbcBatchItemWriter<T> implements ItemWriter<T>, InitializingBean {
* parameter source.
*/
public void afterPropertiesSet() {
Assert.notNull(simpleJdbcTemplate, "A DataSource or a SimpleJdbcTemplate is required.");
Assert.notNull(namedParameterJdbcTemplate, "A DataSource or a NamedParameterJdbcTemplate is required.");
Assert.notNull(sql, "An SQL statement is required.");
List<String> namedParameters = new ArrayList<String>();
parameterCount = JdbcParameterUtils.countParameterPlaceholders(sql, namedParameters);
@@ -174,10 +173,10 @@ public class JdbcBatchItemWriter<T> implements ItemWriter<T>, InitializingBean {
for (T item : items) {
batchArgs[i++] = itemSqlParameterSourceProvider.createSqlParameterSource(item);
}
updateCounts = simpleJdbcTemplate.batchUpdate(sql, batchArgs);
updateCounts = namedParameterJdbcTemplate.batchUpdate(sql, batchArgs);
}
else {
updateCounts = (int[]) simpleJdbcTemplate.getJdbcOperations().execute(sql, new PreparedStatementCallback() {
updateCounts = (int[]) namedParameterJdbcTemplate.getJdbcOperations().execute(sql, new PreparedStatementCallback() {
public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
for (T item : items) {
itemPreparedStatementSetter.setValues(item, ps);
@@ -197,9 +196,6 @@ public class JdbcBatchItemWriter<T> implements ItemWriter<T>, InitializingBean {
}
}
}
}
}
}

View File

@@ -34,7 +34,7 @@ import org.springframework.batch.item.ItemStreamException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
@@ -73,10 +73,6 @@ import org.springframework.util.ClassUtils;
* @since 2.0
*/
public class JdbcPagingItemReader<T> extends AbstractPagingItemReader<T> implements InitializingBean {
/**
*
*/
private static final String START_AFTER_VALUE = "start.after";
public static final int VALUE_NOT_SET = -1;
@@ -87,7 +83,7 @@ public class JdbcPagingItemReader<T> extends AbstractPagingItemReader<T> impleme
private Map<String, Object> parameterValues;
private SimpleJdbcTemplate simpleJdbcTemplate;
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
private RowMapper rowMapper;
@@ -169,7 +165,7 @@ public class JdbcPagingItemReader<T> extends AbstractPagingItemReader<T> impleme
jdbcTemplate.setFetchSize(fetchSize);
}
jdbcTemplate.setMaxRows(getPageSize());
this.simpleJdbcTemplate = new SimpleJdbcTemplate(jdbcTemplate);
namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
Assert.notNull(queryProvider);
queryProvider.init(dataSource);
this.firstPageSql = queryProvider.generateFirstPageQuery(getPageSize());
@@ -196,16 +192,16 @@ public class JdbcPagingItemReader<T> extends AbstractPagingItemReader<T> impleme
}
if (parameterValues != null && parameterValues.size() > 0) {
if (this.queryProvider.isUsingNamedParameters()) {
query = simpleJdbcTemplate.getNamedParameterJdbcOperations().query(firstPageSql,
query = namedParameterJdbcTemplate.query(firstPageSql,
getParameterMap(parameterValues, null), rowCallback);
}
else {
query = simpleJdbcTemplate.getJdbcOperations().query(firstPageSql,
query = getJdbcTemplate().query(firstPageSql,
getParameterList(parameterValues, null).toArray(), rowCallback);
}
}
else {
query = simpleJdbcTemplate.getJdbcOperations().query(firstPageSql, rowCallback);
query = getJdbcTemplate().query(firstPageSql, rowCallback);
}
}
@@ -214,11 +210,11 @@ public class JdbcPagingItemReader<T> extends AbstractPagingItemReader<T> impleme
logger.debug("SQL used for reading remaining pages: [" + remainingPagesSql + "]");
}
if (this.queryProvider.isUsingNamedParameters()) {
query = simpleJdbcTemplate.getNamedParameterJdbcOperations().query(remainingPagesSql,
query = namedParameterJdbcTemplate.query(remainingPagesSql,
getParameterMap(parameterValues, startAfterValue), rowCallback);
}
else {
query = simpleJdbcTemplate.getJdbcOperations().query(remainingPagesSql,
query = getJdbcTemplate().query(remainingPagesSql,
getParameterList(parameterValues, startAfterValue).toArray(), rowCallback);
}
}
@@ -266,11 +262,11 @@ public class JdbcPagingItemReader<T> extends AbstractPagingItemReader<T> impleme
}
};
if (this.queryProvider.isUsingNamedParameters()) {
startAfterValue = simpleJdbcTemplate.getNamedParameterJdbcOperations().queryForObject(jumpToItemSql,
startAfterValue = namedParameterJdbcTemplate.queryForObject(jumpToItemSql,
getParameterMap(parameterValues, startAfterValue), startMapper);
}
else {
startAfterValue = simpleJdbcTemplate.getJdbcOperations().queryForObject(jumpToItemSql,
startAfterValue = getJdbcTemplate().queryForObject(jumpToItemSql,
getParameterList(parameterValues, startAfterValue).toArray(), startMapper);
}
@@ -314,4 +310,7 @@ public class JdbcPagingItemReader<T> extends AbstractPagingItemReader<T> impleme
}
}
private JdbcTemplate getJdbcTemplate() {
return (JdbcTemplate) namedParameterJdbcTemplate.getJdbcOperations();
}
}

View File

@@ -15,24 +15,44 @@
*/
package org.springframework.batch.item.file.transform;
import org.springframework.core.enums.StringCodedLabeledEnum;
import org.springframework.util.Assert;
/**
* @author Dave Syer
*
*/
public class Alignment extends StringCodedLabeledEnum {
public static final Alignment CENTER = new Alignment("CENTER", "center");
public static final Alignment RIGHT = new Alignment("RIGHT", "right");
public static final Alignment LEFT = new Alignment("LEFT", "left");
public enum Alignment {
CENTER("CENTER", "center"),
RIGHT("RIGHT", "right"),
LEFT("LEFT", "left");
private String code;
private String label;
/**
* @param code
* @param label
*/
public Alignment(String code, String label) {
super(code, label);
private Alignment(String code, String label) {
Assert.notNull(code, "'code' must not be null");
this.code = code;
this.label = label;
}
public Comparable getCode() {
return code;
}
public String getStringCode() {
return (String) getCode();
}
public String getLabel() {
if (this.label != null) {
return label;
}
return getCode().toString();
}
}

View File

@@ -23,7 +23,6 @@ import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.util.List;
import java.util.Map;

View File

@@ -0,0 +1,49 @@
package org.springframework.batch.support;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* <p>
* Util class based off {@link org.springframework.test.jdbc.SimpleJdbcTestUtils} but for JdbcTemplate
* rather than the deprecated {@link org.springframework.jdbc.core.simple.SimpleJdbcTemplate}.
*
* This class should be removed when Batch uses Spring 3.2 - see:
* https://jira.springsource.org/browse/SPR-9235
* </p>
*/
public final class JdbcTestUtils {
private static final Log LOG = LogFactory.getLog(JdbcTestUtils.class);
private JdbcTestUtils() {
}
/**
* Count the rows in the given table.
* @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations
* @param tableName table name to count rows in
* @return the number of rows in the table
*/
public static int countRowsInTable(JdbcTemplate jdbcTemplate, String tableName) {
return jdbcTemplate.queryForInt("SELECT COUNT(0) FROM " + tableName);
}
/**
* Delete all rows from the specified tables.
* @param jdbcTemplate the SimpleJdbcTemplate with which to perform JDBC operations
* @param tableNames the names of the tables from which to delete
* @return the total number of rows deleted from all specified tables
*/
public static int deleteFromTables(JdbcTemplate jdbcTemplate, String... tableNames) {
int totalRowCount = 0;
for (String tableName : tableNames) {
int rowCount = jdbcTemplate.update("DELETE FROM " + tableName);
totalRowCount += rowCount;
if (LOG.isInfoEnabled()) {
LOG.info("Deleted " + rowCount + " rows from table " + tableName);
}
}
return totalRowCount;
}
}