package com.peetz.library.service;

import java.util.Collection;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

import com.peetz.library.entity.ArticleEntity;
import com.peetz.library.entity.BookEntity;
import com.peetz.library.entity.BookshelfEntity;
import java.util.ArrayList;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless(name="LibraryService")
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class LibraryServiceImpl implements LibraryService {
    
    @PersistenceContext(unitName = "kontor")
    private EntityManager em;

    @SuppressWarnings("unchecked")
	@Override
    public Collection<BookEntity> getAllBooks() {
        Query query = em.createNamedQuery("Book.findAll");
        ArrayList<BookEntity> bookList = new ArrayList<BookEntity>(query.getResultList());
        return bookList;
    }

    @SuppressWarnings("unchecked")
	@Override
    public Collection<BookshelfEntity> getAllBookshelfs() {
        Query query = em.createNamedQuery("Bookshelf.findAll");
        ArrayList<BookshelfEntity> bookshelfList = new ArrayList<BookshelfEntity>(query.getResultList());
        return bookshelfList;
    }

    @SuppressWarnings("unchecked")
	@Override
    public Collection<ArticleEntity> getAllArticles() {
        Query query = em.createNamedQuery("Article.findAll");
        ArrayList<ArticleEntity> articleList = new ArrayList<ArticleEntity>(query.getResultList());
        return articleList;
    }

    @Override
    public void addBookshelf(String title) {
    }

    @Override
    public void addArticle(String title) {
    }

    @Override
    public BookEntity addBook(String title) {
        return null;
    }

    @Override
    public void saveBook(BookEntity book) {
    }
}
