View Javadoc

1   /*
2    * Copyright 2006-2012 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  
17  package org.springframework.batch.sample.domain.trade.internal;
18  
19  import javax.sql.DataSource;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.springframework.batch.sample.domain.trade.Trade;
24  import org.springframework.batch.sample.domain.trade.TradeDao;
25  import org.springframework.jdbc.core.JdbcOperations;
26  import org.springframework.jdbc.core.JdbcTemplate;
27  import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
28  
29  
30  /**
31   * Writes a Trade object to a database
32   *
33   * @author Robert Kasanicky
34   */
35  public class JdbcTradeDao implements TradeDao {
36  	private Log log = LogFactory.getLog(JdbcTradeDao.class);
37      /**
38       * template for inserting a row
39       */
40      private static final String INSERT_TRADE_RECORD = "INSERT INTO TRADE (id, version, isin, quantity, price, customer) VALUES (?, 0, ?, ? ,?, ?)";
41  
42      /**
43       * handles the processing of sql query
44       */
45      private JdbcOperations jdbcTemplate;
46  
47      /**
48       * database is not expected to be setup for autoincrement
49       */
50      private DataFieldMaxValueIncrementer incrementer;
51  
52      /**
53       * @see TradeDao
54       */
55      public void writeTrade(Trade trade) {
56          Long id = incrementer.nextLongValue();
57          log.debug("Processing: " + trade);
58          jdbcTemplate.update(INSERT_TRADE_RECORD,
59  				id, trade.getIsin(), trade.getQuantity(), trade.getPrice(),
60  				trade.getCustomer());
61      }
62  
63      public void setDataSource(DataSource dataSource) {
64          this.jdbcTemplate = new JdbcTemplate(dataSource);
65      }
66  
67      public void setIncrementer(DataFieldMaxValueIncrementer incrementer) {
68          this.incrementer = incrementer;
69      }
70  
71  }