| 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; |
| 17 | |
| 18 | import org.springframework.batch.item.database.support.IbatisKeyCollector; |
| 19 | import org.springframework.orm.ibatis.SqlMapClientTemplate; |
| 20 | |
| 21 | import com.ibatis.sqlmap.client.SqlMapClient; |
| 22 | |
| 23 | /** |
| 24 | * Extension of {@link DrivingQueryItemReader} that maps keys to objects. An |
| 25 | * iBatis query id must be set to map and return each 'detail record'. |
| 26 | * |
| 27 | * The writer is thread safe after its properties are set (normal singleton |
| 28 | * behaviour). |
| 29 | * |
| 30 | * @author Lucas Ward |
| 31 | * @see IbatisKeyCollector |
| 32 | */ |
| 33 | public class IbatisDrivingQueryItemReader extends DrivingQueryItemReader { |
| 34 | |
| 35 | private String detailsQueryId; |
| 36 | |
| 37 | private SqlMapClientTemplate sqlMapClientTemplate; |
| 38 | |
| 39 | /** |
| 40 | * Overridden read() that uses the returned key as arguments to the details |
| 41 | * query. |
| 42 | * |
| 43 | * @see org.springframework.batch.item.database.DrivingQueryItemReader#read() |
| 44 | */ |
| 45 | public Object read() { |
| 46 | Object key = super.read(); |
| 47 | if (key == null) { |
| 48 | return null; |
| 49 | } |
| 50 | return sqlMapClientTemplate.queryForObject(detailsQueryId, key); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @param detailsQueryId id of the iBATIS select statement that will used to |
| 55 | * retrieve an object for a single primary key from the list returned by |
| 56 | * driving query |
| 57 | */ |
| 58 | public void setDetailsQueryId(String detailsQueryId) { |
| 59 | this.detailsQueryId = detailsQueryId; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Set the {@link SqlMapClientTemplate} to use for this input source. |
| 64 | * |
| 65 | * @param sqlMapClient |
| 66 | */ |
| 67 | public void setSqlMapClient(SqlMapClient sqlMapClient) { |
| 68 | this.sqlMapClientTemplate = new SqlMapClientTemplate(sqlMapClient); |
| 69 | } |
| 70 | } |