EMMA Coverage Report (generated Tue May 06 07:28:24 PDT 2008)
[all classes][org.springframework.batch.item.database.support]

COVERAGE SUMMARY FOR SOURCE FILE [SingleColumnJdbcKeyCollector.java]

nameclass, %method, %block, %line, %
SingleColumnJdbcKeyCollector.java100% (1/1)56%  (5/9)81%  (91/112)70%  (21/30)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SingleColumnJdbcKeyCollector100% (1/1)56%  (5/9)81%  (91/112)70%  (21/30)
afterPropertiesSet (): void 0%   (0/1)0%   (0/9)0%   (0/3)
setJdbcTemplate (JdbcTemplate): void 0%   (0/1)0%   (0/4)0%   (0/2)
setKeyMapper (RowMapper): void 0%   (0/1)0%   (0/4)0%   (0/2)
setSql (String): void 0%   (0/1)0%   (0/4)0%   (0/2)
SingleColumnJdbcKeyCollector (): void 100% (1/1)100% (19/19)100% (4/4)
SingleColumnJdbcKeyCollector (JdbcTemplate, String): void 100% (1/1)100% (15/15)100% (6/6)
retrieveKeys (ExecutionContext): List 100% (1/1)100% (40/40)100% (5/5)
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)

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.util.List;
19 
20import org.springframework.batch.item.ExecutionContext;
21import org.springframework.batch.item.ExecutionContextUserSupport;
22import org.springframework.batch.item.database.KeyCollector;
23import org.springframework.jdbc.core.JdbcTemplate;
24import org.springframework.jdbc.core.RowMapper;
25import org.springframework.jdbc.core.SingleColumnRowMapper;
26import org.springframework.util.Assert;
27import org.springframework.util.ClassUtils;
28import org.springframework.util.StringUtils;
29 
30/**
31 * <p>
32 * Jdbc {@link KeyCollector} implementation that only works for a single column key. A sql query must be passed in which
33 * will be used to return a list of keys. Each key will be mapped by a {@link RowMapper} that returns a mapped key. By
34 * default, the {@link SingleColumnRowMapper} is used, and will convert keys into well known types at runtime. It is
35 * extremely important to note that only one column should be mapped to an object and returned as a key. If multiple
36 * columns are returned as a key in this strategy, then restart will not function properly. Instead a strategy that
37 * supports keys comprised of multiple columns should be used.
38 * </p>
39 * 
40 * <p>
41 * Restartability: Because the key is only one column, restart is made much more simple. Before each commit, the last
42 * processed key is returned to be stored as restart data. Upon restart, that same key is given back to restore from,
43 * using a separate 'RestartQuery'. This means that only the keys remaining to be processed are returned, rather than
44 * returning the original list of keys and iterating forward to that last committed point.
45 * </p>
46 * 
47 * @author Lucas Ward
48 * @see SingleColumnRowMapper
49 */
50public class SingleColumnJdbcKeyCollector extends ExecutionContextUserSupport implements KeyCollector {
51 
52        private static final String RESTART_KEY = "key";
53 
54        private JdbcTemplate jdbcTemplate;
55 
56        private String sql;
57 
58        private String restartSql;
59 
60        private RowMapper keyMapper = new SingleColumnRowMapper();
61 
62        public SingleColumnJdbcKeyCollector() {
63                setName(ClassUtils.getShortName(SingleColumnJdbcKeyCollector.class));
64        }
65 
66        /**
67         * Constructs a new instance using the provided jdbcTemplate and string representing the sql statement that should
68         * be used to retrieve keys.
69         * 
70         * @param jdbcTemplate
71         * @param sql
72         * @throws IllegalArgumentException if jdbcTemplate is null.
73         * @throws IllegalArgumentException if sql string is empty or null.
74         */
75        public SingleColumnJdbcKeyCollector(JdbcTemplate jdbcTemplate, String sql) {
76                this();
77                Assert.notNull(jdbcTemplate, "JdbcTemplate must not be null.");
78                Assert.hasText(sql, "The sql statement must not be null or empty.");
79                this.jdbcTemplate = jdbcTemplate;
80                this.sql = sql;
81        }
82 
83        /*
84         * (non-Javadoc)
85         * 
86         * @see org.springframework.batch.io.driving.KeyGenerationStrategy#retrieveKeys()
87         */
88        public List retrieveKeys(ExecutionContext executionContext) {
89 
90                Assert.notNull(executionContext, "The ExecutionContext must not be null");
91 
92                if (executionContext.containsKey(getKey(RESTART_KEY))) {
93                        Assert.state(StringUtils.hasText(restartSql), "The restart sql query must not be null or empty"
94                                + " in order to restart.");
95                        return jdbcTemplate.query(restartSql, new Object[] { executionContext.get(getKey(RESTART_KEY)) }, keyMapper);
96                } else {
97                        return jdbcTemplate.query(sql, keyMapper);
98                }
99        }
100 
101        /**
102         * Get the restart data representing the last processed key.
103         * 
104         * @throws IllegalArgumentException if key is null.
105         */
106        public void updateContext(Object key, ExecutionContext executionContext) {
107                Assert.notNull(key, "The key must not be null.");
108                Assert.notNull(executionContext, "The ExecutionContext must not be null");
109                executionContext.put(getKey(RESTART_KEY), key);
110        }
111 
112        /*
113         * (non-Javadoc)
114         * 
115         * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
116         */
117        public void afterPropertiesSet() throws Exception {
118                Assert.notNull(jdbcTemplate, "JdbcTemplate must not be null.");
119                Assert.hasText(sql, "The DrivingQuery must not be null or empty.");
120        }
121 
122        /**
123         * Set the {@link RowMapper} to be used to map each key to an object.
124         * 
125         * @param keyMapper
126         */
127        public void setKeyMapper(RowMapper keyMapper) {
128                this.keyMapper = keyMapper;
129        }
130 
131        /**
132         * Set the SQL query to be used to return the remaining keys to be processed.
133         * 
134         * @param restartSql
135         */
136        public void setRestartSql(String restartSql) {
137                this.restartSql = restartSql;
138        }
139 
140        /**
141         * Set the SQL statement to be used to return the keys to be processed.
142         * 
143         * @param sql
144         */
145        public void setSql(String sql) {
146                this.sql = sql;
147        }
148        
149        /**
150         * Set the {@link JdbcTemplate} to be used.
151         * 
152         * @param jdbcTemplate
153         */
154        public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
155                this.jdbcTemplate = jdbcTemplate;
156        }
157}

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