parent
027ebd2a46
commit
b265adec2c
|
@ -193,6 +193,34 @@ public class Database {
|
||||||
return category;
|
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.
|
* gets publisher by id.
|
||||||
*
|
*
|
||||||
|
@ -275,14 +303,30 @@ public class Database {
|
||||||
public Book getBook(long id) {
|
public Book getBook(long id) {
|
||||||
Map.Entry<ResultSet, Connection> entry = getResultSet(id, book);
|
Map.Entry<ResultSet, Connection> entry = getResultSet(id, book);
|
||||||
assert entry != null;
|
assert entry != null;
|
||||||
|
return getBook(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Book getBook(@NotNull Map.Entry<ResultSet, Connection> entry) {
|
||||||
ResultSet rs = entry.getKey();
|
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;
|
Book book = null;
|
||||||
try {
|
try {
|
||||||
Category category = getCategory(rs.getLong(this.category));
|
Category category = getCategory(rs.getLong(this.category));
|
||||||
Publisher publisher = getPublisher(rs.getLong(this.publisher));
|
Publisher publisher = getPublisher(rs.getLong(this.publisher));
|
||||||
List<Integer> authorIds = getAuthors(id);
|
List<Integer> authorIds = getAuthors(rs.getLong("id"));
|
||||||
assert category != null;
|
assert category != null;
|
||||||
assert publisher != null;
|
assert publisher != null;
|
||||||
assert authorIds != 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"),
|
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"),
|
Year.of(rs.getInt("year")), rs.getInt("price"), rs.getString("description"),
|
||||||
rs.getString("image"));
|
rs.getString("image"));
|
||||||
entry.getValue().close();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -310,19 +354,7 @@ public class Database {
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public List<Book> getBooks() {
|
public List<Book> getBooks() {
|
||||||
Map.Entry<ResultSet, Connection> entry = getResultSets(book, "id");
|
List<Long> ids = getIds(book);
|
||||||
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<Book> books = new ArrayList<>();
|
List<Book> books = new ArrayList<>();
|
||||||
for (long id : ids) {
|
for (long id : ids) {
|
||||||
books.add(getBook(id));
|
books.add(getBook(id));
|
||||||
|
@ -359,6 +391,19 @@ public class Database {
|
||||||
return books;
|
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.
|
* get books by category.
|
||||||
*
|
*
|
||||||
|
@ -621,11 +666,16 @@ public class Database {
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
private Map.Entry<ResultSet, Connection> getResultSet(long id, String table) {
|
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();
|
Connection connection = getConnection();
|
||||||
try {
|
try {
|
||||||
assert connection != null;
|
assert connection != null;
|
||||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + table + " WHERE id = ?");
|
PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + table + " WHERE " + column + " = ?");
|
||||||
statement.setLong(1, id);
|
statement.setLong(1, value);
|
||||||
ResultSet resultSet = statement.executeQuery();
|
ResultSet resultSet = statement.executeQuery();
|
||||||
if (resultSet.next()) {
|
if (resultSet.next()) {
|
||||||
return new AbstractMap.SimpleEntry<>(resultSet, connection);
|
return new AbstractMap.SimpleEntry<>(resultSet, connection);
|
||||||
|
|
|
@ -74,20 +74,9 @@ public class BookList extends HttpServlet {
|
||||||
String image = req.getParameter("image");
|
String image = req.getParameter("image");
|
||||||
|
|
||||||
//check isbn
|
//check isbn
|
||||||
List<Book> books = db.getBooks();
|
if (db.existsBook(isbn)) {
|
||||||
boolean found = false;
|
|
||||||
if (books != null) {
|
|
||||||
for(Book b : books){
|
|
||||||
if(b.getIsbn() == isbn){
|
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (found) {
|
|
||||||
out.println("<aside class =\"m-note m-danger\">");
|
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>");
|
out.println("</aside>");
|
||||||
} else {
|
} else {
|
||||||
ArrayList<Author> authors = new ArrayList<>();
|
ArrayList<Author> authors = new ArrayList<>();
|
||||||
|
|
Loading…
Reference in New Issue