Meisten Bugs gefixt
This commit is contained in:
parent
c57fe04916
commit
c9ae2db7e1
@ -59,7 +59,7 @@ public class Database {
|
||||
private Database() {
|
||||
Configuration config = Configuration.get();
|
||||
dataSource = new MariaDbPoolDataSource("jdbc:mysql://" + config.getDatabase().getHostname()
|
||||
+ ":" + config.getDatabase().getPort() + "/" + config.getDatabase().getDatabase());
|
||||
+ ":" + config.getDatabase().getPort() + "/" + config.getDatabase().getDatabase() + "?useUnicode=true&characterEncoding=UTF-8");
|
||||
try {
|
||||
dataSource.setUser(config.getDatabase().getUsername());
|
||||
dataSource.setPassword(config.getDatabase().getPassword());
|
||||
@ -146,25 +146,34 @@ public class Database {
|
||||
return author;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets author by name.
|
||||
*
|
||||
* @param name author name
|
||||
* @return {@link de.hsel.itech.db.pojo.Author}
|
||||
*/
|
||||
@Nullable
|
||||
public Author getAuthor(String name) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSets(author, "name", name);
|
||||
assert entry != null;
|
||||
assert entry.getKey() != null;
|
||||
|
||||
Author author = null;
|
||||
|
||||
Map.Entry<List<ResultSet>, Connection> set = getResultSets(this.author, "name", name);
|
||||
|
||||
if(set == null){
|
||||
return null;
|
||||
}
|
||||
|
||||
if (set.getKey().size() > 0) {
|
||||
try {
|
||||
author = new Author(set.getKey().get(0).getLong("id"), set.getKey().get(0).getString("name"));
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
ResultSet rs = entry.getKey();
|
||||
try {
|
||||
while(rs.next()) {
|
||||
System.out.println(rs.getString("name"));
|
||||
}
|
||||
rs.beforeFirst();
|
||||
if(!rs.next()) {
|
||||
System.out.println("test");
|
||||
}else
|
||||
author = new Author(rs.getLong("id"), rs.getString("name"));
|
||||
entry.getValue().close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
return author;
|
||||
}
|
||||
|
||||
@ -178,15 +187,47 @@ public class Database {
|
||||
public Category getCategory(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSet(id, category);
|
||||
assert entry != null;
|
||||
|
||||
ResultSet rs = entry.getKey();
|
||||
Category category = null;
|
||||
|
||||
try {
|
||||
category = new Category(rs.getLong("id"), rs.getString("name"), rs.getString("image"));
|
||||
entry.getValue().close();
|
||||
while(rs.next()) {
|
||||
System.out.println(rs.getLong("id"));
|
||||
}
|
||||
|
||||
rs.beforeFirst();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return getCategory(entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* get category by name.
|
||||
*
|
||||
* @param name category name
|
||||
* @return {@link de.hsel.itech.db.pojo.Category}
|
||||
*/
|
||||
@Nullable
|
||||
public Category getCategory(String name) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSets(category, "name", name);
|
||||
assert entry != null;
|
||||
|
||||
return getCategory(entry);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Category getCategory(@NotNull Map.Entry<ResultSet, Connection> entry) {
|
||||
ResultSet rs = entry.getKey();
|
||||
Category category = null;
|
||||
try{
|
||||
System.out.println(rs.getStatement());
|
||||
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;
|
||||
}
|
||||
|
||||
@ -212,6 +253,32 @@ public class Database {
|
||||
return publisher;
|
||||
}
|
||||
|
||||
/**
|
||||
* get publisher by name.
|
||||
*
|
||||
* @param name publisher name
|
||||
* @return {@link de.hsel.itech.db.pojo.Publisher}
|
||||
*/
|
||||
@Nullable
|
||||
public Publisher getPublisher(String name) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSets(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.
|
||||
*
|
||||
@ -220,11 +287,12 @@ public class Database {
|
||||
*/
|
||||
@Nullable
|
||||
private List<Integer> getAuthors(long id) {
|
||||
Map.Entry<List<ResultSet>, Connection> entry = getResultSets(id, authorBook, book);
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSets(id, authorBook, book);
|
||||
assert entry != null;
|
||||
try {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
for (ResultSet rs : entry.getKey()) {
|
||||
ResultSet rs = entry.getKey();
|
||||
while (rs.next()) {
|
||||
list.add(rs.getInt(author));
|
||||
}
|
||||
entry.getValue().close();
|
||||
@ -252,7 +320,10 @@ public class Database {
|
||||
Category category = getCategory(rs.getLong(this.category));
|
||||
Publisher publisher = getPublisher(rs.getLong(this.publisher));
|
||||
List<Integer> authorIds = getAuthors(id);
|
||||
assert category != null;
|
||||
assert publisher != null;
|
||||
assert authorIds != null;
|
||||
|
||||
List<Author> authors = new ArrayList<>();
|
||||
for (int i : authorIds) {
|
||||
Author author = getAuthor(i);
|
||||
@ -276,13 +347,14 @@ public class Database {
|
||||
*/
|
||||
@Nullable
|
||||
public List<Book> getBooks() {
|
||||
Map.Entry<List<ResultSet>, Connection> entry = getResultSets(book, "id");
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSets(book, "id");
|
||||
assert entry != null;
|
||||
|
||||
List<Long> ids = new ArrayList<>();
|
||||
try {
|
||||
for (ResultSet resultSet : entry.getKey()) {
|
||||
ids.add(resultSet.getLong("id"));
|
||||
ResultSet rs = entry.getKey();
|
||||
while (rs.next()) {
|
||||
ids.add(rs.getLong("id"));
|
||||
}
|
||||
entry.getValue().close();
|
||||
} catch (SQLException ex) {
|
||||
@ -349,6 +421,47 @@ public class Database {
|
||||
|
||||
int insertCount = 0;
|
||||
|
||||
//author exists ?
|
||||
List<Author> authors = new ArrayList<>();
|
||||
for (Author author : book.getAuthors()) {
|
||||
if (author.getId() == 0) {
|
||||
Author dbAuthor = getAuthor(author.getName());
|
||||
System.out.println(dbAuthor);
|
||||
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(), book.getCategory().getImage()));
|
||||
category = getCategory(book.getCategory().getName());
|
||||
}
|
||||
}else category = book.getCategory();
|
||||
|
||||
assert publisher != null;
|
||||
assert category != null;
|
||||
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO book(title, description, "
|
||||
+ "price, year, publisher, category, image) VALUES (?, ?, ?, ?, ?, ?, ?)");
|
||||
@ -356,12 +469,12 @@ public class Database {
|
||||
statement.setString(2, book.getDescription());
|
||||
statement.setInt(3, book.getPrice());
|
||||
statement.setInt(4, book.getYear().getValue());
|
||||
statement.setLong(5, book.getPublisher().getId());
|
||||
statement.setLong(6, book.getCategory().getId());
|
||||
statement.setLong(5, publisher.getId());
|
||||
statement.setLong(6, category.getId());
|
||||
statement.setString(7, book.getImage());
|
||||
insertCount += statement.executeUpdate();
|
||||
|
||||
for (Author author : book.getAuthors()) {
|
||||
for (Author author : authors) {
|
||||
PreparedStatement authorStatement = connection.prepareStatement("INSERT INTO author_book (author, book) VALUES (?, ?)");
|
||||
statement.setLong(1, author.getId());
|
||||
statement.setLong(2, book.getIsbn());
|
||||
@ -519,9 +632,8 @@ public class Database {
|
||||
|
||||
int deleteCount = 0;
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("DELETE FROM ? WHERE id=?");
|
||||
statement.setString(1, table);
|
||||
statement.setLong(2, id);
|
||||
PreparedStatement statement = connection.prepareStatement("DELETE FROM " + table + " WHERE id=?");
|
||||
statement.setLong(1, id);
|
||||
|
||||
deleteCount = +statement.executeUpdate();
|
||||
|
||||
@ -544,9 +656,8 @@ public class Database {
|
||||
Connection connection = getConnection();
|
||||
try {
|
||||
assert connection != null;
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM ? WHERE ID = ?");
|
||||
statement.setString(1, table);
|
||||
statement.setLong(2, id);
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + table + " WHERE id = ?");
|
||||
statement.setLong(1, id);
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
if (resultSet.next()) {
|
||||
return new AbstractMap.SimpleEntry<>(resultSet, connection);
|
||||
@ -568,14 +679,12 @@ public class Database {
|
||||
* @return {@link java.util.Map.Entry}
|
||||
*/
|
||||
@Nullable
|
||||
private Map.Entry<List<ResultSet>, Connection> getResultSets(long id, @NotNull String table, @NotNull String column) {
|
||||
private Map.Entry<ResultSet, Connection> getResultSets(long id, @NotNull String table, @NotNull String column) {
|
||||
Connection connection = getConnection();
|
||||
try {
|
||||
assert connection != null;
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM ? WHERE ? = ?");
|
||||
statement.setString(1, table);
|
||||
statement.setString(2, column);
|
||||
statement.setLong(3, id);
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + table + " WHERE " + column + " = ?");
|
||||
statement.setLong(1, id);
|
||||
|
||||
return getAllEntries(connection, statement);
|
||||
} catch (SQLException ex) {
|
||||
@ -592,16 +701,12 @@ public class Database {
|
||||
* @return {@link java.util.Map.Entry}
|
||||
*/
|
||||
@Nullable
|
||||
private Map.Entry<List<ResultSet>, Connection> getResultSets(@NotNull String table, @NotNull String column, @NotNull String value) {
|
||||
private Map.Entry<ResultSet, Connection> getResultSets(@NotNull String table, @NotNull String column, @NotNull String value) {
|
||||
Connection connection = getConnection();
|
||||
try {
|
||||
assert connection != null;
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM ? WHERE ? = ?");
|
||||
statement.setString(1, table);
|
||||
statement.setString(2, column);
|
||||
statement.setString(3, value);
|
||||
|
||||
System.out.println(statement);
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + table + " WHERE " + column + " = ?");
|
||||
statement.setString(1, value);
|
||||
|
||||
return getAllEntries(connection, statement);
|
||||
} catch (SQLException ex) {
|
||||
@ -618,13 +723,11 @@ public class Database {
|
||||
* @return {@link java.util.Map.Entry}
|
||||
*/
|
||||
@Nullable
|
||||
private Map.Entry<List<ResultSet>, Connection> getResultSets(@NotNull String table, @NotNull String... columns) {
|
||||
private Map.Entry<ResultSet, Connection> getResultSets(@NotNull String table, @NotNull String... columns) {
|
||||
Connection connection = getConnection();
|
||||
try {
|
||||
assert connection != null;
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT (?) FROM ?");
|
||||
statement.setString(1, String.join(",", columns));
|
||||
statement.setString(2, table);
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT (" + String.join(",", columns) + ") FROM " + table);
|
||||
|
||||
return getAllEntries(connection, statement);
|
||||
} catch (SQLException ex) {
|
||||
@ -641,15 +744,10 @@ public class Database {
|
||||
* @return {@link java.util.Map.Entry}
|
||||
*/
|
||||
@Nullable
|
||||
private Map.Entry<List<ResultSet>, Connection> getAllEntries(@NotNull Connection connection, @NotNull PreparedStatement statement) {
|
||||
private Map.Entry<ResultSet, Connection> getAllEntries(@NotNull Connection connection, @NotNull PreparedStatement statement) {
|
||||
try {
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
|
||||
Map.Entry<List<ResultSet>, Connection> entry = new AbstractMap.SimpleEntry<>(new ArrayList<>(), connection);
|
||||
while (resultSet.next()) {
|
||||
entry.getKey().add(resultSet);
|
||||
}
|
||||
return entry;
|
||||
return new AbstractMap.SimpleEntry<>(resultSet, connection);
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
@ -1,9 +1,6 @@
|
||||
package de.hsel.itech.db.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.*;
|
||||
|
||||
/**
|
||||
* POJO for Category.
|
||||
@ -16,9 +13,15 @@ import lombok.RequiredArgsConstructor;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class Category {
|
||||
|
||||
private long id;
|
||||
@NonNull private String name;
|
||||
@NonNull private String image;
|
||||
private String image;
|
||||
|
||||
public Category(String name, String image) {
|
||||
this.name = name;
|
||||
this.image = image;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package de.hsel.itech.servlet;
|
||||
|
||||
import de.hsel.itech.db.Database;
|
||||
import de.hsel.itech.db.pojo.Author;
|
||||
import de.hsel.itech.db.pojo.Book;
|
||||
import de.hsel.itech.db.pojo.Category;
|
||||
@ -21,7 +22,7 @@ public class BookList extends HttpServlet {
|
||||
throws IOException {
|
||||
|
||||
//get database object
|
||||
de.hsel.itech.db.Database db = de.hsel.itech.db.Database.getInstance();
|
||||
Database db = Database.getInstance();
|
||||
|
||||
//print parameter (debug)
|
||||
Enumeration<String> names = req.getParameterNames();
|
||||
@ -43,26 +44,23 @@ public class BookList extends HttpServlet {
|
||||
String authorName = req.getParameter("author");
|
||||
int price = Integer.parseInt(req.getParameter("price"));
|
||||
int year = Integer.parseInt(req.getParameter("year"));
|
||||
String category = req.getParameter("category");
|
||||
String categoryName = req.getParameter("category");
|
||||
String description = req.getParameter("description");
|
||||
String publisher = req.getParameter("publisher");
|
||||
String image = req.getParameter("image");
|
||||
|
||||
|
||||
ArrayList<Author> authors = new ArrayList<Author>();
|
||||
Author author = null;
|
||||
|
||||
author = db.getAuthor(authorName);
|
||||
|
||||
if(author == null){
|
||||
author = new Author(authorName);
|
||||
db.insert(author);
|
||||
ArrayList<Author> authors = new ArrayList<>();
|
||||
String[] authorNames = authorName.split(",");
|
||||
for(String name : authorNames) {
|
||||
authors.add(new Author(name));
|
||||
}
|
||||
authors.add(author);
|
||||
Category category = new Category();
|
||||
category.setName(categoryName);
|
||||
Book book = new Book(
|
||||
authors,
|
||||
new Publisher(publisher),
|
||||
new Category(category, "DxAzOKSiPoE"),
|
||||
category,
|
||||
title, Year.of(year),price,description, image
|
||||
);
|
||||
db.insert(book);
|
||||
|
Loading…
Reference in New Issue
Block a user