parent
027ebd2a46
commit
b265adec2c
|
@ -134,7 +134,7 @@ public class Database {
|
||||||
Author author = null;
|
Author author = null;
|
||||||
ResultSet rs = entry.getKey();
|
ResultSet rs = entry.getKey();
|
||||||
try {
|
try {
|
||||||
if(rs.next())
|
if (rs.next())
|
||||||
author = new Author(rs.getLong("id"), rs.getString("name"));
|
author = new Author(rs.getLong("id"), rs.getString("name"));
|
||||||
entry.getValue().close();
|
entry.getValue().close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
|
@ -182,17 +182,45 @@ public class Database {
|
||||||
private Category getCategory(@NotNull Map.Entry<ResultSet, Connection> entry) {
|
private Category getCategory(@NotNull Map.Entry<ResultSet, Connection> entry) {
|
||||||
ResultSet rs = entry.getKey();
|
ResultSet rs = entry.getKey();
|
||||||
Category category = null;
|
Category category = null;
|
||||||
try{
|
try {
|
||||||
while(rs.next()) {
|
while (rs.next()) {
|
||||||
category = new Category(rs.getLong("id"), rs.getString("name"), rs.getString("image"));
|
category = new Category(rs.getLong("id"), rs.getString("name"), rs.getString("image"));
|
||||||
}
|
}
|
||||||
entry.getValue().close();
|
entry.getValue().close();
|
||||||
}catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
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.
|
||||||
*
|
*
|
||||||
|
@ -230,7 +258,7 @@ public class Database {
|
||||||
Publisher publisher = null;
|
Publisher publisher = null;
|
||||||
ResultSet rs = entry.getKey();
|
ResultSet rs = entry.getKey();
|
||||||
try {
|
try {
|
||||||
while(rs.next()) {
|
while (rs.next()) {
|
||||||
publisher = new Publisher(rs.getLong("id"), rs.getString("name"));
|
publisher = new Publisher(rs.getLong("id"), rs.getString("name"));
|
||||||
}
|
}
|
||||||
entry.getValue().close();
|
entry.getValue().close();
|
||||||
|
@ -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.
|
||||||
*
|
*
|
||||||
|
@ -394,32 +439,32 @@ public class Database {
|
||||||
dbAuthor = getAuthor(author.getName());
|
dbAuthor = getAuthor(author.getName());
|
||||||
}
|
}
|
||||||
authors.add(dbAuthor);
|
authors.add(dbAuthor);
|
||||||
}else authors.add(author);
|
} else authors.add(author);
|
||||||
}
|
}
|
||||||
|
|
||||||
//publisher exists ?
|
//publisher exists ?
|
||||||
Publisher publisher;
|
Publisher publisher;
|
||||||
if(book.getPublisher().getId() == 0) {
|
if (book.getPublisher().getId() == 0) {
|
||||||
Publisher dbPublisher = getPublisher(book.getPublisher().getName());
|
Publisher dbPublisher = getPublisher(book.getPublisher().getName());
|
||||||
if(dbPublisher != null)
|
if (dbPublisher != null)
|
||||||
publisher = dbPublisher;
|
publisher = dbPublisher;
|
||||||
else {
|
else {
|
||||||
insertCount += insert(new Publisher(book.getPublisher().getName()));
|
insertCount += insert(new Publisher(book.getPublisher().getName()));
|
||||||
publisher = getPublisher(book.getPublisher().getName());
|
publisher = getPublisher(book.getPublisher().getName());
|
||||||
}
|
}
|
||||||
}else publisher = book.getPublisher();
|
} else publisher = book.getPublisher();
|
||||||
|
|
||||||
//category exists ?
|
//category exists ?
|
||||||
Category category;
|
Category category;
|
||||||
if(book.getCategory().getId() == 0) {
|
if (book.getCategory().getId() == 0) {
|
||||||
Category dbCategory = getCategory(book.getCategory().getName());
|
Category dbCategory = getCategory(book.getCategory().getName());
|
||||||
if(dbCategory != null)
|
if (dbCategory != null)
|
||||||
category = dbCategory;
|
category = dbCategory;
|
||||||
else {
|
else {
|
||||||
insertCount += insert(new Category(book.getCategory().getName(), book.getCategory().getImage()));
|
insertCount += insert(new Category(book.getCategory().getName(), book.getCategory().getImage()));
|
||||||
category = getCategory(book.getCategory().getName());
|
category = getCategory(book.getCategory().getName());
|
||||||
}
|
}
|
||||||
}else category = book.getCategory();
|
} else category = book.getCategory();
|
||||||
|
|
||||||
assert publisher != null;
|
assert publisher != null;
|
||||||
assert category != null;
|
assert category != null;
|
||||||
|
@ -438,7 +483,7 @@ public class Database {
|
||||||
insertCount += statement.executeUpdate();
|
insertCount += statement.executeUpdate();
|
||||||
ResultSet resultSet = statement.getGeneratedKeys();
|
ResultSet resultSet = statement.getGeneratedKeys();
|
||||||
long lastId = -1;
|
long lastId = -1;
|
||||||
while(resultSet.next()) {
|
while (resultSet.next()) {
|
||||||
lastId = resultSet.getLong("id");
|
lastId = resultSet.getLong("id");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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