finish him
[facafesta.git] / src / main / java / facafesta / controller / VerItem.java
blobaa123b754e0104c865c7c6e8054734f6a9fdf5c1
1 package facafesta.controller;
3 import java.io.IOException;
5 import javax.persistence.EntityManager;
6 import javax.servlet.ServletException;
7 import javax.servlet.annotation.WebServlet;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
11 import facafesta.Servlet;
12 import facafesta.Util;
13 import facafesta.model.Item;
15 @WebServlet("/item/*")
16 public class VerItem extends Servlet {
18 private static final long serialVersionUID = 1L;
20 @Override
21 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
22 setRequest(req);
23 String[] parts = req.getRequestURI().substring(1).split("/");
24 if (parts.length != 2 || !parts[1].matches("\\d+")) {
25 naoEncontrado(resp);
26 } else {
27 EntityManager manager = Util.getEntityManager();
28 Long idItem = new Long(parts[1]);
29 Item item = (Item) manager.createQuery("from Item where id = :id").setParameter("id", idItem).getSingleResult();
30 if (item == null) {
31 naoEncontrado(resp);
32 } else {
33 req.setAttribute("item", item);
34 getServletContext().getRequestDispatcher("/WEB-INF/item-show.jsp").forward(req, resp);
36 manager.close();
40 private void naoEncontrado(HttpServletResponse resp) throws IOException {
41 addErro("Item não encontrado.");
42 resp.sendRedirect("/itens");