~ Database Configuration
+ random Category image Signed-off-by: Johannes Theiner <j.theiner@live.de>
This commit is contained in:
parent
b265adec2c
commit
813c3aa1f3
7
pom.xml
7
pom.xml
@ -261,11 +261,18 @@
|
||||
<version>13.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>20180813</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mariadb.jdbc</groupId>
|
||||
|
@ -7,12 +7,12 @@ 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;
|
||||
import org.mariadb.jdbc.MariaDbPoolDataSource;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
import java.sql.*;
|
||||
import java.time.Year;
|
||||
import java.util.AbstractMap;
|
||||
@ -105,7 +105,7 @@ public class Database {
|
||||
*/
|
||||
@Nullable
|
||||
public Author getAuthor(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSet(id, author);
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSetById(author, id);
|
||||
assert entry != null;
|
||||
|
||||
ResultSet rs = entry.getKey();
|
||||
@ -127,7 +127,7 @@ public class Database {
|
||||
*/
|
||||
@Nullable
|
||||
public Author getAuthor(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.getKey() != null;
|
||||
|
||||
@ -152,7 +152,7 @@ public class Database {
|
||||
*/
|
||||
@Nullable
|
||||
public Category getCategory(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSet(id, category);
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSetById(category, id);
|
||||
assert entry != null;
|
||||
ResultSet rs = entry.getKey();
|
||||
|
||||
@ -172,12 +172,17 @@ public class Database {
|
||||
*/
|
||||
@Nullable
|
||||
public Category getCategory(String name) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSets(category, "name", name);
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSetsByValue(category, "name", name);
|
||||
assert entry != null;
|
||||
|
||||
return getCategory(entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* get Category from database entry.
|
||||
* @param entry entry
|
||||
* @return Category
|
||||
*/
|
||||
@Nullable
|
||||
private Category getCategory(@NotNull Map.Entry<ResultSet, Connection> entry) {
|
||||
ResultSet rs = entry.getKey();
|
||||
@ -193,6 +198,11 @@ public class Database {
|
||||
return category;
|
||||
}
|
||||
|
||||
/**
|
||||
* get all categories.
|
||||
*
|
||||
* @return list of categories
|
||||
*/
|
||||
@Nullable
|
||||
public List<Category> getCategories() {
|
||||
List<Long> ids = getIds(category);
|
||||
@ -203,23 +213,6 @@ public class Database {
|
||||
return categories;
|
||||
}
|
||||
|
||||
private List<Long> getIds(String table) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSets(table, "id");
|
||||
assert entry != null;
|
||||
|
||||
|
||||
List<Long> ids = new ArrayList<>();
|
||||
try {
|
||||
ResultSet rs = entry.getKey();
|
||||
while (rs.next()) {
|
||||
ids.add(rs.getLong("id"));
|
||||
}
|
||||
entry.getValue().close();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets publisher by id.
|
||||
@ -229,7 +222,7 @@ public class Database {
|
||||
*/
|
||||
@Nullable
|
||||
public Publisher getPublisher(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSet(id, publisher);
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSetById(publisher, id);
|
||||
assert entry != null;
|
||||
|
||||
ResultSet rs = entry.getKey();
|
||||
@ -251,7 +244,7 @@ public class Database {
|
||||
*/
|
||||
@Nullable
|
||||
public Publisher getPublisher(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.getKey() != null;
|
||||
|
||||
@ -277,7 +270,7 @@ public class Database {
|
||||
*/
|
||||
@Nullable
|
||||
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;
|
||||
try {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
@ -301,28 +294,22 @@ public class Database {
|
||||
*/
|
||||
@Nullable
|
||||
public Book getBook(long id) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSet(id, book);
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSetById(book, id);
|
||||
assert entry != null;
|
||||
return getBook(entry);
|
||||
}
|
||||
|
||||
|
||||
private Book getBook(@NotNull Map.Entry<ResultSet, Connection> entry) {
|
||||
ResultSet rs = entry.getKey();
|
||||
|
||||
Book book = null;
|
||||
try {
|
||||
book = buildBook(rs);
|
||||
entry.getValue().close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return book;
|
||||
return buildBook(entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* builds book from entry.
|
||||
*
|
||||
* @param entry Map.Entry
|
||||
* @return Book
|
||||
*/
|
||||
@Nullable
|
||||
private Book buildBook(ResultSet rs) {
|
||||
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));
|
||||
@ -341,6 +328,7 @@ public class Database {
|
||||
Year.of(rs.getInt("year")), rs.getInt("price"), rs.getString("description"),
|
||||
rs.getString("image"));
|
||||
|
||||
entry.getValue().close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -391,19 +379,6 @@ public class Database {
|
||||
return books;
|
||||
}
|
||||
|
||||
public boolean existsBook(long isbn) {
|
||||
Map.Entry<ResultSet, Connection> entry = getResultSet(book, "isbn", isbn);
|
||||
try {
|
||||
if (entry != null && entry.getKey() != null) {
|
||||
entry.getValue().close();
|
||||
return true;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* get books by category.
|
||||
*
|
||||
@ -415,6 +390,25 @@ public class Database {
|
||||
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.
|
||||
@ -461,7 +455,7 @@ public class Database {
|
||||
if (dbCategory != null)
|
||||
category = dbCategory;
|
||||
else {
|
||||
insertCount += insert(new Category(book.getCategory().getName(), book.getCategory().getImage()));
|
||||
insertCount += insert(new Category(book.getCategory().getName(), getRandomImage()));
|
||||
category = getCategory(book.getCategory().getName());
|
||||
}
|
||||
} else category = book.getCategory();
|
||||
@ -656,21 +650,45 @@ public class Database {
|
||||
return deleteCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* get all ids from specified table.
|
||||
*
|
||||
* @param table table
|
||||
* @return list of ids
|
||||
*/
|
||||
private List<Long> getIds(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.
|
||||
*
|
||||
* @param id id from database
|
||||
* @param table table name
|
||||
* @param id id from database
|
||||
* @return {@link java.util.Map.Entry}
|
||||
*/
|
||||
@Nullable
|
||||
private Map.Entry<ResultSet, Connection> getResultSet(long id, String table) {
|
||||
return getResultSet(table, "id", id);
|
||||
private Map.Entry<ResultSet, Connection> getResultSetById(String table, long id) {
|
||||
return getResultSetByValue(table, "id", id);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Map.Entry<ResultSet, Connection> getResultSet(String table, String column, long value) {
|
||||
private Map.Entry<ResultSet, Connection> getResultSetByValue(String table, String column, long value) {
|
||||
Connection connection = getConnection();
|
||||
try {
|
||||
assert connection != null;
|
||||
@ -691,20 +709,20 @@ public class Database {
|
||||
/**
|
||||
* gets specific entries from table.
|
||||
*
|
||||
* @param id id all entries should reference
|
||||
* @param table table name
|
||||
* @param column column to match
|
||||
* @param id id all entries should reference
|
||||
* @return {@link java.util.Map.Entry}
|
||||
*/
|
||||
@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();
|
||||
try {
|
||||
assert connection != null;
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + table + " WHERE " + column + " = ?");
|
||||
statement.setLong(1, id);
|
||||
|
||||
return getAllEntries(connection, statement);
|
||||
return getAllEntriesFromResultSet(connection, statement);
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
@ -719,14 +737,14 @@ public class Database {
|
||||
* @return {@link java.util.Map.Entry}
|
||||
*/
|
||||
@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();
|
||||
try {
|
||||
assert connection != null;
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + table + " WHERE " + column + " = ?");
|
||||
statement.setString(1, value);
|
||||
|
||||
return getAllEntries(connection, statement);
|
||||
return getAllEntriesFromResultSet(connection, statement);
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
@ -741,13 +759,13 @@ public class Database {
|
||||
* @return {@link java.util.Map.Entry}
|
||||
*/
|
||||
@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();
|
||||
try {
|
||||
assert connection != null;
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT (" + String.join(",", columns) + ") FROM " + table);
|
||||
|
||||
return getAllEntries(connection, statement);
|
||||
return getAllEntriesFromResultSet(connection, statement);
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
@ -762,7 +780,7 @@ public class Database {
|
||||
* @return {@link java.util.Map.Entry}
|
||||
*/
|
||||
@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 {
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
return new AbstractMap.SimpleEntry<>(resultSet, connection);
|
||||
@ -771,4 +789,28 @@ public class Database {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private String getRandomImage() {
|
||||
try {
|
||||
InputStream is = new URL("https://api.unsplash.com/photos/random?client_id=bb18673959b1cfd053a4ecb58e85bb8d99a0050ae5b71dac3e7632916eead9aa").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;
|
||||
}
|
||||
}
|
@ -37,7 +37,7 @@ public class AddBook extends HttpServlet {
|
||||
Utillity.addInput(out,"Beschreibung", "description");
|
||||
Utillity.addInput(out,"Verlag", "publisher");
|
||||
Utillity.addInput(out,"Kategorie", "category");
|
||||
Utillity.addInput(out,"Bild", "image", "DxAzOKSiPoE");
|
||||
Utillity.addInput(out,"Bild", "image");
|
||||
Utillity.addSubmitButton(out, "Hinzufügen");
|
||||
out.println("</form>");
|
||||
|
||||
|
@ -13,6 +13,6 @@ public class HelloTest {
|
||||
|
||||
@Test
|
||||
void hello() {
|
||||
assertEquals(2, 1+1);
|
||||
assertEquals(2, 1 + 1);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user