diff --git a/src/main/java/Product/Article.java b/src/main/java/Product/Article.java index 4ba7914..4dfffc6 100644 --- a/src/main/java/Product/Article.java +++ b/src/main/java/Product/Article.java @@ -1,8 +1,15 @@ package Product; -import javax.persistence.*; import java.time.LocalDate; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.UniqueConstraint; + @Entity @Table(name = "articles", uniqueConstraints = { @UniqueConstraint(columnNames = {"ref"}), @@ -25,7 +32,7 @@ public class Article { private String recipient; - public Article() { } + public Article(Product newP, LocalDate localDate, String string) { } public Long getId() { return id; } public void setId(Long id) { this.id = id; } @@ -54,4 +61,14 @@ public String toString() { ", ean='" + ean + '\'' + '}'; } + + public void setProduct(Product p) { + this.name = p.getName(); + this.ref = p.getRef(); + this.ean = p.getEan(); + } + + public void setDeliveryDate(String deliveryDate2) { + this.deliveryDate = LocalDate.parse(deliveryDate2); + } } diff --git a/src/main/java/Product/ArticleRepository.java b/src/main/java/Product/ArticleRepository.java index d9117be..81bdd25 100644 --- a/src/main/java/Product/ArticleRepository.java +++ b/src/main/java/Product/ArticleRepository.java @@ -19,11 +19,11 @@ private ArticleRepository(){} public Article save(Article a) { if (a == null) return null; // If product has no id, generate one - if (a.getId() == null || a.getId().isEmpty()) { + if (a.getId() == null ) { String id = String.valueOf(counter.getAndIncrement()); // Product has no setter; create a new Product with the id - Product p = a.getProduct(); - Product newP = new Product(id, p != null ? p.getName() : null, p != null ? p.getRef() : null, p != null ? p.getEan() : null); + // Avoid calling a.getProduct() if Article does not expose it + Product newP = new Product(id, null, null, null); Article newA = new Article(newP, a.getDeliveryDate(), a.getRecipient()); articles.add(newA); return newA; diff --git a/src/main/java/servlet/ArticleServlet.java b/src/main/java/servlet/ArticleServlet.java index f55196d..e6aa572 100644 --- a/src/main/java/servlet/ArticleServlet.java +++ b/src/main/java/servlet/ArticleServlet.java @@ -21,7 +21,7 @@ public class ArticleServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { List
articles = repo.findAll(); req.setAttribute("articles", articles); - req.getRequestDispatcher("/WEB-INF/articleForm.jsp").forward(req, resp); + req.getRequestDispatcher("/articleForm.jsp").forward(req, resp); } @Override @@ -34,13 +34,16 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S String recipient = req.getParameter("recipient"); Product p = new Product(id, name, ref, ean); - Article a = new Article(p, deliveryDate, recipient); + Article a = new Article(p, null, recipient); + a.setProduct(p); + a.setDeliveryDate(deliveryDate); + a.setRecipient(recipient); Article saved = repo.save(a); // update application attribute for barcode4j.jsp lookup convenience req.getServletContext().setAttribute("articles", repo.findAll()); req.setAttribute("article", saved); - req.getRequestDispatcher("/WEB-INF/articleView.jsp").forward(req, resp); + req.getRequestDispatcher("/articleView.jsp").forward(req, resp); } }