EMMA Coverage Report (generated Fri Jan 30 13:20:29 EST 2009)
[all classes][org.springframework.batch.item.database.support]

COVERAGE SUMMARY FOR SOURCE FILE [MultipleColumnJdbcKeyCollector.java]

nameclass, %method, %block, %line, %
MultipleColumnJdbcKeyCollector.java100% (2/2)58%  (7/12)79%  (115/145)71%  (28.9/41)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class MultipleColumnJdbcKeyCollector100% (1/1)50%  (5/10)77%  (99/129)66%  (22.9/35)
afterPropertiesSet (): void 0%   (0/1)0%   (0/13)0%   (0/4)
setJdbcTemplate (JdbcTemplate): void 0%   (0/1)0%   (0/4)0%   (0/2)
setKeyMapper (RowMapper): void 0%   (0/1)0%   (0/4)0%   (0/2)
setPreparedStatementSetter (ItemPreparedStatementSetter): void 0%   (0/1)0%   (0/4)0%   (0/2)
setSql (String): void 0%   (0/1)0%   (0/4)0%   (0/2)
retrieveKeys (ExecutionContext): List 100% (1/1)98%  (43/44)99%  (5.9/6)
MultipleColumnJdbcKeyCollector (): void 100% (1/1)100% (24/24)100% (5/5)
MultipleColumnJdbcKeyCollector (JdbcTemplate, String): void 100% (1/1)100% (15/15)100% (6/6)
setRestartSql (String): void 100% (1/1)100% (4/4)100% (2/2)
updateContext (Object, ExecutionContext): void 100% (1/1)100% (13/13)100% (4/4)
     
class MultipleColumnJdbcKeyCollector$PreparedStatementSetterKeyWrapper100% (1/1)100% (2/2)100% (16/16)100% (6/6)
MultipleColumnJdbcKeyCollector$PreparedStatementSetterKeyWrapper (Object, Ite... 100% (1/1)100% (9/9)100% (4/4)
setValues (PreparedStatement): void 100% (1/1)100% (7/7)100% (2/2)

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 */
16package org.springframework.batch.item.database.support;
17 
18import java.sql.PreparedStatement;
19import java.sql.SQLException;
20import java.util.List;
21 
22import org.springframework.batch.item.ExecutionContext;
23import org.springframework.batch.item.database.DrivingQueryItemReader;
24import org.springframework.batch.item.database.ItemPreparedStatementSetter;
25import org.springframework.batch.item.database.KeyCollector;
26import org.springframework.batch.item.util.ExecutionContextUserSupport;
27import org.springframework.jdbc.core.ColumnMapRowMapper;
28import org.springframework.jdbc.core.JdbcTemplate;
29import org.springframework.jdbc.core.PreparedStatementSetter;
30import org.springframework.jdbc.core.RowMapper;
31import org.springframework.util.Assert;
32import org.springframework.util.ClassUtils;
33import 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 */
52public 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}

[all classes][org.springframework.batch.item.database.support]
EMMA 2.0.5312 (C) Vladimir Roubtsov