@Override public boolean save(final Product... products) { for (Product p : products) { if (p == null) { throw new IllegalArgumentException( String.format("Input " + "product cannot be null: %s.", p)); } if (productList.size() == 0) { p.setId(id++); productList.add(p); } else { for (Product d : productList) { if (p.getName().equals(d.getName())) { String errorMessage = String.format( "Product with such" + " name already exists: %s. ID - %d", d.getName(), d.getId()); throw new IllegalArgumentException(errorMessage); } } p.setId(id++); productList.add(p); } LOGGER.info(String.format("*** %s has been saved. ID - %d", p.getName(), p.getId())); } return true; }
@Override public Customer getByLogin(String login) { Session session = null; Transaction transaction = null; Customer customer = null; try { session = sf.openSession(); transaction = session.beginTransaction(); Query query = session.createQuery("from Customer where login = "******":login"); query.setString("login", login); customer = (Customer) query.uniqueResult(); transaction.commit(); } catch (RuntimeException e) { try { transaction.rollback(); } catch (RuntimeException re) { LOGGER.error(ROLLBACK_EXC_MSG, re); } throw e; } finally { if (session != null) { session.close(); } } return customer; }
@Override public Set<Product> getAllSortedByName() { LOGGER.info(String.format("%n*** Getting all products from the list " + "ordered by name ***")); Set<Product> nameSortedProductList = new TreeSet<Product>(new NameSorterComparator()); nameSortedProductList.addAll(productList); return nameSortedProductList; }
@Override public boolean remove(final Customer... customers) { Session session = null; Transaction transaction = null; try { session = sf.openSession(); transaction = session.beginTransaction(); for (Customer c : customers) { session.delete(c); } transaction.commit(); } catch (RuntimeException e) { try { transaction.rollback(); } catch (RuntimeException re) { LOGGER.error(ROLLBACK_EXC_MSG, re); } throw e; } finally { if (session != null) { session.close(); } } return true; }
@Override public boolean removeByIds(final Integer... ids) { Session session = null; Transaction transaction = null; Customer customer; try { session = sf.openSession(); transaction = session.beginTransaction(); for (Integer id : ids) { customer = new Customer(); customer.setId(id); session.delete(customer); } transaction.commit(); } catch (RuntimeException e) { try { transaction.rollback(); } catch (RuntimeException re) { LOGGER.error(ROLLBACK_EXC_MSG, re); } throw e; } finally { if (session != null) { session.close(); } } return true; }
@Override public Customer getById(final Integer id) { Session session = null; Transaction transaction = null; Customer customer; try { session = sf.openSession(); transaction = session.beginTransaction(); Query query = session.createQuery("from Customer where id = :id"); query.setInteger("id", id); customer = (Customer) query.uniqueResult(); transaction.commit(); } catch (RuntimeException e) { try { transaction.rollback(); } catch (RuntimeException re) { LOGGER.error(ROLLBACK_EXC_MSG, re); } throw e; } finally { if (session != null) { session.close(); } } return customer; }
@Override public boolean save(final Customer... customers) { Session session = null; Transaction transaction = null; try { session = sf.openSession(); transaction = session.beginTransaction(); for (Customer c : customers) { c.setPassword(PASSWORD_ENCRYPTOR.getCryptString(c.getPassword())); c.setId((Integer) session.save(c)); } transaction.commit(); } catch (RuntimeException e) { try { transaction.rollback(); } catch (RuntimeException re) { LOGGER.error(ROLLBACK_EXC_MSG, re); } throw e; } finally { if (session != null) { session.close(); } } return true; }
@Override public Set<Customer> getAllSortedByInvoice() { Session session = null; Transaction transaction = null; Set<Customer> customerList = Collections.emptySet(); try { session = sf.openSession(); transaction = session.beginTransaction(); List<Customer> list = session.createQuery("from Customer").list(); customerList = new TreeSet<Customer>(new InvoiceSorterComparator()); customerList.addAll(list); transaction.commit(); } catch (RuntimeException e) { try { transaction.rollback(); } catch (RuntimeException re) { LOGGER.error(ROLLBACK_EXC_MSG, re); } throw e; } finally { if (session != null) { session.close(); } } return customerList; }
@Override public Product getById(final Integer id) { if (id == null || id < 1) { throw new IllegalArgumentException( "ID cannot be null and should " + "be greater than 0. Your ID: " + id); } for (Product p : productList) { if (p.getId().equals(id)) { LOGGER.info( String.format("*** Getting product with ID %d from " + "the list of products ***", id)); return p; } else { throw new IllegalArgumentException("There is no product with " + "such ID: " + id); } } return null; }
@Override public boolean update(final Product... products) { for (Product p : products) { if (p == null) { throw new IllegalArgumentException("Input product cannot be " + "null. Your product: " + p); } for (Product d : productList) { if (p.getId().equals(d.getId())) { LOGGER.info(String.format("*** Updating %s ***", p.getName())); d.setName(p.getName()); d.setDescription(p.getDescription()); d.setPrice(p.getPrice()); } } } return false; }
@Override public Product getByName(final String name) { if (name == null || name.equals("")) { throw new IllegalArgumentException( "Input name cannot be null or " + "blank. Your name: " + name); } boolean isInList = false; for (Product p : productList) { if (p.getName().equals(name)) { LOGGER.info(String.format("*** Getting %s ***", p.getName())); return p; } } if (!isInList) { throw new IllegalArgumentException("There is no product with " + "such name: " + name); } return null; }
@Override public boolean remove(final Product... products) { for (Product p : products) { if (p == null) { throw new IllegalArgumentException("Input product cannot be " + "null. Your product: " + p); } if (productList.contains(p)) { LOGGER.info( String.format( "*** %s has been removed from the " + "list of products ***", p.getName())); productList.remove(p); } else { throw new IllegalArgumentException( "There is no product with " + "such name: " + p.getName()); } } return true; }
@Override public boolean updateAll() { Session session = null; Transaction transaction = null; Set<Customer> customerList = Collections.EMPTY_SET; Customer customer; try { session = sf.openSession(); customerList = getAllSortedById(); transaction = session.beginTransaction(); for (Customer c : customerList) { customer = new Customer(); customer.setId(c.getId()); customer.setFirstName(c.getFirstName()); customer.setLastName(c.getLastName()); customer.setCardNumber(c.getCardNumber()); customer.setInvoice(0.0); customer.setLogin(c.getLogin()); customer.setPassword(c.getPassword()); customer.addProductToShoppingBasket(c.getShoppingBasket()); session.update(customer); } transaction.commit(); } catch (RuntimeException e) { try { transaction.rollback(); } catch (RuntimeException re) { LOGGER.error(ROLLBACK_EXC_MSG, re); } throw e; } finally { if (session != null) { session.close(); } } return true; }
@Override public boolean removeAll() { Session session = null; Transaction transaction = null; try { session = sf.openSession(); transaction = session.beginTransaction(); Query query = session.createQuery("delete from Customer"); query.executeUpdate(); transaction.commit(); } catch (RuntimeException e) { try { transaction.rollback(); } catch (RuntimeException re) { LOGGER.error(ROLLBACK_EXC_MSG, re); } throw e; } finally { if (session != null) { session.close(); } } return true; }
@Override public Set<Product> getAllSortedById() { LOGGER.info(String.format("%n*** Getting all products from the list " + "ordered by ID ***")); return productList; }
@Override public boolean removeAll() { LOGGER.info("*** Removing all products from the list of products ***"); productList.clear(); return true; }