package com.ibtp.kontor.tradingcards.dal;

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

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

/**
 * Created by TPEETZ on 27.01.2015.
 */
public class BaseSetImpl extends BaseImpl implements BaseSetDao {

    @Override
    public BaseSetEntity getById(Long id) {
        Query query = getEntityManager().createNamedQuery("BaseSet.findById");
        query.setParameter("id", id);
        return (BaseSetEntity)query.getSingleResult();
    }

    @Override
    public Collection<BaseSetEntity> findByIds(List<Long> ids) {
        return null;
    }

    @Override
    public Collection<BaseSetEntity> findByName(String name) {
        Query query = getEntityManager().createNamedQuery("BaseSet.findByName");
        query.setParameter("name", name);
        return query.getResultList();
    }

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

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