Search in sources :

Example 1 with BookDao

use of com.example.getstarted.daos.BookDao in project getting-started-java by GoogleCloudPlatform.

the class ListBookServlet method doGet.

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    BookDao dao = (BookDao) this.getServletContext().getAttribute("dao");
    String startCursor = req.getParameter("cursor");
    List<Book> books = null;
    String endCursor = null;
    try {
        Result<Book> result = dao.listBooks(startCursor);
        logger.log(Level.INFO, "Retrieved list of all books");
        books = result.getResult();
        endCursor = result.getCursor();
    } catch (Exception e) {
        throw new ServletException("Error listing books", e);
    }
    req.getSession().getServletContext().setAttribute("books", books);
    StringBuilder bookNames = new StringBuilder();
    for (Book book : books) {
        bookNames.append(book.getTitle()).append(" ");
    }
    logger.log(Level.INFO, "Loaded books: " + bookNames.toString());
    req.setAttribute("cursor", endCursor);
    req.setAttribute("page", "list");
    req.getRequestDispatcher("/base.jsp").forward(req, resp);
}
Also used : ServletException(javax.servlet.ServletException) Book(com.example.getstarted.objects.Book) BookDao(com.example.getstarted.daos.BookDao) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 2 with BookDao

use of com.example.getstarted.daos.BookDao in project getting-started-java by GoogleCloudPlatform.

the class ReadBookServlet method doGet.

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String id = req.getParameter("id");
    BookDao dao = (BookDao) this.getServletContext().getAttribute("dao");
    Book book = dao.readBook(id);
    logger.log(Level.INFO, "Read book with id {0}", id);
    req.setAttribute("book", book);
    req.setAttribute("page", "view");
    req.getRequestDispatcher("/base.jsp").forward(req, resp);
}
Also used : Book(com.example.getstarted.objects.Book) BookDao(com.example.getstarted.daos.BookDao)

Example 3 with BookDao

use of com.example.getstarted.daos.BookDao in project getting-started-java by GoogleCloudPlatform.

the class DeleteBookServlet method doGet.

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String id = req.getParameter("id");
    BookDao dao = (BookDao) this.getServletContext().getAttribute("dao");
    dao.deleteBook(id);
    resp.sendRedirect("/books");
}
Also used : BookDao(com.example.getstarted.daos.BookDao)

Example 4 with BookDao

use of com.example.getstarted.daos.BookDao in project getting-started-java by GoogleCloudPlatform.

the class CreateBookServlet method doPost.

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    assert ServletFileUpload.isMultipartContent(req);
    CloudStorageHelper storageHelper = (CloudStorageHelper) getServletContext().getAttribute("storageHelper");
    String newImageUrl = null;
    Map<String, String> params = new HashMap<String, String>();
    try {
        FileItemIterator iter = new ServletFileUpload().getItemIterator(req);
        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            if (item.isFormField()) {
                params.put(item.getFieldName(), Streams.asString(item.openStream()));
            } else if (!Strings.isNullOrEmpty(item.getName())) {
                newImageUrl = storageHelper.uploadFile(item, System.getenv("BOOKSHELF_BUCKET"));
            }
        }
    } catch (FileUploadException e) {
        throw new IOException(e);
    }
    String createdByString = "";
    String createdByIdString = "";
    HttpSession session = req.getSession();
    if (session.getAttribute("userEmail") != null) {
        // Does the user have a logged in session?
        createdByString = (String) session.getAttribute("userEmail");
        createdByIdString = (String) session.getAttribute("userId");
    }
    Book book = new Book.Builder().author(params.get("author")).description(params.get("description")).publishedDate(params.get("publishedDate")).title(params.get("title")).imageUrl(null == newImageUrl ? params.get("imageUrl") : newImageUrl).createdBy(createdByString).createdById(createdByIdString).build();
    BookDao dao = (BookDao) this.getServletContext().getAttribute("dao");
    String id = dao.createBook(book);
    logger.log(Level.INFO, "Created book {0}", book);
    resp.sendRedirect("/read?id=" + id);
}
Also used : CloudStorageHelper(com.example.getstarted.util.CloudStorageHelper) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) IOException(java.io.IOException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) Book(com.example.getstarted.objects.Book) BookDao(com.example.getstarted.daos.BookDao) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 5 with BookDao

use of com.example.getstarted.daos.BookDao in project getting-started-java by GoogleCloudPlatform.

the class BookshelfContextListener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent event) {
    // This function is called when the application starts and will safely set a few required
    // context attributes such as the BookDao.
    BookDao dao = (BookDao) event.getServletContext().getAttribute("dao");
    if (dao == null) {
        dao = new FirestoreDao();
        event.getServletContext().setAttribute("dao", dao);
    }
    Boolean isCloudStorageConfigured = (Boolean) event.getServletContext().getAttribute("isCloudStorageConfigured");
    if (isCloudStorageConfigured == null) {
        event.getServletContext().setAttribute("isCloudStorageConfigured", !Strings.isNullOrEmpty(System.getenv("BOOKSHELF_BUCKET")));
    }
    CloudStorageHelper storageHelper = (CloudStorageHelper) event.getServletContext().getAttribute("storageHelper");
    if (storageHelper == null) {
        storageHelper = new CloudStorageHelper();
        event.getServletContext().setAttribute("storageHelper", storageHelper);
    }
}
Also used : FirestoreDao(com.example.getstarted.daos.FirestoreDao) BookDao(com.example.getstarted.daos.BookDao)

Aggregations

BookDao (com.example.getstarted.daos.BookDao)9 Book (com.example.getstarted.objects.Book)6 IOException (java.io.IOException)5 ServletException (javax.servlet.ServletException)5 CloudStorageHelper (com.example.getstarted.util.CloudStorageHelper)3 FileUploadException (org.apache.commons.fileupload.FileUploadException)3 HashMap (java.util.HashMap)2 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)2 FileItemStream (org.apache.commons.fileupload.FileItemStream)2 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)2 CloudSqlDao (com.example.getstarted.daos.CloudSqlDao)1 DatastoreDao (com.example.getstarted.daos.DatastoreDao)1 FirestoreDao (com.example.getstarted.daos.FirestoreDao)1 SQLException (java.sql.SQLException)1 HttpSession (javax.servlet.http.HttpSession)1