Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1. package com.dao.daoimpl;
  2.  
  3. import com.dao.AbstractDao;
  4. import com.dao.Users;
  5. import com.dao.connection.DВConnectionPool;
  6. import com.dao.constants.ColumnName;
  7. import com.dao.constants.SqlRequest;
  8. import com.dao.exceptions.DaoException;
  9. import com.dao.utils.ClosingUtil;
  10. import com.dao.utils.EntityBuilder;
  11. import com.dao.utils.PaymentSystemLogger;
  12.  
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17.  
  18.  
  19. public class UsersDaoImpl extends AbstractDao<Users>{
  20.  
  21. private static UsersDaoImpl storage;
  22. private String message;
  23.  
  24. public static synchronized UsersDaoImpl getInstance(){
  25. if(storage == null){
  26. storage = new UsersDaoImpl();
  27. }
  28. return storage;
  29. }
  30.  
  31. @Override
  32. public void add(Users users) throws DaoException {
  33. try {
  34. connection = DВConnectionPool.getInstance().getConnection();
  35. statement = connection.prepareStatement(SqlRequest.ADD_USER);
  36. statement.setString(1, users.getName());
  37. statement.setString(2, users.getEmail());
  38. statement.setString(3, users.getLogin());
  39. statement.setString(4, users.getPassword());
  40. statement.setInt(5, users.getAge());
  41. statement.setInt(6, users.getId());
  42. statement.executeUpdate();
  43. }
  44. catch (SQLException e){
  45. message = "Unable to add the user account ";
  46. PaymentSystemLogger.getInstance().logError(getClass(), message);
  47. throw new DaoException(message, e);
  48. }
  49. finally{
  50. ClosingUtil.close(statement);
  51. }
  52. }
  53.  
  54. @Override
  55. public void delete(int id) throws DaoException {
  56. try {
  57. connection = DВConnectionPool.getInstance().getConnection();
  58. statement = connection.prepareStatement(SqlRequest.DELETE_OPERATION_BY_ID);
  59. statement.setInt(6, id);
  60. statement.executeUpdate();
  61. }
  62. catch (SQLException e){
  63. message = "Unable to delete the operation ";
  64. PaymentSystemLogger.getInstance().logError(getClass(), message);
  65. throw new DaoException(message, e);
  66. }
  67. finally{
  68. ClosingUtil.close(statement);
  69. }
  70. }
  71.  
  72. @Override
  73. public Users getById(int id) throws DaoException {
  74. Users user = null;
  75. try {
  76. connection = DВConnectionPool.getInstance().getConnection();
  77. statement = connection.prepareStatement(SqlRequest.GET_OPERATION_BY_ID);
  78. statement.setInt(1, id);
  79. result = statement.executeQuery();
  80. while (result.next()) {
  81. user = buildUser(result);
  82. }
  83. }
  84. catch (SQLException e){
  85. message = "Unable to return the operation ";
  86. PaymentSystemLogger.getInstance().logError(getClass(), message);
  87. throw new DaoException(message, e);
  88. }
  89. finally{
  90. ClosingUtil.close(result);
  91. ClosingUtil.close(statement);
  92. }
  93. return user;
  94. }
  95.  
  96. public boolean isAuthorized(String login, String password) throws DaoException{
  97. boolean isLogIn = false;
  98. try {
  99. connection = DВConnectionPool.getInstance().getConnection();
  100. statement = connection.prepareStatement(SqlRequest.CHECK_AUTHORIZATION);
  101. statement.setString(1, login);
  102. statement.setString(2, password);
  103. result = statement.executeQuery();
  104. if (result.next()) {
  105. isLogIn = true;
  106. }
  107. }
  108. catch (SQLException e){
  109. message = "Unable to check the user ";
  110. PaymentSystemLogger.getInstance().logError(getClass(), message);
  111. throw new DaoException(message, e);
  112. }
  113. finally{
  114. ClosingUtil.close(result);
  115. ClosingUtil.close(statement);
  116. }
  117. return isLogIn;
  118. }
  119.  
  120. public Users getByLogin(String login) throws DaoException{
  121. Users user = null;
  122. try {
  123. connection = DВConnectionPool.getInstance().getConnection();
  124. statement = connection.prepareStatement(SqlRequest.GET_USER_BY_LOGIN);
  125. statement.setString(1, login);
  126. result = statement.executeQuery();
  127. while (result.next()) {
  128. user = buildUser(result);
  129. }
  130. }
  131. catch (SQLException e){
  132. message = "Unable to return the user ";
  133. PaymentSystemLogger.getInstance().logError(getClass(), message);
  134. throw new DaoException(message, e);
  135. }
  136. finally{
  137. ClosingUtil.close(result);
  138. ClosingUtil.close(statement);
  139. }
  140. return user;
  141. }
  142.  
  143. @Override
  144. public List<Users> getAll() throws DaoException {
  145. List<Users> list = new ArrayList<>();
  146. try {
  147. connection = DВConnectionPool.getInstance().getConnection();
  148. statement = connection.prepareStatement(SqlRequest.GET_ALL_CLIENTS);
  149. result = statement.executeQuery();
  150. while (result.next()) {
  151. Users user = new Users();
  152. user.setName(result.getString(ColumnName.USER_NAME));
  153. list.add(user);
  154. }
  155. }
  156. catch (SQLException e){
  157. message = "Unable to return list of users ";
  158. PaymentSystemLogger.getInstance().logError(getClass(), message);
  159. throw new DaoException(message, e);
  160. }
  161. finally{
  162. ClosingUtil.close(result);
  163. ClosingUtil.close(statement);
  164. }
  165. return list;
  166. }
  167.  
  168. private Users buildUser(ResultSet result) throws SQLException{
  169. int id = result.getInt(ColumnName.USER_ID);
  170. String name = result.getString(ColumnName.USER_NAME);
  171. String email = result.getString(ColumnName.USER_EMAIL);
  172. String login = result.getString(ColumnName.USER_LOGIN);
  173. String password = result.getString(ColumnName.USER_PASSWORD);
  174. int age = result.getInt(ColumnName.USER_AGE);
  175. Users user = EntityBuilder.buildUser(id,name,email,login,password,age);
  176. return user;
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement