package com.ibtp.kontor.tradingcards.dal;

import com.ibtp.kontor.dal.BaseImpl;
import com.ibtp.kontor.tradingcards.entity.SportEntity;

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

/**
 *
 * @author tpeetz
 */
public class SportImpl extends BaseImpl implements SportDao {

    @Override
    public SportEntity getById(Long id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

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

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

    @Override
    public Collection<SportEntity> findAll() {
        Query query = getEntityManager().createNamedQuery("Sport.findAll");
        return query.getResultList();
    }

    @Override
    public SportEntity addSport(String name) {
        SportEntity sport = new SportEntity(name);
        store(sport);
        return sport;
    }

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

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