package com.peetz.comics.dal;

import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.peetz.comics.entity.ComicEntity;
import com.peetz.comics.entity.PublisherEntity;

@Stateless(name = "ComicDao")
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class ComicImpl implements ComicDao {

    @PersistenceContext(unitName = "kontor")
    private EntityManager em;

    @Override
    public ComicEntity getById(Long id) {
        Query q = em.createNamedQuery("Comic.findById");
	q.setParameter("id", id);
	ComicEntity entity = (ComicEntity)q.getSingleResult();
	return entity;
    }

    @Override
    public List<ComicEntity> findByIds(List<Long> ids) {
		// TODO Auto-generated method stub
		return null;
    }

    @Override
    public List<ComicEntity> findByTitle(String title) {
    	Query q = em.createNamedQuery("Comic.findByTitle");
    	q.setParameter("title", title);
    	@SuppressWarnings("unchecked")
    	List<ComicEntity> resultList = q.getResultList();
    	return resultList;
    }

    @Override
    public ComicEntity assignPublisher(ComicEntity comic,
    		PublisherEntity publisher) {
    	// TODO Auto-generated method stub
    	return null;
    }

    @Override
    public ComicEntity store(ComicEntity entity) {
    	em.persist(entity);
    	return entity;
    }

    @Override
    public void delete(ComicEntity entity) {
    	em.remove(entity);
    }
}
