@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 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 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; }