From d205e62419521ca1f4b272f0d695a035932986cd Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 5 Jun 2019 10:25:42 +0200 Subject: [PATCH] + first two payment methods being displayed Signed-off-by: Johannes Theiner --- src/main/java/de/hsel/itech/db/Database.java | 8 ++- src/main/java/de/hsel/itech/db/OrderDB.java | 2 +- src/main/java/de/hsel/itech/db/PaymentDB.java | 56 ++++++++++++----- .../java/de/hsel/itech/jsf/AddressBean.java | 8 +-- .../java/de/hsel/itech/jsf/PaymentBean.java | 49 ++++++++++++--- src/main/java/de/hsel/itech/jsf/UserBean.java | 3 +- src/main/resources/database.sql | 2 +- src/main/webapp/payments.xhtml | 63 +++++++++++++++++++ 8 files changed, 155 insertions(+), 36 deletions(-) create mode 100644 src/main/webapp/payments.xhtml diff --git a/src/main/java/de/hsel/itech/db/Database.java b/src/main/java/de/hsel/itech/db/Database.java index 41ed41e..a1ce039 100644 --- a/src/main/java/de/hsel/itech/db/Database.java +++ b/src/main/java/de/hsel/itech/db/Database.java @@ -277,10 +277,12 @@ public class Database { PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + table + " WHERE " + column + " = ?"); statement.setLong(1, value); ResultSet resultSet = statement.executeQuery(); - if (resultSet.next()) { - return new AbstractMap.SimpleEntry<>(resultSet, connection); + Map.Entry entry = null; + + while (resultSet.next()) { + entry = new AbstractMap.SimpleEntry<>(resultSet, connection); } - return null; + return entry; } catch (SQLException ex) { ex.printStackTrace(); diff --git a/src/main/java/de/hsel/itech/db/OrderDB.java b/src/main/java/de/hsel/itech/db/OrderDB.java index 94386ea..981c2fd 100644 --- a/src/main/java/de/hsel/itech/db/OrderDB.java +++ b/src/main/java/de/hsel/itech/db/OrderDB.java @@ -27,7 +27,7 @@ public class OrderDB { ResultSet rs = entry.getKey(); List items = getItems(id); User user = database.user().get(rs.getLong("user")); - Payment payment = database.payment().get(rs.getLong("payment")); + Payment payment = database.payment().get(rs.getLong("payment"), user); Address address = database.address().get(rs.getLong("address"), user); Order order = new Order(id, user, items, rs.getDate("date"), rs.getLong("price"), payment, address); diff --git a/src/main/java/de/hsel/itech/db/PaymentDB.java b/src/main/java/de/hsel/itech/db/PaymentDB.java index 1cec6a0..5c4dadb 100644 --- a/src/main/java/de/hsel/itech/db/PaymentDB.java +++ b/src/main/java/de/hsel/itech/db/PaymentDB.java @@ -32,7 +32,11 @@ public class PaymentDB { try { ResultSet rs = entry.getKey(); - PaymentType type = new PaymentType(id, rs.getString("name")); + rs.beforeFirst(); + PaymentType type = null; + while(rs.next()) { + type = new PaymentType(id, rs.getString("name")); + } entry.getValue().close(); return type; @@ -43,15 +47,16 @@ public class PaymentDB { } @NotNull - public List get(@NotNull User user) { + public List get(@NotNull User user) { List list = new ArrayList<>(); Map.Entry entry = database.getResultSetByValue(database.tableUserPayment, "user", user.getId()); - assert entry != null; + if(entry == null) return list; try { ResultSet rs = entry.getKey(); + rs.beforeFirst(); while(rs.next()) { - list.add(get(rs.getLong("id"))); + list.add(get(rs.getLong("id"), user)); } entry.getValue().close(); @@ -63,19 +68,19 @@ public class PaymentDB { } @Nullable - public Payment get(long id) { + public Payment get(long id, User user) { Map.Entry metadata = getMetadata(id); assert metadata != null; switch (metadata.getValue().getName()) { case "PayPal": - return getPayPal(id, metadata.getKey()); + return getPayPal(id, user); case "CreditCard": - return getCreditCard(id, metadata.getKey()); + return getCreditCard(id, user); case "DebitCard": - return getDebitCard(id, metadata.getKey()); + return getDebitCard(id, user); case "Invoice": - return getInvoice(id, metadata.getKey()); + return getInvoice(id, user); } return null; } @@ -87,7 +92,11 @@ public class PaymentDB { try { ResultSet rs = entry.getKey(); - PayPalPayment payment = new PayPalPayment(id, user, rs.getString("email"), rs.getString("authCode")); + rs.beforeFirst(); + PayPalPayment payment = null; + while(rs.next()) { + payment = new PayPalPayment(id, user, rs.getString("mail"), rs.getString("auth")); + } entry.getValue().close(); return payment; @@ -105,7 +114,12 @@ public class PaymentDB { try { ResultSet rs = entry.getKey(); - CreditCardPayment payment = new CreditCardPayment(id, user, rs.getInt("number"), rs.getString("owner"), rs.getDate("expiration"), rs.getInt("checksum")); + rs.beforeFirst(); + CreditCardPayment payment = null; + while(rs.next()) { + payment = new CreditCardPayment(id, user, rs.getInt("number"), rs.getString("owner"), rs.getDate("expiration"), rs.getInt("checksum")); + } + entry.getValue().close(); return payment; @@ -123,7 +137,11 @@ public class PaymentDB { try { ResultSet rs = entry.getKey(); - DebitCardPayment payment = new DebitCardPayment(id, user, rs.getString("iban"), rs.getString("bic"), rs.getString("owner")); + rs.beforeFirst(); + DebitCardPayment payment = null; + while(rs.next()) { + payment = new DebitCardPayment(id, user, rs.getString("iban"), rs.getString("bic"), rs.getString("owner")); + } entry.getValue().close(); return payment; @@ -140,9 +158,12 @@ public class PaymentDB { try { ResultSet rs = entry.getKey(); + rs.beforeFirst(); InvoicePayment payment = null; - if(rs.getLong("id") == id && rs.getLong("user") == user.getId()) { - payment = new InvoicePayment(id, user); + while(rs.next()) { + if(rs.getLong("id") == id && rs.getLong("user") == user.getId()) { + payment = new InvoicePayment(id, user); + } } entry.getValue().close(); @@ -296,8 +317,11 @@ public class PaymentDB { try { ResultSet rs = entry.getKey(); - Map.Entry metadata = new AbstractMap.SimpleEntry<>(database.user().get(rs.getLong("user")), getType(rs.getLong("type"))); - + rs.beforeFirst(); + Map.Entry metadata = null; + while(rs.next()) { + metadata = new AbstractMap.SimpleEntry<>(database.user().get(rs.getLong("user")), getType(rs.getLong("type"))); + } entry.getValue().close(); return metadata; diff --git a/src/main/java/de/hsel/itech/jsf/AddressBean.java b/src/main/java/de/hsel/itech/jsf/AddressBean.java index 506510a..ef25d6c 100644 --- a/src/main/java/de/hsel/itech/jsf/AddressBean.java +++ b/src/main/java/de/hsel/itech/jsf/AddressBean.java @@ -6,10 +6,10 @@ import de.hsel.itech.db.pojo.User; import lombok.Getter; import lombok.Setter; -import javax.faces.annotation.ManagedProperty; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; import javax.faces.context.FacesContext; +import java.io.Serializable; import java.util.ArrayList; import java.util.List; @@ -20,11 +20,7 @@ import java.util.List; */ @ManagedBean @RequestScoped -public class AddressBean { - - @Setter - @ManagedProperty("#{userBean.user}") - private User user; +public class AddressBean implements Serializable { public User getUser() { FacesContext context = FacesContext.getCurrentInstance(); diff --git a/src/main/java/de/hsel/itech/jsf/PaymentBean.java b/src/main/java/de/hsel/itech/jsf/PaymentBean.java index 64524a3..265e6ed 100644 --- a/src/main/java/de/hsel/itech/jsf/PaymentBean.java +++ b/src/main/java/de/hsel/itech/jsf/PaymentBean.java @@ -1,23 +1,56 @@ package de.hsel.itech.jsf; import de.hsel.itech.db.Database; -import de.hsel.itech.db.pojo.Payment; -import de.hsel.itech.db.pojo.User; +import de.hsel.itech.db.pojo.*; -import javax.faces.annotation.ManagedProperty; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; +import javax.faces.context.FacesContext; +import java.util.ArrayList; import java.util.List; @ManagedBean @SessionScoped public class PaymentBean { - @ManagedProperty("#{userBean.user}") - private User user; - - public List getMethods() { - return Database.getInstance().payment().get(user); + public User getUser() { + FacesContext context = FacesContext.getCurrentInstance(); + return (User) context.getApplication().evaluateExpressionGet(context,"#{userBean.user}", User.class); } + public List getCreditCard() { + List list = new ArrayList<>(); + for(Payment payment : Database.getInstance().payment().get(getUser())) { + if(payment instanceof CreditCardPayment) + list.add((CreditCardPayment) payment); + } + return list; + } + + public List getDebitCard() { + List list = new ArrayList<>(); + for(Payment payment : Database.getInstance().payment().get(getUser())) { + if(payment instanceof DebitCardPayment) + list.add((DebitCardPayment) payment); + } + return list; + } + + public List getPayPal() { + List list = new ArrayList<>(); + for (Payment payment : Database.getInstance().payment().get(getUser())) { + if(payment instanceof PayPalPayment) + list.add((PayPalPayment) payment); + } + return list; + } + + public List getInvoice() { + List list = new ArrayList<>(); + for(Payment payment : Database.getInstance().payment().get(getUser())) { + if(payment instanceof InvoicePayment) + list.add((InvoicePayment) payment); + } + return list; + } } \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/jsf/UserBean.java b/src/main/java/de/hsel/itech/jsf/UserBean.java index b0ef05c..b7190f7 100644 --- a/src/main/java/de/hsel/itech/jsf/UserBean.java +++ b/src/main/java/de/hsel/itech/jsf/UserBean.java @@ -7,10 +7,11 @@ import lombok.Setter; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; +import java.io.Serializable; @ManagedBean @SessionScoped -public class UserBean { +public class UserBean implements Serializable { @Getter @Setter diff --git a/src/main/resources/database.sql b/src/main/resources/database.sql index 5c58543..c35a3dc 100644 --- a/src/main/resources/database.sql +++ b/src/main/resources/database.sql @@ -4,7 +4,7 @@ CREATE TABLE IF NOT EXISTS author(id bigint primary key auto_increment, name var CREATE TABLE IF NOT EXISTS author_book(author bigint, book bigint, primary key(author, book), foreign key (author) REFERENCES author(id), foreign key (book) references book(id) on delete cascade); CREATE TABLE IF NOT EXISTS category(id bigint primary key auto_increment, name varchar(25), image varchar(11)); CREATE TABLE IF NOT EXISTS user(id bigint primary key auto_increment, name varchar(50), email varchar(255), password binary(60), type tinyint); -CREATE TABLE IF NOT EXISTS address(id bigint primary key auto_increment, name varchar(100), street varchar(100), number smallint, zipCode smallint, city varchar(50)); +CREATE TABLE IF NOT EXISTS address(id bigint primary key auto_increment, name varchar(100), street varchar(100), number smallint, zipCode int(6), city varchar(50)); CREATE TABLE IF NOT EXISTS user_address(id bigint primary key auto_increment, user bigint, address bigint, foreign key (user) REFERENCES user(id), foreign key (address) references address(id) on delete cascade); CREATE TABLE IF NOT EXISTS payment_type(id bigint primary key auto_increment, name varchar(25) unique); INSERT IGNORE INTO payment_type (name) values ('CreditCard'); diff --git a/src/main/webapp/payments.xhtml b/src/main/webapp/payments.xhtml new file mode 100644 index 0000000..40e2fe3 --- /dev/null +++ b/src/main/webapp/payments.xhtml @@ -0,0 +1,63 @@ + + + + + + + +

Zahlungsmöglichkeiten

+ + + + +

Kreditkarte

+ + +
+
+ #{payment.owner}
+ #{payment.number}
+ #{payment.checksum}
+ #{payment.expiration} +
+
+
+
+ +
+ +
+
+
+
+ + +

PayPal

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