use of com.example.getstarted.objects.Book 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.objects.Book in project getting-started-java by GoogleCloudPlatform.
the class UpdateBookServlet method doPost.
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
BookDao dao = (BookDao) this.getServletContext().getAttribute("dao");
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, getServletContext().getInitParameter("bookshelf.bucket"));
}
}
} catch (FileUploadException e) {
throw new IOException(e);
}
try {
Book oldBook = dao.readBook(Long.decode(params.get("id")));
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).id(Long.decode(params.get("id"))).createdBy(oldBook.getCreatedBy()).createdById(oldBook.getCreatedById()).build();
dao.updateBook(book);
resp.sendRedirect("/read?id=" + params.get("id"));
} catch (Exception e) {
throw new ServletException("Error updating book", e);
}
}
use of com.example.getstarted.objects.Book in project getting-started-java by GoogleCloudPlatform.
the class UpdateBookServlet method doGet.
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
BookDao dao = (BookDao) this.getServletContext().getAttribute("dao");
try {
Book book = dao.readBook(Long.decode(req.getParameter("id")));
req.setAttribute("book", book);
req.setAttribute("action", "Edit");
req.setAttribute("destination", "update");
req.setAttribute("page", "form");
req.getRequestDispatcher("/base.jsp").forward(req, resp);
} catch (Exception e) {
throw new ServletException("Error loading book for editing", e);
}
}
use of com.example.getstarted.objects.Book in project getting-started-java by GoogleCloudPlatform.
the class CloudSqlDao method listBooksByUser.
// [END listbooks]
// [START listbyuser]
@Override
public Result<Book> listBooksByUser(String userId, String startCursor) throws SQLException {
int offset = 0;
if (startCursor != null && !startCursor.equals("")) {
offset = Integer.parseInt(startCursor);
}
final String listBooksString = "SELECT SQL_CALC_FOUND_ROWS author, createdBy, createdById, " + "description, id, publishedDate, title, imageUrl FROM books WHERE createdById = ? " + "ORDER BY title ASC LIMIT 10 OFFSET ?";
try (Connection conn = DriverManager.getConnection(sqlUrl);
PreparedStatement listBooksStmt = conn.prepareStatement(listBooksString)) {
listBooksStmt.setString(1, userId);
listBooksStmt.setInt(2, offset);
List<Book> resultBooks = new ArrayList<>();
try (ResultSet rs = listBooksStmt.executeQuery()) {
while (rs.next()) {
Book book = new Book.Builder().author(rs.getString(Book.AUTHOR)).createdBy(rs.getString(Book.CREATED_BY)).createdById(rs.getString(Book.CREATED_BY_ID)).description(rs.getString(Book.DESCRIPTION)).id(rs.getLong(Book.ID)).publishedDate(rs.getString(Book.PUBLISHED_DATE)).title(rs.getString(Book.TITLE)).imageUrl(rs.getString(Book.IMAGE_URL)).build();
resultBooks.add(book);
}
}
try (ResultSet rs = conn.createStatement().executeQuery("SELECT FOUND_ROWS()")) {
int totalNumRows = 0;
if (rs.next()) {
totalNumRows = rs.getInt(1);
}
if (totalNumRows > offset + 10) {
return new Result<>(resultBooks, Integer.toString(offset + 10));
} else {
return new Result<>(resultBooks);
}
}
}
}
use of com.example.getstarted.objects.Book in project getting-started-java by GoogleCloudPlatform.
the class CloudSqlDao method listBooks.
// [END delete]
// [START listbooks]
@Override
public Result<Book> listBooks(String cursor) throws SQLException {
int offset = 0;
if (cursor != null && !cursor.equals("")) {
offset = Integer.parseInt(cursor);
}
final String listBooksString = "SELECT SQL_CALC_FOUND_ROWS author, createdBy, createdById, " + "description, id, publishedDate, title, imageUrl FROM books5 ORDER BY title ASC " + "LIMIT 10 OFFSET ?";
try (Connection conn = DriverManager.getConnection(sqlUrl);
PreparedStatement listBooksStmt = conn.prepareStatement(listBooksString)) {
listBooksStmt.setInt(1, offset);
List<Book> resultBooks = new ArrayList<>();
try (ResultSet rs = listBooksStmt.executeQuery()) {
while (rs.next()) {
Book book = new Book.Builder().author(rs.getString(Book.AUTHOR)).createdBy(rs.getString(Book.CREATED_BY)).createdById(rs.getString(Book.CREATED_BY_ID)).description(rs.getString(Book.DESCRIPTION)).id(rs.getLong(Book.ID)).publishedDate(rs.getString(Book.PUBLISHED_DATE)).title(rs.getString(Book.TITLE)).imageUrl(rs.getString(Book.IMAGE_URL)).build();
resultBooks.add(book);
}
}
try (ResultSet rs = conn.createStatement().executeQuery("SELECT FOUND_ROWS()")) {
int totalNumRows = 0;
if (rs.next()) {
totalNumRows = rs.getInt(1);
}
if (totalNumRows > offset + 10) {
return new Result<>(resultBooks, Integer.toString(offset + 10));
} else {
return new Result<>(resultBooks);
}
}
}
}
Aggregations