vendredi 21 mai 2021

Can I call multiple DAOs in the method body of another DAO to fill the parameters by ResultSet and return an object?

I have getBookByResultSet method that creates Book object by resultSet and boolean parameter isDetailedBook (true if we want to display Book in Single page when User click view more button, false if we going to display all Books). And I am using Builder Pattern to set all attributes. Then I can use getBookByResultSet method in other methods like selectAllBooks(), selectBookByID() in this way:

Book book = getMangaByResultSet(resultSet, false); // to get not detailed book

   

public class BookDAOImpl implements BookDAO {

    public Book getBookByResultSet(ResultSet resultSet, Boolean isDetailedBook) throws SQLException {

        PublisherDAO publisherDAO = new PublisherDAOImpl();
        AuthorDAO authorDAO       = new AuthorDAOImpl();
        GenreDAO genreDAO         = new GenreDAOImpl();

        Long id = resultSet.getLong("id");
        String title = resultSet.getString("title");
        String description = resultSet.getString("description");

        // filling book to display in list for DisplayAllBookService
        byte[] coverBytes = coverDAO.selectCoverByID(resultSet.getLong("cover_id"));
        String cover = ImageManager.encodeByteToString(coverBytes);
        Book.Builder manga = new Book.Builder().setID(id)
                .setTitle(title)

        // additional fields filling to display single book DisplaySingleBookService
        if (isDetailedBook) {
            manga.setDescription(description)
                    .setPublisher(publisherDAO.selectPublisherByMangaID(publisherID))
                    .setAuthors(authorDAO.selectAllAuthorsByMangaID(id))
                    .setGenres(genreDAO.selectGenresByMangaLanguageID(id, sessionLanguageID))
        }
        return manga.build();
    }
}

My Service looks like this:

public class DisplayAllBooks implements Service {

@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
    BookDAO bookDAO = new BookDAOImpl();
    List<Book> books = bookDAO.selectAllBooks();

    request.setAttribute(ALL_MANGAS, books);
    RequestDispatcher dispatcher = request.getRequestDispatcher(BOOKS_JSP);
    dispatcher.forward(request, response);
}

The problem is I can not understand how I can get rid of calling another DAOs in BookDAO with Services. Or in my case, can I leave it like this?

Thanks!




Aucun commentaire:

Enregistrer un commentaire