diff --git a/src/main/java/de/hsel/itech/db/Database.java b/src/main/java/de/hsel/itech/db/Database.java index 7999bc0..41ed41e 100644 --- a/src/main/java/de/hsel/itech/db/Database.java +++ b/src/main/java/de/hsel/itech/db/Database.java @@ -39,6 +39,12 @@ public class Database { final String tableCartBooks = "cart_books"; final String tableOrder = "order"; final String tableOrderBook = "order_book"; + final String tablePaymentType = "payment_type"; + final String tablePaymentPayPal = "payment_paypal"; + final String tablePaymentCredit = "payment_credit"; + final String tablePaymentDebit = "payment_debit"; + final String tablePaymentInvoice = "payment_invoice"; + final String tableUserPayment = "user_payment"; private AuthorDB authorDB; private BookDB bookDB; @@ -48,6 +54,7 @@ public class Database { private AddressDB addressDB; private ShoppingCartDB shoppingCartDB; private OrderDB orderDB; + private PaymentDB paymentDB; private static Database instance; @@ -168,6 +175,13 @@ public class Database { return orderDB; } + @Contract + public PaymentDB payment() { + if(paymentDB == null) + paymentDB = new PaymentDB(this); + return paymentDB; + } + /** * deletes entry from table. diff --git a/src/main/java/de/hsel/itech/db/PaymentDB.java b/src/main/java/de/hsel/itech/db/PaymentDB.java new file mode 100644 index 0000000..9865c6e --- /dev/null +++ b/src/main/java/de/hsel/itech/db/PaymentDB.java @@ -0,0 +1,155 @@ +package de.hsel.itech.db; + + +import de.hsel.itech.db.pojo.*; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.AbstractMap; +import java.util.Map; + +/** + * + * @author Johannes Theiner + * @version 0.1 + * @since 0.5 + */ +public class PaymentDB { + + private Database database; + + PaymentDB(Database database) { + this.database = database; + } + + @Nullable + private PaymentType getType(long id) { + Map.Entry entry = database.getResultSetById(database.tablePaymentType, id); + assert entry != null; + + try { + ResultSet rs = entry.getKey(); + PaymentType type = new PaymentType(id, rs.getString("name")); + entry.getValue().close(); + + return type; + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + @Nullable + public Payment get(long id) { + Map.Entry metadata = getMetadata(id); + assert metadata != null; + + String typeName = metadata.getValue().getName(); + if(typeName.equals("PayPal")) { + return getPayPal(id, metadata.getKey()); + } + if(typeName.equals("CreditCard")) { + return getCreditCard(id, metadata.getKey()); + } + if(typeName.equals("DebitCard")) { + return getDebitCard(id, metadata.getKey()); + } + if(typeName.equals("Invoice")) { + return getInvoice(id, metadata.getKey()); + } + + return null; + } + + @Nullable + private PayPalPayment getPayPal(long id, @NotNull User user) { + Map.Entry entry = database.getResultSetById(database.tablePaymentPayPal, id); + assert entry != null; + + try { + ResultSet rs = entry.getKey(); + PayPalPayment payment = new PayPalPayment(id, user, rs.getString("email"), rs.getString("authCode")); + entry.getValue().close(); + + return payment; + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + @Nullable + private CreditCardPayment getCreditCard(long id, @NotNull User user) { + Map.Entry entry = database.getResultSetById(database.tablePaymentCredit, id); + assert entry != null; + + + try { + ResultSet rs = entry.getKey(); + CreditCardPayment payment = new CreditCardPayment(id, user, rs.getInt("number"), rs.getString("owner"), rs.getDate("expiration"), rs.getInt("checksum")); + entry.getValue().close(); + + return payment; + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + + @Nullable + private DebitCardPayment getDebitCard(long id, @NotNull User user) { + Map.Entry entry = database.getResultSetById(database.tablePaymentDebit, id); + assert entry != null; + + try { + ResultSet rs = entry.getKey(); + DebitCardPayment payment = new DebitCardPayment(id, user, rs.getString("iban"), rs.getString("bic"), rs.getString("owner")); + entry.getValue().close(); + + return payment; + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + @Nullable + private InvoicePayment getInvoice(long id, @NotNull User user) { + Map.Entry entry = database.getResultSetById(database.tablePaymentInvoice, id); + assert entry != null; + + try { + ResultSet rs = entry.getKey(); + InvoicePayment payment = new InvoicePayment(id, user); + entry.getValue().close(); + + return payment; + } catch (SQLException e) { + e.printStackTrace(); + } + + return null; + } + + @Nullable + private Map.Entry getMetadata(long id) { + Map.Entry entry = database.getResultSetById(database.tableUserPayment, id); + assert entry != null; + + try { + ResultSet rs = entry.getKey(); + Map.Entry metadata = new AbstractMap.SimpleEntry<>(database.user().get(rs.getLong("user")), getType(rs.getLong("type"))); + + entry.getValue().close(); + + return metadata; + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } +} \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/pojo/CreditCardPayment.java b/src/main/java/de/hsel/itech/db/pojo/CreditCardPayment.java index 068d29a..783c0dc 100644 --- a/src/main/java/de/hsel/itech/db/pojo/CreditCardPayment.java +++ b/src/main/java/de/hsel/itech/db/pojo/CreditCardPayment.java @@ -1,10 +1,10 @@ package de.hsel.itech.db.pojo; import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.NonNull; -import lombok.RequiredArgsConstructor; -import java.time.LocalDate; +import java.sql.Date; /** * POJO for CreditCard Payment. @@ -15,12 +15,21 @@ import java.time.LocalDate; **/ @Data -@RequiredArgsConstructor +@EqualsAndHashCode(callSuper = true) public class CreditCardPayment extends Payment{ @NonNull private User user; @NonNull private int number; @NonNull private String owner; - @NonNull private LocalDate expiration; + @NonNull private Date expiration; @NonNull private int checksum; + + public CreditCardPayment(long id, User user, int number, String owner, Date expiration, int checksum) { + super(id, new PaymentType("CreditCard")); + this.user = user; + this.number = number; + this.owner = owner; + this.expiration = expiration; + this.checksum = checksum; + } } \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/pojo/DebitCardPayment.java b/src/main/java/de/hsel/itech/db/pojo/DebitCardPayment.java index 1ec2cb7..ee2a385 100644 --- a/src/main/java/de/hsel/itech/db/pojo/DebitCardPayment.java +++ b/src/main/java/de/hsel/itech/db/pojo/DebitCardPayment.java @@ -1,9 +1,8 @@ package de.hsel.itech.db.pojo; -import lombok.AllArgsConstructor; import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.NonNull; -import lombok.RequiredArgsConstructor; /** * POJO for DebitCard Payment. @@ -14,11 +13,20 @@ import lombok.RequiredArgsConstructor; **/ @Data -@RequiredArgsConstructor +@EqualsAndHashCode(callSuper = true) public class DebitCardPayment extends Payment{ @NonNull private User user; @NonNull private String iban; @NonNull private String bic; @NonNull private String owner; + + public DebitCardPayment(long id, User user, String iban, String bic, String owner) { + super(id, new PaymentType("DebitCard")); + this.user = user; + this.iban = iban; + this.bic = bic; + this.owner = owner; + } + } \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/pojo/InvoicePayment.java b/src/main/java/de/hsel/itech/db/pojo/InvoicePayment.java index ca1c090..e925b97 100644 --- a/src/main/java/de/hsel/itech/db/pojo/InvoicePayment.java +++ b/src/main/java/de/hsel/itech/db/pojo/InvoicePayment.java @@ -1,8 +1,8 @@ package de.hsel.itech.db.pojo; import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.NonNull; -import lombok.RequiredArgsConstructor; /** * POJO for Invoice Payment. @@ -13,8 +13,13 @@ import lombok.RequiredArgsConstructor; **/ @Data -@RequiredArgsConstructor +@EqualsAndHashCode(callSuper = true) public class InvoicePayment extends Payment{ @NonNull private User user; + + public InvoicePayment(long id, User user) { + super(id, new PaymentType("Invoice")); + this.user = user; + } } \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/pojo/PayPalPayment.java b/src/main/java/de/hsel/itech/db/pojo/PayPalPayment.java index 082dc3d..163b76d 100644 --- a/src/main/java/de/hsel/itech/db/pojo/PayPalPayment.java +++ b/src/main/java/de/hsel/itech/db/pojo/PayPalPayment.java @@ -12,8 +12,8 @@ import lombok.NonNull; * @since 0.1 **/ -@EqualsAndHashCode(callSuper = true) @Data +@EqualsAndHashCode(callSuper = true) public class PayPalPayment extends Payment{ @NonNull private User user; diff --git a/src/main/resources/database.sql b/src/main/resources/database.sql index f0d0f96..35b769b 100644 --- a/src/main/resources/database.sql +++ b/src/main/resources/database.sql @@ -13,10 +13,11 @@ INSERT IGNORE INTO payment_type (name) values ('CreditCard'); INSERT IGNORE INTO payment_type (name) values ('DebitCard'); INSERT IGNORE INTO payment_type (name) values ('Invoice'); INSERT IGNORE INTO payment_type (name) values ('PayPal'); -CREATE TABLE IF NOT EXISTS payment_debit(id bigint primary key auto_increment, iban varchar(34), bic varchar(8), owner varchar(50)); -CREATE TABLE IF NOT EXISTS payment_credit(id bigint primary key auto_increment, number varchar(19), owner varchar(50), expiration DATE, checksum tinyint); -CREATE TABLE IF NOT EXISTS payment_invoice(id bigint primary key auto_increment); -CREATE TABLE IF NOT EXISTS payment_paypal(id bigint primary key auto_increment, mail varchar(255), auth varchar(255)); +CREATE TABLE IF NOT EXISTS payment_debit(id bigint primary key, iban varchar(34), bic varchar(8), owner varchar(50)); +CREATE TABLE IF NOT EXISTS payment_credit(id bigint primary key, number varchar(19), owner varchar(50), expiration DATE, checksum tinyint); +CREATE TABLE IF NOT EXISTS payment_invoice(id bigint primary key); +CREATE TABLE IF NOT EXISTS payment_paypal(id bigint primary key, mail varchar(255), auth varchar(255)); +CREATE TABLE IF NOT EXISTS user_payment(id bigint primary key auto_increment, type bigint, user bigint, foreign key (type) references payment_type(id), foreign key (user) references user(id)); CREATE TABLE IF NOT EXISTS `order`(id bigint primary key auto_increment, user bigint, price bigint); CREATE TABLE IF NOT EXISTS order_book(`order` bigint, book bigint, count tinyint, primary key (`order`, book, count), foreign key (`order`) references `order`(id), foreign key (book) references `order`(id)); CREATE TABLE IF NOT EXISTS cart_books(user bigint, book bigint, count tinyint, primary key(book, user), foreign key (book) REFERENCES book(id), foreign key (user) REFERENCES user(id)); \ No newline at end of file