package com.ibtp.kontor.comics.dal;

import com.ibtp.kontor.comics.entity.IssueEntity;
import com.ibtp.kontor.dal.BaseImpl;

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

/**
 * Created by TPEETZ on 28.01.2015.
 */
public class IssueImpl extends BaseImpl implements IssueDao {

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

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

    @Override
    public Collection<IssueEntity> findByNumber(String number) {
        Query query = getEntityManager().createNamedQuery("Issue.findByNumber");
        query.setParameter("number", number);
        return query.getResultList();
    }

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

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

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