From 07924cfbdfe7cef325847b7bbf867f9ba093aa65 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Sun, 19 May 2019 14:26:45 +0200 Subject: [PATCH] ~ refactor database into multiple files Signed-off-by: Johannes Theiner --- pom.xml | 1 + src/main/java/de/hsel/itech/db/AuthorDB.java | 133 ++++ src/main/java/de/hsel/itech/db/BookDB.java | 265 ++++++++ .../java/de/hsel/itech/db/CategoryDB.java | 133 ++++ src/main/java/de/hsel/itech/db/Database.java | 587 ++---------------- .../java/de/hsel/itech/db/PublisherDB.java | 110 ++++ src/main/java/de/hsel/itech/db/pojo/Book.java | 2 +- .../java/de/hsel/itech/jsf/CategoryBean.java | 4 +- .../java/de/hsel/itech/jsf/SettingsBean.java | 24 + .../java/de/hsel/itech/servlet/BookList.java | 10 +- src/main/webapp/categories.jsp | 2 +- src/main/webapp/category.jsp | 4 +- src/main/webapp/index.html | 1 + src/main/webapp/template.xhtml | 31 +- 14 files changed, 736 insertions(+), 571 deletions(-) create mode 100644 src/main/java/de/hsel/itech/db/AuthorDB.java create mode 100644 src/main/java/de/hsel/itech/db/BookDB.java create mode 100644 src/main/java/de/hsel/itech/db/CategoryDB.java create mode 100644 src/main/java/de/hsel/itech/db/PublisherDB.java create mode 100644 src/main/java/de/hsel/itech/jsf/SettingsBean.java diff --git a/pom.xml b/pom.xml index 81ce4a5..d666d95 100644 --- a/pom.xml +++ b/pom.xml @@ -147,6 +147,7 @@ + org.mariadb.jdbc mariadb-java-client diff --git a/src/main/java/de/hsel/itech/db/AuthorDB.java b/src/main/java/de/hsel/itech/db/AuthorDB.java new file mode 100644 index 0000000..2e4bcb0 --- /dev/null +++ b/src/main/java/de/hsel/itech/db/AuthorDB.java @@ -0,0 +1,133 @@ +package de.hsel.itech.db; + +import de.hsel.itech.db.pojo.Author; +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.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.5 + **/ +public class AuthorDB { + + private Database database; + + + AuthorDB(Database database) { + this.database = database; + } + + /** + * gets tableAuthor by id. + * + * @param id ID from database + * @return {@link de.hsel.itech.db.pojo.Author} + */ + @Nullable + public Author get(long id) { + Map.Entry entry = database.getResultSetById(database.tableAuthor, id); + 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; + } + + /** + * gets tableAuthor by name. + * + * @param name tableAuthor name + * @return {@link de.hsel.itech.db.pojo.Author} + */ + @Nullable + public Author get(@NotNull String name) { + Map.Entry entry = database.getResultSetsByValue(database.tableAuthor, "name", name); + assert entry != null; + assert entry.getKey() != null; + + Author author = null; + ResultSet rs = entry.getKey(); + try { + if (rs.next()) + author = new Author(rs.getLong("id"), rs.getString("name")); + entry.getValue().close(); + } catch (SQLException e) { + e.printStackTrace(); + } + + return author; + } + + /** + * inserts Author. + * + * @param author Author + * @return insert count + */ + public int insert(@NotNull Author author) { + Connection connection = database.getConnection(); + assert connection != null; + + int insertCount = 0; + try { + PreparedStatement statement = connection.prepareStatement("INSERT INTO author (name) VALUES (?)"); + statement.setString(1, author.getName()); + + insertCount += statement.executeUpdate(); + + connection.close(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + return insertCount; + } + + /** + * deletes tableAuthor from database. + * + * @param id tableAuthor id + * @return deletion count + */ + public int delete(long id) { + return database.delete(id, database.tableAuthor); + } + + /** + * gets list of tableAuthor ids from database. + * + * @param id tableBook id from database + * @return {@link java.util.List} + */ + @Nullable + List getAll(long id) { + Map.Entry entry = database.getResultSetsById(database.tableAuthorBook, database.tableBook, id); + assert entry != null; + try { + List list = new ArrayList<>(); + ResultSet rs = entry.getKey(); + while (rs.next()) { + list.add(rs.getInt(database.tableAuthor)); + } + entry.getValue().close(); + return list; + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } +} \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/BookDB.java b/src/main/java/de/hsel/itech/db/BookDB.java new file mode 100644 index 0000000..27d4fd3 --- /dev/null +++ b/src/main/java/de/hsel/itech/db/BookDB.java @@ -0,0 +1,265 @@ +package de.hsel.itech.db; + +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.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.sql.*; +import java.time.Year; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.5 + **/ +public class BookDB { + private Database database; + + BookDB(Database database) { + this.database = database; + } + + /** + * gets tableBook by id. + * + * @param id ID from database + * @return {@link de.hsel.itech.db.pojo.Book} + */ + @Nullable + public Book get(long id) { + Map.Entry entry = database.getResultSetById(database.tableBook, id); + assert entry != null; + return build(entry); + } + + /** + * builds tableBook from entry. + * + * @param entry Map.Entry + * @return BookDB + */ + @Nullable + private Book build(@NotNull Map.Entry entry) { + + 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; + + 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")); + + entry.getValue().close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return book; + } + + /** + * gets all books. + * + * @return {@link java.util.List} + */ + @Nullable + public List getAll() { + List ids = database.getIds(database.tableBook); + List books = new ArrayList<>(); + for (long id : ids) { + books.add(get(id)); + } + return books; + } + + /** + * gets books by tableCategory. + * + * @param category tableCategory id + * @return {@link java.util.List} + */ + @Nullable + public List getAll(long category) { + Connection connection = database.getConnection(); + assert connection != null; + + List books = new ArrayList<>(); + try { + PreparedStatement statement = connection.prepareStatement("SELECT (id) FROM book WHERE category = ?"); + statement.setLong(1, category); + + ResultSet resultSet = statement.executeQuery(); + + while (resultSet.next()) { + books.add(get(resultSet.getLong("id"))); + } + + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return books; + } + + /** + * getAll books by tableCategory. + * + * @param category Category object + * @return {@link java.util.List} + */ + @Nullable + public List getAll(@NotNull Category category) { + return getAll(category.getId()); + } + + /** + * exists tableBook with isbn ? + * + * @param isbn isbn + * @return true if tableBook with isbn exists + */ + public boolean exists(long isbn) { + Map.Entry entry = database.getResultSetByValue(database.tableBook, "isbn", isbn); + try { + if (entry != null && entry.getKey() != null) { + entry.getValue().close(); + return true; + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + return false; + } + + /** + * deletes tableBook from database. + * + * @param id tableBook id + * @return deletion count + */ + public int delete(long id) { + int deleteCount = 0; + + try { + Connection connection = database.getConnection(); + assert connection != null; + + PreparedStatement statement = connection.prepareStatement("DELETE FROM author_book WHERE book = ?"); + statement.setLong(1, id); + deleteCount = +statement.executeUpdate(); + + connection.close(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + + deleteCount += database.delete(id, database.tableBook); + + return deleteCount; + } + + + /** + * Inserts tableBook into database. + * authors, publishers, and tableCategory have to exist. + * + * @param book BookDB + * @return insert count + */ + public int insert(@NotNull Book book) { + Connection connection = database.getConnection(); + assert connection != null; + + int insertCount = 0; + + //tableAuthor exists ? + List authors = new ArrayList<>(); + for (Author author : book.getAuthors()) { + if (author.getId() == 0) { + Author dbAuthor = database.author().get(author.getName()); + if (dbAuthor == null) { + insertCount += database.author().insert(author); + dbAuthor = database.author().get(author.getName()); + } + authors.add(dbAuthor); + } else authors.add(author); + } + + //tablePublisher exists ? + Publisher publisher; + if (book.getPublisher().getId() == 0) { + Publisher dbPublisher = database.publisher().get(book.getPublisher().getName()); + if (dbPublisher != null) + publisher = dbPublisher; + else { + insertCount += database.publisher().insert(new Publisher(book.getPublisher().getName())); + publisher = database.publisher().get(book.getPublisher().getName()); + } + } else publisher = book.getPublisher(); + + //tableCategory exists ? + Category category; + if (book.getCategory().getId() == 0) { + Category dbCategory = database.category().get(book.getCategory().getName()); + if (dbCategory != null) + category = dbCategory; + else { + insertCount += database.category().insert(new Category(book.getCategory().getName(), database.getRandomImage())); + category = database.category().get(book.getCategory().getName()); + } + } else category = book.getCategory(); + + assert publisher != null; + assert category != null; + + //insert tableBook + try { + PreparedStatement statement = connection.prepareStatement("INSERT INTO book(isbn, title, description, " + + "price, year, publisher, category, image) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS); + statement.setLong(1, book.getIsbn()); + statement.setString(2, book.getTitle()); + statement.setString(3, book.getDescription()); + statement.setInt(4, book.getPrice()); + statement.setInt(5, book.getYear().getValue()); + statement.setLong(6, publisher.getId()); + statement.setLong(7, category.getId()); + statement.setString(8, book.getImage()); + insertCount += statement.executeUpdate(); + ResultSet resultSet = statement.getGeneratedKeys(); + long lastId = -1; + while (resultSet.next()) { + lastId = resultSet.getLong("id"); + } + + //add tableBook to tableAuthor + for (Author author : authors) { + PreparedStatement authorStatement = connection.prepareStatement("INSERT INTO " + database.tableAuthorBook + " (author, book) VALUES (?, ?);"); + authorStatement.setLong(1, author.getId()); + authorStatement.setLong(2, lastId); + insertCount += authorStatement.executeUpdate(); + } + + connection.close(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + return insertCount; + } +} \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/CategoryDB.java b/src/main/java/de/hsel/itech/db/CategoryDB.java new file mode 100644 index 0000000..0276309 --- /dev/null +++ b/src/main/java/de/hsel/itech/db/CategoryDB.java @@ -0,0 +1,133 @@ +package de.hsel.itech.db; + +import de.hsel.itech.db.pojo.Category; +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.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.5 + **/ +public class CategoryDB { + + private Database database; + + CategoryDB(Database database) { + this.database = database; + } + + /** + * gets tableCategory by id. + * + * @param id ID from database + * @return {@link de.hsel.itech.db.pojo.Category} + */ + @Nullable + public Category get(long id) { + Map.Entry entry = database.getResultSetById(database.tableCategory, id); + if(entry != null) { + ResultSet rs = entry.getKey(); + + try { + rs.beforeFirst(); + } catch (SQLException e) { + e.printStackTrace(); + } + return get(entry); + } + return null; + } + + /** + * getAll tableCategory by name. + * + * @param name tableCategory name + * @return {@link de.hsel.itech.db.pojo.Category} + */ + @Nullable + public Category get(@NotNull String name) { + Map.Entry entry = database.getResultSetsByValue(database.tableCategory, "name", name); + assert entry != null; + + return get(entry); + } + + /** + * getAll Category from database entry. + * @param entry entry + * @return Category + */ + @Nullable + private Category get(@NotNull Map.Entry entry) { + ResultSet rs = entry.getKey(); + Category category = null; + try { + while (rs.next()) { + category = new Category(rs.getLong("id"), rs.getString("name"), rs.getString("image")); + } + entry.getValue().close(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + return category; + } + + /** + * getAll all categories. + * + * @return list of categories + */ + @Nullable + public List getAll() { + List ids = database.getIds(database.tableCategory); + List categories = new ArrayList<>(); + for (long id : ids) { + categories.add(get(id)); + } + return categories; + } + + /** + * deletes tableCategory from database. + * + * @param id tableCategory count + * @return deletion count + */ + public int delete(long id) { + return database.delete(id, database.tableCategory); + } + + /** + * inserts Category into database. + * + * @param category Category + * @return insert count + */ + public int insert(@NotNull Category category) { + Connection connection = database.getConnection(); + assert connection != null; + + int insertCount = 0; + + try { + PreparedStatement statement = connection.prepareStatement("INSERT INTO category (name, image) VALUES (?, ?)"); + statement.setString(1, category.getName()); + statement.setString(2, category.getImage()); + insertCount += statement.executeUpdate(); + + connection.close(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + return insertCount; + } +} \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/Database.java b/src/main/java/de/hsel/itech/db/Database.java index 42033e2..a9b89a0 100644 --- a/src/main/java/de/hsel/itech/db/Database.java +++ b/src/main/java/de/hsel/itech/db/Database.java @@ -1,10 +1,6 @@ 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.NotNull; import org.jetbrains.annotations.Nullable; import org.json.JSONObject; @@ -13,8 +9,10 @@ import org.mariadb.jdbc.MariaDbPoolDataSource; import java.io.*; import java.net.URL; import java.nio.charset.Charset; -import java.sql.*; -import java.time.Year; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.AbstractMap; import java.util.ArrayList; import java.util.List; @@ -29,11 +27,16 @@ import java.util.Map; */ public class Database { - private final String book = "book"; - private final String author = "author"; - private final String authorBook = "author_book"; - private final String publisher = "publisher"; - private final String category = "category"; + final String tableBook = "book"; + final String tableAuthor = "author"; + final String tableAuthorBook = "author_book"; + final String tablePublisher = "publisher"; + final String tableCategory = "category"; + + private AuthorDB authorDB; + private BookDB bookDB; + private CategoryDB categoryDB; + private PublisherDB publisherDB; private static Database instance; @@ -88,7 +91,7 @@ public class Database { * @return {@link java.sql.Connection} */ @Nullable - private Connection getConnection() { + Connection getConnection() { try { return dataSource.getConnection(); } catch (SQLException e) { @@ -97,537 +100,30 @@ public class Database { return null; } - /** - * gets author by id. - * - * @param id ID from database - * @return {@link de.hsel.itech.db.pojo.Author} - */ - @Nullable - public Author getAuthor(long id) { - Map.Entry entry = getResultSetById(author, id); - 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; + public BookDB book() { + if(bookDB == null) + bookDB = new BookDB(this); + return bookDB; } - /** - * gets author by name. - * - * @param name author name - * @return {@link de.hsel.itech.db.pojo.Author} - */ - @Nullable - public Author getAuthor(@NotNull String name) { - Map.Entry entry = getResultSetsByValue(author, "name", name); - assert entry != null; - assert entry.getKey() != null; - - Author author = null; - ResultSet rs = entry.getKey(); - try { - if (rs.next()) - author = new Author(rs.getLong("id"), rs.getString("name")); - entry.getValue().close(); - } catch (SQLException e) { - e.printStackTrace(); - } - - return author; + public AuthorDB author() { + if(authorDB == null) + authorDB = new AuthorDB(this); + return authorDB; } - /** - * gets category by id. - * - * @param id ID from database - * @return {@link de.hsel.itech.db.pojo.Category} - */ - @Nullable - public Category getCategory(long id) { - Map.Entry entry = getResultSetById(category, id); - if(entry != null) { - ResultSet rs = entry.getKey(); - - try { - rs.beforeFirst(); - } catch (SQLException e) { - e.printStackTrace(); - } - return getCategory(entry); - } - return null; + public CategoryDB category() { + if (categoryDB == null) + categoryDB = new CategoryDB(this); + return categoryDB; } - /** - * get category by name. - * - * @param name category name - * @return {@link de.hsel.itech.db.pojo.Category} - */ - @Nullable - public Category getCategory(@NotNull String name) { - Map.Entry entry = getResultSetsByValue(category, "name", name); - assert entry != null; - - return getCategory(entry); + public PublisherDB publisher() { + if(publisherDB == null) + publisherDB = new PublisherDB(this); + return publisherDB; } - /** - * get Category from database entry. - * @param entry entry - * @return Category - */ - @Nullable - private Category getCategory(@NotNull Map.Entry entry) { - ResultSet rs = entry.getKey(); - Category category = null; - try { - while (rs.next()) { - category = new Category(rs.getLong("id"), rs.getString("name"), rs.getString("image")); - } - entry.getValue().close(); - } catch (SQLException ex) { - ex.printStackTrace(); - } - return category; - } - - /** - * get all categories. - * - * @return list of categories - */ - @Nullable - public List getCategories() { - List ids = getIds(category); - List categories = new ArrayList<>(); - for (long id : ids) { - categories.add(getCategory(id)); - } - return categories; - } - - - /** - * gets publisher by id. - * - * @param id ID from database - * @return {@link de.hsel.itech.db.pojo.Publisher} - */ - @Nullable - public Publisher getPublisher(long id) { - Map.Entry entry = getResultSetById(publisher, id); - 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; - } - - /** - * get publisher by name. - * - * @param name publisher name - * @return {@link de.hsel.itech.db.pojo.Publisher} - */ - @Nullable - public Publisher getPublisher(@NotNull String name) { - Map.Entry entry = getResultSetsByValue(publisher, "name", name); - assert entry != null; - assert entry.getKey() != null; - - Publisher publisher = null; - ResultSet rs = entry.getKey(); - try { - while (rs.next()) { - publisher = new Publisher(rs.getLong("id"), rs.getString("name")); - } - entry.getValue().close(); - } catch (SQLException e) { - e.printStackTrace(); - } - - return publisher; - } - - /** - * gets list of author ids from database. - * - * @param id book id from database - * @return {@link java.util.List} - */ - @Nullable - private List getAuthors(long id) { - Map.Entry entry = getResultSetsById(authorBook, book, id); - assert entry != null; - try { - List list = new ArrayList<>(); - ResultSet rs = entry.getKey(); - while (rs.next()) { - list.add(rs.getInt(author)); - } - entry.getValue().close(); - return list; - } catch (SQLException e) { - e.printStackTrace(); - } - return null; - } - - /** - * gets book by id. - * - * @param id ID from database - * @return {@link de.hsel.itech.db.pojo.Book} - */ - @Nullable - public Book getBook(long id) { - Map.Entry entry = getResultSetById(book, id); - assert entry != null; - return buildBook(entry); - } - - /** - * builds book from entry. - * - * @param entry Map.Entry - * @return Book - */ - @Nullable - private Book buildBook(@NotNull Map.Entry entry) { - - Book book = null; - ResultSet rs = entry.getKey(); - try { - Category category = getCategory(rs.getLong(this.category)); - Publisher publisher = getPublisher(rs.getLong(this.publisher)); - List authorIds = getAuthors(rs.getLong("id")); - assert category != null; - assert publisher != null; - 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"), 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) { - e.printStackTrace(); - } - return book; - } - - /** - * gets all books. - * - * @return {@link java.util.List} - */ - @Nullable - public List getBooks() { - List ids = getIds(book); - List books = new ArrayList<>(); - for (long id : ids) { - books.add(getBook(id)); - } - return books; - } - - /** - * gets books by category. - * - * @param category category id - * @return {@link java.util.List} - */ - @Nullable - public List getBooks(long category) { - Connection connection = getConnection(); - assert connection != null; - - List books = new ArrayList<>(); - try { - PreparedStatement statement = connection.prepareStatement("SELECT (id) FROM book WHERE category = ?"); - statement.setLong(1, category); - - ResultSet resultSet = statement.executeQuery(); - - while (resultSet.next()) { - books.add(getBook(resultSet.getLong("id"))); - } - - connection.close(); - } catch (SQLException e) { - e.printStackTrace(); - } - return books; - } - - /** - * get books by category. - * - * @param category Category object - * @return {@link java.util.List} - */ - @Nullable - public List getBooks(@NotNull Category category) { - return getBooks(category.getId()); - } - - /** - * exists book with isbn ? - * - * @param isbn isbn - * @return true if book with isbn exists - */ - public boolean existsBook(long isbn) { - Map.Entry entry = getResultSetByValue(book, "isbn", isbn); - try { - if (entry != null && entry.getKey() != null) { - entry.getValue().close(); - return true; - } - } catch (SQLException ex) { - ex.printStackTrace(); - } - return false; - } - - - /** - * Inserts book into database. - * authors, publishers, and category have to exist. - * - * @param book Book - * @return insert count - */ - public int insert(@NotNull Book book) { - Connection connection = getConnection(); - assert connection != null; - - int insertCount = 0; - - //author exists ? - List authors = new ArrayList<>(); - for (Author author : book.getAuthors()) { - if (author.getId() == 0) { - Author dbAuthor = getAuthor(author.getName()); - if (dbAuthor == null) { - insertCount += insert(author); - dbAuthor = getAuthor(author.getName()); - } - authors.add(dbAuthor); - } else authors.add(author); - } - - //publisher exists ? - Publisher publisher; - if (book.getPublisher().getId() == 0) { - Publisher dbPublisher = getPublisher(book.getPublisher().getName()); - if (dbPublisher != null) - publisher = dbPublisher; - else { - insertCount += insert(new Publisher(book.getPublisher().getName())); - publisher = getPublisher(book.getPublisher().getName()); - } - } else publisher = book.getPublisher(); - - //category exists ? - Category category; - if (book.getCategory().getId() == 0) { - Category dbCategory = getCategory(book.getCategory().getName()); - if (dbCategory != null) - category = dbCategory; - else { - insertCount += insert(new Category(book.getCategory().getName(), getRandomImage())); - category = getCategory(book.getCategory().getName()); - } - } else category = book.getCategory(); - - assert publisher != null; - assert category != null; - - //insert book - try { - PreparedStatement statement = connection.prepareStatement("INSERT INTO book(isbn, title, description, " + - "price, year, publisher, category, image) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS); - statement.setLong(1, book.getIsbn()); - statement.setString(2, book.getTitle()); - statement.setString(3, book.getDescription()); - statement.setInt(4, book.getPrice()); - statement.setInt(5, book.getYear().getValue()); - statement.setLong(6, publisher.getId()); - statement.setLong(7, category.getId()); - statement.setString(8, book.getImage()); - insertCount += statement.executeUpdate(); - ResultSet resultSet = statement.getGeneratedKeys(); - long lastId = -1; - while (resultSet.next()) { - lastId = resultSet.getLong("id"); - } - - //add book to author - for (Author author : authors) { - PreparedStatement authorStatement = connection.prepareStatement("INSERT INTO " + authorBook + " (author, book) VALUES (?, ?);"); - authorStatement.setLong(1, author.getId()); - authorStatement.setLong(2, lastId); - insertCount += authorStatement.executeUpdate(); - } - - connection.close(); - } catch (SQLException ex) { - ex.printStackTrace(); - } - return insertCount; - } - - /** - * inserts Category into database. - * - * @param category Category - * @return insert count - */ - public int insert(@NotNull Category category) { - Connection connection = getConnection(); - assert connection != null; - - int insertCount = 0; - - try { - PreparedStatement statement = connection.prepareStatement("INSERT INTO category (name, image) VALUES (?, ?)"); - statement.setString(1, category.getName()); - statement.setString(2, category.getImage()); - insertCount += statement.executeUpdate(); - - connection.close(); - } catch (SQLException ex) { - ex.printStackTrace(); - } - return insertCount; - } - - /** - * inserts Author. - * - * @param author Author - * @return insert count - */ - public int insert(@NotNull Author author) { - Connection connection = getConnection(); - assert connection != null; - - int insertCount = 0; - try { - PreparedStatement statement = connection.prepareStatement("INSERT INTO author (name) VALUES (?)"); - statement.setString(1, author.getName()); - - insertCount += statement.executeUpdate(); - - connection.close(); - } catch (SQLException ex) { - ex.printStackTrace(); - } - return insertCount; - } - - /** - * inserts Publisher. - * - * @param publisher Publisher - * @return insert count - */ - public int insert(@NotNull Publisher publisher) { - Connection connection = getConnection(); - assert connection != null; - - int insertCount = 0; - try { - PreparedStatement statement = connection.prepareStatement("INSERT INTO publisher (name) VALUES (?)"); - statement.setString(1, publisher.getName()); - - insertCount += statement.executeUpdate(); - - connection.close(); - } catch (SQLException ex) { - ex.printStackTrace(); - } - return insertCount; - } - - /** - * deletes book from database. - * - * @param id book id - * @return deletion count - */ - public int deleteBook(long id) { - int deleteCount = 0; - - try { - Connection connection = getConnection(); - assert connection != null; - - PreparedStatement statement = connection.prepareStatement("DELETE FROM author_book WHERE book = ?"); - statement.setLong(1, id); - deleteCount = +statement.executeUpdate(); - - connection.close(); - } catch (SQLException ex) { - ex.printStackTrace(); - } - - deleteCount += delete(id, book); - - return deleteCount; - } - - /** - * deletes author from database. - * - * @param id author id - * @return deletion count - */ - public int deleteAuthor(long id) { - return delete(id, author); - } - - /** - * deletes publisher from database. - * - * @param id publisher id - * @return deletion count - */ - public int deletePublisher(long id) { - return delete(id, publisher); - } - - /** - * deletes category from database. - * - * @param id category count - * @return deletion count - */ - public int deleteCategory(long id) { - return delete(id, category); - } /** @@ -637,7 +133,7 @@ public class Database { * @param table table name * @return deletion count */ - private int delete(long id, @NotNull String table) { + int delete(long id, @NotNull String table) { Connection connection = getConnection(); assert connection != null; @@ -655,12 +151,12 @@ public class Database { } /** - * get all ids from specified table. + * getAll all ids from specified table. * * @param table table * @return list of ids */ - private List getIds(@NotNull String table) { + List getIds(@NotNull String table) { Map.Entry entry = getColumnsFromResultSet(table, "id"); assert entry != null; @@ -687,12 +183,12 @@ public class Database { * @return {@link java.util.Map.Entry} */ @Nullable - private Map.Entry getResultSetById(@NotNull String table, long id) { + Map.Entry getResultSetById(@NotNull String table, long id) { return getResultSetByValue(table, "id", id); } @Nullable - private Map.Entry getResultSetByValue(@NotNull String table, @NotNull String column, long value) { + Map.Entry getResultSetByValue(@NotNull String table, @NotNull String column, long value) { Connection connection = getConnection(); try { assert connection != null; @@ -719,7 +215,7 @@ public class Database { * @return {@link java.util.Map.Entry} */ @Nullable - private Map.Entry getResultSetsById(@NotNull String table, @NotNull String column, long id) { + Map.Entry getResultSetsById(@NotNull String table, @NotNull String column, long id) { Connection connection = getConnection(); try { assert connection != null; @@ -741,7 +237,7 @@ public class Database { * @return {@link java.util.Map.Entry} */ @Nullable - private Map.Entry getResultSetsByValue(@NotNull String table, @NotNull String column, @NotNull String value) { + Map.Entry getResultSetsByValue(@NotNull String table, @NotNull String column, @NotNull String value) { Connection connection = getConnection(); try { assert connection != null; @@ -795,10 +291,9 @@ public class Database { } @Nullable - private String getRandomImage() { + String getRandomImage() { try { - InputStream is = new URL("https://api.unsplash.com/photos/random?client_id=" + Configuration.get().getUnsplash().getAccessKey()).openStream(); - try { + try (InputStream is = new URL("https://api.unsplash.com/photos/random?client_id=" + Configuration.get().getUnsplash().getAccessKey()).openStream()) { BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); StringBuilder sb = new StringBuilder(); int cp; @@ -807,8 +302,6 @@ public class Database { } JSONObject json = new JSONObject(sb.toString()); return json.getString("id"); - } finally { - is.close(); } diff --git a/src/main/java/de/hsel/itech/db/PublisherDB.java b/src/main/java/de/hsel/itech/db/PublisherDB.java new file mode 100644 index 0000000..bc52179 --- /dev/null +++ b/src/main/java/de/hsel/itech/db/PublisherDB.java @@ -0,0 +1,110 @@ +package de.hsel.itech.db; + +import de.hsel.itech.db.pojo.Publisher; +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.util.Map; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.5 + **/ +public class PublisherDB { + + private Database database; + + PublisherDB(Database database) { + this.database = database; + } + + /** + * gets tablePublisher by id. + * + * @param id ID from database + * @return {@link de.hsel.itech.db.pojo.Publisher} + */ + @Nullable + public Publisher get(long id) { + Map.Entry entry = database.getResultSetById(database.tablePublisher, id); + 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; + } + + /** + * getAll tablePublisher by name. + * + * @param name tablePublisher name + * @return {@link de.hsel.itech.db.pojo.Publisher} + */ + @Nullable + public Publisher get(@NotNull String name) { + Map.Entry entry = database.getResultSetsByValue(database.tablePublisher, "name", name); + assert entry != null; + assert entry.getKey() != null; + + Publisher publisher = null; + ResultSet rs = entry.getKey(); + try { + while (rs.next()) { + publisher = new Publisher(rs.getLong("id"), rs.getString("name")); + } + entry.getValue().close(); + } catch (SQLException e) { + e.printStackTrace(); + } + + return publisher; + } + + + + + /** + * inserts Publisher. + * + * @param publisher Publisher + * @return insert count + */ + public int insert(@NotNull Publisher publisher) { + Connection connection = database.getConnection(); + assert connection != null; + + int insertCount = 0; + try { + PreparedStatement statement = connection.prepareStatement("INSERT INTO publisher (name) VALUES (?)"); + statement.setString(1, publisher.getName()); + + insertCount += statement.executeUpdate(); + + connection.close(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + return insertCount; + } + + /** + * deletes tablePublisher from database. + * + * @param id tablePublisher id + * @return deletion count + */ + public int delete(long id) { + return database.delete(id, database.tablePublisher); + } +} \ 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 e110d87..0d29601 100644 --- a/src/main/java/de/hsel/itech/db/pojo/Book.java +++ b/src/main/java/de/hsel/itech/db/pojo/Book.java @@ -9,7 +9,7 @@ import java.time.Year; import java.util.List; /** - * POJO for Book. + * POJO for BookDB. * * @author Johannes Theiner * @version 0.1 diff --git a/src/main/java/de/hsel/itech/jsf/CategoryBean.java b/src/main/java/de/hsel/itech/jsf/CategoryBean.java index 325fbf8..e61b49e 100644 --- a/src/main/java/de/hsel/itech/jsf/CategoryBean.java +++ b/src/main/java/de/hsel/itech/jsf/CategoryBean.java @@ -33,10 +33,10 @@ public class CategoryBean implements Serializable { } public List getCategories() { - return Database.getInstance().getCategories(); + return Database.getInstance().category().getAll(); } public List getBooks() { - return Database.getInstance().getBooks(category); + return Database.getInstance().book().getAll(category); } } \ 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 new file mode 100644 index 0000000..1fc50a6 --- /dev/null +++ b/src/main/java/de/hsel/itech/jsf/SettingsBean.java @@ -0,0 +1,24 @@ +package de.hsel.itech.jsf; + +import lombok.Getter; +import lombok.Setter; + +import javax.faces.bean.ManagedBean; +import javax.faces.bean.SessionScoped; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.5 + **/ + +@ManagedBean +@SessionScoped +public class SettingsBean { + + + @Getter + @Setter + private boolean darkTheme = false; + +} \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/servlet/BookList.java b/src/main/java/de/hsel/itech/servlet/BookList.java index b2f2ddc..71e7209 100644 --- a/src/main/java/de/hsel/itech/servlet/BookList.java +++ b/src/main/java/de/hsel/itech/servlet/BookList.java @@ -38,7 +38,7 @@ public class BookList extends HttpServlet { protected void doPost(@NotNull final HttpServletRequest req, @NotNull final HttpServletResponse resp) throws IOException { req.setCharacterEncoding(StandardCharsets.UTF_8.name()); - //get database object + //getAll database object Database db = Database.getInstance(); resp.setCharacterEncoding(StandardCharsets.UTF_8.name()); @@ -51,7 +51,7 @@ public class BookList extends HttpServlet { String param = req.getParameter("removeid"); if (param != null && Long.parseLong(param) != 0) { long isbn = Long.parseLong(param); - db.deleteBook(isbn); + db.book().delete(isbn); resp.sendRedirect("booklist"); } @@ -79,7 +79,7 @@ public class BookList extends HttpServlet { String image = req.getParameter("image"); //check isbn - if (db.existsBook(isbn)) { + if (db.book().exists(isbn)) { out.println(""); @@ -96,7 +96,7 @@ public class BookList extends HttpServlet { category, title, Year.of(year), price, description, image ); - db.insert(book); + db.book().insert(book); resp.sendRedirect("booklist"); } } catch (Exception e) { @@ -110,7 +110,7 @@ public class BookList extends HttpServlet { //list all books - List books = db.getBooks(); + List books = db.book().getAll(); if (books != null) { //book entries diff --git a/src/main/webapp/categories.jsp b/src/main/webapp/categories.jsp index fb879ff..e8b0398 100644 --- a/src/main/webapp/categories.jsp +++ b/src/main/webapp/categories.jsp @@ -12,7 +12,7 @@

Kategorien

<% - for(Category category : Objects.requireNonNull(Database.getInstance().getCategories())) { + for(Category category : Objects.requireNonNull(Database.getInstance().category().getAll())) { %>
diff --git a/src/main/webapp/category.jsp b/src/main/webapp/category.jsp index 3bbe69a..195f3e0 100644 --- a/src/main/webapp/category.jsp +++ b/src/main/webapp/category.jsp @@ -22,7 +22,7 @@ } else { long id = Long.valueOf(idValue); Database db = Database.getInstance(); - category = db.getCategory(id); + category = db.category().get(id); if (category != null) { %>
@@ -30,7 +30,7 @@
<% - for (Book book : Objects.requireNonNull(db.getBooks(category))) { + for (Book book : db.book().getAll(category)) { %>
diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index 8b1863a..57218fe 100644 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -8,6 +8,7 @@
  • Grobspezifikation
  • Backend
  • Kategorienübersicht
  • +
  • Warengruppe ausgeben
  • \ No newline at end of file diff --git a/src/main/webapp/template.xhtml b/src/main/webapp/template.xhtml index 387b93e..c1b8873 100644 --- a/src/main/webapp/template.xhtml +++ b/src/main/webapp/template.xhtml @@ -14,7 +14,7 @@ - + Amazon Light @@ -37,7 +37,7 @@