Advertisement
Guest User

Untitled

a guest
May 27th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.09 KB | None | 0 0
  1. package at.ac.tuwien.sepm.assignment.groupphase.demeter.dao;
  2.  
  3. import at.ac.tuwien.sepm.assignment.groupphase.demeter.dao.InterfacesDAO.IDishDAO;
  4. import at.ac.tuwien.sepm.assignment.groupphase.demeter.dao.InterfacesDAO.IImageManagementDAO;
  5. import at.ac.tuwien.sepm.assignment.groupphase.demeter.dto.Allergen;
  6. import at.ac.tuwien.sepm.assignment.groupphase.demeter.dto.Dish;
  7. import at.ac.tuwien.sepm.assignment.groupphase.demeter.dto.DishSearch;
  8. import at.ac.tuwien.sepm.assignment.groupphase.demeter.dto.Ingredient;
  9. import at.ac.tuwien.sepm.assignment.groupphase.exception.FileException;
  10. import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException;
  11. import at.ac.tuwien.sepm.assignment.groupphase.util.Category;
  12. import at.ac.tuwien.sepm.assignment.groupphase.util.JDBCConnectionManager;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.stereotype.Repository;
  16.  
  17. import java.lang.invoke.MethodHandles;
  18. import java.sql.Connection;
  19. import java.sql.SQLException;
  20. import java.sql.Statement;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23.  
  24. @Repository
  25. public class DishDAO implements IDishDAO {
  26. private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
  27.  
  28. private static final String GET_ALL_DISHES_QUERY = "SELECT * FROM dish WHERE deleted = false";
  29. private static final String GET_ALL_INGREDIENTS_FOR_THIS_DISH = "SELECT * FROM ingredient WHERE id IN (SELECT ingredient_id FROM dish_ingredient WHERE dish_id = ?)";
  30. private static final String ADD_NEW_DISH = "INSERT INTO dish (name, description, price, category, deleted) VALUES (?, ?, ?, ?, ?)";
  31. private static final String ADD_INGREDIENTS_TO_DISHES = "INSERT INTO dish_ingredient (dish_id, ingredient_id) VALUES (?, ?)";
  32. private static final String UPDATE_DISH = "UPDATE dish SET name = ?, description = ?, price = ?, category = ? WHERE id = ? ";
  33. private static final String DELETE_INGREDIENTS_IN_DISH = "DELETE FROM dish_ingredient WHERE dish_id = ?";
  34. private static final String DELETE_DISH = "UPDATE dish SET deleted = true WHERE id = ?";
  35. private static final String GET_ALLERGENS_IN_DISH = "SELECT * FROM allergen WHERE symbol IN (SELECT allergen_symbol FROM dish_allergen WHERE dish_id = ?)";
  36. private static final String ADD_ALLERGENS_TO_DISH = "INSERT INTO dish_allergen (dish_id, allergen_symbol) VALUES (?,?)";
  37. private static final String DELETE_ALLERGENS_IN_DISH = "DELETE FROM dish_allergen WHERE dish_id = ?";
  38. private static final String SEARCH_DISHES = "select dish.id, dish.name, dish.description, dish.price, dish.category FROM dish JOIN (SELECT dish_id, ingredient.name FROM dish_ingredient JOIN ingredient ON ingredient_id = ingredient.id where ingredient.name regexp ?) ing ON dish.id = ing.dish_id where dish.id not in (SELECT dish_id AS id FROM dish_allergen JOIN ALLERGEN ON allergen_symbol = symbol WHERE allergen_symbol regexp ?) group by dish.id having count(*) LIKE ? AND dish.deleted = false AND dish.name ILIKE ? AND dish.price BETWEEN ? AND ?";
  39.  
  40. private IImageManagementDAO imageManagement;
  41. private Connection connection;
  42.  
  43. public DishDAO(JDBCConnectionManager jdbcConnectionManager, IImageManagementDAO imageManagement) throws PersistenceException {
  44. try {
  45. connection = jdbcConnectionManager.getConnection();
  46. this.imageManagement = imageManagement;
  47. } catch (SQLException e) {
  48. LOG.error(e.getMessage());
  49. throw new PersistenceException(e);
  50. }
  51. }
  52.  
  53.  
  54. @Override
  55. public ArrayList<Dish> getAllDishes() throws PersistenceException {
  56. ArrayList<Dish> searchResult = new ArrayList<>();
  57. LOG.info("Retrieving all dishes from the database");
  58. try {
  59. var getAllDishesStatement = connection.prepareStatement(GET_ALL_DISHES_QUERY);
  60. var resultSet = getAllDishesStatement.executeQuery();
  61. while (resultSet.next()){
  62. var dish = new Dish();
  63. dish.setId(resultSet.getLong(1));
  64. dish.setName(resultSet.getString(2));
  65. dish.setDescription(resultSet.getString(3));
  66. dish.setPrice(resultSet.getDouble(4));
  67. dish.setCategory(Category.valueOf(resultSet.getString(5)));
  68. getIngredients(dish);
  69. getAllergensInDish(dish);
  70.  
  71. try {
  72. dish.setImageFile(imageManagement.getImageFile(dish.getId()));
  73. if(dish.getImageFile()==null){
  74. dish.setPathOfImage(null);
  75. }
  76. else {
  77. dish.setPathOfImage(dish.getImageFile().getPath());
  78. }
  79. }catch (FileException e){
  80. dish.setImageFile(null);
  81. }
  82.  
  83. searchResult.add(dish);
  84. }
  85. getAllDishesStatement.close();
  86. resultSet.close();
  87. } catch (SQLException e) {
  88. LOG.error(e.getMessage());
  89. throw new PersistenceException(e);
  90. }
  91. return searchResult;
  92. }
  93.  
  94. @Override
  95. public ArrayList<Dish> searchDishes(DishSearch ds) throws PersistenceException {
  96. ArrayList<Dish> searchResult = new ArrayList<>();
  97. LOG.info("Searching dishes in the database");
  98. try {
  99. var searchStatement = connection.prepareStatement(SEARCH_DISHES);
  100.  
  101. List<Ingredient> ingredients = ds.getIngredients();
  102. String ingNr = "%";
  103. StringBuilder ingRegEx = new StringBuilder(".*");
  104. if(!ingredients.isEmpty()){
  105. ingNr = String.valueOf(ingredients.size());
  106. ingRegEx = new StringBuilder();
  107. for (Ingredient in: ingredients) {
  108. ingRegEx.append(in.getName()).append("|");
  109. }
  110. ingRegEx.deleteCharAt(ingRegEx.length()-1);
  111. }
  112. searchStatement.setString(1,ingRegEx.toString());
  113. searchStatement.setString(3,ingNr);
  114.  
  115. StringBuilder allRegEx = new StringBuilder(" ");
  116. List<Allergen> allergens = ds.getAllergens();
  117. if(!allergens.isEmpty()){
  118. allRegEx = new StringBuilder();
  119. for (Allergen all: allergens) {
  120. allRegEx.append(all.getSymbol()).append("|");
  121. }
  122. allRegEx.deleteCharAt(allRegEx.length()-1);
  123. }
  124. searchStatement.setString(2,allRegEx.toString());
  125.  
  126. searchStatement.setString(4,"%" + ds.getName() + "%");
  127.  
  128. if(ds.getPriceFrom() == null){
  129. searchStatement.setDouble(5,0d);
  130. }else{
  131. searchStatement.setDouble(5,ds.getPriceFrom());
  132. }
  133.  
  134. if(ds.getPriceTo() == null){
  135. searchStatement.setDouble(6,999999d);
  136. }else{
  137. searchStatement.setDouble(6,ds.getPriceTo());
  138. }
  139.  
  140. LOG.debug("Executing query: " + searchStatement.toString());
  141.  
  142. var resultSet = searchStatement.executeQuery();
  143. while (resultSet.next()){
  144. var dish = new Dish();
  145. dish.setId(resultSet.getLong(1));
  146. dish.setName(resultSet.getString(2));
  147. dish.setDescription(resultSet.getString(3));
  148. dish.setPrice(resultSet.getDouble(4));
  149. dish.setCategory(Category.valueOf(resultSet.getString(5)));
  150. getIngredients(dish);
  151. getAllergensInDish(dish);
  152.  
  153. try {
  154. dish.setImageFile(imageManagement.getImageFile(dish.getId()));
  155. if(dish.getImageFile() != null){
  156. dish.setPathOfImage(dish.getImageFile().getPath());
  157. }
  158. }catch (FileException e){
  159. dish.setImageFile(null);
  160. }
  161.  
  162. searchResult.add(dish);
  163. }
  164.  
  165. searchStatement.close();
  166. resultSet.close();
  167.  
  168. } catch (SQLException e) {
  169. LOG.error(e.getMessage());
  170. throw new PersistenceException(e);
  171. }
  172.  
  173. LOG.debug("Returning list: " + searchResult.toString());
  174. return searchResult;
  175. }
  176.  
  177.  
  178. @Override
  179. public ArrayList<Ingredient> getIngredients(Dish dish) throws PersistenceException {
  180. ArrayList<Ingredient> searchResult = new ArrayList<>();
  181. LOG.info("Retrieving all ingredients for this dish");
  182. try {
  183. var getAllIngredientsStatement = connection.prepareStatement(GET_ALL_INGREDIENTS_FOR_THIS_DISH);
  184. getAllIngredientsStatement.setLong(1, dish.getId());
  185. var resultSet = getAllIngredientsStatement.executeQuery();
  186. while (resultSet.next()){
  187. var ingredient = new Ingredient();
  188. ingredient.setId(resultSet.getLong(1));
  189. ingredient.setName(resultSet.getString(2));
  190. ingredient.setDeleted(resultSet.getBoolean(3));
  191. searchResult.add(ingredient);
  192. }
  193. getAllIngredientsStatement.close();
  194. resultSet.close();
  195. }catch (SQLException e){
  196. LOG.error(e.getMessage());
  197. throw new PersistenceException(e);
  198. }
  199. dish.setListOfIngredients(searchResult);
  200. return searchResult;
  201. }
  202.  
  203. @Override
  204. public void addNewDish(Dish dish) throws PersistenceException {
  205. LOG.info("Adding new dish");
  206. try{
  207. var addNewDishStatement = connection.prepareStatement(ADD_NEW_DISH, Statement.RETURN_GENERATED_KEYS);
  208. addNewDishStatement.setString(1, dish.getName());
  209. addNewDishStatement.setString(2, dish.getDescription());
  210. addNewDishStatement.setDouble(3, dish.getPrice());
  211. addNewDishStatement.setString(4, dish.getCategory().name());
  212. addNewDishStatement.setBoolean(5, false);
  213. addNewDishStatement.executeUpdate();
  214.  
  215. var generatedKeys = addNewDishStatement.getGeneratedKeys();
  216. generatedKeys.next();
  217. dish.setId(generatedKeys.getLong(1));
  218.  
  219. if (dish.getImageFile() != null && dish.getImageFile().exists()){
  220. imageManagement.createImageFile(dish.getImageFile(), dish.getId());
  221. }
  222.  
  223. saveDishIngredients(dish);
  224. saveDishAllergens(dish);
  225.  
  226. addNewDishStatement.close();
  227. }catch (SQLException | FileException e){
  228. LOG.error(e.getMessage());
  229. throw new PersistenceException(e);
  230. }
  231. }
  232.  
  233. @Override
  234. public void saveDishAllergens(Dish dish) throws PersistenceException {
  235. try {
  236. var addAllergensToDish = connection.prepareStatement(ADD_ALLERGENS_TO_DISH);
  237. for (var i : dish.getListOfAllergens()) {
  238. addAllergensToDish.setLong(1, dish.getId());
  239. addAllergensToDish.setString(2, i.getSymbol().toString());
  240. addAllergensToDish.executeUpdate();
  241. }
  242. addAllergensToDish.close();
  243. }catch (SQLException e){
  244. LOG.error(e.getMessage());
  245. throw new PersistenceException(e);
  246. }
  247. }
  248.  
  249. @Override
  250. public void saveDishIngredients(Dish dish) throws PersistenceException {
  251. try{
  252. var addIngredientsToDish = connection.prepareStatement(ADD_INGREDIENTS_TO_DISHES);
  253. for (var i : dish.getListOfIngredients()) {
  254. addIngredientsToDish.setLong(1, dish.getId());
  255. addIngredientsToDish.setLong(2, i.getId());
  256. addIngredientsToDish.executeUpdate();
  257. }
  258. addIngredientsToDish.close();
  259. }catch (SQLException e){
  260. LOG.error(e.getMessage());
  261. throw new PersistenceException(e);
  262. }
  263. }
  264.  
  265. @Override
  266. public void deleteDishIngredientsAllergens(Dish dish, String query) throws PersistenceException {
  267. try {
  268. var deleteIngredientsStatement = connection.prepareStatement(query);
  269. deleteIngredientsStatement.setLong(1, dish.getId());
  270. deleteIngredientsStatement.executeUpdate();
  271. deleteIngredientsStatement.close();
  272.  
  273. if (query.equals("UPDATE dish SET deleted = true WHERE id = ?")){
  274. imageManagement.deleteImageFile(dish);
  275. }
  276.  
  277. } catch (SQLException | FileException e) {
  278. LOG.error(e.getMessage());
  279. throw new PersistenceException(e);
  280. }
  281. }
  282.  
  283. @Override
  284. public void updateDish(Dish dish) throws PersistenceException {
  285. LOG.info("Updating dish");
  286. try {
  287. var updateDishStatement = connection.prepareStatement(UPDATE_DISH);
  288. updateDishStatement.setString(1, dish.getName());
  289. updateDishStatement.setString(2, dish.getDescription());
  290. updateDishStatement.setDouble(3, dish.getPrice());
  291. updateDishStatement.setString(4, dish.getCategory().name());
  292. updateDishStatement.setLong(5, dish.getId());
  293.  
  294. updateDishStatement.executeUpdate();
  295. deleteDishIngredientsAllergens(dish, DELETE_INGREDIENTS_IN_DISH);
  296. saveDishIngredients(dish);
  297. deleteDishIngredientsAllergens(dish, DELETE_ALLERGENS_IN_DISH);
  298. saveDishAllergens(dish);
  299.  
  300. if (dish.getPathOfImage() != null) {
  301. imageManagement.deleteImageFile(dish);
  302. if (dish.getImageFile() != null && dish.getImageFile().exists()) {
  303. imageManagement.createImageFile(dish.getImageFile(), dish.getId());
  304. }
  305. }
  306.  
  307. updateDishStatement.close();
  308. } catch (SQLException | FileException e) {
  309. LOG.error(e.getMessage());
  310. throw new PersistenceException(e);
  311. }
  312. }
  313.  
  314. @Override
  315. public void deleteDish(Dish dish) throws PersistenceException {
  316. LOG.info("Deleting dish.");
  317. deleteDishIngredientsAllergens(dish, DELETE_DISH);
  318. deleteDishIngredientsAllergens(dish, DELETE_INGREDIENTS_IN_DISH);
  319. deleteDishIngredientsAllergens(dish, DELETE_ALLERGENS_IN_DISH);
  320. }
  321.  
  322. @Override
  323. public ArrayList<Allergen> getAllergensInDish(Dish dish) throws PersistenceException {
  324. ArrayList<Allergen> searchResult = new ArrayList<>();
  325. try {
  326. var getAllergensInDish = connection.prepareStatement(GET_ALLERGENS_IN_DISH);
  327. getAllergensInDish.setLong(1, dish.getId());
  328. var resultSet = getAllergensInDish.executeQuery();
  329. while (resultSet.next()){
  330. var allergen = new Allergen();
  331. allergen.setName(resultSet.getString(2));
  332. allergen.setSymbol(resultSet.getString(1).charAt(0));
  333. searchResult.add(allergen);
  334. }
  335. getAllergensInDish.close();
  336. }catch (SQLException e){
  337. LOG.error(e.getMessage());
  338. throw new PersistenceException(e);
  339. }
  340. dish.setListOfAllergens(searchResult);
  341. return searchResult;
  342. }
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement