Java tutorial
/* * 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 airport.database.services.chat; import airport.database.dispatcher.airplane.FlightDaoImpl; import airport.database.services.users.User; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.namedparam.SqlParameterSource; import org.springframework.stereotype.Repository; /** * * @author mcdoker */ @Repository public class ChatDaoImpl implements ChatDao { private final static Logger LOG = Logger.getLogger(ChatDaoImpl.class); @Autowired private NamedParameterJdbcTemplate jdbcTemplate; private final static String PARAMETER_SQL_QUERY_NUMBER_MESSAGE = "number_message"; private final static String PARAMETER_SQL_QUERY_LOGIN = "loginUser"; private final static String PARAMETER_SQL_QUERY_TEXT = "text"; private final static String PARAMETER_SQL_QUERY_DATE = "date"; private final static String PARAMETER_SQL_QUERY_USER_ID = "userid"; private final static String SQL_QUERY_GET_MESSAGES_MISS = "SELECT * " + "FROM message " + "WHERE id_message > :number_message"; private final static String SQL_QUERY_ADD_MESSAGE = "INSERT INTO message " + "(text, user_id, date_recieve) " + "VALUES (:text, :userid, :date)"; private final static String SQL_QUERY_SEEK_USER = "SELECT u.user_id " + "FROM users u " + "WHERE (u.login = :loginUser)"; private final static String SQL_QUERY_LAST_NUMBER_MESSAGE = "SELECT last_value from message_id_message_seq"; @Override public List<Message> getMessageMiss(int number) { List<Message> resultMessages; SqlParameterSource parameterUser = new MapSqlParameterSource(PARAMETER_SQL_QUERY_NUMBER_MESSAGE, number); try { resultMessages = jdbcTemplate.query(SQL_QUERY_GET_MESSAGES_MISS, parameterUser, new MessageMapper()); } catch (EmptyResultDataAccessException e) { if (LOG.isInfoEnabled()) { LOG.info("new messages aren't present. Number message : " + number); } return new ArrayList<>(); } if (LOG.isInfoEnabled()) { LOG.info("get messages miss. Number message : " + number + ". Result : " + resultMessages); } return resultMessages; } @Override public void addMessage(User user, String text, Timestamp timestampMessage) { SqlParameterSource parameterUser = new MapSqlParameterSource(PARAMETER_SQL_QUERY_LOGIN, user.getLogin()); int numberUser = jdbcTemplate.queryForObject(SQL_QUERY_SEEK_USER, parameterUser, Integer.class); MapSqlParameterSource parameterMessage = new MapSqlParameterSource(); parameterMessage.addValue(PARAMETER_SQL_QUERY_USER_ID, numberUser); parameterMessage.addValue(PARAMETER_SQL_QUERY_TEXT, text); parameterMessage.addValue(PARAMETER_SQL_QUERY_DATE, timestampMessage); jdbcTemplate.update(SQL_QUERY_ADD_MESSAGE, parameterMessage); if (LOG.isInfoEnabled()) { LOG.info( "add message. User : " + user + ". Text : " + text + ". Time : " + timestampMessage.toString()); } } @Override public int getLastNumberMessage() { int numberMessageResult = jdbcTemplate.getJdbcOperations().queryForObject(SQL_QUERY_LAST_NUMBER_MESSAGE, Integer.class); if (LOG.isInfoEnabled()) { LOG.info("get last number Message : " + numberMessageResult); } return numberMessageResult; } private final class MessageMapper implements RowMapper<Message> { private final Map<Integer, String> users = new HashMap<>(); private final static String ARGUMENT_TEXT = "text"; private final static String ARGUMENT_ID_MESSAGE = "id_message"; private final static String ARGUMENT_USER_ID = "user_id"; private final static String ARGUMENT_DATE_RECIEVE = "date_recieve"; private final static String SQL_QUERY_SEEK_USER = "SELECT u.login " + "FROM users u " + "WHERE (u.user_id = :userid)"; @Override public Message mapRow(ResultSet rs, int rowNum) throws SQLException { Message messageResult = new Message(); int userId = rs.getInt(ARGUMENT_USER_ID); if (users.containsKey(userId)) { messageResult.setUserName(users.get(userId)); } else { String loginUser = jdbcTemplate.queryForObject(SQL_QUERY_SEEK_USER, new MapSqlParameterSource(PARAMETER_SQL_QUERY_USER_ID, userId), String.class); users.put(userId, loginUser); messageResult.setUserName(loginUser); } SimpleDateFormat dateFormat = new SimpleDateFormat("HH : mm"); Date timeMessage = new Date(rs.getTimestamp(ARGUMENT_DATE_RECIEVE).getTime()); messageResult.setDateRecieve(dateFormat.format(timeMessage)); messageResult.setIdMessage(rs.getInt(ARGUMENT_ID_MESSAGE)); messageResult.setText(rs.getString(ARGUMENT_TEXT)); return messageResult; } } }