com.tsg.techsupportmvc.dao.MessageBoardDaoDbImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.tsg.techsupportmvc.dao.MessageBoardDaoDbImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tsg.techsupportmvc.dao;

import com.tsg.techsupportmvc.dto.MessageBoardMessage;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author apprentice
 */
public class MessageBoardDaoDbImpl implements MessageBoardDao {

    private static final String SQL_INSERT_MESSAGE = "INSERT INTO `messageBoard` (`userId_FK`, `title`, `dateSubmitted`, `messageBody`) values (?, ?, ?, ?)";

    private static final String SQL_SELECT_ALL_MESSAGES = "SELECT m.*, u.displayName FROM `messageBoard` as m JOIN `users` AS u ON m.userID_FK = u.userId ORDER BY `messageId` DESC";

    private static final String SQL_DELETE_MESSAGE = "DELETE FROM `messageBoard` WHERE `messageId` = ?";

    private static final String SQL_UPDATE_MESSAGE = "UPDATE `messageBoard` SET `userId_FK` = ?, `title` = ?, `dateSubmitted` = ?, `messageBody` = ? WHERE `messageId` = ?";

    private static final String SQL_SELECT_MESSAGE = "SELECT m.*, u.displayName FROM `messageBoard` as m JOIN `users` AS u ON m.userID_FK = u.userId WHERE `messageId` = ?";

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public MessageBoardMessage addMessage(MessageBoardMessage message) {

        jdbcTemplate.update(SQL_INSERT_MESSAGE, message.getUserId_FK(), message.getTitle(),
                message.getSubmittedDate(), message.getMessageBody());

        message.setMessageId(jdbcTemplate.queryForObject("SELECT LAST_INSERT_ID()", Integer.class));

        return message;
    }

    @Override
    public void removeMessage(int id) {

        jdbcTemplate.update(SQL_DELETE_MESSAGE, id);

    }

    @Override
    public void updateMessage(MessageBoardMessage message) {

        jdbcTemplate.update(SQL_UPDATE_MESSAGE, message.getUserId_FK(), message.getTitle(),
                message.getSubmittedDate(), message.getMessageBody(), message.getMessageId());

    }

    @Override
    public List<MessageBoardMessage> getAllMessages() {
        return jdbcTemplate.query(SQL_SELECT_ALL_MESSAGES, new MessageMapper());
    }

    @Override
    public MessageBoardMessage getById(int id) {

        try {

            return jdbcTemplate.queryForObject(SQL_SELECT_MESSAGE, new MessageMapper(), id);

        } catch (EmptyResultDataAccessException e) {

            return null;

        }
    }

    private static final class MessageMapper implements RowMapper<MessageBoardMessage> {

        @Override
        public MessageBoardMessage mapRow(ResultSet rs, int i) throws SQLException {

            MessageBoardMessage message = new MessageBoardMessage();
            message.setMessageId(rs.getInt("messageId"));
            message.setTitle(rs.getString("title"));
            message.setUserId_FK(rs.getString("userId_FK"));
            message.setDisplayName(rs.getString("displayName"));
            message.setSubmittedDate(rs.getString("dateSubmitted"));
            message.setMessageBody(rs.getString("messageBody"));

            return message;

        }

    }

}