package com.softgraf.vendas.model.repositorio;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import com.softgraf.vendas.model.entity.Item;
import com.softgraf.vendas.model.repositorio.abstrato.AbstractRepositorio;

public class RepositorioItem implements AbstractRepositorio<Item> {

	private static final long serialVersionUID = -6845761984113430944L;
	private EntityManager em;

	public RepositorioItem(EntityManager em) {
		this.em = em;
	}

	@Override
	public boolean adicionar(Item item) {
		EntityTransaction tx = em.getTransaction();
		try {
			
			tx.begin();
			em.persist(item);
			tx.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}

	@Override
	public Item buscarPorId(Integer id) {
		return em.find(Item.class, id);
	}

	@Override
	public boolean atualizar(Item item) {
		EntityTransaction tx = em.getTransaction();
		try {
			
			tx.begin();
			em.merge(item);
			tx.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}

	@Override
	public boolean remover(Item item) {
		EntityTransaction tx = em.getTransaction();
		
		try {
			
			tx.begin();
			em.remove(item);
			tx.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}

	@Override
	public List<Item> listarTodos() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Item> listarTodos(int maximo) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
