维护入口
This commit is contained in:
@@ -27,6 +27,8 @@ import javax.servlet.http.HttpSession;
|
||||
public class BookManagementServlet extends HttpServlet {
|
||||
private static final String MANAGE_JSP = "/WEB-INF/jsp/books/manage.jsp";
|
||||
private static final String FORM_JSP = "/WEB-INF/jsp/books/form.jsp";
|
||||
private static final String CATEGORY_MANAGE_JSP = "/WEB-INF/jsp/books/categories.jsp";
|
||||
private static final String CATEGORY_FORM_JSP = "/WEB-INF/jsp/books/category-form.jsp";
|
||||
private static final String UNAUTHORIZED_JSP = "/WEB-INF/jsp/auth/unauthorized.jsp";
|
||||
private static final String FLASH_SUCCESS = "flashSuccess";
|
||||
private static final String FLASH_ERROR = "flashError";
|
||||
@@ -50,6 +52,19 @@ public class BookManagementServlet extends HttpServlet {
|
||||
showEditForm(request, response);
|
||||
return;
|
||||
}
|
||||
if ("/book-categories".equals(path)) {
|
||||
showCategoryList(request, response);
|
||||
return;
|
||||
}
|
||||
if ("/book-categories/new".equals(path)) {
|
||||
renderCategoryForm(request, response, "Create category", "/book-categories", new BookCategory(),
|
||||
Collections.emptyMap(), Collections.emptyMap(), null);
|
||||
return;
|
||||
}
|
||||
if ("/book-categories/edit".equals(path)) {
|
||||
showEditCategoryForm(request, response);
|
||||
return;
|
||||
}
|
||||
if (!"/books".equals(path)) {
|
||||
response.sendError(HttpServletResponse.SC_NOT_FOUND);
|
||||
return;
|
||||
@@ -73,6 +88,18 @@ public class BookManagementServlet extends HttpServlet {
|
||||
deleteBook(request, response);
|
||||
return;
|
||||
}
|
||||
if ("/book-categories".equals(path)) {
|
||||
createCategory(request, response);
|
||||
return;
|
||||
}
|
||||
if ("/book-categories/update".equals(path)) {
|
||||
updateCategory(request, response);
|
||||
return;
|
||||
}
|
||||
if ("/book-categories/delete".equals(path)) {
|
||||
deleteCategory(request, response);
|
||||
return;
|
||||
}
|
||||
|
||||
response.sendError(HttpServletResponse.SC_NOT_FOUND);
|
||||
}
|
||||
@@ -116,6 +143,32 @@ public class BookManagementServlet extends HttpServlet {
|
||||
Collections.emptyMap(), Collections.emptyMap(), null);
|
||||
}
|
||||
|
||||
private void showCategoryList(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
applyFlash(request);
|
||||
|
||||
ServiceResult<List<BookCategory>> result = bookService.listCategories();
|
||||
request.setAttribute("categories", result.isSuccessful() ? result.getData() : Collections.emptyList());
|
||||
if (!result.isSuccessful()) {
|
||||
request.setAttribute("errorMessage", result.getMessage());
|
||||
}
|
||||
request.getRequestDispatcher(CATEGORY_MANAGE_JSP).forward(request, response);
|
||||
}
|
||||
|
||||
private void showEditCategoryForm(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
long id = requiredLong(request.getParameter("id"), -1L);
|
||||
ServiceResult<Optional<BookCategory>> result = bookService.findCategory(id);
|
||||
if (!result.isSuccessful() || !result.getData().isPresent()) {
|
||||
flashError(request, result.isSuccessful() ? "Category was not found." : result.getMessage());
|
||||
response.sendRedirect(request.getContextPath() + "/book-categories");
|
||||
return;
|
||||
}
|
||||
|
||||
renderCategoryForm(request, response, "Edit category", "/book-categories/update", result.getData().get(),
|
||||
Collections.emptyMap(), Collections.emptyMap(), null);
|
||||
}
|
||||
|
||||
private void createBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
BookForm form = readBookForm(request, false);
|
||||
if (!form.getErrors().isEmpty()) {
|
||||
@@ -167,6 +220,60 @@ public class BookManagementServlet extends HttpServlet {
|
||||
response.sendRedirect(request.getContextPath() + "/books");
|
||||
}
|
||||
|
||||
private void createCategory(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
CategoryForm form = readCategoryForm(request, false);
|
||||
if (!form.getErrors().isEmpty()) {
|
||||
renderCategoryForm(request, response, "Create category", "/book-categories", form.getCategory(),
|
||||
form.getValues(), form.getErrors(), "Please correct the highlighted category fields.");
|
||||
return;
|
||||
}
|
||||
|
||||
ServiceResult<Long> result = bookService.createCategory(currentUser(request), form.getCategory());
|
||||
if (!result.isSuccessful()) {
|
||||
handleCategoryFormFailure(request, response, "Create category", "/book-categories", form, result);
|
||||
return;
|
||||
}
|
||||
|
||||
flashSuccess(request, result.getMessage());
|
||||
response.sendRedirect(request.getContextPath() + "/book-categories");
|
||||
}
|
||||
|
||||
private void updateCategory(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
CategoryForm form = readCategoryForm(request, true);
|
||||
if (!form.getErrors().isEmpty()) {
|
||||
renderCategoryForm(request, response, "Edit category", "/book-categories/update", form.getCategory(),
|
||||
form.getValues(), form.getErrors(), "Please correct the highlighted category fields.");
|
||||
return;
|
||||
}
|
||||
|
||||
ServiceResult<Void> result = bookService.updateCategory(currentUser(request), form.getCategory());
|
||||
if (!result.isSuccessful()) {
|
||||
handleCategoryFormFailure(request, response, "Edit category", "/book-categories/update", form, result);
|
||||
return;
|
||||
}
|
||||
|
||||
flashSuccess(request, result.getMessage());
|
||||
response.sendRedirect(request.getContextPath() + "/book-categories");
|
||||
}
|
||||
|
||||
private void deleteCategory(HttpServletRequest request, HttpServletResponse response)
|
||||
throws IOException, ServletException {
|
||||
long id = requiredLong(request.getParameter("id"), -1L);
|
||||
ServiceResult<Void> result = bookService.deleteCategory(currentUser(request), id);
|
||||
if (isPermissionDenied(result)) {
|
||||
forwardDenied(request, response, result.getMessage());
|
||||
return;
|
||||
}
|
||||
if (result.isSuccessful()) {
|
||||
flashSuccess(request, result.getMessage());
|
||||
} else {
|
||||
flashError(request, result.getMessage());
|
||||
}
|
||||
response.sendRedirect(request.getContextPath() + "/book-categories");
|
||||
}
|
||||
|
||||
private void handleFormFailure(HttpServletRequest request, HttpServletResponse response, String title,
|
||||
String action, BookForm form, ServiceResult<?> result)
|
||||
throws ServletException, IOException {
|
||||
@@ -178,6 +285,17 @@ public class BookManagementServlet extends HttpServlet {
|
||||
result.getMessage());
|
||||
}
|
||||
|
||||
private void handleCategoryFormFailure(HttpServletRequest request, HttpServletResponse response, String title,
|
||||
String action, CategoryForm form, ServiceResult<?> result)
|
||||
throws ServletException, IOException {
|
||||
if (isPermissionDenied(result)) {
|
||||
forwardDenied(request, response, result.getMessage());
|
||||
return;
|
||||
}
|
||||
renderCategoryForm(request, response, title, action, form.getCategory(), form.getValues(),
|
||||
result.getErrors(), result.getMessage());
|
||||
}
|
||||
|
||||
private void renderForm(HttpServletRequest request, HttpServletResponse response, String title, String action,
|
||||
Book book, Map<String, String> formValues, Map<String, String> errors, String errorMessage)
|
||||
throws ServletException, IOException {
|
||||
@@ -199,6 +317,21 @@ public class BookManagementServlet extends HttpServlet {
|
||||
request.getRequestDispatcher(FORM_JSP).forward(request, response);
|
||||
}
|
||||
|
||||
private void renderCategoryForm(HttpServletRequest request, HttpServletResponse response, String title,
|
||||
String action, BookCategory category, Map<String, String> formValues,
|
||||
Map<String, String> errors, String errorMessage)
|
||||
throws ServletException, IOException {
|
||||
request.setAttribute("formTitle", title);
|
||||
request.setAttribute("formAction", action);
|
||||
request.setAttribute("category", category);
|
||||
request.setAttribute("formValues", formValues);
|
||||
request.setAttribute("errors", errors);
|
||||
if (errorMessage != null && !errorMessage.isEmpty()) {
|
||||
request.setAttribute("errorMessage", errorMessage);
|
||||
}
|
||||
request.getRequestDispatcher(CATEGORY_FORM_JSP).forward(request, response);
|
||||
}
|
||||
|
||||
private BookForm readBookForm(HttpServletRequest request, boolean requireId) {
|
||||
Map<String, String> values = formValues(request);
|
||||
Map<String, String> errors = new LinkedHashMap<>();
|
||||
@@ -237,6 +370,27 @@ public class BookManagementServlet extends HttpServlet {
|
||||
return values;
|
||||
}
|
||||
|
||||
private CategoryForm readCategoryForm(HttpServletRequest request, boolean requireId) {
|
||||
Map<String, String> values = categoryFormValues(request);
|
||||
Map<String, String> errors = new LinkedHashMap<>();
|
||||
BookCategory category = new BookCategory();
|
||||
|
||||
if (requireId) {
|
||||
category.setId(parseLong(values.get("id"), "id", "Select a valid category.", errors));
|
||||
}
|
||||
category.setName(values.get("name"));
|
||||
category.setDescription(values.get("description"));
|
||||
return new CategoryForm(category, values, errors);
|
||||
}
|
||||
|
||||
private Map<String, String> categoryFormValues(HttpServletRequest request) {
|
||||
Map<String, String> values = new LinkedHashMap<>();
|
||||
values.put("id", trim(request.getParameter("id")));
|
||||
values.put("name", trim(request.getParameter("name")));
|
||||
values.put("description", trim(request.getParameter("description")));
|
||||
return values;
|
||||
}
|
||||
|
||||
private BookSearchCriteria searchCriteria(HttpServletRequest request) {
|
||||
return new BookSearchCriteria(
|
||||
request.getParameter("identifier"),
|
||||
@@ -368,4 +522,28 @@ public class BookManagementServlet extends HttpServlet {
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class CategoryForm {
|
||||
private final BookCategory category;
|
||||
private final Map<String, String> values;
|
||||
private final Map<String, String> errors;
|
||||
|
||||
private CategoryForm(BookCategory category, Map<String, String> values, Map<String, String> errors) {
|
||||
this.category = category;
|
||||
this.values = values;
|
||||
this.errors = errors;
|
||||
}
|
||||
|
||||
private BookCategory getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
private Map<String, String> getValues() {
|
||||
return values;
|
||||
}
|
||||
|
||||
private Map<String, String> getErrors() {
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user