package br.com.senacagil.springmvc.dao;

/**
 * (c) 2007, D George
 * Spring / JPA / Maven Example Code
 * 
 * Demonstrates EJB 3.0 QL
 * 
 * reference: http://www.ibm.com/developerworks/edu/j-dw-java-spring2-i.html
 */
import java.util.List;
import br.com.senacagil.springmvc.model.*;

import org.springframework.orm.jpa.support.JpaDaoSupport;

public class JPACustomerDAO extends JpaDaoSupport implements CustomerDAO {

	public Customer findById(long id) {
		return getJpaTemplate().find(Customer.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<Customer> findAll() {
		return getJpaTemplate().find("select c from Customer c");
	}

	@SuppressWarnings("unchecked")
	public List<Customer> findByAddressStreetName(String street) {
		return getJpaTemplate().find(
				"select c from Customer c where c.addr.street = ?1", street);
	}

	@SuppressWarnings("unchecked")
	public List<Customer> findByCustomerLastName(String lastName) {
		return getJpaTemplate().find(
				"select c from Customer c where c.lastName = ?1", lastName);
	}
	
	@SuppressWarnings("unchecked")
	public List<Customer> findByCustomerPartialName(String partName) {
		return getJpaTemplate().find(
				"select c from Customer c where c.lastName LIKE ?1", partName);
	}

	public Customer save(Customer c) {
		getJpaTemplate().persist(c);
		return c;
	}

	public Customer update(Customer c) {
		return getJpaTemplate().merge(c);
	}

	public void delete(Customer c) {
		getJpaTemplate().remove(c);

	}
}
