From 6d6a1c9b2a0468d425789ae8d17f033f6c9f5059 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 5 Jun 2019 18:41:59 +0200 Subject: [PATCH] ordering process done Signed-off-by: Johannes Theiner --- pom.xml | 7 --- src/main/java/de/hsel/itech/db/AuthorDB.java | 5 +- src/main/java/de/hsel/itech/db/BookDB.java | 36 +++++++----- .../java/de/hsel/itech/db/PublisherDB.java | 5 +- .../java/de/hsel/itech/jsf/AddressBean.java | 4 +- src/main/java/de/hsel/itech/jsf/CartBean.java | 58 +++++++++++-------- src/main/webapp/addressSelection.xhtml | 16 ++--- src/main/webapp/cart.xhtml | 10 ++-- src/main/webapp/confirmation.xhtml | 21 ++++--- src/main/webapp/paymentSelection.xhtml | 38 ++++++------ 10 files changed, 114 insertions(+), 86 deletions(-) diff --git a/pom.xml b/pom.xml index dab145f..b2c9ace 100644 --- a/pom.xml +++ b/pom.xml @@ -180,13 +180,6 @@ 5.4.0 test - - - - javaee - javaee-api - 5 - diff --git a/src/main/java/de/hsel/itech/db/AuthorDB.java b/src/main/java/de/hsel/itech/db/AuthorDB.java index 2e4bcb0..972ae0c 100644 --- a/src/main/java/de/hsel/itech/db/AuthorDB.java +++ b/src/main/java/de/hsel/itech/db/AuthorDB.java @@ -40,7 +40,10 @@ public class AuthorDB { ResultSet rs = entry.getKey(); Author author = null; try { - author = new Author(rs.getLong("id"), rs.getString("name")); + rs.beforeFirst(); + while(rs.next()) { + author = new Author(rs.getLong("id"), rs.getString("name")); + } entry.getValue().close(); } catch (SQLException e) { e.printStackTrace(); diff --git a/src/main/java/de/hsel/itech/db/BookDB.java b/src/main/java/de/hsel/itech/db/BookDB.java index 27d4fd3..e92b6d0 100644 --- a/src/main/java/de/hsel/itech/db/BookDB.java +++ b/src/main/java/de/hsel/itech/db/BookDB.java @@ -35,6 +35,11 @@ public class BookDB { public Book get(long id) { Map.Entry entry = database.getResultSetById(database.tableBook, id); assert entry != null; + try { + entry.getKey().beforeFirst(); + } catch (SQLException e) { + e.printStackTrace(); + } return build(entry); } @@ -50,22 +55,25 @@ public class BookDB { Book book = null; ResultSet rs = entry.getKey(); try { - Category category = database.category().get(rs.getLong(database.tableCategory)); - Publisher publisher = database.publisher().get(rs.getLong(database.tablePublisher)); - List authorIds = database.author().getAll(rs.getLong("id")); - assert category != null; - assert publisher != null; - assert authorIds != null; + rs.beforeFirst(); + while(rs.next()) { + Category category = database.category().get(rs.getLong(database.tableCategory)); + Publisher publisher = database.publisher().get(rs.getLong(database.tablePublisher)); + List authorIds = database.author().getAll(rs.getLong("id")); + assert category != null; + assert publisher != null; + assert authorIds != null; - List authors = new ArrayList<>(); - for (int i : authorIds) { - Author author = database.author().get(i); - assert author != null; - authors.add(author); + List authors = new ArrayList<>(); + for (int i : authorIds) { + Author author = database.author().get(i); + assert author != null; + authors.add(author); + } + book = new Book(rs.getLong("id"), rs.getLong("isbn"), authors, publisher, category, rs.getString("title"), + Year.of(rs.getInt("year")), rs.getInt("price"), rs.getString("description"), + rs.getString("image")); } - book = new Book(rs.getLong("id"), rs.getLong("isbn"), authors, publisher, category, rs.getString("title"), - Year.of(rs.getInt("year")), rs.getInt("price"), rs.getString("description"), - rs.getString("image")); entry.getValue().close(); } catch (SQLException e) { diff --git a/src/main/java/de/hsel/itech/db/PublisherDB.java b/src/main/java/de/hsel/itech/db/PublisherDB.java index bc52179..e250c46 100644 --- a/src/main/java/de/hsel/itech/db/PublisherDB.java +++ b/src/main/java/de/hsel/itech/db/PublisherDB.java @@ -37,7 +37,10 @@ public class PublisherDB { ResultSet rs = entry.getKey(); Publisher publisher = null; try { - publisher = new Publisher(rs.getLong("id"), rs.getString("name")); + rs.beforeFirst(); + while(rs.next()) { + publisher = new Publisher(rs.getLong("id"), rs.getString("name")); + } entry.getValue().close(); } catch (SQLException e) { e.printStackTrace(); diff --git a/src/main/java/de/hsel/itech/jsf/AddressBean.java b/src/main/java/de/hsel/itech/jsf/AddressBean.java index 98a41a8..c8e33f2 100644 --- a/src/main/java/de/hsel/itech/jsf/AddressBean.java +++ b/src/main/java/de/hsel/itech/jsf/AddressBean.java @@ -7,7 +7,7 @@ import lombok.Getter; import lombok.Setter; import javax.faces.bean.ManagedBean; -import javax.faces.bean.RequestScoped; +import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; import java.io.Serializable; import java.util.ArrayList; @@ -19,7 +19,7 @@ import java.util.List; * @version 1.0 */ @ManagedBean -@RequestScoped +@SessionScoped public class AddressBean implements Serializable { public User getUser() { diff --git a/src/main/java/de/hsel/itech/jsf/CartBean.java b/src/main/java/de/hsel/itech/jsf/CartBean.java index 2b4a3d9..47d6d65 100644 --- a/src/main/java/de/hsel/itech/jsf/CartBean.java +++ b/src/main/java/de/hsel/itech/jsf/CartBean.java @@ -1,16 +1,14 @@ package de.hsel.itech.jsf; import de.hsel.itech.db.Database; -import de.hsel.itech.db.pojo.Book; -import de.hsel.itech.db.pojo.OrderItem; -import de.hsel.itech.db.pojo.ShoppingCartItem; -import de.hsel.itech.db.pojo.User; +import de.hsel.itech.db.pojo.*; import lombok.Setter; import javax.faces.annotation.ManagedProperty; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; +import java.sql.Date; import java.util.ArrayList; import java.util.List; @@ -24,36 +22,46 @@ public class CartBean { public User getUser() { FacesContext context = FacesContext.getCurrentInstance(); - return (User) context.getApplication().evaluateExpressionGet(context,"#{userBean.user}", User.class); + return (User) context.getApplication().evaluateExpressionGet(context, "#{userBean.user}", User.class); } - public String add(Book book){ - if(getUser() != null){ - for(ShoppingCartItem item : getItems()){ - if(item.getArticle().getId() == book.getId()){ + public Payment getPayment () { + FacesContext context = FacesContext.getCurrentInstance(); + return (Payment) context.getApplication().evaluateExpressionGet(context, "#{paymentBean.selected}", Payment.class); + } + + public Address getAddress() { + FacesContext context = FacesContext.getCurrentInstance(); + return (Address) context.getApplication().evaluateExpressionGet(context, "#{addressBean.selected}", Address.class); + } + + public String add(Book book) { + if (getUser() != null) { + for (ShoppingCartItem item : getItems()) { + if (item.getArticle().getId() == book.getId()) { Database.getInstance().shoppingCart().delete(item); item.setCount(item.getCount() + 1); Database.getInstance().shoppingCart().insert(item); return "cart.jsf"; } } - Database.getInstance().shoppingCart().insert(new ShoppingCartItem(getUser(),book,1)); + Database.getInstance().shoppingCart().insert(new ShoppingCartItem(getUser(), book, 1)); } return "cart.jsf"; } - public String remove(ShoppingCartItem item){ - if(getUser() != null){ + public String remove(ShoppingCartItem item) { + if (getUser() != null) { Database.getInstance().shoppingCart().delete(item); } return "cart.jsf"; } - public List getItems(){ + public List getItems() { Database db = Database.getInstance(); - if(getUser()!=null){ + if (getUser() != null) { return db.shoppingCart().get(getUser()); - }else{ + } else { return new ArrayList(); } } @@ -63,31 +71,31 @@ public class CartBean { }*/ - public String buy(){ + public String buy() { ArrayList orderList = new ArrayList<>(); - for(ShoppingCartItem item : getItems()){ - orderList.add(new OrderItem(item.getArticle(),item.getCount())); + for (ShoppingCartItem item : getItems()) { + orderList.add(new OrderItem(item.getArticle(), item.getCount())); } - //Database.getInstance().order().insert(new Order(getUser(),orderList, new Date(234235235),sum(),null,null)); - for(ShoppingCartItem item : getItems()) { + Database.getInstance().order().insert(new Order(getUser(), orderList, new Date(System.currentTimeMillis()), sum(), getPayment(), getAddress())); + for (ShoppingCartItem item : getItems()) { Database.getInstance().shoppingCart().delete(item); } - return "thanks.jsp"; + return "index.jsf"; } - public int getItemCount(){ + public int getItemCount() { return getItems().size(); } - public long sum(){ + public long sum() { int sum = 0; - for(ShoppingCartItem item : getItems()){ + for (ShoppingCartItem item : getItems()) { sum += item.getCount() * item.getArticle().getPrice(); } return sum; } - public String getSum(){ + public String getSum() { long sum = sum(); String res = ""; res += sum / 100; diff --git a/src/main/webapp/addressSelection.xhtml b/src/main/webapp/addressSelection.xhtml index a176760..54e11d5 100644 --- a/src/main/webapp/addressSelection.xhtml +++ b/src/main/webapp/addressSelection.xhtml @@ -16,13 +16,15 @@ - -
- #{address.name}
- #{address.street} #{address.number}
- #{address.zipCode} #{address.city} -
-
+ + +
+ #{address.name}
+ #{address.street} #{address.number}
+ #{address.zipCode} #{address.city} +
+
+
diff --git a/src/main/webapp/cart.xhtml b/src/main/webapp/cart.xhtml index 58d205b..4aeb477 100644 --- a/src/main/webapp/cart.xhtml +++ b/src/main/webapp/cart.xhtml @@ -45,22 +45,22 @@ - +

Summe(#{cartBean.itemCount} Items): #{cartBean.sum}€

- +

Der Warenkorb ist Leer

- +
diff --git a/src/main/webapp/confirmation.xhtml b/src/main/webapp/confirmation.xhtml index 9801818..9cd7495 100644 --- a/src/main/webapp/confirmation.xhtml +++ b/src/main/webapp/confirmation.xhtml @@ -15,14 +15,21 @@

Addresse:

- Max Mustermann
- Musterstraße 42
- 12345 Musterstadt + #{addressBean.selected.name}
+ #{addressBean.selected.street} #{addressBean.selected.number}
+ #{addressBean.selected.zipCode} #{addressBean.selected.city}
-

Konto

- IBAN: 12345••••••••••••••••••••••••111
- Inhaber: Max Mustermann
+

#{paymentBean.selected.type.name}

+ + Inhaber: #{paymentBean.selected.owner}
+ Nummer : #{paymentBean.selected.number} / #{paymentBean.selected.checksum} + gültig bis: #{paymentBean.selected.expiration} +
+ + + Konto: #{paymentBean.selected.email} +
@@ -43,7 +50,7 @@ - +

Summe: #{cartBean.sum}€

diff --git a/src/main/webapp/paymentSelection.xhtml b/src/main/webapp/paymentSelection.xhtml index 0905b7e..ba86608 100644 --- a/src/main/webapp/paymentSelection.xhtml +++ b/src/main/webapp/paymentSelection.xhtml @@ -17,18 +17,20 @@

Kreditkarte

- -
-
- #{payment.owner}
- #{payment.number} / - #{payment.checksum}
- Ablaufdatum: - - + + +
+
+ #{payment.owner}
+ #{payment.number} / + #{payment.checksum}
+ Ablaufdatum: + + +
-
- + + @@ -37,13 +39,15 @@

PayPal

- -
-
- #{payment.email} + + +
+
+ #{payment.email} +
-
- + +