package org.sistema.test.Test.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.sistema.test.Test.HibernateSession;
import org.sistema.test.Test.dao.CustomerDAO;
import org.sistema.test.Test.model.Customer;

/**
 * Hibernate specific Customer DAO
 * 
 * @author Eugenia Pérez Martínez
 *
 */
public class HibernateCustomerDAO implements CustomerDAO {
	/*
	 * selects one customer by Id
	 * 
	 * @param id
	 * 
	 * @return Customer
	 */
	public Customer selectById(Long id) {
		SessionFactory sessionFactory = HibernateSession.getSessionFactory();
		Session session = sessionFactory.openSession();
		Customer customer = (Customer) session.get(Customer.class, id);
		session.close();
		return customer;
	}

	/*
	 * retrieves all customers from db
	 * 
	 * @return List of customers
	 */
	public List<Customer> selectAll() {
		SessionFactory sessionFactory = HibernateSession.getSessionFactory();
		Session session = sessionFactory.openSession();
		List<Customer> customers = session.createCriteria(Customer.class).list();
		session.close();
		return customers;
	}

	/*
	 * inserts a new customer in database retrieves generated id and sets to
	 * customer instance
	 * 
	 * @param new customer
	 */
	public void insert(Customer customer) {
		SessionFactory sessionFactory = HibernateSession.getSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Long id = (Long) session.save(customer);
		customer.setId(id);
		session.getTransaction().commit();
		session.close();
	}

	/*
	 * updates customer
	 * 
	 * @param customer to update
	 */
	public void update(Customer customer) {
		SessionFactory sessionFactory = HibernateSession.getSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.merge(customer);
		session.getTransaction().commit();
		session.close();
	}

	/*
	 * delete given customer
	 * 
	 * @param customer to delete
	 */
	public void delete(Customer customer) {
		SessionFactory sessionFactory = HibernateSession.getSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.delete(customer);
		session.getTransaction().commit();
		session.close();
	}
}
