Advertisement
Guest User

Untitled

a guest
May 27th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.21 KB | None | 0 0
  1. package by.botyanov.library.dao;
  2.  
  3. import by.botyanov.library.dao.pool.ConnectionPool;
  4. import by.botyanov.library.dao.pool.ProxyConnection;
  5. import by.botyanov.library.domain.User;
  6. import by.botyanov.library.domain.UserRole;
  7. import org.apache.log4j.Logger;
  8.  
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15.  
  16. public class UserDao implements Dao<User> {
  17. private static final Logger LOG = Logger.getLogger(UserDao.class);
  18.  
  19. private static final String REGISTER_USER = "INSERT INTO user (login, password, first_name, last_name, role) VALUES (?, ?, ?, ?, ?);";
  20. private static final String FETCH_ALL_USERS = "SELECT * FROM user;";
  21. private static final String FIND_USER_BY_LOGIN = "SELECT * FROM user WHERE login = ?;";
  22. private static final String FIND_USER_BY_ID = "SELECT * FROM user WHERE id = ?;";
  23. private static final String DELETE_USER_BY_ID = "DELETE FROM user WHERE id = ?";
  24. private static final String UPDATE_USER_INFO = "UPDATE user SET login = ?, password = ?, first_name = ?, last_name = ?, role = ?, blocked = ? WHERE id = ?;";
  25. private static final String BLOCK_USER_TOGGLE = "UPDATE user SET blocked = !blocked WHERE login = ?";
  26.  
  27. private UserDao() {
  28. }
  29.  
  30. private static class UserDaoHolder {
  31. private final static UserDao instance = new UserDao();
  32. }
  33.  
  34. public static UserDao getInstance() {
  35. return UserDaoHolder.instance;
  36. }
  37.  
  38. @Override
  39. public void add(User user) throws DAOException {
  40. String login = user.getLogin();
  41. String password = user.getPassword();
  42. String firstName = user.getFirstName();
  43. String lastName = user.getLastName();
  44. String role = user.getUserRole().toString().toLowerCase();
  45. try (ProxyConnection connection = ConnectionPool.getInstance().getConnection();
  46. PreparedStatement preparedStatement = connection.prepareStatement(REGISTER_USER)) {
  47. preparedStatement.setString(1, login);
  48. preparedStatement.setString(2, password);
  49. preparedStatement.setString(3, firstName);
  50. preparedStatement.setString(4, lastName);
  51. preparedStatement.setString(5, role);
  52. preparedStatement.executeUpdate();
  53. LOG.info("User " + login + " has been saved");
  54. } catch (SQLException e) {
  55. throw new DAOException(e);
  56. }
  57. }
  58.  
  59. //UNUSED
  60. @Override
  61. public User findById(long id) throws DAOException {
  62. User user = null;
  63. try (ProxyConnection connection = ConnectionPool.getInstance().getConnection();
  64. PreparedStatement preparedStatement = connection.prepareStatement(FIND_USER_BY_ID)) {
  65. preparedStatement.setLong(1, id);
  66. ResultSet resultSet = preparedStatement.executeQuery();
  67. if (resultSet.next()) {
  68. user = new User();
  69. user.setId(id);
  70. user.setLogin(resultSet.getString("login"));
  71. user.setPassword(resultSet.getString("password"));
  72. user.setFirstName(resultSet.getString("first_name"));
  73. user.setLastName(resultSet.getString("last_name"));
  74. user.setUserRole(UserRole.valueOf(resultSet.getString("role")));
  75. user.setBlocked(resultSet.getBoolean("blocked"));
  76. }
  77. } catch (SQLException e) {
  78. throw new DAOException(e);
  79. }
  80. return user;
  81. }
  82.  
  83. @Override
  84. public List<User> getAll() throws DAOException {
  85. List<User> users = new ArrayList<>();
  86. User user;
  87. try (ProxyConnection connection = ConnectionPool.getInstance().getConnection();
  88. Statement statement = connection.createStatement()) {
  89. ResultSet resultSet = statement.executeQuery(FETCH_ALL_USERS);
  90. while (resultSet.next()) {
  91. user = new User(resultSet.getLong("id"), resultSet.getString("login"), resultSet.getString("password"),
  92. resultSet.getString("first_name"), resultSet.getString("last_name"),
  93. UserRole.valueOf(resultSet.getString("role").toUpperCase()), resultSet.getBoolean("blocked"));
  94. users.add(user);
  95. }
  96. } catch (SQLException e) {
  97. throw new DAOException(e);
  98. }
  99. return users;
  100. }
  101.  
  102. @Override
  103. public void update(User user) throws DAOException {
  104. try (ProxyConnection connection = ConnectionPool.getInstance().getConnection();
  105. PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_USER_INFO)) {
  106. preparedStatement.setString(1, user.getLogin());
  107. preparedStatement.setString(2, user.getPassword());
  108. preparedStatement.setString(3, user.getFirstName());
  109. preparedStatement.setString(4, user.getLastName());
  110. preparedStatement.setString(5, user.getUserRole().toString());
  111. preparedStatement.setBoolean(6, user.isBlocked());
  112. preparedStatement.setLong(7, user.getId());
  113. preparedStatement.executeUpdate();
  114. } catch (SQLException e) {
  115. throw new DAOException(e);
  116. }
  117. }
  118.  
  119. //UNUSED
  120. @Override
  121. public void delete(long id) throws DAOException {
  122. try (ProxyConnection connection = ConnectionPool.getInstance().getConnection();
  123. PreparedStatement preparedStatement = connection.prepareStatement(DELETE_USER_BY_ID)) {
  124. preparedStatement.setLong(1, id);
  125. preparedStatement.executeUpdate();
  126. } catch (SQLException e) {
  127. throw new DAOException(e);
  128. }
  129. }
  130.  
  131. public User findByLogin(String login) throws DAOException {
  132. LOG.debug("UserDao.findByLogin()");
  133. User user = null;
  134. try (ProxyConnection connection = ConnectionPool.getInstance().getConnection();
  135. PreparedStatement preparedStatement = connection.prepareStatement(FIND_USER_BY_LOGIN)) {
  136. preparedStatement.setString(1, login);
  137. ResultSet resultSet = preparedStatement.executeQuery();
  138. if (resultSet.next()) {
  139. user = new User(resultSet.getLong("id"),resultSet.getString("login"), resultSet.getString("password"),
  140. resultSet.getString("first_name"), resultSet.getString("last_name"),
  141. UserRole.valueOf(resultSet.getString("role").toUpperCase()),resultSet.getBoolean("blocked"));
  142. }
  143. } catch (SQLException e) {
  144. throw new DAOException(e);
  145. }
  146. return user;
  147. }
  148.  
  149. @Deprecated
  150. public void toggleBlockUser(String login) throws DAOException {
  151. LOG.debug("UserDao.toggleBlockUser()");
  152. try (ProxyConnection connection = ConnectionPool.getInstance().getConnection();
  153. PreparedStatement preparedStatement = connection.prepareStatement(BLOCK_USER_TOGGLE)) {
  154. preparedStatement.setString(1, login);
  155. LOG.debug(preparedStatement.toString());
  156. preparedStatement.executeUpdate();
  157. } catch (SQLException e) {
  158. throw new DAOException(e);
  159. }
  160. }
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement