package com.ibtp.kontor.tradingcards.dal;

import com.ibtp.kontor.dal.BaseImpl;
import com.ibtp.kontor.tradingcards.entity.BaseSetEntity;
import com.ibtp.kontor.tradingcards.entity.ManufacturerEntity;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author tpeetz
 */
public class ManufacturerImpl extends BaseImpl implements ManufacturerDao {

    @Override
    public ManufacturerEntity getById(Long id) {
        Query q = getEntityManager().createNamedQuery("Manufacturer.findById");
        q.setParameter("id", id);
        return (ManufacturerEntity)q.getSingleResult();
    }

    @Override
    public List<ManufacturerEntity> findByIds(List<Long> ids) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @SuppressWarnings("unchecked")
	@Override
    public List<ManufacturerEntity> findByName(String name) {
        Query q = getEntityManager().createNamedQuery("Manufacturer.findByName");
        q.setParameter("name", name);
        return q.getResultList();
    }

    @Override
    public ManufacturerEntity assignBaseSet(ManufacturerEntity comic, BaseSetEntity baseSet) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ManufacturerEntity addManufacturer(String name) {
        ManufacturerEntity manufacturer = new ManufacturerEntity(name);
        store(manufacturer);
        return manufacturer;
    }

    @Override
    public ManufacturerEntity store(ManufacturerEntity entity) {
        EntityManager em = getEntityManager();
        em.getTransaction().begin();
        em.persist(entity);
        em.getTransaction().commit();
    	return entity;
    }

    @Override
    public void delete(ManufacturerEntity entity) {
        EntityManager em = getEntityManager();
        em.getTransaction().begin();
        em.remove(entity);
        em.getTransaction().commit();
    }
}
