diff --git a/src/main/java/de/hsel/itech/db/Database.java b/src/main/java/de/hsel/itech/db/Database.java index 3b9267e..d6ba986 100644 --- a/src/main/java/de/hsel/itech/db/Database.java +++ b/src/main/java/de/hsel/itech/db/Database.java @@ -1,16 +1,26 @@ package de.hsel.itech.db; import de.hsel.itech.config.Configuration; +import de.hsel.itech.db.pojo.Author; +import de.hsel.itech.db.pojo.Book; +import de.hsel.itech.db.pojo.Category; +import de.hsel.itech.db.pojo.Publisher; import org.jetbrains.annotations.Nullable; import org.mariadb.jdbc.MariaDbPoolDataSource; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; +import java.net.URL; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.time.Year; +import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; /** * @@ -41,7 +51,9 @@ public class Database { Connection connection = getConnection(); assert connection != null; - try(BufferedReader br = new BufferedReader(new FileReader(getClass().getClassLoader().getResource("database.sql").getFile()))) { + URL file = getClass().getClassLoader().getResource("database.sql"); + assert file != null; + try(BufferedReader br = new BufferedReader(new FileReader(file.getFile()))) { for (String line = br.readLine(); line != null; line = br.readLine()) { PreparedStatement statement = connection.prepareStatement(line); statement.executeUpdate(); @@ -88,8 +100,141 @@ public class Database { } + @Nullable + private Map.Entry getResultSet(long id, String table) { + Connection connection = getConnection(); + try{ + assert connection != null; + PreparedStatement statement = connection.prepareStatement("SELECT * FROM ? WHERE ID = ?"); + statement.setString(1, table); + statement.setLong(2, id); + ResultSet resultSet = statement.executeQuery(); + if(resultSet.next()) { + return new AbstractMap.SimpleEntry<>(resultSet, connection); + } + return null; + + }catch (SQLException ex) { + ex.printStackTrace(); + } + return null; + } + + @Nullable + private Map.Entry, Connection> getResultSets(long id, String table, String column) { + Connection connection = getConnection(); + try { + assert connection != null; + PreparedStatement statement = connection.prepareStatement("SELECT * FROM ? WHERE ? = ?"); + statement.setString(1, table); + statement.setString(2, column); + statement.setLong(3, id); + + ResultSet resultSet = statement.executeQuery(); + + Map.Entry, Connection> entry = new AbstractMap.SimpleEntry<>(new ArrayList<>(), connection); + while(resultSet.next()) { + entry.getKey().add(resultSet); + } + return entry; + }catch (SQLException ex) { + ex.printStackTrace(); + } + return null; + } + @Nullable + public Author getAuthor(long id) { + Map.Entry entry = getResultSet(id, "author"); + assert entry != null; + + ResultSet rs = entry.getKey(); + Author author = null; + try { + author = new Author(rs.getLong("id"), rs.getString("name")); + entry.getValue().close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return author; + } + + @Nullable + public Category getCategory(long id) { + Map.Entry entry = getResultSet(id, "category"); + assert entry != null; + + ResultSet rs = entry.getKey(); + Category category = null; + try { + category = new Category(rs.getLong("id"), rs.getString("name"), rs.getString("image")); + entry.getValue().close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return category; + } + + @Nullable + public Publisher getPublisher(long id) { + Map.Entry entry = getResultSet(id, "category"); + assert entry != null; + + ResultSet rs = entry.getKey(); + Publisher publisher = null; + try { + publisher = new Publisher(rs.getLong("id"), rs.getString("name")); + entry.getValue().close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return publisher; + } + + @Nullable + private List getAuthors(long id) { + Map.Entry, Connection> entry = getResultSets(id, "author_book", "book"); + assert entry != null; + try { + List list = new ArrayList<>(); + for(ResultSet rs : entry.getKey()) { + list.add(rs.getInt("author")); + } + entry.getValue().close(); + return list; + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + @Nullable + public Book getBook(long id) { + Map.Entry entry = getResultSet(id, "book"); + assert entry != null; + + ResultSet rs = entry.getKey(); + Book book = null; + try { + Category category = getCategory(rs.getLong("category")); + Publisher publisher = getPublisher(rs.getLong("publisher")); + List authorIds = getAuthors(id); + assert authorIds != null; + List authors = new ArrayList<>(); + for(int i : authorIds) { + Author author = getAuthor(i); + assert author != null; + authors.add(author); + } + book = new Book(rs.getLong("id"), 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) { + e.printStackTrace(); + } + return book; + } + } \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/pojo/Book.java b/src/main/java/de/hsel/itech/db/pojo/Book.java index 79160e1..8599fb7 100644 --- a/src/main/java/de/hsel/itech/db/pojo/Book.java +++ b/src/main/java/de/hsel/itech/db/pojo/Book.java @@ -4,6 +4,7 @@ import lombok.AllArgsConstructor; import lombok.Data; import java.time.Year; +import java.util.List; /** * @author Johannes Theiner @@ -15,8 +16,8 @@ import java.time.Year; @AllArgsConstructor public class Book { - private int isbn; - private Author author; + private long isbn; + private List authors; private Publisher publisher; private Category category; private String title; diff --git a/src/main/java/de/hsel/itech/db/pojo/Category.java b/src/main/java/de/hsel/itech/db/pojo/Category.java index 685ca42..6c6a166 100644 --- a/src/main/java/de/hsel/itech/db/pojo/Category.java +++ b/src/main/java/de/hsel/itech/db/pojo/Category.java @@ -13,7 +13,7 @@ import lombok.Data; @AllArgsConstructor public class Category { - private int id; + private long id; private String name; private String image; }