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);
}
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);
}
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");
}
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);
}
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);
}
}
Aggregations