1 | /* |
2 | * Copyright 2006-2007 the original author or authors. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | package org.springframework.batch.item.database.support; |
17 | |
18 | import java.sql.PreparedStatement; |
19 | import java.sql.SQLException; |
20 | import java.util.List; |
21 | |
22 | import org.springframework.batch.item.ExecutionContext; |
23 | import org.springframework.batch.item.database.DrivingQueryItemReader; |
24 | import org.springframework.batch.item.database.ItemPreparedStatementSetter; |
25 | import org.springframework.batch.item.database.KeyCollector; |
26 | import org.springframework.batch.item.util.ExecutionContextUserSupport; |
27 | import org.springframework.jdbc.core.ColumnMapRowMapper; |
28 | import org.springframework.jdbc.core.JdbcTemplate; |
29 | import org.springframework.jdbc.core.PreparedStatementSetter; |
30 | import org.springframework.jdbc.core.RowMapper; |
31 | import org.springframework.util.Assert; |
32 | import org.springframework.util.ClassUtils; |
33 | import org.springframework.util.StringUtils; |
34 | |
35 | /** |
36 | * <p> |
37 | * JDBC implementation of the {@link KeyCollector} interface that works for |
38 | * composite keys. (i.e. keys represented by multiple columns) A SQL query to be |
39 | * used to return the keys and a {@link ItemPreparedStatementSetter} to map each |
40 | * row in the result set to an Object must be set in order to work correctly. |
41 | * </p> |
42 | * |
43 | * The implementation is thread-safe as long as the |
44 | * {@link #setPreparedStatementSetter(ItemPreparedStatementSetter)} and |
45 | * {@link #setKeyMapper(RowMapper)} are thread-safe (true for default values). |
46 | * |
47 | * @author Lucas Ward |
48 | * |
49 | * @see DrivingQueryItemReader |
50 | * @see ItemPreparedStatementSetter |
51 | */ |
52 | public class MultipleColumnJdbcKeyCollector extends ExecutionContextUserSupport implements KeyCollector { |
53 | |
54 | private static final String CURRENT_KEY = "current.key"; |
55 | |
56 | private JdbcTemplate jdbcTemplate; |
57 | |
58 | private RowMapper keyMapper = new ColumnMapRowMapper(); |
59 | |
60 | private ItemPreparedStatementSetter preparedStatementSetter = new ColumnMapItemPreparedStatementSetter(); |
61 | |
62 | private String sql; |
63 | |
64 | private String restartSql; |
65 | |
66 | public MultipleColumnJdbcKeyCollector() { |
67 | setName(ClassUtils.getShortName(MultipleColumnJdbcKeyCollector.class)); |
68 | } |
69 | |
70 | /** |
71 | * Construct a new ItemReader. |
72 | * |
73 | * @param jdbcTemplate |
74 | * @param sql - SQL statement that returns all keys to process. object. |
75 | */ |
76 | public MultipleColumnJdbcKeyCollector(JdbcTemplate jdbcTemplate, String sql) { |
77 | this(); |
78 | Assert.notNull(jdbcTemplate, "The JdbcTemplate must not be null."); |
79 | Assert.hasText(sql, "The sql statement must not be null or empty."); |
80 | this.jdbcTemplate = jdbcTemplate; |
81 | this.sql = sql; |
82 | } |
83 | |
84 | /* |
85 | * (non-Javadoc) |
86 | * |
87 | * @see |
88 | * org.springframework.batch.io.sql.scratch.AbstractDrivingQueryItemReader |
89 | * #retrieveKeys() |
90 | */ |
91 | public List retrieveKeys(ExecutionContext executionContext) { |
92 | |
93 | Assert.state(keyMapper != null, "KeyMapper must not be null."); |
94 | Assert.state(StringUtils.hasText(restartSql), "The RestartQuery must not be null or empty" |
95 | + " in order to restart."); |
96 | |
97 | if (executionContext.size() > 0) { |
98 | Object key = executionContext.get(getKey(CURRENT_KEY)); |
99 | return jdbcTemplate.query(restartSql, new PreparedStatementSetterKeyWrapper(key, preparedStatementSetter), |
100 | keyMapper); |
101 | } |
102 | else { |
103 | return jdbcTemplate.query(sql, keyMapper); |
104 | } |
105 | } |
106 | |
107 | /* |
108 | * (non-Javadoc) |
109 | * |
110 | * @see |
111 | * org.springframework.batch.io.driving.KeyGenerator#getKeyAsExecutionContext |
112 | * (java.lang.Object) |
113 | */ |
114 | public void updateContext(Object key, ExecutionContext executionContext) { |
115 | Assert.notNull(key, "The key must not be null"); |
116 | Assert.notNull(executionContext, "The ExecutionContext must not be null"); |
117 | executionContext.put(getKey(CURRENT_KEY), key); |
118 | } |
119 | |
120 | /** |
121 | * Set the query to use to retrieve keys in order to restore the previous |
122 | * state for restart. |
123 | * |
124 | * @param restartQuery |
125 | */ |
126 | public void setRestartSql(String restartQuery) { |
127 | this.restartSql = restartQuery; |
128 | } |
129 | |
130 | /* |
131 | * (non-Javadoc) |
132 | * |
133 | * @see |
134 | * org.springframework.beans.factory.InitializingBean#afterPropertiesSet() |
135 | */ |
136 | public void afterPropertiesSet() throws Exception { |
137 | Assert.notNull(jdbcTemplate, "The JdbcTemplate must not be null."); |
138 | Assert.hasText(sql, "The DrivingQuery must not be null or empty."); |
139 | Assert.notNull(keyMapper, "The key RowMapper must not be null."); |
140 | } |
141 | |
142 | /** |
143 | * Set the {@link RowMapper} to be used to map a result set to keys. |
144 | * |
145 | * @param keyMapper |
146 | */ |
147 | public void setKeyMapper(RowMapper keyMapper) { |
148 | this.keyMapper = keyMapper; |
149 | } |
150 | |
151 | /** |
152 | * Set the sql statement used to generate the keys list. |
153 | * |
154 | * @param sql |
155 | */ |
156 | public void setSql(String sql) { |
157 | this.sql = sql; |
158 | } |
159 | |
160 | public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { |
161 | this.jdbcTemplate = jdbcTemplate; |
162 | } |
163 | |
164 | public void setPreparedStatementSetter(ItemPreparedStatementSetter preparedStatementSetter) { |
165 | this.preparedStatementSetter = preparedStatementSetter; |
166 | } |
167 | |
168 | private static class PreparedStatementSetterKeyWrapper implements PreparedStatementSetter { |
169 | |
170 | private Object key; |
171 | |
172 | private ItemPreparedStatementSetter pss; |
173 | |
174 | public PreparedStatementSetterKeyWrapper(Object key, ItemPreparedStatementSetter pss) { |
175 | this.key = key; |
176 | this.pss = pss; |
177 | } |
178 | |
179 | public void setValues(PreparedStatement ps) throws SQLException { |
180 | pss.setValues(key, ps); |
181 | } |
182 | } |
183 | } |