package com.javahonk.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.javahonk.model.Person;

@Transactional
public class PersonDAOImpl implements PersonDAO{
	
	SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public void insertPerson(Person person) {
		Session session = sessionFactory.getCurrentSession();
		session.save(person);
		
	}	

	public void updatePerson(Integer personId) {
		
		Person person = new Person();
		person.setId(personId);
		person.setCity("NY");
		person.setLastName("Honk");
		person.setState("NY");
		person.setZip("12345");	
		person.setFirstName("New Update Java");	
		this.sessionFactory.getCurrentSession().update(person);		
		
	}
	
	public List<Person> findAllPerson() {
		List<Person> list = (List<Person>)this.sessionFactory
				.getCurrentSession().createQuery("from Person")
				.list();		
		return list;
		
	}
	
	public void deletePerson(Integer personId) {
		
		Person person2 = (Person) sessionFactory.getCurrentSession()
				.get(Person.class, personId);
        if (null != person2) {
            this.sessionFactory.getCurrentSession().delete(person2);
        }
		
	}	

}
