From b265adec2c2e21b0199eb5b0de1e59d4145260dd Mon Sep 17 00:00:00 2001 From: joethei Date: Tue, 30 Apr 2019 15:53:50 +0200 Subject: [PATCH] + existsBook + getCategories --- src/main/java/de/hsel/itech/db/Database.java | 110 +++++++++++++----- .../java/de/hsel/itech/servlet/BookList.java | 15 +-- 2 files changed, 82 insertions(+), 43 deletions(-) diff --git a/src/main/java/de/hsel/itech/db/Database.java b/src/main/java/de/hsel/itech/db/Database.java index 2e4e927..b473d2d 100644 --- a/src/main/java/de/hsel/itech/db/Database.java +++ b/src/main/java/de/hsel/itech/db/Database.java @@ -134,7 +134,7 @@ public class Database { Author author = null; ResultSet rs = entry.getKey(); try { - if(rs.next()) + if (rs.next()) author = new Author(rs.getLong("id"), rs.getString("name")); entry.getValue().close(); } catch (SQLException e) { @@ -182,17 +182,45 @@ public class Database { private Category getCategory(@NotNull Map.Entry entry) { ResultSet rs = entry.getKey(); Category category = null; - try{ - while(rs.next()) { + try { + while (rs.next()) { category = new Category(rs.getLong("id"), rs.getString("name"), rs.getString("image")); } entry.getValue().close(); - }catch (SQLException ex) { + } catch (SQLException ex) { ex.printStackTrace(); } return category; } + @Nullable + public List getCategories() { + List ids = getIds(category); + List categories = new ArrayList<>(); + for (long id : ids) { + categories.add(getCategory(id)); + } + return categories; + } + + private List getIds(String table) { + Map.Entry entry = getResultSets(table, "id"); + assert entry != null; + + + List ids = new ArrayList<>(); + try { + ResultSet rs = entry.getKey(); + while (rs.next()) { + ids.add(rs.getLong("id")); + } + entry.getValue().close(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + return ids; + } + /** * gets publisher by id. * @@ -230,7 +258,7 @@ public class Database { Publisher publisher = null; ResultSet rs = entry.getKey(); try { - while(rs.next()) { + while (rs.next()) { publisher = new Publisher(rs.getLong("id"), rs.getString("name")); } entry.getValue().close(); @@ -275,14 +303,30 @@ public class Database { public Book getBook(long id) { Map.Entry entry = getResultSet(id, book); assert entry != null; + return getBook(entry); + } + private Book getBook(@NotNull Map.Entry entry) { ResultSet rs = entry.getKey(); + + Book book = null; + try { + book = buildBook(rs); + entry.getValue().close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return book; + } + + @Nullable + private Book buildBook(ResultSet rs) { Book book = null; try { Category category = getCategory(rs.getLong(this.category)); Publisher publisher = getPublisher(rs.getLong(this.publisher)); - List authorIds = getAuthors(id); + List authorIds = getAuthors(rs.getLong("id")); assert category != null; assert publisher != null; assert authorIds != null; @@ -296,7 +340,7 @@ public class Database { 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(); } @@ -310,19 +354,7 @@ public class Database { */ @Nullable public List getBooks() { - Map.Entry entry = getResultSets(book, "id"); - assert entry != null; - - List ids = new ArrayList<>(); - try { - ResultSet rs = entry.getKey(); - while (rs.next()) { - ids.add(rs.getLong("id")); - } - entry.getValue().close(); - } catch (SQLException ex) { - ex.printStackTrace(); - } + List ids = getIds(book); List books = new ArrayList<>(); for (long id : ids) { books.add(getBook(id)); @@ -359,6 +391,19 @@ public class Database { return books; } + public boolean existsBook(long isbn) { + Map.Entry entry = getResultSet(book, "isbn", isbn); + try { + if (entry != null && entry.getKey() != null) { + entry.getValue().close(); + return true; + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + return false; + } + /** * get books by category. * @@ -394,32 +439,32 @@ public class Database { dbAuthor = getAuthor(author.getName()); } authors.add(dbAuthor); - }else authors.add(author); + } else authors.add(author); } //publisher exists ? Publisher publisher; - if(book.getPublisher().getId() == 0) { + if (book.getPublisher().getId() == 0) { Publisher dbPublisher = getPublisher(book.getPublisher().getName()); - if(dbPublisher != null) + if (dbPublisher != null) publisher = dbPublisher; else { insertCount += insert(new Publisher(book.getPublisher().getName())); publisher = getPublisher(book.getPublisher().getName()); } - }else publisher = book.getPublisher(); + } else publisher = book.getPublisher(); //category exists ? Category category; - if(book.getCategory().getId() == 0) { + if (book.getCategory().getId() == 0) { Category dbCategory = getCategory(book.getCategory().getName()); - if(dbCategory != null) + if (dbCategory != null) category = dbCategory; else { insertCount += insert(new Category(book.getCategory().getName(), book.getCategory().getImage())); category = getCategory(book.getCategory().getName()); } - }else category = book.getCategory(); + } else category = book.getCategory(); assert publisher != null; assert category != null; @@ -438,7 +483,7 @@ public class Database { insertCount += statement.executeUpdate(); ResultSet resultSet = statement.getGeneratedKeys(); long lastId = -1; - while(resultSet.next()) { + while (resultSet.next()) { lastId = resultSet.getLong("id"); } @@ -621,11 +666,16 @@ public class Database { */ @Nullable private Map.Entry getResultSet(long id, String table) { + return getResultSet(table, "id", id); + } + + @Nullable + private Map.Entry getResultSet(String table, String column, long value) { Connection connection = getConnection(); try { assert connection != null; - PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + table + " WHERE id = ?"); - statement.setLong(1, id); + 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); diff --git a/src/main/java/de/hsel/itech/servlet/BookList.java b/src/main/java/de/hsel/itech/servlet/BookList.java index 2e94022..99ac43c 100644 --- a/src/main/java/de/hsel/itech/servlet/BookList.java +++ b/src/main/java/de/hsel/itech/servlet/BookList.java @@ -74,20 +74,9 @@ public class BookList extends HttpServlet { String image = req.getParameter("image"); //check isbn - List books = db.getBooks(); - boolean found = false; - if (books != null) { - for(Book b : books){ - if(b.getIsbn() == isbn){ - found = true; - break; - } - } - } - - if (found) { + if (db.existsBook(isbn)) { out.println(""); } else { ArrayList authors = new ArrayList<>();