From c9785a5f32809fc2574d19f98fa6a455a9875ee4 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 1 May 2019 16:34:12 +0200 Subject: [PATCH] ~ cleanup Signed-off-by: Johannes Theiner --- .../de/hsel/itech/config/Configuration.java | 2 ++ .../java/de/hsel/itech/config/Unsplash.java | 20 ++++++++++++++++ src/main/java/de/hsel/itech/db/Database.java | 18 +++++++------- .../java/de/hsel/itech/servlet/AddBook.java | 23 +++++++++--------- .../java/de/hsel/itech/servlet/BookList.java | 24 +++++++++---------- .../java/de/hsel/itech/servlet/Utillity.java | 24 ++++++++++++------- 6 files changed, 71 insertions(+), 40 deletions(-) create mode 100644 src/main/java/de/hsel/itech/config/Unsplash.java diff --git a/src/main/java/de/hsel/itech/config/Configuration.java b/src/main/java/de/hsel/itech/config/Configuration.java index 3412c35..c9aee3f 100644 --- a/src/main/java/de/hsel/itech/config/Configuration.java +++ b/src/main/java/de/hsel/itech/config/Configuration.java @@ -25,6 +25,8 @@ public class Configuration { @Getter private Database database; + @Getter + private Unsplash unsplash; /** diff --git a/src/main/java/de/hsel/itech/config/Unsplash.java b/src/main/java/de/hsel/itech/config/Unsplash.java new file mode 100644 index 0000000..3d77a1a --- /dev/null +++ b/src/main/java/de/hsel/itech/config/Unsplash.java @@ -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; +} \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/Database.java b/src/main/java/de/hsel/itech/db/Database.java index 8617ce0..1cf745f 100644 --- a/src/main/java/de/hsel/itech/db/Database.java +++ b/src/main/java/de/hsel/itech/db/Database.java @@ -126,7 +126,7 @@ public class Database { * @return {@link de.hsel.itech.db.pojo.Author} */ @Nullable - public Author getAuthor(String name) { + public Author getAuthor(@NotNull String name) { Map.Entry entry = getResultSetsByValue(author, "name", name); assert entry != null; assert entry.getKey() != null; @@ -171,7 +171,7 @@ public class Database { * @return {@link de.hsel.itech.db.pojo.Category} */ @Nullable - public Category getCategory(String name) { + public Category getCategory(@NotNull String name) { Map.Entry entry = getResultSetsByValue(category, "name", name); assert entry != null; @@ -243,7 +243,7 @@ public class Database { * @return {@link de.hsel.itech.db.pojo.Publisher} */ @Nullable - public Publisher getPublisher(String name) { + public Publisher getPublisher(@NotNull String name) { Map.Entry entry = getResultSetsByValue(publisher, "name", name); assert entry != null; assert entry.getKey() != null; @@ -463,6 +463,7 @@ public class Database { 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); @@ -481,6 +482,7 @@ public class Database { 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()); @@ -633,7 +635,7 @@ public class Database { * @param table table name * @return deletion count */ - private int delete(long id, String table) { + private int delete(long id, @NotNull String table) { Connection connection = getConnection(); assert connection != null; @@ -656,7 +658,7 @@ public class Database { * @param table table * @return list of ids */ - private List getIds(String table) { + private List getIds(@NotNull String table) { Map.Entry entry = getColumnsFromResultSet(table, "id"); assert entry != null; @@ -683,12 +685,12 @@ public class Database { * @return {@link java.util.Map.Entry} */ @Nullable - private Map.Entry getResultSetById(String table, long id) { + private Map.Entry getResultSetById(@NotNull String table, long id) { return getResultSetByValue(table, "id", id); } @Nullable - private Map.Entry getResultSetByValue(String table, String column, long value) { + private Map.Entry getResultSetByValue(@NotNull String table, @NotNull String column, long value) { Connection connection = getConnection(); try { assert connection != null; @@ -793,7 +795,7 @@ public class Database { @Nullable private String getRandomImage() { try { - InputStream is = new URL("https://api.unsplash.com/photos/random?client_id=bb18673959b1cfd053a4ecb58e85bb8d99a0050ae5b71dac3e7632916eead9aa").openStream(); + 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(); diff --git a/src/main/java/de/hsel/itech/servlet/AddBook.java b/src/main/java/de/hsel/itech/servlet/AddBook.java index 321e023..77798b5 100644 --- a/src/main/java/de/hsel/itech/servlet/AddBook.java +++ b/src/main/java/de/hsel/itech/servlet/AddBook.java @@ -1,5 +1,7 @@ package de.hsel.itech.servlet; +import org.jetbrains.annotations.NotNull; + import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -10,7 +12,6 @@ import java.io.PrintWriter; /** * Servlet: form to add a book to the database * - * * @author Julian Hinxlage * @version 0.1 * @since 0.1 @@ -20,7 +21,7 @@ import java.io.PrintWriter; 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 { resp.setCharacterEncoding("utf-8"); @@ -30,19 +31,19 @@ public class AddBook extends HttpServlet { out.println("
"); Utillity.addValidationInput(out, "ISBN", "isbn", "text", "0", "[0-9]*"); - Utillity.addInput(out,"Titel", "title"); - Utillity.addInput(out,"Author", "author"); - Utillity.addValidationInput(out,"Preis", "price", "text", "0", "[0-9]*(,[0-9]{1,2})?"); - Utillity.addValidationInput(out,"Jahr", "year", "text", "2019", "[0-9]*"); - Utillity.addInput(out,"Beschreibung", "description"); - Utillity.addInput(out,"Verlag", "publisher"); - Utillity.addInput(out,"Kategorie", "category"); - Utillity.addInput(out,"Bild", "image"); + Utillity.addInput(out, "Titel", "title"); + Utillity.addInput(out, "Author", "author"); + Utillity.addValidationInput(out, "Preis", "price", "text", "0", "[0-9]*(,[0-9]{1,2})?"); + Utillity.addValidationInput(out, "Jahr", "year", "text", "2019", "[0-9]*"); + Utillity.addInput(out, "Beschreibung", "description"); + Utillity.addInput(out, "Verlag", "publisher"); + Utillity.addInput(out, "Kategorie", "category"); + Utillity.addInput(out, "Bild", "image"); Utillity.addSubmitButton(out, "Hinzufügen"); out.println("
"); - Utillity.addButton(out,"Zurück zur Buchliste", "booklist"); + Utillity.addButton(out, "Zurück zur Buchliste", "booklist"); Utillity.insertFile(out, "template_footer.html"); } diff --git a/src/main/java/de/hsel/itech/servlet/BookList.java b/src/main/java/de/hsel/itech/servlet/BookList.java index 99ac43c..b2f2ddc 100644 --- a/src/main/java/de/hsel/itech/servlet/BookList.java +++ b/src/main/java/de/hsel/itech/servlet/BookList.java @@ -5,6 +5,7 @@ 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 javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @@ -20,7 +21,6 @@ import java.util.List; /** * Servlet: list of all books and remove function * - * * @author Julian Hinxlage * @version 0.1 * @since 0.1 @@ -29,14 +29,19 @@ import java.util.List; @WebServlet("/booklist") public class BookList extends HttpServlet { - protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) - throws IOException { + @Override + 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()); //get database object Database db = Database.getInstance(); - resp.setCharacterEncoding("utf-8"); + resp.setCharacterEncoding(StandardCharsets.UTF_8.name()); PrintWriter out = resp.getWriter(); //header template @@ -94,8 +99,7 @@ public class BookList extends HttpServlet { db.insert(book); resp.sendRedirect("booklist"); } - } - catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); out.println("