diff --git a/pom.xml b/pom.xml
index 188835c..b4bb633 100644
--- a/pom.xml
+++ b/pom.xml
@@ -261,11 +261,18 @@
13.0
compile
+
+
com.google.code.gson
gson
2.8.0
+
+ org.json
+ json
+ 20180813
+
org.mariadb.jdbc
diff --git a/src/main/java/de/hsel/itech/db/Database.java b/src/main/java/de/hsel/itech/db/Database.java
index b473d2d..8617ce0 100644
--- a/src/main/java/de/hsel/itech/db/Database.java
+++ b/src/main/java/de/hsel/itech/db/Database.java
@@ -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 entry = getResultSet(id, author);
+ Map.Entry 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 entry = getResultSets(author, "name", name);
+ Map.Entry 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 entry = getResultSet(id, category);
+ Map.Entry 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 entry = getResultSets(category, "name", name);
+ Map.Entry 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 entry) {
ResultSet rs = entry.getKey();
@@ -193,6 +198,11 @@ public class Database {
return category;
}
+ /**
+ * get all categories.
+ *
+ * @return list of categories
+ */
@Nullable
public List getCategories() {
List ids = getIds(category);
@@ -203,23 +213,6 @@ public class Database {
return categories;
}
- private List getIds(String table) {
- Map.Entry entry = getResultSets(table, "id");
- assert entry != null;
-
-
- List 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 entry = getResultSet(id, publisher);
+ Map.Entry 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 entry = getResultSets(publisher, "name", name);
+ Map.Entry entry = getResultSetsByValue(publisher, "name", name);
assert entry != null;
assert entry.getKey() != null;
@@ -277,7 +270,7 @@ public class Database {
*/
@Nullable
private List getAuthors(long id) {
- Map.Entry entry = getResultSets(id, authorBook, book);
+ Map.Entry entry = getResultSetsById(authorBook, book, id);
assert entry != null;
try {
List list = new ArrayList<>();
@@ -301,28 +294,22 @@ public class Database {
*/
@Nullable
public Book getBook(long id) {
- Map.Entry entry = getResultSet(id, book);
+ Map.Entry entry = getResultSetById(book, id);
assert entry != null;
- return getBook(entry);
- }
-
-
- private Book getBook(@NotNull Map.Entry 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 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 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 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 getIds(String table) {
+ Map.Entry entry = getColumnsFromResultSet(table, "id");
+ assert entry != null;
+
+
+ List 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 getResultSet(long id, String table) {
- return getResultSet(table, "id", id);
+ private Map.Entry getResultSetById(String table, long id) {
+ return getResultSetByValue(table, "id", id);
}
@Nullable
- private Map.Entry getResultSet(String table, String column, long value) {
+ private Map.Entry 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 getResultSets(long id, @NotNull String table, @NotNull String column) {
+ private Map.Entry 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 getResultSets(@NotNull String table, @NotNull String column, @NotNull String value) {
+ private Map.Entry 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 getResultSets(@NotNull String table, @NotNull String... columns) {
+ private Map.Entry 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 getAllEntries(@NotNull Connection connection, @NotNull PreparedStatement statement) {
+ private Map.Entry 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;
+ }
}
\ No newline at end of file
diff --git a/src/main/java/de/hsel/itech/servlet/AddBook.java b/src/main/java/de/hsel/itech/servlet/AddBook.java
index 32f8e36..321e023 100644
--- a/src/main/java/de/hsel/itech/servlet/AddBook.java
+++ b/src/main/java/de/hsel/itech/servlet/AddBook.java
@@ -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("");
diff --git a/src/test/java/de/hsel/itech/HelloTest.java b/src/test/java/de/hsel/itech/HelloTest.java
index f9cc0f2..feca89c 100644
--- a/src/test/java/de/hsel/itech/HelloTest.java
+++ b/src/test/java/de/hsel/itech/HelloTest.java
@@ -13,6 +13,6 @@ public class HelloTest {
@Test
void hello() {
- assertEquals(2, 1+1);
+ assertEquals(2, 1 + 1);
}
}
\ No newline at end of file