parent
027ebd2a46
commit
b265adec2c
|
@ -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<ResultSet, Connection> 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<Category> getCategories() {
|
||||
List<Long> ids = getIds(category);
|
||||
List<Category> categories = new ArrayList<>();
|
||||
for (long id : ids) {
|
||||
categories.add(getCategory(id));
|
||||
}
|
||||
return categories;
|
||||
}
|
||||
|
||||
private List<Long> getIds(String table) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSets(table, "id");
|
||||
assert entry != null;
|
||||
|
||||
|
||||
List<Long> 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<ResultSet, Connection> entry = getResultSet(id, book);
|
||||
assert entry != null;
|
||||
return getBook(entry);
|
||||
}
|
||||
|
||||
|
||||
private Book getBook(@NotNull Map.Entry<ResultSet, Connection> 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<Integer> authorIds = getAuthors(id);
|
||||
List<Integer> 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<Book> getBooks() {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSets(book, "id");
|
||||
assert entry != null;
|
||||
|
||||
List<Long> 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<Long> ids = getIds(book);
|
||||
List<Book> 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<ResultSet, Connection> 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<ResultSet, Connection> getResultSet(long id, String table) {
|
||||
return getResultSet(table, "id", id);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Map.Entry<ResultSet, Connection> 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);
|
||||
|
|
|
@ -74,20 +74,9 @@ public class BookList extends HttpServlet {
|
|||
String image = req.getParameter("image");
|
||||
|
||||
//check isbn
|
||||
List<Book> 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("<aside class =\"m-note m-danger\">");
|
||||
out.println("<h3>Es gibt breits ein buch mit dieser ISBN </h3>");
|
||||
out.println("<h3>Es gibt bereits ein Buch mit dieser ISBN </h3>");
|
||||
out.println("</aside>");
|
||||
} else {
|
||||
ArrayList<Author> authors = new ArrayList<>();
|
||||
|
|
Loading…
Reference in New Issue