@Override
 @Transactional(propagation = Propagation.REQUIRED)
 public BookEntity getBookById(int bookId) throws ForbiddenException, NoSuchEntityException {
   BookEntity book = bookDao.getNotNull(bookId);
   bookAvailabilityService.isBookAvailable(book);
   return book;
 }
 @Override
 @Transactional
 public Boolean deleteBookToKeyword(int bookId, int authorId)
     throws NoSuchEntityException, ForbiddenException {
   bookAvailabilityService.isCurrentUsersBook(bookDao.getNotNull(bookId));
   return bookKeywordDao.delete(bookId, authorId);
 }
 @Override
 @Transactional
 public Boolean deleteBookToAuthor(int bookId, int authorId)
     throws NoSuchEntityException, ForbiddenException {
   bookAvailabilityService.canDeleteRelationshipToAuthor(bookDao.getNotNull(bookId), authorId);
   return bookAuthorDao.delete(bookId, authorId);
 }
 @Override
 @Transactional(propagation = Propagation.REQUIRED)
 public Boolean deleteBook(int bookId) throws NoSuchEntityException, ForbiddenException {
   BookEntity book = bookDao.getNotNull(bookId);
   bookAvailabilityService.isBookAvailable(book);
   book.setStatus(BookStatusesEnum.DELETED);
   return bookDao.update(book).getStatus().equals(BookStatusesEnum.DELETED);
 }
 @Override
 @Transactional(propagation = Propagation.REQUIRED)
 public List<BookEntity> getBooks(int offset, int limit)
     throws NoSuchEntityException, NotSupportedFieldException, ForbiddenException,
         AuthRequiredException {
   sessionUtils.allowedForUserWithRole(RolesEnum.admin);
   List<BookEntity> books = bookDao.getNotNull(offset, limit);
   return books;
 }
 @Override
 @Transactional
 public boolean changeStatus(int id, BookStatusesEnum status)
     throws NoSuchEntityException, ForbiddenException, WrongStatusException {
   if (status == null) throw new WrongStatusException();
   BookEntity book = bookDao.getNotNull(id);
   bookAvailabilityService.isStatusChangeAvailable(book, status);
   book.setStatus(status);
   if (status == BookStatusesEnum.ACTIVE) book.setDateChanged(new Date());
   book = bookDao.update(book);
   return book != null;
 }
  @Override
  @Transactional
  public List<AuthorEntity> getBookAuthors(int id) throws NoSuchEntityException {
    BookEntity book = bookDao.getNotNull(id);
    List<AuthorEntity> list = Lists.newArrayList();

    if (book.getBookToAuthor() != null) {
      for (BookAuthorEntity b2a : book.getBookToAuthor()) list.add(b2a.getAuthor());
    }

    return list;
  }
 @Override
 @Transactional
 public List<KeywordEntity> getBookKeywords(int bookId) throws NoSuchEntityException {
   BookEntity bookEntity = bookDao.getNotNull(bookId);
   List<KeywordEntity> res = null;
   if ((bookEntity != null)
       && (bookEntity.getBookToKeywords() != null)
       && (bookEntity.getBookToKeywords().size() > 0)) {
     res = new ArrayList<KeywordEntity>();
     for (BookKeywordEntity key : bookEntity.getBookToKeywords()) res.add(key.getKeyword());
   }
   return res;
 }
 @Override
 @Transactional(propagation = Propagation.REQUIRED)
 public Set<BookOnPortalEntity> getBookPortals(int bookId) throws NoSuchEntityException {
   BookEntity book = bookDao.getNotNull(bookId);
   try {
     bookAvailabilityService.isCurrentUsersBook(book);
     return book.getBookPortals();
   } catch (ForbiddenException e) {
     if (book.getBookPortals() == null || book.getBookPortals().size() == 0) return null;
     Set<BookOnPortalEntity> list = new HashSet<BookOnPortalEntity>();
     for (BookOnPortalEntity portal : book.getBookPortals())
       if (BookPortalStatusEnum.ACTIVE.equals(portal.getStatus())) list.add(portal);
     return list;
   }
 }
 @Override
 public int countBookComments(int bookId) throws NoSuchEntityException {
   bookDao.getNotNull(bookId);
   return bookCommentDao.count(bookId).intValue();
 }