Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/main/java/Product/Article.java
Original file line number Diff line number Diff line change
@@ -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"}),
Expand All @@ -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; }
Expand Down Expand Up @@ -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);
}
}
6 changes: 3 additions & 3 deletions src/main/java/Product/ArticleRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/servlet/ArticleServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ArticleServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Article> articles = repo.findAll();
req.setAttribute("articles", articles);
req.getRequestDispatcher("/WEB-INF/articleForm.jsp").forward(req, resp);
req.getRequestDispatcher("/articleForm.jsp").forward(req, resp);
}

@Override
Expand All @@ -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);
}
}
Loading