package com.ibtp.kontor.comics.dal;

import com.ibtp.kontor.comics.entity.PublisherEntity;
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 20.01.2015.
 */
public class PublisherImpl extends BaseImpl implements PublisherDao {

    @Override
    public PublisherEntity getById(Long id) {
        return null;
    }

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

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

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

    @Override
    public PublisherEntity addPublisher(String name) {
        PublisherEntity publisher = new PublisherEntity(name);
        store(publisher);
        return publisher;
    }

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

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