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.Game;
022import org.springframework.batch.item.ItemWriter;
023import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
024import org.springframework.jdbc.core.namedparam.SqlParameterSource;
025import org.springframework.jdbc.core.support.JdbcDaoSupport;
026import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
027
028public class JdbcGameDao extends JdbcDaoSupport implements ItemWriter<Game> {
029
030        private SimpleJdbcInsert insertGame;
031
032        @Override
033        protected void initDao() throws Exception {
034                super.initDao();
035                insertGame = new SimpleJdbcInsert(getDataSource()).withTableName("GAMES").usingColumns("player_id", "year_no",
036                                "team", "week", "opponent", " completes", "attempts", "passing_yards", "passing_td", "interceptions",
037                                "rushes", "rush_yards", "receptions", "receptions_yards", "total_td");
038        }
039
040        @Override
041        public void write(List<? extends Game> games) {
042
043                for (Game game : games) {
044
045                        SqlParameterSource values = new MapSqlParameterSource().addValue("player_id", game.getId()).addValue(
046                                        "year_no", game.getYear()).addValue("team", game.getTeam()).addValue("week", game.getWeek())
047                                        .addValue("opponent", game.getOpponent()).addValue("completes", game.getCompletes()).addValue(
048                                                        "attempts", game.getAttempts()).addValue("passing_yards", game.getPassingYards()).addValue(
049                                                        "passing_td", game.getPassingTd()).addValue("interceptions", game.getInterceptions())
050                                        .addValue("rushes", game.getRushes()).addValue("rush_yards", game.getRushYards()).addValue(
051                                                        "receptions", game.getReceptions()).addValue("receptions_yards", game.getReceptionYards())
052                                        .addValue("total_td", game.getTotalTd());
053                        this.insertGame.execute(values);
054
055                }
056
057        }
058
059}