package com.ibtp.kontor.comics.dal;

import com.ibtp.kontor.comics.entity.VolumeEntity;
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 VolumeImpl extends BaseImpl implements VolumeDao {

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

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

    @Override
    public Collection<VolumeEntity> findByTitle(String title) {
        Query query = getEntityManager().createNamedQuery("Volume.findByTitle");
        query.setParameter("title", title);
        return query.getResultList();
    }

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

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

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