main.java.net.bornil.db.service.jdbc.JdbcEventDao.java Source code

Java tutorial

Introduction

Here is the source code for main.java.net.bornil.db.service.jdbc.JdbcEventDao.java

Source

/*
 * Copyright 2010-2013 Bornil Photography
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package main.java.net.bornil.db.service.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import main.java.net.bornil.db.entity.Event;
import main.java.net.bornil.db.service.EventDao;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;

/**
 * @author MaMuN
 * @since 2012/08/25
 */
@Repository
public class JdbcEventDao implements EventDao {

    private static final Logger logger = Logger.getLogger(JdbcEventDao.class.getName());

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    final static class EventRowMapper implements RowMapper<Event> {
        public Event mapRow(ResultSet resultSet, int i) throws SQLException {
            Event evt = new Event();
            evt.setEvtId(resultSet.getInt("EVT_ID"));
            evt.setEvtStatus(resultSet.getInt("EVT_STATUS"));
            evt.setEvtClass(resultSet.getString("EVT_CLASS"));
            evt.setCreatedBy(resultSet.getString("EVT_CREATED_BY"));
            evt.setUpdatedBy(resultSet.getString("EVT_UPDATED_BY"));
            return evt;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see main.java.net.bornil.db.EventService#getEvents()
     */
    @Override
    public List<Event> getEvents() {
        return this.jdbcTemplate.query(
                "select EVT_ID, EVT_STATUS, EVT_CLASS, EVT_CREATED_BY, EVT_UPDATED_BY from EVENT",
                new EventRowMapper());
    }

    @Override
    public Event getEvent(int evtId) {
        try {
            return this.jdbcTemplate.queryForObject(
                    "select EVT_ID, EVT_STATUS, EVT_CLASS, EVT_CREATED_BY, EVT_UPDATED_BY from EVENT where EVT_ID = ?",
                    new Object[] { evtId }, new EventRowMapper());
        } catch (IncorrectResultSizeDataAccessException ex) {
            return new Event();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * main.java.net.bornil.db.EventService#createEvent(main.java.net.bornil
     * .db.entity.Event)
     */
    @Override
    public int createEvent(Event event) {
        SimpleJdbcInsert insertEvent = new SimpleJdbcInsert(this.jdbcTemplate).withTableName("EVENT")
                .usingColumns("EVT_STATUS", "EVT_CLASS", "EVT_CREATED_BY", "EVT_CREATED_ON")
                .usingGeneratedKeyColumns("EVT_ID");

        Map<String, Object> args = new HashMap<String, Object>(4);
        args.put("EVT_STATUS", 1100);
        args.put("EVT_CLASS", event.getEvtClass());
        args.put("EVT_CREATED_BY", "MaMuN");
        args.put("EVT_CREATED_ON", new Date());
        Number newId = insertEvent.executeAndReturnKey(args);
        event.setEvtId(newId.intValue());
        return event.getEvtId();
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * main.java.net.bornil.db.service.EventDao#updateEvent(main.java.net.bornil
     * .db.entity.Event)
     */
    @Override
    public int updateEvent(Event event) {
        return this.jdbcTemplate.update(
                "UPDATE EVENT SET EVT_CLASS = ?, EVT_UPDATED_BY = ?, EVT_UPDATED_ON = ? WHERE EVT_ID = ?",
                event.getEvtClass(), "MaMuN", Calendar.getInstance().getTime(), event.getEvtId());
    }

}