Meisten Bugs gefixt

This commit is contained in:
Johannes Theiner 2019-04-24 16:15:15 +02:00
parent c57fe04916
commit c9ae2db7e1
3 changed files with 171 additions and 72 deletions

View File

@ -59,7 +59,7 @@ public class Database {
private Database() { private Database() {
Configuration config = Configuration.get(); Configuration config = Configuration.get();
dataSource = new MariaDbPoolDataSource("jdbc:mysql://" + config.getDatabase().getHostname() 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 { try {
dataSource.setUser(config.getDatabase().getUsername()); dataSource.setUser(config.getDatabase().getUsername());
dataSource.setPassword(config.getDatabase().getPassword()); dataSource.setPassword(config.getDatabase().getPassword());
@ -146,24 +146,33 @@ public class Database {
return author; return author;
} }
/**
* gets author by name.
*
* @param name author name
* @return {@link de.hsel.itech.db.pojo.Author}
*/
@Nullable @Nullable
public Author getAuthor(String name) { public Author getAuthor(String name) {
Map.Entry<ResultSet, Connection> entry = getResultSets(author, "name", name);
assert entry != null;
assert entry.getKey() != null;
Author author = null; Author author = null;
ResultSet rs = entry.getKey();
Map.Entry<List<ResultSet>, Connection> set = getResultSets(this.author, "name", name);
if(set == null){
return null;
}
if (set.getKey().size() > 0) {
try { try {
author = new Author(set.getKey().get(0).getLong("id"), set.getKey().get(0).getString("name")); 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) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
}
return author; return author;
} }
@ -178,15 +187,47 @@ public class Database {
public Category getCategory(long id) { public Category getCategory(long id) {
Map.Entry<ResultSet, Connection> entry = getResultSet(id, category); Map.Entry<ResultSet, Connection> entry = getResultSet(id, category);
assert entry != null; assert entry != null;
ResultSet rs = entry.getKey(); ResultSet rs = entry.getKey();
Category category = null;
try { try {
category = new Category(rs.getLong("id"), rs.getString("name"), rs.getString("image")); while(rs.next()) {
entry.getValue().close(); System.out.println(rs.getLong("id"));
}
rs.beforeFirst();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); 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; return category;
} }
@ -212,6 +253,32 @@ public class Database {
return publisher; 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. * gets list of author ids from database.
* *
@ -220,11 +287,12 @@ public class Database {
*/ */
@Nullable @Nullable
private List<Integer> getAuthors(long id) { 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; assert entry != null;
try { try {
List<Integer> list = new ArrayList<>(); List<Integer> list = new ArrayList<>();
for (ResultSet rs : entry.getKey()) { ResultSet rs = entry.getKey();
while (rs.next()) {
list.add(rs.getInt(author)); list.add(rs.getInt(author));
} }
entry.getValue().close(); entry.getValue().close();
@ -252,7 +320,10 @@ public class Database {
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(id);
assert category != null;
assert publisher != null;
assert authorIds != null; assert authorIds != null;
List<Author> authors = new ArrayList<>(); List<Author> authors = new ArrayList<>();
for (int i : authorIds) { for (int i : authorIds) {
Author author = getAuthor(i); Author author = getAuthor(i);
@ -276,13 +347,14 @@ public class Database {
*/ */
@Nullable @Nullable
public List<Book> getBooks() { public List<Book> getBooks() {
Map.Entry<List<ResultSet>, Connection> entry = getResultSets(book, "id"); Map.Entry<ResultSet, Connection> entry = getResultSets(book, "id");
assert entry != null; assert entry != null;
List<Long> ids = new ArrayList<>(); List<Long> ids = new ArrayList<>();
try { try {
for (ResultSet resultSet : entry.getKey()) { ResultSet rs = entry.getKey();
ids.add(resultSet.getLong("id")); while (rs.next()) {
ids.add(rs.getLong("id"));
} }
entry.getValue().close(); entry.getValue().close();
} catch (SQLException ex) { } catch (SQLException ex) {
@ -349,6 +421,47 @@ public class Database {
int insertCount = 0; 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 { try {
PreparedStatement statement = connection.prepareStatement("INSERT INTO book(title, description, " PreparedStatement statement = connection.prepareStatement("INSERT INTO book(title, description, "
+ "price, year, publisher, category, image) VALUES (?, ?, ?, ?, ?, ?, ?)"); + "price, year, publisher, category, image) VALUES (?, ?, ?, ?, ?, ?, ?)");
@ -356,12 +469,12 @@ public class Database {
statement.setString(2, book.getDescription()); statement.setString(2, book.getDescription());
statement.setInt(3, book.getPrice()); statement.setInt(3, book.getPrice());
statement.setInt(4, book.getYear().getValue()); statement.setInt(4, book.getYear().getValue());
statement.setLong(5, book.getPublisher().getId()); statement.setLong(5, publisher.getId());
statement.setLong(6, book.getCategory().getId()); statement.setLong(6, category.getId());
statement.setString(7, book.getImage()); statement.setString(7, book.getImage());
insertCount += statement.executeUpdate(); insertCount += statement.executeUpdate();
for (Author author : book.getAuthors()) { for (Author author : authors) {
PreparedStatement authorStatement = connection.prepareStatement("INSERT INTO author_book (author, book) VALUES (?, ?)"); PreparedStatement authorStatement = connection.prepareStatement("INSERT INTO author_book (author, book) VALUES (?, ?)");
statement.setLong(1, author.getId()); statement.setLong(1, author.getId());
statement.setLong(2, book.getIsbn()); statement.setLong(2, book.getIsbn());
@ -519,9 +632,8 @@ public class Database {
int deleteCount = 0; int deleteCount = 0;
try { try {
PreparedStatement statement = connection.prepareStatement("DELETE FROM ? WHERE id=?"); PreparedStatement statement = connection.prepareStatement("DELETE FROM " + table + " WHERE id=?");
statement.setString(1, table); statement.setLong(1, id);
statement.setLong(2, id);
deleteCount = +statement.executeUpdate(); deleteCount = +statement.executeUpdate();
@ -544,9 +656,8 @@ public class Database {
Connection connection = getConnection(); Connection connection = getConnection();
try { try {
assert connection != null; assert connection != null;
PreparedStatement statement = connection.prepareStatement("SELECT * FROM ? WHERE ID = ?"); PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + table + " WHERE id = ?");
statement.setString(1, table); statement.setLong(1, id);
statement.setLong(2, id);
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);
@ -568,14 +679,12 @@ public class Database {
* @return {@link java.util.Map.Entry} * @return {@link java.util.Map.Entry}
*/ */
@Nullable @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(); Connection connection = getConnection();
try { try {
assert connection != null; assert connection != null;
PreparedStatement statement = connection.prepareStatement("SELECT * FROM ? WHERE ? = ?"); PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + table + " WHERE " + column + " = ?");
statement.setString(1, table); statement.setLong(1, id);
statement.setString(2, column);
statement.setLong(3, id);
return getAllEntries(connection, statement); return getAllEntries(connection, statement);
} catch (SQLException ex) { } catch (SQLException ex) {
@ -592,16 +701,12 @@ public class Database {
* @return {@link java.util.Map.Entry} * @return {@link java.util.Map.Entry}
*/ */
@Nullable @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(); Connection connection = getConnection();
try { try {
assert connection != null; assert connection != null;
PreparedStatement statement = connection.prepareStatement("SELECT * FROM ? WHERE ? = ?"); PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + table + " WHERE " + column + " = ?");
statement.setString(1, table); statement.setString(1, value);
statement.setString(2, column);
statement.setString(3, value);
System.out.println(statement);
return getAllEntries(connection, statement); return getAllEntries(connection, statement);
} catch (SQLException ex) { } catch (SQLException ex) {
@ -618,13 +723,11 @@ public class Database {
* @return {@link java.util.Map.Entry} * @return {@link java.util.Map.Entry}
*/ */
@Nullable @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(); Connection connection = getConnection();
try { try {
assert connection != null; assert connection != null;
PreparedStatement statement = connection.prepareStatement("SELECT (?) FROM ?"); PreparedStatement statement = connection.prepareStatement("SELECT (" + String.join(",", columns) + ") FROM " + table);
statement.setString(1, String.join(",", columns));
statement.setString(2, table);
return getAllEntries(connection, statement); return getAllEntries(connection, statement);
} catch (SQLException ex) { } catch (SQLException ex) {
@ -641,15 +744,10 @@ public class Database {
* @return {@link java.util.Map.Entry} * @return {@link java.util.Map.Entry}
*/ */
@Nullable @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 { try {
ResultSet resultSet = statement.executeQuery(); ResultSet resultSet = statement.executeQuery();
return new AbstractMap.SimpleEntry<>(resultSet, connection);
Map.Entry<List<ResultSet>, Connection> entry = new AbstractMap.SimpleEntry<>(new ArrayList<>(), connection);
while (resultSet.next()) {
entry.getKey().add(resultSet);
}
return entry;
} catch (SQLException ex) { } catch (SQLException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }

View File

@ -1,9 +1,6 @@
package de.hsel.itech.db.pojo; package de.hsel.itech.db.pojo;
import lombok.AllArgsConstructor; import lombok.*;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
/** /**
* POJO for Category. * POJO for Category.
@ -16,9 +13,15 @@ import lombok.RequiredArgsConstructor;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@RequiredArgsConstructor @RequiredArgsConstructor
@NoArgsConstructor
public class Category { public class Category {
private long id; private long id;
@NonNull private String name; @NonNull private String name;
@NonNull private String image; private String image;
public Category(String name, String image) {
this.name = name;
this.image = image;
}
} }

View File

@ -1,5 +1,6 @@
package de.hsel.itech.servlet; package de.hsel.itech.servlet;
import de.hsel.itech.db.Database;
import de.hsel.itech.db.pojo.Author; import de.hsel.itech.db.pojo.Author;
import de.hsel.itech.db.pojo.Book; import de.hsel.itech.db.pojo.Book;
import de.hsel.itech.db.pojo.Category; import de.hsel.itech.db.pojo.Category;
@ -21,7 +22,7 @@ public class BookList extends HttpServlet {
throws IOException { throws IOException {
//get database object //get database object
de.hsel.itech.db.Database db = de.hsel.itech.db.Database.getInstance(); Database db = Database.getInstance();
//print parameter (debug) //print parameter (debug)
Enumeration<String> names = req.getParameterNames(); Enumeration<String> names = req.getParameterNames();
@ -43,26 +44,23 @@ public class BookList extends HttpServlet {
String authorName = req.getParameter("author"); String authorName = req.getParameter("author");
int price = Integer.parseInt(req.getParameter("price")); int price = Integer.parseInt(req.getParameter("price"));
int year = Integer.parseInt(req.getParameter("year")); int year = Integer.parseInt(req.getParameter("year"));
String category = req.getParameter("category"); String categoryName = req.getParameter("category");
String description = req.getParameter("description"); String description = req.getParameter("description");
String publisher = req.getParameter("publisher"); String publisher = req.getParameter("publisher");
String image = req.getParameter("image"); String image = req.getParameter("image");
ArrayList<Author> authors = new ArrayList<Author>(); ArrayList<Author> authors = new ArrayList<>();
Author author = null; String[] authorNames = authorName.split(",");
for(String name : authorNames) {
author = db.getAuthor(authorName); authors.add(new Author(name));
if(author == null){
author = new Author(authorName);
db.insert(author);
} }
authors.add(author); Category category = new Category();
category.setName(categoryName);
Book book = new Book( Book book = new Book(
authors, authors,
new Publisher(publisher), new Publisher(publisher),
new Category(category, "DxAzOKSiPoE"), category,
title, Year.of(year),price,description, image title, Year.of(year),price,description, image
); );
db.insert(book); db.insert(book);