~ cleanup
Signed-off-by: Johannes Theiner <j.theiner@live.de>
This commit is contained in:
parent
813c3aa1f3
commit
c9785a5f32
|
@ -25,6 +25,8 @@ public class Configuration {
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private Database database;
|
private Database database;
|
||||||
|
@Getter
|
||||||
|
private Unsplash unsplash;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
|
@ -126,7 +126,7 @@ 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 = getResultSetsByValue(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;
|
||||||
|
@ -171,7 +171,7 @@ 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 = getResultSetsByValue(category, "name", name);
|
Map.Entry<ResultSet, Connection> entry = getResultSetsByValue(category, "name", name);
|
||||||
assert entry != null;
|
assert entry != null;
|
||||||
|
|
||||||
|
@ -243,7 +243,7 @@ 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 = getResultSetsByValue(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;
|
||||||
|
@ -463,6 +463,7 @@ public class Database {
|
||||||
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);
|
||||||
|
@ -481,6 +482,7 @@ public class Database {
|
||||||
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());
|
||||||
|
@ -633,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;
|
||||||
|
|
||||||
|
@ -656,7 +658,7 @@ public class Database {
|
||||||
* @param table table
|
* @param table table
|
||||||
* @return list of ids
|
* @return list of ids
|
||||||
*/
|
*/
|
||||||
private List<Long> getIds(String table) {
|
private List<Long> getIds(@NotNull String table) {
|
||||||
Map.Entry<ResultSet, Connection> entry = getColumnsFromResultSet(table, "id");
|
Map.Entry<ResultSet, Connection> entry = getColumnsFromResultSet(table, "id");
|
||||||
assert entry != null;
|
assert entry != null;
|
||||||
|
|
||||||
|
@ -683,12 +685,12 @@ public class Database {
|
||||||
* @return {@link java.util.Map.Entry}
|
* @return {@link java.util.Map.Entry}
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
private Map.Entry<ResultSet, Connection> getResultSetById(String table, long id) {
|
private Map.Entry<ResultSet, Connection> getResultSetById(@NotNull String table, long id) {
|
||||||
return getResultSetByValue(table, "id", id);
|
return getResultSetByValue(table, "id", id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private Map.Entry<ResultSet, Connection> getResultSetByValue(String table, String column, long value) {
|
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;
|
||||||
|
@ -793,7 +795,7 @@ public class Database {
|
||||||
@Nullable
|
@Nullable
|
||||||
private String getRandomImage() {
|
private String getRandomImage() {
|
||||||
try {
|
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 {
|
try {
|
||||||
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
|
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
|
@ -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");
|
||||||
|
|
|
@ -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
|
||||||
|
@ -94,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>");
|
||||||
|
@ -105,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) {
|
||||||
|
@ -167,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 + "\">");
|
||||||
|
|
Loading…
Reference in New Issue