001/*
002 * Copyright 2006-2007 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.batch.sample.domain.football.internal;
018
019import java.util.List;
020
021import org.springframework.batch.core.test.football.PlayerSummary;
022import org.springframework.batch.item.ItemWriter;
023import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
024import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
025
026import javax.sql.DataSource;
027
028public class JdbcPlayerSummaryDao implements ItemWriter<PlayerSummary> {
029
030        private static final String INSERT_SUMMARY = "INSERT into PLAYER_SUMMARY(ID, YEAR_NO, COMPLETES, ATTEMPTS, PASSING_YARDS, PASSING_TD, "
031                        + "INTERCEPTIONS, RUSHES, RUSH_YARDS, RECEPTIONS, RECEPTIONS_YARDS, TOTAL_TD) "
032                        + "values(:id, :year, :completes, :attempts, :passingYards, :passingTd, "
033                        + ":interceptions, :rushes, :rushYards, :receptions, :receptionYards, :totalTd)";
034
035    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
036
037    @Override
038        public void write(List<? extends PlayerSummary> summaries) {
039
040                for (PlayerSummary summary : summaries) {
041
042                        MapSqlParameterSource args = new MapSqlParameterSource().addValue("id", summary.getId()).addValue("year",
043                                        summary.getYear()).addValue("completes", summary.getCompletes()).addValue("attempts",
044                                        summary.getAttempts()).addValue("passingYards", summary.getPassingYards()).addValue("passingTd",
045                                        summary.getPassingTd()).addValue("interceptions", summary.getInterceptions()).addValue("rushes",
046                                        summary.getRushes()).addValue("rushYards", summary.getRushYards()).addValue("receptions",
047                                        summary.getReceptions()).addValue("receptionYards", summary.getReceptionYards()).addValue(
048                                        "totalTd", summary.getTotalTd());
049
050                        namedParameterJdbcTemplate.update(INSERT_SUMMARY, args);
051                }
052        }
053
054    public void setDataSource(DataSource dataSource) {
055        this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
056    }
057}