From 9f3b31113ff6727dc6acce48d7d62745025c639d Mon Sep 17 00:00:00 2001 From: joethei Date: Tue, 4 Jun 2019 17:43:05 +0200 Subject: [PATCH] + Address pages --- pom.xml | 6 ++ src/main/java/de/hsel/itech/db/AddressDB.java | 56 +++++-------- src/main/java/de/hsel/itech/db/OrderDB.java | 5 +- src/main/java/de/hsel/itech/db/pojo/User.java | 6 +- .../java/de/hsel/itech/jsf/AddressBean.java | 78 +++++++++++++++++++ .../java/de/hsel/itech/jsf/SettingsBean.java | 2 +- src/main/java/de/hsel/itech/jsf/UserBean.java | 5 +- src/main/webapp/address-add.xhtml | 33 ++++++++ src/main/webapp/address.xhtml | 37 +++++++++ src/main/webapp/login.xhtml | 2 +- src/main/webapp/template.xhtml | 9 ++- 11 files changed, 189 insertions(+), 50 deletions(-) create mode 100644 src/main/java/de/hsel/itech/jsf/AddressBean.java create mode 100644 src/main/webapp/address-add.xhtml create mode 100644 src/main/webapp/address.xhtml diff --git a/pom.xml b/pom.xml index 79f2417..b2c9ace 100644 --- a/pom.xml +++ b/pom.xml @@ -161,6 +161,12 @@ 0.3m + + org.glassfish.web + el-impl + 2.2 + + org.junit.jupiter diff --git a/src/main/java/de/hsel/itech/db/AddressDB.java b/src/main/java/de/hsel/itech/db/AddressDB.java index dbe969c..e6654ad 100644 --- a/src/main/java/de/hsel/itech/db/AddressDB.java +++ b/src/main/java/de/hsel/itech/db/AddressDB.java @@ -5,10 +5,7 @@ import de.hsel.itech.db.pojo.User; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.sql.*; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -34,17 +31,18 @@ public class AddressDB { * @param user user to get addresses from * @return list of users addresses */ - @NotNull - public List
getAll(@NotNull User user) { + @Nullable + public List
getAll(User user) { + if(user == null) return null; Map.Entry entry = database.getResultSetsByValue(database.tableUserAddress, "user", String.valueOf(user.getId())); - assert entry != null; + if(entry == null) return null; List
list = new ArrayList<>(); try { ResultSet rs = entry.getKey(); while(rs.next()) { - list.add(new Address(user, rs.getLong("id"), rs.getString("name"), rs.getString("street"), rs.getString("number"), rs.getInt("zipCode"), rs.getString("city"))); + list.add(get(rs.getLong("address"), user)); } entry.getValue().close(); @@ -62,13 +60,13 @@ public class AddressDB { * @return specific address */ @Nullable - public Address get(long id) { + public Address get(long id, User user) { Map.Entry entry = database.getResultSetById(database.tableAddress, id); assert entry != null; try { ResultSet rs = entry.getKey(); - Address address = new Address(getUserFromId(id), id, rs.getString("name"), rs.getString("street"), rs.getString("number"), rs.getInt("zipCode"), rs.getString("city")); + Address address = new Address(user, id, rs.getString("name"), rs.getString("street"), rs.getString("number"), rs.getInt("zipCode"), rs.getString("city")); entry.getValue().close(); return address; @@ -78,26 +76,6 @@ public class AddressDB { return null; } - /** - * get user from address id. - * - * @param id address id - * @return User - */ - @Nullable - private User getUserFromId(long id) { - Map.Entry entry = database.getResultSetByValue(database.tableUserAddress, "address", id); - assert entry != null; - - try { - User user = database.user().get(entry.getKey().getLong("user")); - entry.getValue().close(); - return user; - } catch (SQLException e) { - e.printStackTrace(); - } - return null; - } /** * insert address into database. @@ -106,13 +84,12 @@ public class AddressDB { * @return insert count */ public int insert(@NotNull Address address) { - Connection connection = database.getConnection(); int insertCount = 0; try { assert connection != null; - PreparedStatement statement = connection.prepareStatement("INSERT INTO address (name, street, number, zipCode, city) VALUES (?, ?, ?, ?, ?)"); + PreparedStatement statement = connection.prepareStatement("INSERT INTO address (name, street, number, zipCode, city) VALUES (?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS); statement.setString(1, address.getName()); statement.setString(2, address.getStreet()); statement.setString(3, address.getNumber()); @@ -121,11 +98,18 @@ public class AddressDB { insertCount += statement.executeUpdate(); - PreparedStatement relationStatement = connection.prepareStatement("INSERT INTO user_address (user, address) VALUES (?, ?)"); - relationStatement.setLong(1, address.getUser().getId()); - relationStatement.setLong(2, address.getId()); + ResultSet keys = statement.getGeneratedKeys(); + Long id = null; + while(keys.next()) { + id = keys.getLong("id"); + } + if(id != null) { + PreparedStatement relationStatement = connection.prepareStatement("INSERT INTO user_address (user, address) VALUES (?, ?)"); + relationStatement.setLong(1, address.getUser().getId()); + relationStatement.setLong(2, id); - insertCount += statement.executeUpdate(); + insertCount += relationStatement.executeUpdate(); + } connection.close(); } catch (SQLException e) { diff --git a/src/main/java/de/hsel/itech/db/OrderDB.java b/src/main/java/de/hsel/itech/db/OrderDB.java index f85705a..94386ea 100644 --- a/src/main/java/de/hsel/itech/db/OrderDB.java +++ b/src/main/java/de/hsel/itech/db/OrderDB.java @@ -26,9 +26,10 @@ 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")); - Address address = database.address().get(rs.getLong("address")); - Order order = new Order(id, database.user().get(rs.getLong("user")), items, rs.getDate("date"), rs.getLong("price"), payment, address); + Address address = database.address().get(rs.getLong("address"), user); + Order order = new Order(id, user, items, rs.getDate("date"), rs.getLong("price"), payment, address); entry.getValue().close(); return order; diff --git a/src/main/java/de/hsel/itech/db/pojo/User.java b/src/main/java/de/hsel/itech/db/pojo/User.java index e24b2e9..8d8f75c 100644 --- a/src/main/java/de/hsel/itech/db/pojo/User.java +++ b/src/main/java/de/hsel/itech/db/pojo/User.java @@ -1,9 +1,6 @@ package de.hsel.itech.db.pojo; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; +import lombok.*; /** * POJO for User. @@ -14,6 +11,7 @@ import lombok.RequiredArgsConstructor; */ @Data +@NoArgsConstructor @AllArgsConstructor @RequiredArgsConstructor public class User { diff --git a/src/main/java/de/hsel/itech/jsf/AddressBean.java b/src/main/java/de/hsel/itech/jsf/AddressBean.java new file mode 100644 index 0000000..506510a --- /dev/null +++ b/src/main/java/de/hsel/itech/jsf/AddressBean.java @@ -0,0 +1,78 @@ +package de.hsel.itech.jsf; + +import de.hsel.itech.db.Database; +import de.hsel.itech.db.pojo.Address; +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.util.ArrayList; +import java.util.List; + +/** + * + * @author Johannes Theiner + * @version 1.0 + */ +@ManagedBean +@RequestScoped +public class AddressBean { + + @Setter + @ManagedProperty("#{userBean.user}") + private User user; + + public User getUser() { + FacesContext context = FacesContext.getCurrentInstance(); + return (User) context.getApplication().evaluateExpressionGet(context,"#{userBean.user}", User.class); + } + + @Getter + @Setter + private String name; + + @Getter + @Setter + private String street; + + @Getter + @Setter + private String number; + + @Getter + @Setter + private int zipCode = -1; + + @Getter + @Setter + private String city; + + @Getter + private boolean valid = true; + + + public List
getAddressList() { + List
list = Database.getInstance().address().getAll(getUser()); + if(list == null) return new ArrayList<>(); + return list; + } + + public String add() { + if(getUser() != null && name != null && street != null && number != null && zipCode != -1 && city != null) { + Database.getInstance().address().insert(new Address(getUser(), name, street, number, zipCode, city)); + name = null; + number = null; + zipCode = -1; + city = null; + valid = true; + return "address.xhtml"; + } + valid = false; + return ""; + } + +} \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/jsf/SettingsBean.java b/src/main/java/de/hsel/itech/jsf/SettingsBean.java index 1fc50a6..c7e27c8 100644 --- a/src/main/java/de/hsel/itech/jsf/SettingsBean.java +++ b/src/main/java/de/hsel/itech/jsf/SettingsBean.java @@ -19,6 +19,6 @@ public class SettingsBean { @Getter @Setter - private boolean darkTheme = false; + private boolean darkTheme = true; } \ 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 203a2f1..b0ef05c 100644 --- a/src/main/java/de/hsel/itech/jsf/UserBean.java +++ b/src/main/java/de/hsel/itech/jsf/UserBean.java @@ -13,7 +13,8 @@ import javax.faces.bean.SessionScoped; public class UserBean { @Getter - private User user; + @Setter + private User user = new User(); @Getter @Setter @@ -45,7 +46,7 @@ public class UserBean { if (db.user().verify(user, password)) { loggedIn = true; - this.user = user; + setUser(user); password = null; username = null; return "index.xhtml"; diff --git a/src/main/webapp/address-add.xhtml b/src/main/webapp/address-add.xhtml new file mode 100644 index 0000000..d6d31f3 --- /dev/null +++ b/src/main/webapp/address-add.xhtml @@ -0,0 +1,33 @@ + + + + + + + +
+
+ + Name +
+ Straße +
+ Hausnummer +
+ Postleitzahl +
+ Ort +
+ +
+
+
+ +
+
+
+ diff --git a/src/main/webapp/address.xhtml b/src/main/webapp/address.xhtml new file mode 100644 index 0000000..93914da --- /dev/null +++ b/src/main/webapp/address.xhtml @@ -0,0 +1,37 @@ + + + + + + + +

Adressen

+ + + +
+
+ #{address.name}
+ #{address.street} #{address.number}
+ #{address.zipCode} #{address.city} +
+
+
+
+ +
+ +
+
+
+
+ + +
+
+
+ diff --git a/src/main/webapp/login.xhtml b/src/main/webapp/login.xhtml index 3f05980..2b5c9a4 100644 --- a/src/main/webapp/login.xhtml +++ b/src/main/webapp/login.xhtml @@ -10,7 +10,7 @@ - + diff --git a/src/main/webapp/template.xhtml b/src/main/webapp/template.xhtml index 28d705f..7828748 100644 --- a/src/main/webapp/template.xhtml +++ b/src/main/webapp/template.xhtml @@ -37,7 +37,7 @@