~ refactor database into multiple files
Signed-off-by: Johannes Theiner <j.theiner@live.de>
This commit is contained in:
parent
59d944fccd
commit
07924cfbdf
1
pom.xml
1
pom.xml
|
@ -147,6 +147,7 @@
|
|||
</dependency>
|
||||
|
||||
|
||||
<!--Database-->
|
||||
<dependency>
|
||||
<groupId>org.mariadb.jdbc</groupId>
|
||||
<artifactId>mariadb-java-client</artifactId>
|
||||
|
|
|
@ -0,0 +1,133 @@
|
|||
package de.hsel.itech.db;
|
||||
|
||||
import de.hsel.itech.db.pojo.Author;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.5
|
||||
**/
|
||||
public class AuthorDB {
|
||||
|
||||
private Database database;
|
||||
|
||||
|
||||
AuthorDB(Database database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets tableAuthor by id.
|
||||
*
|
||||
* @param id ID from database
|
||||
* @return {@link de.hsel.itech.db.pojo.Author}
|
||||
*/
|
||||
@Nullable
|
||||
public Author get(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = database.getResultSetById(database.tableAuthor, id);
|
||||
assert entry != null;
|
||||
|
||||
ResultSet rs = entry.getKey();
|
||||
Author author = null;
|
||||
try {
|
||||
author = new Author(rs.getLong("id"), rs.getString("name"));
|
||||
entry.getValue().close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return author;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets tableAuthor by name.
|
||||
*
|
||||
* @param name tableAuthor name
|
||||
* @return {@link de.hsel.itech.db.pojo.Author}
|
||||
*/
|
||||
@Nullable
|
||||
public Author get(@NotNull String name) {
|
||||
Map.Entry<ResultSet, Connection> entry = database.getResultSetsByValue(database.tableAuthor, "name", name);
|
||||
assert entry != null;
|
||||
assert entry.getKey() != null;
|
||||
|
||||
Author author = null;
|
||||
ResultSet rs = entry.getKey();
|
||||
try {
|
||||
if (rs.next())
|
||||
author = new Author(rs.getLong("id"), rs.getString("name"));
|
||||
entry.getValue().close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return author;
|
||||
}
|
||||
|
||||
/**
|
||||
* inserts Author.
|
||||
*
|
||||
* @param author Author
|
||||
* @return insert count
|
||||
*/
|
||||
public int insert(@NotNull Author author) {
|
||||
Connection connection = database.getConnection();
|
||||
assert connection != null;
|
||||
|
||||
int insertCount = 0;
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO author (name) VALUES (?)");
|
||||
statement.setString(1, author.getName());
|
||||
|
||||
insertCount += statement.executeUpdate();
|
||||
|
||||
connection.close();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return insertCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* deletes tableAuthor from database.
|
||||
*
|
||||
* @param id tableAuthor id
|
||||
* @return deletion count
|
||||
*/
|
||||
public int delete(long id) {
|
||||
return database.delete(id, database.tableAuthor);
|
||||
}
|
||||
|
||||
/**
|
||||
* gets list of tableAuthor ids from database.
|
||||
*
|
||||
* @param id tableBook id from database
|
||||
* @return {@link java.util.List}
|
||||
*/
|
||||
@Nullable
|
||||
List<Integer> getAll(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = database.getResultSetsById(database.tableAuthorBook, database.tableBook, id);
|
||||
assert entry != null;
|
||||
try {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
ResultSet rs = entry.getKey();
|
||||
while (rs.next()) {
|
||||
list.add(rs.getInt(database.tableAuthor));
|
||||
}
|
||||
entry.getValue().close();
|
||||
return list;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,265 @@
|
|||
package de.hsel.itech.db;
|
||||
|
||||
import de.hsel.itech.db.pojo.Author;
|
||||
import de.hsel.itech.db.pojo.Book;
|
||||
import de.hsel.itech.db.pojo.Category;
|
||||
import de.hsel.itech.db.pojo.Publisher;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.*;
|
||||
import java.time.Year;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.5
|
||||
**/
|
||||
public class BookDB {
|
||||
private Database database;
|
||||
|
||||
BookDB(Database database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets tableBook by id.
|
||||
*
|
||||
* @param id ID from database
|
||||
* @return {@link de.hsel.itech.db.pojo.Book}
|
||||
*/
|
||||
@Nullable
|
||||
public Book get(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = database.getResultSetById(database.tableBook, id);
|
||||
assert entry != null;
|
||||
return build(entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* builds tableBook from entry.
|
||||
*
|
||||
* @param entry Map.Entry
|
||||
* @return BookDB
|
||||
*/
|
||||
@Nullable
|
||||
private Book build(@NotNull Map.Entry<ResultSet, Connection> entry) {
|
||||
|
||||
Book book = null;
|
||||
ResultSet rs = entry.getKey();
|
||||
try {
|
||||
Category category = database.category().get(rs.getLong(database.tableCategory));
|
||||
Publisher publisher = database.publisher().get(rs.getLong(database.tablePublisher));
|
||||
List<Integer> authorIds = database.author().getAll(rs.getLong("id"));
|
||||
assert category != null;
|
||||
assert publisher != null;
|
||||
assert authorIds != null;
|
||||
|
||||
List<Author> authors = new ArrayList<>();
|
||||
for (int i : authorIds) {
|
||||
Author author = database.author().get(i);
|
||||
assert author != null;
|
||||
authors.add(author);
|
||||
}
|
||||
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();
|
||||
}
|
||||
return book;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets all books.
|
||||
*
|
||||
* @return {@link java.util.List}
|
||||
*/
|
||||
@Nullable
|
||||
public List<Book> getAll() {
|
||||
List<Long> ids = database.getIds(database.tableBook);
|
||||
List<Book> books = new ArrayList<>();
|
||||
for (long id : ids) {
|
||||
books.add(get(id));
|
||||
}
|
||||
return books;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets books by tableCategory.
|
||||
*
|
||||
* @param category tableCategory id
|
||||
* @return {@link java.util.List}
|
||||
*/
|
||||
@Nullable
|
||||
public List<Book> getAll(long category) {
|
||||
Connection connection = database.getConnection();
|
||||
assert connection != null;
|
||||
|
||||
List<Book> books = new ArrayList<>();
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT (id) FROM book WHERE category = ?");
|
||||
statement.setLong(1, category);
|
||||
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
|
||||
while (resultSet.next()) {
|
||||
books.add(get(resultSet.getLong("id")));
|
||||
}
|
||||
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return books;
|
||||
}
|
||||
|
||||
/**
|
||||
* getAll books by tableCategory.
|
||||
*
|
||||
* @param category Category object
|
||||
* @return {@link java.util.List}
|
||||
*/
|
||||
@Nullable
|
||||
public List<Book> getAll(@NotNull Category category) {
|
||||
return getAll(category.getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* exists tableBook with isbn ?
|
||||
*
|
||||
* @param isbn isbn
|
||||
* @return true if tableBook with isbn exists
|
||||
*/
|
||||
public boolean exists(long isbn) {
|
||||
Map.Entry<ResultSet, Connection> entry = database.getResultSetByValue(database.tableBook, "isbn", isbn);
|
||||
try {
|
||||
if (entry != null && entry.getKey() != null) {
|
||||
entry.getValue().close();
|
||||
return true;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* deletes tableBook from database.
|
||||
*
|
||||
* @param id tableBook id
|
||||
* @return deletion count
|
||||
*/
|
||||
public int delete(long id) {
|
||||
int deleteCount = 0;
|
||||
|
||||
try {
|
||||
Connection connection = database.getConnection();
|
||||
assert connection != null;
|
||||
|
||||
PreparedStatement statement = connection.prepareStatement("DELETE FROM author_book WHERE book = ?");
|
||||
statement.setLong(1, id);
|
||||
deleteCount = +statement.executeUpdate();
|
||||
|
||||
connection.close();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
deleteCount += database.delete(id, database.tableBook);
|
||||
|
||||
return deleteCount;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Inserts tableBook into database.
|
||||
* authors, publishers, and tableCategory have to exist.
|
||||
*
|
||||
* @param book BookDB
|
||||
* @return insert count
|
||||
*/
|
||||
public int insert(@NotNull Book book) {
|
||||
Connection connection = database.getConnection();
|
||||
assert connection != null;
|
||||
|
||||
int insertCount = 0;
|
||||
|
||||
//tableAuthor exists ?
|
||||
List<Author> authors = new ArrayList<>();
|
||||
for (Author author : book.getAuthors()) {
|
||||
if (author.getId() == 0) {
|
||||
Author dbAuthor = database.author().get(author.getName());
|
||||
if (dbAuthor == null) {
|
||||
insertCount += database.author().insert(author);
|
||||
dbAuthor = database.author().get(author.getName());
|
||||
}
|
||||
authors.add(dbAuthor);
|
||||
} else authors.add(author);
|
||||
}
|
||||
|
||||
//tablePublisher exists ?
|
||||
Publisher publisher;
|
||||
if (book.getPublisher().getId() == 0) {
|
||||
Publisher dbPublisher = database.publisher().get(book.getPublisher().getName());
|
||||
if (dbPublisher != null)
|
||||
publisher = dbPublisher;
|
||||
else {
|
||||
insertCount += database.publisher().insert(new Publisher(book.getPublisher().getName()));
|
||||
publisher = database.publisher().get(book.getPublisher().getName());
|
||||
}
|
||||
} else publisher = book.getPublisher();
|
||||
|
||||
//tableCategory exists ?
|
||||
Category category;
|
||||
if (book.getCategory().getId() == 0) {
|
||||
Category dbCategory = database.category().get(book.getCategory().getName());
|
||||
if (dbCategory != null)
|
||||
category = dbCategory;
|
||||
else {
|
||||
insertCount += database.category().insert(new Category(book.getCategory().getName(), database.getRandomImage()));
|
||||
category = database.category().get(book.getCategory().getName());
|
||||
}
|
||||
} else category = book.getCategory();
|
||||
|
||||
assert publisher != null;
|
||||
assert category != null;
|
||||
|
||||
//insert tableBook
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO book(isbn, title, description, " +
|
||||
"price, year, publisher, category, image) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
|
||||
statement.setLong(1, book.getIsbn());
|
||||
statement.setString(2, book.getTitle());
|
||||
statement.setString(3, book.getDescription());
|
||||
statement.setInt(4, book.getPrice());
|
||||
statement.setInt(5, book.getYear().getValue());
|
||||
statement.setLong(6, publisher.getId());
|
||||
statement.setLong(7, category.getId());
|
||||
statement.setString(8, book.getImage());
|
||||
insertCount += statement.executeUpdate();
|
||||
ResultSet resultSet = statement.getGeneratedKeys();
|
||||
long lastId = -1;
|
||||
while (resultSet.next()) {
|
||||
lastId = resultSet.getLong("id");
|
||||
}
|
||||
|
||||
//add tableBook to tableAuthor
|
||||
for (Author author : authors) {
|
||||
PreparedStatement authorStatement = connection.prepareStatement("INSERT INTO " + database.tableAuthorBook + " (author, book) VALUES (?, ?);");
|
||||
authorStatement.setLong(1, author.getId());
|
||||
authorStatement.setLong(2, lastId);
|
||||
insertCount += authorStatement.executeUpdate();
|
||||
}
|
||||
|
||||
connection.close();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return insertCount;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,133 @@
|
|||
package de.hsel.itech.db;
|
||||
|
||||
import de.hsel.itech.db.pojo.Category;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.5
|
||||
**/
|
||||
public class CategoryDB {
|
||||
|
||||
private Database database;
|
||||
|
||||
CategoryDB(Database database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets tableCategory by id.
|
||||
*
|
||||
* @param id ID from database
|
||||
* @return {@link de.hsel.itech.db.pojo.Category}
|
||||
*/
|
||||
@Nullable
|
||||
public Category get(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = database.getResultSetById(database.tableCategory, id);
|
||||
if(entry != null) {
|
||||
ResultSet rs = entry.getKey();
|
||||
|
||||
try {
|
||||
rs.beforeFirst();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return get(entry);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* getAll tableCategory by name.
|
||||
*
|
||||
* @param name tableCategory name
|
||||
* @return {@link de.hsel.itech.db.pojo.Category}
|
||||
*/
|
||||
@Nullable
|
||||
public Category get(@NotNull String name) {
|
||||
Map.Entry<ResultSet, Connection> entry = database.getResultSetsByValue(database.tableCategory, "name", name);
|
||||
assert entry != null;
|
||||
|
||||
return get(entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* getAll Category from database entry.
|
||||
* @param entry entry
|
||||
* @return Category
|
||||
*/
|
||||
@Nullable
|
||||
private Category get(@NotNull Map.Entry<ResultSet, Connection> entry) {
|
||||
ResultSet rs = entry.getKey();
|
||||
Category category = null;
|
||||
try {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* getAll all categories.
|
||||
*
|
||||
* @return list of categories
|
||||
*/
|
||||
@Nullable
|
||||
public List<Category> getAll() {
|
||||
List<Long> ids = database.getIds(database.tableCategory);
|
||||
List<Category> categories = new ArrayList<>();
|
||||
for (long id : ids) {
|
||||
categories.add(get(id));
|
||||
}
|
||||
return categories;
|
||||
}
|
||||
|
||||
/**
|
||||
* deletes tableCategory from database.
|
||||
*
|
||||
* @param id tableCategory count
|
||||
* @return deletion count
|
||||
*/
|
||||
public int delete(long id) {
|
||||
return database.delete(id, database.tableCategory);
|
||||
}
|
||||
|
||||
/**
|
||||
* inserts Category into database.
|
||||
*
|
||||
* @param category Category
|
||||
* @return insert count
|
||||
*/
|
||||
public int insert(@NotNull Category category) {
|
||||
Connection connection = database.getConnection();
|
||||
assert connection != null;
|
||||
|
||||
int insertCount = 0;
|
||||
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO category (name, image) VALUES (?, ?)");
|
||||
statement.setString(1, category.getName());
|
||||
statement.setString(2, category.getImage());
|
||||
insertCount += statement.executeUpdate();
|
||||
|
||||
connection.close();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return insertCount;
|
||||
}
|
||||
}
|
|
@ -1,10 +1,6 @@
|
|||
package de.hsel.itech.db;
|
||||
|
||||
import de.hsel.itech.config.Configuration;
|
||||
import de.hsel.itech.db.pojo.Author;
|
||||
import de.hsel.itech.db.pojo.Book;
|
||||
import de.hsel.itech.db.pojo.Category;
|
||||
import de.hsel.itech.db.pojo.Publisher;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.json.JSONObject;
|
||||
|
@ -13,8 +9,10 @@ import org.mariadb.jdbc.MariaDbPoolDataSource;
|
|||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
import java.sql.*;
|
||||
import java.time.Year;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.AbstractMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -29,11 +27,16 @@ import java.util.Map;
|
|||
*/
|
||||
public class Database {
|
||||
|
||||
private final String book = "book";
|
||||
private final String author = "author";
|
||||
private final String authorBook = "author_book";
|
||||
private final String publisher = "publisher";
|
||||
private final String category = "category";
|
||||
final String tableBook = "book";
|
||||
final String tableAuthor = "author";
|
||||
final String tableAuthorBook = "author_book";
|
||||
final String tablePublisher = "publisher";
|
||||
final String tableCategory = "category";
|
||||
|
||||
private AuthorDB authorDB;
|
||||
private BookDB bookDB;
|
||||
private CategoryDB categoryDB;
|
||||
private PublisherDB publisherDB;
|
||||
|
||||
private static Database instance;
|
||||
|
||||
|
@ -88,7 +91,7 @@ public class Database {
|
|||
* @return {@link java.sql.Connection}
|
||||
*/
|
||||
@Nullable
|
||||
private Connection getConnection() {
|
||||
Connection getConnection() {
|
||||
try {
|
||||
return dataSource.getConnection();
|
||||
} catch (SQLException e) {
|
||||
|
@ -97,537 +100,30 @@ public class Database {
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets author by id.
|
||||
*
|
||||
* @param id ID from database
|
||||
* @return {@link de.hsel.itech.db.pojo.Author}
|
||||
*/
|
||||
@Nullable
|
||||
public Author getAuthor(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSetById(author, id);
|
||||
assert entry != null;
|
||||
|
||||
ResultSet rs = entry.getKey();
|
||||
Author author = null;
|
||||
try {
|
||||
author = new Author(rs.getLong("id"), rs.getString("name"));
|
||||
entry.getValue().close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return author;
|
||||
public BookDB book() {
|
||||
if(bookDB == null)
|
||||
bookDB = new BookDB(this);
|
||||
return bookDB;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets author by name.
|
||||
*
|
||||
* @param name author name
|
||||
* @return {@link de.hsel.itech.db.pojo.Author}
|
||||
*/
|
||||
@Nullable
|
||||
public Author getAuthor(@NotNull String name) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSetsByValue(author, "name", name);
|
||||
assert entry != null;
|
||||
assert entry.getKey() != null;
|
||||
|
||||
Author author = null;
|
||||
ResultSet rs = entry.getKey();
|
||||
try {
|
||||
if (rs.next())
|
||||
author = new Author(rs.getLong("id"), rs.getString("name"));
|
||||
entry.getValue().close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return author;
|
||||
public AuthorDB author() {
|
||||
if(authorDB == null)
|
||||
authorDB = new AuthorDB(this);
|
||||
return authorDB;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets category by id.
|
||||
*
|
||||
* @param id ID from database
|
||||
* @return {@link de.hsel.itech.db.pojo.Category}
|
||||
*/
|
||||
@Nullable
|
||||
public Category getCategory(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSetById(category, id);
|
||||
if(entry != null) {
|
||||
ResultSet rs = entry.getKey();
|
||||
|
||||
try {
|
||||
rs.beforeFirst();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return getCategory(entry);
|
||||
}
|
||||
return null;
|
||||
public CategoryDB category() {
|
||||
if (categoryDB == null)
|
||||
categoryDB = new CategoryDB(this);
|
||||
return categoryDB;
|
||||
}
|
||||
|
||||
/**
|
||||
* get category by name.
|
||||
*
|
||||
* @param name category name
|
||||
* @return {@link de.hsel.itech.db.pojo.Category}
|
||||
*/
|
||||
@Nullable
|
||||
public Category getCategory(@NotNull String name) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSetsByValue(category, "name", name);
|
||||
assert entry != null;
|
||||
|
||||
return getCategory(entry);
|
||||
public PublisherDB publisher() {
|
||||
if(publisherDB == null)
|
||||
publisherDB = new PublisherDB(this);
|
||||
return publisherDB;
|
||||
}
|
||||
|
||||
/**
|
||||
* get Category from database entry.
|
||||
* @param entry entry
|
||||
* @return Category
|
||||
*/
|
||||
@Nullable
|
||||
private Category getCategory(@NotNull Map.Entry<ResultSet, Connection> entry) {
|
||||
ResultSet rs = entry.getKey();
|
||||
Category category = null;
|
||||
try {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* get all categories.
|
||||
*
|
||||
* @return list of categories
|
||||
*/
|
||||
@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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* gets publisher by id.
|
||||
*
|
||||
* @param id ID from database
|
||||
* @return {@link de.hsel.itech.db.pojo.Publisher}
|
||||
*/
|
||||
@Nullable
|
||||
public Publisher getPublisher(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSetById(publisher, id);
|
||||
assert entry != null;
|
||||
|
||||
ResultSet rs = entry.getKey();
|
||||
Publisher publisher = null;
|
||||
try {
|
||||
publisher = new Publisher(rs.getLong("id"), rs.getString("name"));
|
||||
entry.getValue().close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return publisher;
|
||||
}
|
||||
|
||||
/**
|
||||
* get publisher by name.
|
||||
*
|
||||
* @param name publisher name
|
||||
* @return {@link de.hsel.itech.db.pojo.Publisher}
|
||||
*/
|
||||
@Nullable
|
||||
public Publisher getPublisher(@NotNull String name) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSetsByValue(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.
|
||||
*
|
||||
* @param id book id from database
|
||||
* @return {@link java.util.List}
|
||||
*/
|
||||
@Nullable
|
||||
private List<Integer> getAuthors(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSetsById(authorBook, book, id);
|
||||
assert entry != null;
|
||||
try {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
ResultSet rs = entry.getKey();
|
||||
while (rs.next()) {
|
||||
list.add(rs.getInt(author));
|
||||
}
|
||||
entry.getValue().close();
|
||||
return list;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets book by id.
|
||||
*
|
||||
* @param id ID from database
|
||||
* @return {@link de.hsel.itech.db.pojo.Book}
|
||||
*/
|
||||
@Nullable
|
||||
public Book getBook(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSetById(book, id);
|
||||
assert entry != null;
|
||||
return buildBook(entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* builds book from entry.
|
||||
*
|
||||
* @param entry Map.Entry
|
||||
* @return Book
|
||||
*/
|
||||
@Nullable
|
||||
private Book buildBook(@NotNull Map.Entry<ResultSet, Connection> entry) {
|
||||
|
||||
Book book = null;
|
||||
ResultSet rs = entry.getKey();
|
||||
try {
|
||||
Category category = getCategory(rs.getLong(this.category));
|
||||
Publisher publisher = getPublisher(rs.getLong(this.publisher));
|
||||
List<Integer> authorIds = getAuthors(rs.getLong("id"));
|
||||
assert category != null;
|
||||
assert publisher != null;
|
||||
assert authorIds != null;
|
||||
|
||||
List<Author> authors = new ArrayList<>();
|
||||
for (int i : authorIds) {
|
||||
Author author = getAuthor(i);
|
||||
assert author != null;
|
||||
authors.add(author);
|
||||
}
|
||||
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();
|
||||
}
|
||||
return book;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets all books.
|
||||
*
|
||||
* @return {@link java.util.List}
|
||||
*/
|
||||
@Nullable
|
||||
public List<Book> getBooks() {
|
||||
List<Long> ids = getIds(book);
|
||||
List<Book> books = new ArrayList<>();
|
||||
for (long id : ids) {
|
||||
books.add(getBook(id));
|
||||
}
|
||||
return books;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets books by category.
|
||||
*
|
||||
* @param category category id
|
||||
* @return {@link java.util.List}
|
||||
*/
|
||||
@Nullable
|
||||
public List<Book> getBooks(long category) {
|
||||
Connection connection = getConnection();
|
||||
assert connection != null;
|
||||
|
||||
List<Book> books = new ArrayList<>();
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT (id) FROM book WHERE category = ?");
|
||||
statement.setLong(1, category);
|
||||
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
|
||||
while (resultSet.next()) {
|
||||
books.add(getBook(resultSet.getLong("id")));
|
||||
}
|
||||
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return books;
|
||||
}
|
||||
|
||||
/**
|
||||
* get books by category.
|
||||
*
|
||||
* @param category Category object
|
||||
* @return {@link java.util.List}
|
||||
*/
|
||||
@Nullable
|
||||
public List<Book> getBooks(@NotNull Category category) {
|
||||
return getBooks(category.getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* exists book with isbn ?
|
||||
*
|
||||
* @param isbn isbn
|
||||
* @return true if book with isbn exists
|
||||
*/
|
||||
public boolean existsBook(long isbn) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSetByValue(book, "isbn", isbn);
|
||||
try {
|
||||
if (entry != null && entry.getKey() != null) {
|
||||
entry.getValue().close();
|
||||
return true;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Inserts book into database.
|
||||
* authors, publishers, and category have to exist.
|
||||
*
|
||||
* @param book Book
|
||||
* @return insert count
|
||||
*/
|
||||
public int insert(@NotNull Book book) {
|
||||
Connection connection = getConnection();
|
||||
assert connection != null;
|
||||
|
||||
int insertCount = 0;
|
||||
|
||||
//author exists ?
|
||||
List<Author> authors = new ArrayList<>();
|
||||
for (Author author : book.getAuthors()) {
|
||||
if (author.getId() == 0) {
|
||||
Author dbAuthor = getAuthor(author.getName());
|
||||
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(), getRandomImage()));
|
||||
category = getCategory(book.getCategory().getName());
|
||||
}
|
||||
} else category = book.getCategory();
|
||||
|
||||
assert publisher != null;
|
||||
assert category != null;
|
||||
|
||||
//insert book
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO book(isbn, title, description, " +
|
||||
"price, year, publisher, category, image) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
|
||||
statement.setLong(1, book.getIsbn());
|
||||
statement.setString(2, book.getTitle());
|
||||
statement.setString(3, book.getDescription());
|
||||
statement.setInt(4, book.getPrice());
|
||||
statement.setInt(5, book.getYear().getValue());
|
||||
statement.setLong(6, publisher.getId());
|
||||
statement.setLong(7, category.getId());
|
||||
statement.setString(8, book.getImage());
|
||||
insertCount += statement.executeUpdate();
|
||||
ResultSet resultSet = statement.getGeneratedKeys();
|
||||
long lastId = -1;
|
||||
while (resultSet.next()) {
|
||||
lastId = resultSet.getLong("id");
|
||||
}
|
||||
|
||||
//add book to author
|
||||
for (Author author : authors) {
|
||||
PreparedStatement authorStatement = connection.prepareStatement("INSERT INTO " + authorBook + " (author, book) VALUES (?, ?);");
|
||||
authorStatement.setLong(1, author.getId());
|
||||
authorStatement.setLong(2, lastId);
|
||||
insertCount += authorStatement.executeUpdate();
|
||||
}
|
||||
|
||||
connection.close();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return insertCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* inserts Category into database.
|
||||
*
|
||||
* @param category Category
|
||||
* @return insert count
|
||||
*/
|
||||
public int insert(@NotNull Category category) {
|
||||
Connection connection = getConnection();
|
||||
assert connection != null;
|
||||
|
||||
int insertCount = 0;
|
||||
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO category (name, image) VALUES (?, ?)");
|
||||
statement.setString(1, category.getName());
|
||||
statement.setString(2, category.getImage());
|
||||
insertCount += statement.executeUpdate();
|
||||
|
||||
connection.close();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return insertCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* inserts Author.
|
||||
*
|
||||
* @param author Author
|
||||
* @return insert count
|
||||
*/
|
||||
public int insert(@NotNull Author author) {
|
||||
Connection connection = getConnection();
|
||||
assert connection != null;
|
||||
|
||||
int insertCount = 0;
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO author (name) VALUES (?)");
|
||||
statement.setString(1, author.getName());
|
||||
|
||||
insertCount += statement.executeUpdate();
|
||||
|
||||
connection.close();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return insertCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* inserts Publisher.
|
||||
*
|
||||
* @param publisher Publisher
|
||||
* @return insert count
|
||||
*/
|
||||
public int insert(@NotNull Publisher publisher) {
|
||||
Connection connection = getConnection();
|
||||
assert connection != null;
|
||||
|
||||
int insertCount = 0;
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO publisher (name) VALUES (?)");
|
||||
statement.setString(1, publisher.getName());
|
||||
|
||||
insertCount += statement.executeUpdate();
|
||||
|
||||
connection.close();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return insertCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* deletes book from database.
|
||||
*
|
||||
* @param id book id
|
||||
* @return deletion count
|
||||
*/
|
||||
public int deleteBook(long id) {
|
||||
int deleteCount = 0;
|
||||
|
||||
try {
|
||||
Connection connection = getConnection();
|
||||
assert connection != null;
|
||||
|
||||
PreparedStatement statement = connection.prepareStatement("DELETE FROM author_book WHERE book = ?");
|
||||
statement.setLong(1, id);
|
||||
deleteCount = +statement.executeUpdate();
|
||||
|
||||
connection.close();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
deleteCount += delete(id, book);
|
||||
|
||||
return deleteCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* deletes author from database.
|
||||
*
|
||||
* @param id author id
|
||||
* @return deletion count
|
||||
*/
|
||||
public int deleteAuthor(long id) {
|
||||
return delete(id, author);
|
||||
}
|
||||
|
||||
/**
|
||||
* deletes publisher from database.
|
||||
*
|
||||
* @param id publisher id
|
||||
* @return deletion count
|
||||
*/
|
||||
public int deletePublisher(long id) {
|
||||
return delete(id, publisher);
|
||||
}
|
||||
|
||||
/**
|
||||
* deletes category from database.
|
||||
*
|
||||
* @param id category count
|
||||
* @return deletion count
|
||||
*/
|
||||
public int deleteCategory(long id) {
|
||||
return delete(id, category);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
@ -637,7 +133,7 @@ public class Database {
|
|||
* @param table table name
|
||||
* @return deletion count
|
||||
*/
|
||||
private int delete(long id, @NotNull String table) {
|
||||
int delete(long id, @NotNull String table) {
|
||||
Connection connection = getConnection();
|
||||
assert connection != null;
|
||||
|
||||
|
@ -655,12 +151,12 @@ public class Database {
|
|||
}
|
||||
|
||||
/**
|
||||
* get all ids from specified table.
|
||||
* getAll all ids from specified table.
|
||||
*
|
||||
* @param table table
|
||||
* @return list of ids
|
||||
*/
|
||||
private List<Long> getIds(@NotNull String table) {
|
||||
List<Long> getIds(@NotNull String table) {
|
||||
Map.Entry<ResultSet, Connection> entry = getColumnsFromResultSet(table, "id");
|
||||
assert entry != null;
|
||||
|
||||
|
@ -687,12 +183,12 @@ public class Database {
|
|||
* @return {@link java.util.Map.Entry}
|
||||
*/
|
||||
@Nullable
|
||||
private Map.Entry<ResultSet, Connection> getResultSetById(@NotNull String table, long id) {
|
||||
Map.Entry<ResultSet, Connection> getResultSetById(@NotNull String table, long id) {
|
||||
return getResultSetByValue(table, "id", id);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Map.Entry<ResultSet, Connection> getResultSetByValue(@NotNull String table, @NotNull String column, long value) {
|
||||
Map.Entry<ResultSet, Connection> getResultSetByValue(@NotNull String table, @NotNull String column, long value) {
|
||||
Connection connection = getConnection();
|
||||
try {
|
||||
assert connection != null;
|
||||
|
@ -719,7 +215,7 @@ public class Database {
|
|||
* @return {@link java.util.Map.Entry}
|
||||
*/
|
||||
@Nullable
|
||||
private Map.Entry<ResultSet, Connection> getResultSetsById(@NotNull String table, @NotNull String column, long id) {
|
||||
Map.Entry<ResultSet, Connection> getResultSetsById(@NotNull String table, @NotNull String column, long id) {
|
||||
Connection connection = getConnection();
|
||||
try {
|
||||
assert connection != null;
|
||||
|
@ -741,7 +237,7 @@ public class Database {
|
|||
* @return {@link java.util.Map.Entry}
|
||||
*/
|
||||
@Nullable
|
||||
private Map.Entry<ResultSet, Connection> getResultSetsByValue(@NotNull String table, @NotNull String column, @NotNull String value) {
|
||||
Map.Entry<ResultSet, Connection> getResultSetsByValue(@NotNull String table, @NotNull String column, @NotNull String value) {
|
||||
Connection connection = getConnection();
|
||||
try {
|
||||
assert connection != null;
|
||||
|
@ -795,10 +291,9 @@ public class Database {
|
|||
}
|
||||
|
||||
@Nullable
|
||||
private String getRandomImage() {
|
||||
String getRandomImage() {
|
||||
try {
|
||||
InputStream is = new URL("https://api.unsplash.com/photos/random?client_id=" + Configuration.get().getUnsplash().getAccessKey()).openStream();
|
||||
try {
|
||||
try (InputStream is = new URL("https://api.unsplash.com/photos/random?client_id=" + Configuration.get().getUnsplash().getAccessKey()).openStream()) {
|
||||
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int cp;
|
||||
|
@ -807,8 +302,6 @@ public class Database {
|
|||
}
|
||||
JSONObject json = new JSONObject(sb.toString());
|
||||
return json.getString("id");
|
||||
} finally {
|
||||
is.close();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,110 @@
|
|||
package de.hsel.itech.db;
|
||||
|
||||
import de.hsel.itech.db.pojo.Publisher;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.5
|
||||
**/
|
||||
public class PublisherDB {
|
||||
|
||||
private Database database;
|
||||
|
||||
PublisherDB(Database database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets tablePublisher by id.
|
||||
*
|
||||
* @param id ID from database
|
||||
* @return {@link de.hsel.itech.db.pojo.Publisher}
|
||||
*/
|
||||
@Nullable
|
||||
public Publisher get(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = database.getResultSetById(database.tablePublisher, id);
|
||||
assert entry != null;
|
||||
|
||||
ResultSet rs = entry.getKey();
|
||||
Publisher publisher = null;
|
||||
try {
|
||||
publisher = new Publisher(rs.getLong("id"), rs.getString("name"));
|
||||
entry.getValue().close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return publisher;
|
||||
}
|
||||
|
||||
/**
|
||||
* getAll tablePublisher by name.
|
||||
*
|
||||
* @param name tablePublisher name
|
||||
* @return {@link de.hsel.itech.db.pojo.Publisher}
|
||||
*/
|
||||
@Nullable
|
||||
public Publisher get(@NotNull String name) {
|
||||
Map.Entry<ResultSet, Connection> entry = database.getResultSetsByValue(database.tablePublisher, "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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* inserts Publisher.
|
||||
*
|
||||
* @param publisher Publisher
|
||||
* @return insert count
|
||||
*/
|
||||
public int insert(@NotNull Publisher publisher) {
|
||||
Connection connection = database.getConnection();
|
||||
assert connection != null;
|
||||
|
||||
int insertCount = 0;
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO publisher (name) VALUES (?)");
|
||||
statement.setString(1, publisher.getName());
|
||||
|
||||
insertCount += statement.executeUpdate();
|
||||
|
||||
connection.close();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return insertCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* deletes tablePublisher from database.
|
||||
*
|
||||
* @param id tablePublisher id
|
||||
* @return deletion count
|
||||
*/
|
||||
public int delete(long id) {
|
||||
return database.delete(id, database.tablePublisher);
|
||||
}
|
||||
}
|
|
@ -9,7 +9,7 @@ import java.time.Year;
|
|||
import java.util.List;
|
||||
|
||||
/**
|
||||
* POJO for Book.
|
||||
* POJO for BookDB.
|
||||
*
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
|
|
|
@ -33,10 +33,10 @@ public class CategoryBean implements Serializable {
|
|||
}
|
||||
|
||||
public List<Category> getCategories() {
|
||||
return Database.getInstance().getCategories();
|
||||
return Database.getInstance().category().getAll();
|
||||
}
|
||||
|
||||
public List<Book> getBooks() {
|
||||
return Database.getInstance().getBooks(category);
|
||||
return Database.getInstance().book().getAll(category);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package de.hsel.itech.jsf;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.faces.bean.ManagedBean;
|
||||
import javax.faces.bean.SessionScoped;
|
||||
|
||||
/**
|
||||
* @author Johannes Theiner
|
||||
* @version 0.1
|
||||
* @since 0.5
|
||||
**/
|
||||
|
||||
@ManagedBean
|
||||
@SessionScoped
|
||||
public class SettingsBean {
|
||||
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private boolean darkTheme = false;
|
||||
|
||||
}
|
|
@ -38,7 +38,7 @@ public class BookList extends HttpServlet {
|
|||
protected void doPost(@NotNull final HttpServletRequest req, @NotNull final HttpServletResponse resp) throws IOException {
|
||||
req.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||
|
||||
//get database object
|
||||
//getAll database object
|
||||
Database db = Database.getInstance();
|
||||
|
||||
resp.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||
|
@ -51,7 +51,7 @@ public class BookList extends HttpServlet {
|
|||
String param = req.getParameter("removeid");
|
||||
if (param != null && Long.parseLong(param) != 0) {
|
||||
long isbn = Long.parseLong(param);
|
||||
db.deleteBook(isbn);
|
||||
db.book().delete(isbn);
|
||||
resp.sendRedirect("booklist");
|
||||
}
|
||||
|
||||
|
@ -79,7 +79,7 @@ public class BookList extends HttpServlet {
|
|||
String image = req.getParameter("image");
|
||||
|
||||
//check isbn
|
||||
if (db.existsBook(isbn)) {
|
||||
if (db.book().exists(isbn)) {
|
||||
out.println("<aside class =\"m-note m-danger\">");
|
||||
out.println("<h3>Es gibt bereits ein Buch mit dieser ISBN </h3>");
|
||||
out.println("</aside>");
|
||||
|
@ -96,7 +96,7 @@ public class BookList extends HttpServlet {
|
|||
category,
|
||||
title, Year.of(year), price, description, image
|
||||
);
|
||||
db.insert(book);
|
||||
db.book().insert(book);
|
||||
resp.sendRedirect("booklist");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
@ -110,7 +110,7 @@ public class BookList extends HttpServlet {
|
|||
|
||||
|
||||
//list all books
|
||||
List<Book> books = db.getBooks();
|
||||
List<Book> books = db.book().getAll();
|
||||
if (books != null) {
|
||||
|
||||
//book entries
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
<h1 class="m-text-center">Kategorien</h1>
|
||||
<div class="m-row">
|
||||
<%
|
||||
for(Category category : Objects.requireNonNull(Database.getInstance().getCategories())) {
|
||||
for(Category category : Objects.requireNonNull(Database.getInstance().category().getAll())) {
|
||||
%>
|
||||
<div class="m-col-t-4">
|
||||
<a href="category.jsp?id=<%= category.getId() %>">
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
} else {
|
||||
long id = Long.valueOf(idValue);
|
||||
Database db = Database.getInstance();
|
||||
category = db.getCategory(id);
|
||||
category = db.category().get(id);
|
||||
if (category != null) {
|
||||
%>
|
||||
<div class="m-container">
|
||||
|
@ -30,7 +30,7 @@
|
|||
</h1>
|
||||
<div class="m-row">
|
||||
<%
|
||||
for (Book book : Objects.requireNonNull(db.getBooks(category))) {
|
||||
for (Book book : db.book().getAll(category)) {
|
||||
%>
|
||||
<div class="m-col-t-4">
|
||||
<figure class="m-figure">
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
<li><a href="preview">Grobspezifikation</a></li>
|
||||
<li><a href="booklist">Backend</a></li>
|
||||
<li><a href="categories.jsp">Kategorienübersicht</a></li>
|
||||
<li><a href="category.jsf">Warengruppe ausgeben</a></li>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
<link href="css/custom.css" rel="stylesheet"/>
|
||||
<link href="css/slick-theme.css" rel="stylesheet"/>
|
||||
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.css"/>
|
||||
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.css"/>
|
||||
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css"/>
|
||||
|
||||
<title>Amazon Light</title>
|
||||
|
@ -37,7 +37,7 @@
|
|||
<div id="m-navbar-collapse" class="m-col-t-12 m-show-m m-col-m-none m-right-m">
|
||||
<div class="m-row">
|
||||
<ol class="m-col-t-6 m-col-m-none">
|
||||
<li><a href="#">Katalog</a></li>
|
||||
<li><a href="category.jsf">Katalog</a></li>
|
||||
<li><a href="#">(1) Warenkorb</a></li>
|
||||
</ol>
|
||||
<ol class="m-col-t-6 m-col-m-none" start="4">
|
||||
|
@ -55,14 +55,19 @@
|
|||
<a href="#"><i class="fas fa-ellipsis-v"/></a>
|
||||
<ol>
|
||||
<li>Dark Theme
|
||||
<div class="onoffswitch">
|
||||
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox"
|
||||
id="myonoffswitch" onchange="changeTheme()"/>
|
||||
<label class="onoffswitch-label" for="myonoffswitch">
|
||||
<span class="onoffswitch-inner"/>
|
||||
<span class="onoffswitch-switch"/>
|
||||
</label>
|
||||
</div>
|
||||
<h:form>
|
||||
<div class="onoffswitch">
|
||||
|
||||
<h:selectBooleanCheckbox styleClass="onoffswitch-checkbox"
|
||||
id="myonoffswitch"
|
||||
title="onoffswitch" onchange="changeTheme()"
|
||||
value="#{settingsBean.darkTheme}"/>
|
||||
<h:outputLabel styleClass="onoffswitch-label" for="myonoffswitch">
|
||||
<span class="onoffswitch-inner"/>
|
||||
<span class="onoffswitch-switch"/>
|
||||
</h:outputLabel>
|
||||
</div>
|
||||
</h:form>
|
||||
</li>
|
||||
</ol>
|
||||
</li>
|
||||
|
@ -132,8 +137,7 @@
|
|||
<script type="text/javascript" src="//cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.min.js"/>
|
||||
<script>
|
||||
function changeTheme() {
|
||||
|
||||
if (document.getElementById('myonoffswitch').checked) {
|
||||
if ('#{settingsBean.darkTheme}' === 'true') {
|
||||
swapStyleSheet("dark", "#22272e");
|
||||
} else {
|
||||
swapStyleSheet("light", "#cb4b16");
|
||||
|
@ -151,7 +155,8 @@
|
|||
infinite: true,
|
||||
slidesToShow: 6,
|
||||
slidesToScroll: 3
|
||||
})
|
||||
});
|
||||
changeTheme();
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
|
Loading…
Reference in New Issue