/FoodUp-persistence/src/main/java/org/foodup/persistence/dao/UserDao.java

https://gitlab.com/FoodUpProject/FoodUp · Java · 573 lines · 306 code · 169 blank · 98 comment · 4 complexity · 4480fd76d234bd9eeef9f660dc87f3e6 MD5 · raw file

  1. package org.foodup.persistence.dao;
  2. /**
  3. * @author Meritxell Martí Martínez <m.marblu@gmail.com>
  4. * @author Cristóbal Cabezas Mateos <ccabezasmateos@gmail.com>
  5. * @author Escola del treball <correu@escoladeltreball.org>
  6. *
  7. * This is free software, licensed under the GNU General Public License v3.
  8. * See http://www.gnu.org/licenses/gpl.html for more information.
  9. */
  10. import java.sql.Connection;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.sql.Timestamp;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import org.foodup.persistence.DBUtils;
  18. import org.foodup.persistence.entity.Aliment;
  19. import org.foodup.persistence.entity.User;
  20. class UserDao extends BaseDao {
  21. // SELECTS
  22. public int getNextSequence(Connection con) throws SQLException {
  23. PreparedStatement ps = null;
  24. ResultSet rs = null;
  25. try {
  26. ps = con.prepareStatement("SELECT nextval('users_seq')");
  27. rs = ps.executeQuery();
  28. return rs.next() ? rs.getInt(1) : -1;
  29. } finally {
  30. DBUtils.closeResources(rs, ps);
  31. }
  32. }
  33. /**
  34. * Get user info
  35. *
  36. * @param id
  37. * user
  38. * @return User or null if something went wrong
  39. * @throws SQLException
  40. */
  41. public User getUser(Connection con, int id) throws SQLException {
  42. PreparedStatement ps = null;
  43. ResultSet rs = null;
  44. try {
  45. ps = con.prepareStatement("SELECT age, name_user, email, last_update, intolerance FROM users WHERE id=?");
  46. ps.setInt(1, id);
  47. rs = ps.executeQuery();
  48. User user = null;
  49. if (rs.next()) {
  50. user = new User();
  51. user.setQuantity(getNullableInteger(rs, "age"));
  52. user.setName(rs.getString("name_user"));
  53. user.setMail(rs.getString("email"));
  54. user.setLastUpdate(rs.getTimestamp("last_update").getTime());
  55. user.setIntolerance(getNullableInteger(rs, "intolerance"));
  56. user.setId(id);
  57. }
  58. return user;
  59. } finally {
  60. DBUtils.closeResources(rs, ps);
  61. }
  62. }
  63. public String getPasswordById(Connection con, int id) throws SQLException {
  64. PreparedStatement ps = null;
  65. ResultSet rs = null;
  66. try {
  67. ps = con.prepareStatement("SELECT password FROM users WHERE id = ?");
  68. ps.setInt(1, id);
  69. rs = ps.executeQuery();
  70. return rs.next() ? rs.getString(1) : null;
  71. } finally {
  72. DBUtils.closeResources(rs, ps);
  73. }
  74. }
  75. public String getPasswordByMail(Connection con, String email) throws SQLException {
  76. PreparedStatement ps = null;
  77. ResultSet rs = null;
  78. try {
  79. ps = con.prepareStatement("SELECT password FROM users WHERE email = ?");
  80. ps.setString(1, email);
  81. rs = ps.executeQuery();
  82. return rs.next() ? rs.getString(1) : null;
  83. } finally {
  84. DBUtils.closeResources(rs, ps);
  85. }
  86. }
  87. public int getUserIdByMail(Connection con, String email) throws SQLException {
  88. PreparedStatement ps = null;
  89. ResultSet rs = null;
  90. try {
  91. ps = con.prepareStatement("SELECT id FROM users WHERE email = ?");
  92. ps.setString(1, email);
  93. rs = ps.executeQuery();
  94. return rs.next() ? rs.getInt(1) : -1;
  95. } finally {
  96. DBUtils.closeResources(rs, ps);
  97. }
  98. }
  99. public int getUserId(Connection con, String email, String password) throws SQLException {
  100. PreparedStatement ps = null;
  101. ResultSet rs = null;
  102. try {
  103. ps = con.prepareStatement("SELECT id FROM users WHERE email = ? AND password = ?");
  104. ps.setString(1, email);
  105. ps.setString(2, password);
  106. rs = ps.executeQuery();
  107. return rs.next() ? rs.getInt(1) : -1;
  108. } finally {
  109. DBUtils.closeResources(rs, ps);
  110. }
  111. }
  112. public int getUserId(Connection con, String email) throws SQLException {
  113. PreparedStatement ps = null;
  114. ResultSet rs = null;
  115. try {
  116. ps = con.prepareStatement("SELECT id FROM users WHERE email = ?");
  117. ps.setString(1, email);
  118. rs = ps.executeQuery();
  119. return rs.next() ? rs.getInt(1) : -1;
  120. } finally {
  121. DBUtils.closeResources(rs, ps);
  122. }
  123. }
  124. public byte[] getUserPhoto(Connection con, int id) throws SQLException {
  125. PreparedStatement ps = null;
  126. ResultSet rs = null;
  127. try {
  128. ps = con.prepareStatement("SELECT picture_file FROM user_pictures WHERE id_user = ?");
  129. ps.setInt(1, id);
  130. rs = ps.executeQuery();
  131. return rs.next() ? rs.getBytes("picture_file") : null;
  132. } finally {
  133. DBUtils.closeResources(rs, ps);
  134. }
  135. }
  136. /**
  137. * Get friends of user
  138. *
  139. * @param idUser
  140. * @return List<User> or null if something went wrong
  141. * @throws SQLException
  142. */
  143. public List<User> getColeguis(Connection con, int idUser) throws SQLException {
  144. PreparedStatement ps = null;
  145. ResultSet rs = null;
  146. try {
  147. ps = con.prepareStatement(
  148. "SELECT id, age, name_user, email, last_update, intolerance FROM users INNER JOIN my_coleguis ON id = id_colegui WHERE id_user = ?");
  149. ps.setInt(1, idUser);
  150. rs = ps.executeQuery();
  151. List<User> coleguis = new ArrayList<>();
  152. User user = null;
  153. while (rs.next()) {
  154. user = new User();
  155. // basic info
  156. user.setId(rs.getInt("id"));
  157. user.setQuantity(getNullableInteger(rs, "age"));
  158. user.setName(rs.getString("name_user"));
  159. user.setMail(rs.getString("email"));
  160. user.setLastUpdate(rs.getTimestamp("last_update").getTime());
  161. user.setIntolerance(getNullableInteger(rs, "intolerance"));
  162. coleguis.add(user);
  163. }
  164. return coleguis;
  165. } finally {
  166. DBUtils.closeResources(rs, ps);
  167. }
  168. }
  169. /**
  170. * Get allergies from user
  171. *
  172. * @param idUser
  173. * @return List<Aliments> or null if something went wrong
  174. * @throws SQLException
  175. */
  176. public List<Aliment> getUserAllergies(Connection con, int idUser) throws SQLException {
  177. PreparedStatement ps = null;
  178. ResultSet rs = null;
  179. try {
  180. ps = con.prepareStatement("SELECT id, name_aliment, food_group FROM aliments INNER JOIN users_restriction ON id = id_aliment WHERE id_user = ?");
  181. ps.setInt(1, idUser);
  182. rs = ps.executeQuery();
  183. List<Aliment> allergies = new ArrayList<>();
  184. Aliment aliment;
  185. if (rs.next()) {
  186. aliment = new Aliment();
  187. aliment.setId(rs.getInt("id"));
  188. aliment.setName(rs.getString("name_aliment"));
  189. aliment.setFoodGroup(rs.getInt("food_group"));
  190. allergies.add(aliment);
  191. }
  192. return allergies;
  193. } finally {
  194. DBUtils.closeResources(rs, ps);
  195. }
  196. }
  197. // INSERTS
  198. public int insertImageUser(Connection con, int idUser, byte[] file) throws SQLException {
  199. PreparedStatement ps = null;
  200. try {
  201. ps = con.prepareStatement("INSERT INTO user_pictures VALUES (?, ?)");
  202. ps.setInt(1, idUser);
  203. ps.setBytes(2, file);
  204. return ps.executeUpdate();
  205. } finally {
  206. DBUtils.closeResources(ps);
  207. }
  208. }
  209. public int insertUser(Connection con, User user, String password) throws SQLException {
  210. PreparedStatement ps = null;
  211. try {
  212. ps = con.prepareStatement("INSERT INTO users (id, age, name_user, email, password, last_update, intolerance) VALUES (?, ?, ?, ?, ?, ?, ?)");
  213. ps.setInt(1, user.getId());
  214. setNullableInteger(ps, 2, user.getQuantity());
  215. ps.setString(3, user.getName());
  216. ps.setString(4, user.getMail());
  217. ps.setString(5, password);
  218. ps.setTimestamp(6, new Timestamp(user.getLastUpdate()));
  219. setNullableInteger(ps, 7, user.getIntolerance());
  220. return ps.executeUpdate();
  221. } finally {
  222. DBUtils.closeResources(ps);
  223. }
  224. }
  225. /**
  226. * Insert user's FoodUp
  227. * @param idUser
  228. * @param idFoodUp
  229. * @throws SQLException
  230. */
  231. public int insertFoodUpOfUser(Connection con, int idUser, int idFoodUp) throws SQLException {
  232. PreparedStatement ps = null;
  233. try {
  234. ps = con.prepareStatement("INSERT INTO relation_foodups_users VALUES (?, ?)");
  235. ps.setInt(1, idFoodUp);
  236. ps.setInt(2, idUser);
  237. return ps.executeUpdate();
  238. } finally {
  239. DBUtils.closeResources(ps);
  240. }
  241. }
  242. /**
  243. * Insert allergies
  244. *
  245. * @param idUser
  246. * @param idNewAliment
  247. * @throws SQLException
  248. */
  249. public int[] insertAllergies(Connection con, int idUser, List<Aliment> aliments) throws SQLException {
  250. PreparedStatement ps = null;
  251. try {
  252. ps = con.prepareStatement("INSERT INTO users_restriction VALUES (?,?)");
  253. for (Aliment aliment: aliments) {
  254. ps.setInt(1, idUser);
  255. ps.setInt(2, aliment.getId());
  256. ps.addBatch();
  257. }
  258. return ps.executeBatch();
  259. } finally {
  260. DBUtils.closeResources(ps);
  261. }
  262. }
  263. /**
  264. * Insert user's friend
  265. * @param idUser
  266. * @param idColegui
  267. * @throws SQLException
  268. */
  269. public int insertColegui(Connection con, int idUser, int idColegui) throws SQLException {
  270. PreparedStatement ps = null;
  271. try {
  272. ps = con.prepareStatement("INSERT INTO my_coleguis VALUES (?, ?) WHERE id_user != id_colegui");
  273. ps.setInt(1, idUser);
  274. ps.setInt(2, idColegui);
  275. return ps.executeUpdate();
  276. } finally {
  277. DBUtils.closeResources(ps);
  278. }
  279. }
  280. // UPDATEs
  281. public int updateUserInfo(Connection con, User user) throws SQLException {
  282. PreparedStatement ps = null;
  283. try {
  284. ps = con.prepareStatement(
  285. "UPDATE users SET name_user = ?, age = ?, last_update = ?, intolerance = ? WHERE id = ?");
  286. ps.setString(1, user.getName());
  287. ps.setInt(2, user.getQuantity());
  288. ps.setTimestamp(3, new Timestamp(user.getLastUpdate()));
  289. setNullableInteger(ps, 4, user.getIntolerance());
  290. ps.setInt(5, user.getId());
  291. return ps.executeUpdate();
  292. } finally {
  293. DBUtils.closeResources(ps);
  294. }
  295. }
  296. /**
  297. * @param con a database connection
  298. * @param id
  299. * @param email
  300. * @return row count have been updated, 0 otherwise
  301. * @throws SQLException if a database access error occurs or if a restriction is violated
  302. */
  303. public int updateUserMail(Connection con, int id, String email) throws SQLException {
  304. PreparedStatement ps = null;
  305. try {
  306. ps = con.prepareStatement("UPDATE users SET email = ? WHERE id = ?");
  307. ps.setString(1, email);
  308. ps.setInt(2, id);
  309. return ps.executeUpdate();
  310. } finally {
  311. DBUtils.closeResources(ps);
  312. }
  313. }
  314. // DELETES
  315. /**
  316. * Delete image profile of user
  317. * @param con a database connection
  318. * @param idUser
  319. * @return row count have been removed, 0 otherwise
  320. * @throws SQLException if a database access error occurs or if a restriction is violated
  321. */
  322. public int deleteUserImage(Connection con, int idUser) throws SQLException {
  323. PreparedStatement ps = null;
  324. try {
  325. ps = con.prepareStatement("DELETE FROM user_pictures WHERE id_user= ?");
  326. ps.setInt(1, idUser);
  327. return ps.executeUpdate();
  328. } finally {
  329. DBUtils.closeResources(ps);
  330. }
  331. }
  332. /**
  333. * Delete colegui of user
  334. * @param con a database connection
  335. * @param idUser
  336. * @param idColeguiToRemove
  337. * @return row count have been removed, 0 otherwise
  338. * @throws SQLException if a database access error occurs or if a restriction is violated
  339. */
  340. public int deleteColegui(Connection con, int idUser, int idColeguiToRemove) throws SQLException {
  341. PreparedStatement ps = null;
  342. try {
  343. ps = con.prepareStatement("DELETE FROM my_coleguis WHERE id_user= ? AND id_colegui= ?");
  344. ps.setInt(1, idUser);
  345. ps.setInt(2, idColeguiToRemove);
  346. return ps.executeUpdate();
  347. } finally {
  348. DBUtils.closeResources(ps);
  349. }
  350. }
  351. /**
  352. * Delete a user
  353. * @param con a database connection
  354. * @param idUser
  355. * @return row count have been removed, 0 otherwise
  356. * @throws SQLException if a database access error occurs or if a restriction is violated
  357. */
  358. public int deleteUser(Connection con, int idUser) throws SQLException {
  359. PreparedStatement ps = null;
  360. try {
  361. ps = con.prepareStatement("DELETE FROM users WHERE id_user= ?");
  362. ps.setInt(1, idUser);
  363. return ps.executeUpdate();
  364. } finally {
  365. DBUtils.closeResources(ps);
  366. }
  367. }
  368. /**
  369. * @param con a database connection
  370. * @param idUser
  371. * @param idFoodUp
  372. * @return row count have been removed, 0 otherwise
  373. * @throws SQLException if a database access error occurs or if a restriction is violated
  374. */
  375. public int deleteUserFoodUp(Connection con, int idUser, int idFoodUp) throws SQLException {
  376. PreparedStatement ps = null;
  377. try {
  378. ps = con.prepareStatement("DELETE FROM relation_foodups_users WHERE id_user=? AND id_foodup=?");
  379. ps.setInt(1, idUser);
  380. ps.setInt(2, idFoodUp);
  381. return ps.executeUpdate();
  382. } finally {
  383. DBUtils.closeResources(ps);
  384. }
  385. }
  386. /**
  387. * Delete all allergies of user
  388. * @param con a database connection
  389. * @param idUser
  390. * @param idFoodUp
  391. * @return row count have been removed, 0 otherwise
  392. * @throws SQLException if a database access error occurs or if a restriction is violated
  393. */
  394. public int deleteAllergies(Connection con, int idUser) throws SQLException {
  395. PreparedStatement ps = null;
  396. try {
  397. ps = con.prepareStatement("DELETE FROM users_restriction WHERE id_user=?");
  398. ps.setInt(1, idUser);
  399. return ps.executeUpdate();
  400. } finally {
  401. DBUtils.closeResources(ps);
  402. }
  403. }
  404. }