req.getRequestDispatcher("/index.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
YFDish dish = new YFDish();
ArrayList<YFTheme> themes = new ArrayList<YFTheme>();
boolean isOk = true;
if (req.getParameter("id") != null && !req.getParameter("id").isEmpty()) {
try {
dish = dishService.getDishById(Long.parseLong(req.getParameter("id")));
} catch (Exception e) {
req.setAttribute("idError", "error.");
isOk = false;
}
}
if (req.getParameter("dishCategory") != null && !req.getParameter("dishCategory").isEmpty()) {
try {
dish.setDishCategory(dishCategoryService.getDishCategoryById(Long.parseLong(req.getParameter("dishCategory"))));
} catch (Exception e) {
req.setAttribute("dishCategoriesError", "error.");
isOk = false;
}
}
if (req.getParameterValues("assignedThemes") != null && req.getParameterValues("assignedThemes").length > 0) {
for (String idString : req.getParameterValues("assignedThemes")) {
try {
themes.add(themeService.get(Long.parseLong(idString)));
} catch (Exception e) {
req.setAttribute("themeError", "error");
isOk = false;
break;
}
}
dish.setThemes(themes);
}
if (req.getParameter("name") == null || req.getParameter("name").isEmpty()) {
req.setAttribute("nameError", "may not be empty");
isOk = false;
} else {
dish.setName(req.getParameter("name"));
}
try {
dish.setPrice(Float.parseFloat(req.getParameter("price")));
} catch (Exception e) {
req.setAttribute("priceError", "must be of the form X.Y");
isOk = false;
}
if (isOk) {
dish.setDescription(req.getParameter("description"));
if (dish.getId() != null) {
dishService.updateDish(dish);
} else {
dishService.createDish(dish);
}
resp.sendRedirect(req.getContextPath() + "/administrator/dish/list");