Merge branch 'julian2' of Studium/Internet-Technologien into master
This commit is contained in:
commit
2ef4187b37
7
pom.xml
7
pom.xml
@ -261,11 +261,18 @@
|
|||||||
<version>13.0</version>
|
<version>13.0</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.code.gson</groupId>
|
<groupId>com.google.code.gson</groupId>
|
||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
<version>2.8.0</version>
|
<version>2.8.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.json</groupId>
|
||||||
|
<artifactId>json</artifactId>
|
||||||
|
<version>20180813</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mariadb.jdbc</groupId>
|
<groupId>org.mariadb.jdbc</groupId>
|
||||||
|
@ -25,6 +25,8 @@ public class Configuration {
|
|||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private Database database;
|
private Database database;
|
||||||
|
@Getter
|
||||||
|
private Unsplash unsplash;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
20
src/main/java/de/hsel/itech/config/Unsplash.java
Normal file
20
src/main/java/de/hsel/itech/config/Unsplash.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package de.hsel.itech.config;
|
||||||
|
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Johannes Theiner
|
||||||
|
* @version 0.1
|
||||||
|
* @since 0.1
|
||||||
|
**/
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@ToString
|
||||||
|
@EqualsAndHashCode
|
||||||
|
public class Unsplash {
|
||||||
|
|
||||||
|
private String accessKey;
|
||||||
|
private String secretKey;
|
||||||
|
}
|
@ -7,12 +7,12 @@ import de.hsel.itech.db.pojo.Category;
|
|||||||
import de.hsel.itech.db.pojo.Publisher;
|
import de.hsel.itech.db.pojo.Publisher;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.json.JSONObject;
|
||||||
import org.mariadb.jdbc.MariaDbPoolDataSource;
|
import org.mariadb.jdbc.MariaDbPoolDataSource;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.*;
|
||||||
import java.io.FileReader;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.time.Year;
|
import java.time.Year;
|
||||||
import java.util.AbstractMap;
|
import java.util.AbstractMap;
|
||||||
@ -105,7 +105,7 @@ public class Database {
|
|||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public Author getAuthor(long id) {
|
public Author getAuthor(long id) {
|
||||||
Map.Entry<ResultSet, Connection> entry = getResultSet(id, author);
|
Map.Entry<ResultSet, Connection> entry = getResultSetById(author, id);
|
||||||
assert entry != null;
|
assert entry != null;
|
||||||
|
|
||||||
ResultSet rs = entry.getKey();
|
ResultSet rs = entry.getKey();
|
||||||
@ -126,15 +126,15 @@ public class Database {
|
|||||||
* @return {@link de.hsel.itech.db.pojo.Author}
|
* @return {@link de.hsel.itech.db.pojo.Author}
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public Author getAuthor(String name) {
|
public Author getAuthor(@NotNull String name) {
|
||||||
Map.Entry<ResultSet, Connection> entry = getResultSets(author, "name", name);
|
Map.Entry<ResultSet, Connection> entry = getResultSetsByValue(author, "name", name);
|
||||||
assert entry != null;
|
assert entry != null;
|
||||||
assert entry.getKey() != null;
|
assert entry.getKey() != null;
|
||||||
|
|
||||||
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) {
|
||||||
@ -152,7 +152,7 @@ public class Database {
|
|||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public Category getCategory(long id) {
|
public Category getCategory(long id) {
|
||||||
Map.Entry<ResultSet, Connection> entry = getResultSet(id, category);
|
Map.Entry<ResultSet, Connection> entry = getResultSetById(category, id);
|
||||||
assert entry != null;
|
assert entry != null;
|
||||||
ResultSet rs = entry.getKey();
|
ResultSet rs = entry.getKey();
|
||||||
|
|
||||||
@ -171,28 +171,49 @@ public class Database {
|
|||||||
* @return {@link de.hsel.itech.db.pojo.Category}
|
* @return {@link de.hsel.itech.db.pojo.Category}
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public Category getCategory(String name) {
|
public Category getCategory(@NotNull String name) {
|
||||||
Map.Entry<ResultSet, Connection> entry = getResultSets(category, "name", name);
|
Map.Entry<ResultSet, Connection> entry = getResultSetsByValue(category, "name", name);
|
||||||
assert entry != null;
|
assert entry != null;
|
||||||
|
|
||||||
return getCategory(entry);
|
return getCategory(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get Category from database entry.
|
||||||
|
* @param entry entry
|
||||||
|
* @return Category
|
||||||
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
* gets publisher by id.
|
||||||
*
|
*
|
||||||
@ -201,7 +222,7 @@ public class Database {
|
|||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public Publisher getPublisher(long id) {
|
public Publisher getPublisher(long id) {
|
||||||
Map.Entry<ResultSet, Connection> entry = getResultSet(id, publisher);
|
Map.Entry<ResultSet, Connection> entry = getResultSetById(publisher, id);
|
||||||
assert entry != null;
|
assert entry != null;
|
||||||
|
|
||||||
ResultSet rs = entry.getKey();
|
ResultSet rs = entry.getKey();
|
||||||
@ -222,15 +243,15 @@ public class Database {
|
|||||||
* @return {@link de.hsel.itech.db.pojo.Publisher}
|
* @return {@link de.hsel.itech.db.pojo.Publisher}
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public Publisher getPublisher(String name) {
|
public Publisher getPublisher(@NotNull String name) {
|
||||||
Map.Entry<ResultSet, Connection> entry = getResultSets(publisher, "name", name);
|
Map.Entry<ResultSet, Connection> entry = getResultSetsByValue(publisher, "name", name);
|
||||||
assert entry != null;
|
assert entry != null;
|
||||||
assert entry.getKey() != null;
|
assert entry.getKey() != null;
|
||||||
|
|
||||||
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();
|
||||||
@ -249,7 +270,7 @@ public class Database {
|
|||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
private List<Integer> getAuthors(long id) {
|
private List<Integer> getAuthors(long id) {
|
||||||
Map.Entry<ResultSet, Connection> entry = getResultSets(id, authorBook, book);
|
Map.Entry<ResultSet, Connection> entry = getResultSetsById(authorBook, book, id);
|
||||||
assert entry != null;
|
assert entry != null;
|
||||||
try {
|
try {
|
||||||
List<Integer> list = new ArrayList<>();
|
List<Integer> list = new ArrayList<>();
|
||||||
@ -273,16 +294,26 @@ public class Database {
|
|||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public Book getBook(long id) {
|
public Book getBook(long id) {
|
||||||
Map.Entry<ResultSet, Connection> entry = getResultSet(id, book);
|
Map.Entry<ResultSet, Connection> entry = getResultSetById(book, id);
|
||||||
assert entry != null;
|
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) {
|
||||||
|
|
||||||
ResultSet rs = entry.getKey();
|
|
||||||
Book book = null;
|
Book book = null;
|
||||||
|
ResultSet rs = entry.getKey();
|
||||||
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,6 +327,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();
|
entry.getValue().close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -310,19 +342,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));
|
||||||
@ -370,6 +390,25 @@ public class Database {
|
|||||||
return getBooks(category.getId());
|
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.
|
* Inserts book into database.
|
||||||
@ -394,36 +433,37 @@ 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(), getRandomImage()));
|
||||||
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;
|
||||||
|
|
||||||
|
//insert book
|
||||||
try {
|
try {
|
||||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO book(isbn, title, description, " +
|
PreparedStatement statement = connection.prepareStatement("INSERT INTO book(isbn, title, description, " +
|
||||||
"price, year, publisher, category, image) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
|
"price, year, publisher, category, image) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
|
||||||
@ -438,10 +478,11 @@ 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");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//add book to author
|
||||||
for (Author author : authors) {
|
for (Author author : authors) {
|
||||||
PreparedStatement authorStatement = connection.prepareStatement("INSERT INTO " + authorBook + " (author, book) VALUES (?, ?);");
|
PreparedStatement authorStatement = connection.prepareStatement("INSERT INTO " + authorBook + " (author, book) VALUES (?, ?);");
|
||||||
authorStatement.setLong(1, author.getId());
|
authorStatement.setLong(1, author.getId());
|
||||||
@ -594,7 +635,7 @@ public class Database {
|
|||||||
* @param table table name
|
* @param table table name
|
||||||
* @return deletion count
|
* @return deletion count
|
||||||
*/
|
*/
|
||||||
private int delete(long id, String table) {
|
private int delete(long id, @NotNull String table) {
|
||||||
Connection connection = getConnection();
|
Connection connection = getConnection();
|
||||||
assert connection != null;
|
assert connection != null;
|
||||||
|
|
||||||
@ -611,21 +652,50 @@ public class Database {
|
|||||||
return deleteCount;
|
return deleteCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get all ids from specified table.
|
||||||
|
*
|
||||||
|
* @param table table
|
||||||
|
* @return list of ids
|
||||||
|
*/
|
||||||
|
private List<Long> getIds(@NotNull String table) {
|
||||||
|
Map.Entry<ResultSet, Connection> entry = getColumnsFromResultSet(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 specific entry from database.
|
* gets specific entry from database.
|
||||||
*
|
*
|
||||||
* @param id id from database
|
|
||||||
* @param table table name
|
* @param table table name
|
||||||
|
* @param id id from database
|
||||||
* @return {@link java.util.Map.Entry}
|
* @return {@link java.util.Map.Entry}
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
private Map.Entry<ResultSet, Connection> getResultSet(long id, String table) {
|
private 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) {
|
||||||
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);
|
||||||
@ -641,20 +711,20 @@ public class Database {
|
|||||||
/**
|
/**
|
||||||
* gets specific entries from table.
|
* gets specific entries from table.
|
||||||
*
|
*
|
||||||
* @param id id all entries should reference
|
|
||||||
* @param table table name
|
* @param table table name
|
||||||
* @param column column to match
|
* @param column column to match
|
||||||
|
* @param id id all entries should reference
|
||||||
* @return {@link java.util.Map.Entry}
|
* @return {@link java.util.Map.Entry}
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
private Map.Entry<ResultSet, Connection> getResultSets(long id, @NotNull String table, @NotNull String column) {
|
private Map.Entry<ResultSet, Connection> getResultSetsById(@NotNull String table, @NotNull String column, long id) {
|
||||||
Connection connection = getConnection();
|
Connection connection = getConnection();
|
||||||
try {
|
try {
|
||||||
assert connection != null;
|
assert connection != null;
|
||||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + table + " WHERE " + column + " = ?");
|
PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + table + " WHERE " + column + " = ?");
|
||||||
statement.setLong(1, id);
|
statement.setLong(1, id);
|
||||||
|
|
||||||
return getAllEntries(connection, statement);
|
return getAllEntriesFromResultSet(connection, statement);
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -669,14 +739,14 @@ public class Database {
|
|||||||
* @return {@link java.util.Map.Entry}
|
* @return {@link java.util.Map.Entry}
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
private Map.Entry<ResultSet, Connection> getResultSets(@NotNull String table, @NotNull String column, @NotNull String value) {
|
private Map.Entry<ResultSet, Connection> getResultSetsByValue(@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 " + table + " WHERE " + column + " = ?");
|
PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + table + " WHERE " + column + " = ?");
|
||||||
statement.setString(1, value);
|
statement.setString(1, value);
|
||||||
|
|
||||||
return getAllEntries(connection, statement);
|
return getAllEntriesFromResultSet(connection, statement);
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -691,13 +761,13 @@ public class Database {
|
|||||||
* @return {@link java.util.Map.Entry}
|
* @return {@link java.util.Map.Entry}
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
private Map.Entry<ResultSet, Connection> getResultSets(@NotNull String table, @NotNull String... columns) {
|
private Map.Entry<ResultSet, Connection> getColumnsFromResultSet(@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 (" + String.join(",", columns) + ") FROM " + table);
|
PreparedStatement statement = connection.prepareStatement("SELECT (" + String.join(",", columns) + ") FROM " + table);
|
||||||
|
|
||||||
return getAllEntries(connection, statement);
|
return getAllEntriesFromResultSet(connection, statement);
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -712,7 +782,7 @@ public class Database {
|
|||||||
* @return {@link java.util.Map.Entry}
|
* @return {@link java.util.Map.Entry}
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
private Map.Entry<ResultSet, Connection> getAllEntries(@NotNull Connection connection, @NotNull PreparedStatement statement) {
|
private Map.Entry<ResultSet, Connection> getAllEntriesFromResultSet(@NotNull Connection connection, @NotNull PreparedStatement statement) {
|
||||||
try {
|
try {
|
||||||
ResultSet resultSet = statement.executeQuery();
|
ResultSet resultSet = statement.executeQuery();
|
||||||
return new AbstractMap.SimpleEntry<>(resultSet, connection);
|
return new AbstractMap.SimpleEntry<>(resultSet, connection);
|
||||||
@ -721,4 +791,28 @@ public class Database {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private String getRandomImage() {
|
||||||
|
try {
|
||||||
|
InputStream is = new URL("https://api.unsplash.com/photos/random?client_id=" + Configuration.get().getUnsplash().getAccessKey()).openStream();
|
||||||
|
try {
|
||||||
|
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
int cp;
|
||||||
|
while ((cp = rd.read()) != -1) {
|
||||||
|
sb.append((char) cp);
|
||||||
|
}
|
||||||
|
JSONObject json = new JSONObject(sb.toString());
|
||||||
|
return json.getString("id");
|
||||||
|
} finally {
|
||||||
|
is.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,5 +1,7 @@
|
|||||||
package de.hsel.itech.servlet;
|
package de.hsel.itech.servlet;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import javax.servlet.annotation.WebServlet;
|
import javax.servlet.annotation.WebServlet;
|
||||||
import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServlet;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
@ -10,7 +12,6 @@ import java.io.PrintWriter;
|
|||||||
/**
|
/**
|
||||||
* Servlet: form to add a book to the database
|
* Servlet: form to add a book to the database
|
||||||
*
|
*
|
||||||
*
|
|
||||||
* @author Julian Hinxlage
|
* @author Julian Hinxlage
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
@ -20,7 +21,7 @@ import java.io.PrintWriter;
|
|||||||
public class AddBook extends HttpServlet {
|
public class AddBook extends HttpServlet {
|
||||||
|
|
||||||
|
|
||||||
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
|
protected void doGet(@NotNull final HttpServletRequest req, @NotNull final HttpServletResponse resp)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
|
||||||
resp.setCharacterEncoding("utf-8");
|
resp.setCharacterEncoding("utf-8");
|
||||||
@ -30,19 +31,19 @@ public class AddBook extends HttpServlet {
|
|||||||
|
|
||||||
out.println("<form class=\"m-container\" action=\"booklist\" method=post>");
|
out.println("<form class=\"m-container\" action=\"booklist\" method=post>");
|
||||||
Utillity.addValidationInput(out, "ISBN", "isbn", "text", "0", "[0-9]*");
|
Utillity.addValidationInput(out, "ISBN", "isbn", "text", "0", "[0-9]*");
|
||||||
Utillity.addInput(out,"Titel", "title");
|
Utillity.addInput(out, "Titel", "title");
|
||||||
Utillity.addInput(out,"Author", "author");
|
Utillity.addInput(out, "Author", "author");
|
||||||
Utillity.addValidationInput(out,"Preis", "price", "text", "0", "[0-9]*(,[0-9]{1,2})?");
|
Utillity.addValidationInput(out, "Preis", "price", "text", "0", "[0-9]*(,[0-9]{1,2})?");
|
||||||
Utillity.addValidationInput(out,"Jahr", "year", "text", "2019", "[0-9]*");
|
Utillity.addValidationInput(out, "Jahr", "year", "text", "2019", "[0-9]*");
|
||||||
Utillity.addInput(out,"Beschreibung", "description");
|
Utillity.addInput(out, "Beschreibung", "description");
|
||||||
Utillity.addInput(out,"Verlag", "publisher");
|
Utillity.addInput(out, "Verlag", "publisher");
|
||||||
Utillity.addInput(out,"Kategorie", "category");
|
Utillity.addInput(out, "Kategorie", "category");
|
||||||
Utillity.addInput(out,"Bild", "image", "DxAzOKSiPoE");
|
Utillity.addInput(out, "Bild", "image");
|
||||||
Utillity.addSubmitButton(out, "Hinzufügen");
|
Utillity.addSubmitButton(out, "Hinzufügen");
|
||||||
out.println("</form>");
|
out.println("</form>");
|
||||||
|
|
||||||
|
|
||||||
Utillity.addButton(out,"Zurück zur Buchliste", "booklist");
|
Utillity.addButton(out, "Zurück zur Buchliste", "booklist");
|
||||||
|
|
||||||
Utillity.insertFile(out, "template_footer.html");
|
Utillity.insertFile(out, "template_footer.html");
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ 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;
|
||||||
import de.hsel.itech.db.pojo.Publisher;
|
import de.hsel.itech.db.pojo.Publisher;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import javax.servlet.annotation.WebServlet;
|
import javax.servlet.annotation.WebServlet;
|
||||||
import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServlet;
|
||||||
@ -20,7 +21,6 @@ import java.util.List;
|
|||||||
/**
|
/**
|
||||||
* Servlet: list of all books and remove function
|
* Servlet: list of all books and remove function
|
||||||
*
|
*
|
||||||
*
|
|
||||||
* @author Julian Hinxlage
|
* @author Julian Hinxlage
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
@ -29,14 +29,19 @@ import java.util.List;
|
|||||||
@WebServlet("/booklist")
|
@WebServlet("/booklist")
|
||||||
public class BookList extends HttpServlet {
|
public class BookList extends HttpServlet {
|
||||||
|
|
||||||
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
|
@Override
|
||||||
throws IOException {
|
protected void doGet(@NotNull HttpServletRequest req, @NotNull HttpServletResponse resp) throws IOException {
|
||||||
|
doPost(req, resp);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doPost(@NotNull final HttpServletRequest req, @NotNull final HttpServletResponse resp) throws IOException {
|
||||||
req.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
req.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||||
|
|
||||||
//get database object
|
//get database object
|
||||||
Database db = Database.getInstance();
|
Database db = Database.getInstance();
|
||||||
|
|
||||||
resp.setCharacterEncoding("utf-8");
|
resp.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||||
PrintWriter out = resp.getWriter();
|
PrintWriter out = resp.getWriter();
|
||||||
|
|
||||||
//header template
|
//header template
|
||||||
@ -74,20 +79,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<>();
|
||||||
@ -105,8 +99,7 @@ public class BookList extends HttpServlet {
|
|||||||
db.insert(book);
|
db.insert(book);
|
||||||
resp.sendRedirect("booklist");
|
resp.sendRedirect("booklist");
|
||||||
}
|
}
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e){
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
out.println("<aside class =\"m-note m-danger\">");
|
out.println("<aside class =\"m-note m-danger\">");
|
||||||
out.println("<h3>Es gibt ein Fehler in der Eingabe </h3>");
|
out.println("<h3>Es gibt ein Fehler in der Eingabe </h3>");
|
||||||
@ -116,7 +109,6 @@ public class BookList extends HttpServlet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//list all books
|
//list all books
|
||||||
List<Book> books = db.getBooks();
|
List<Book> books = db.getBooks();
|
||||||
if (books != null) {
|
if (books != null) {
|
||||||
@ -136,7 +128,7 @@ public class BookList extends HttpServlet {
|
|||||||
out.println("ISBN: " + book.getIsbn() + "</br>");
|
out.println("ISBN: " + book.getIsbn() + "</br>");
|
||||||
|
|
||||||
List<String> authors = new ArrayList<>();
|
List<String> authors = new ArrayList<>();
|
||||||
for(Author author : book.getAuthors()) {
|
for (Author author : book.getAuthors()) {
|
||||||
authors.add(author.getName());
|
authors.add(author.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,9 +170,4 @@ public class BookList extends HttpServlet {
|
|||||||
Utillity.insertFile(out, "template_footer.html");
|
Utillity.insertFile(out, "template_footer.html");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp)
|
|
||||||
throws IOException {
|
|
||||||
doGet(req, resp);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,22 @@
|
|||||||
package de.hsel.itech.servlet;
|
package de.hsel.itech.servlet;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Julian Hinxlage
|
||||||
|
* @version 0.1
|
||||||
|
* @since 0.1
|
||||||
|
*/
|
||||||
public class Utillity {
|
public class Utillity {
|
||||||
|
|
||||||
static public void insertFile(PrintWriter out, String fileName) {
|
public static void insertFile(@NotNull PrintWriter out, @NotNull String fileName) {
|
||||||
try (BufferedReader br = new BufferedReader(new FileReader(Utillity.class.getClassLoader().getResource(fileName).getFile()))) {
|
try (BufferedReader br = new BufferedReader(new FileReader(Objects.requireNonNull(Utillity.class.getClassLoader().getResource(fileName)).getFile()))) {
|
||||||
for (String line = br.readLine(); line != null; line = br.readLine()) {
|
for (String line = br.readLine(); line != null; line = br.readLine()) {
|
||||||
out.println(line);
|
out.println(line);
|
||||||
}
|
}
|
||||||
@ -17,11 +25,11 @@ public class Utillity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static public void addInput(PrintWriter out, String label, String name){
|
public static void addInput(@NotNull PrintWriter out, @NotNull String label, @NotNull String name) {
|
||||||
addInput(out,label,name,"");
|
addInput(out, label, name, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
static public void addInput(PrintWriter out, String label, String name, String value){
|
public static void addInput(@NotNull PrintWriter out, @NotNull String label, @NotNull String name, @NotNull String value) {
|
||||||
out.println(" <div class=\"m-row\">");
|
out.println(" <div class=\"m-row\">");
|
||||||
out.println(" <div class=\"m-col-l-2 m-push-l-5\">");
|
out.println(" <div class=\"m-col-l-2 m-push-l-5\">");
|
||||||
out.println(label);
|
out.println(label);
|
||||||
@ -30,7 +38,7 @@ public class Utillity {
|
|||||||
out.println("</div>");
|
out.println("</div>");
|
||||||
}
|
}
|
||||||
|
|
||||||
static public void addValidationInput(PrintWriter out, String label, String name, String type, String value, String pattern){
|
public static void addValidationInput(@NotNull PrintWriter out, @NotNull String label, @NotNull String name, @NotNull String type, @NotNull String value, @NotNull String pattern) {
|
||||||
out.println(" <div class=\"m-row\">");
|
out.println(" <div class=\"m-row\">");
|
||||||
out.println(" <div class=\"m-col-l-2 m-push-l-5\">");
|
out.println(" <div class=\"m-col-l-2 m-push-l-5\">");
|
||||||
out.println(label);
|
out.println(label);
|
||||||
@ -39,7 +47,7 @@ public class Utillity {
|
|||||||
out.println("</div>");
|
out.println("</div>");
|
||||||
}
|
}
|
||||||
|
|
||||||
static public void addButton(PrintWriter out, String label, String url){
|
public static void addButton(@NotNull PrintWriter out, @NotNull String label, @NotNull String url) {
|
||||||
out.println(" <div class=\"m-row\">");
|
out.println(" <div class=\"m-row\">");
|
||||||
out.println(" <div class=\"m-col-l-4 m-push-l-4\">");
|
out.println(" <div class=\"m-col-l-4 m-push-l-4\">");
|
||||||
out.println(" <div class=\"m-button m-success\">");
|
out.println(" <div class=\"m-button m-success\">");
|
||||||
@ -51,7 +59,7 @@ public class Utillity {
|
|||||||
out.println("</div>");
|
out.println("</div>");
|
||||||
}
|
}
|
||||||
|
|
||||||
static public void addSubmitButton(PrintWriter out, String label){
|
public static void addSubmitButton(@NotNull PrintWriter out, @NotNull String label) {
|
||||||
out.println(" <div class=\"m-row\">");
|
out.println(" <div class=\"m-row\">");
|
||||||
out.println(" <div class=\"m-col-l-4 m-push-l-5\">");
|
out.println(" <div class=\"m-col-l-4 m-push-l-5\">");
|
||||||
out.println(" <input type=\"" + "submit" + "\" value=\"" + label + "\">");
|
out.println(" <input type=\"" + "submit" + "\" value=\"" + label + "\">");
|
||||||
|
@ -13,6 +13,6 @@ public class HelloTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void hello() {
|
void hello() {
|
||||||
assertEquals(2, 1+1);
|
assertEquals(2, 1 + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user