From 5849812afd649c1fe198620ddd72087ba12337f0 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Thu, 25 Apr 2019 11:31:47 +0200 Subject: [PATCH] changed booklist entries added value validation to addbook form added simple Error massages --- src/main/java/de/hsel/itech/db/Database.java | 1 + .../java/de/hsel/itech/servlet/AddBook.java | 17 ++- .../java/de/hsel/itech/servlet/BookList.java | 136 ++++++++++++------ .../java/de/hsel/itech/servlet/Utillity.java | 26 ++-- src/main/resources/database.sql | 2 +- 5 files changed, 116 insertions(+), 66 deletions(-) diff --git a/src/main/java/de/hsel/itech/db/Database.java b/src/main/java/de/hsel/itech/db/Database.java index 146d2a6..9ea4650 100644 --- a/src/main/java/de/hsel/itech/db/Database.java +++ b/src/main/java/de/hsel/itech/db/Database.java @@ -300,6 +300,7 @@ public class Database { Map.Entry entry = getResultSet(id, book); assert entry != null; + ResultSet rs = entry.getKey(); Book book = null; try { diff --git a/src/main/java/de/hsel/itech/servlet/AddBook.java b/src/main/java/de/hsel/itech/servlet/AddBook.java index e50e7c0..a53e4d5 100644 --- a/src/main/java/de/hsel/itech/servlet/AddBook.java +++ b/src/main/java/de/hsel/itech/servlet/AddBook.java @@ -6,6 +6,14 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; +/** + * Servlet: form to add a book to the database + * + * + * @author Julian Hinxlage + * @version 0.1 + * @since 0.1 + */ public class AddBook extends HttpServlet { @@ -18,20 +26,19 @@ public class AddBook extends HttpServlet { Utillity.insertFile(out, "template_head.html"); out.println("
"); - Utillity.addInput(out, "ISBN", "isbn"); + Utillity.addValidationInput(out, "ISBN", "isbn", "text", "0", "[0-9]*"); Utillity.addInput(out,"Titel", "title"); Utillity.addInput(out,"Author", "author"); - Utillity.addInput(out,"Preis", "price", "0"); - Utillity.addInput(out,"Jahr", "year", "2019"); + 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", "DxAzOKSiPoE"); - Utillity.addTypeButton(out, "Hinzufügen", "submit"); + Utillity.addSubmitButton(out, "Hinzufügen"); out.println("
"); - //Utillity.addButton(out,"Hinzufügen", "/itech/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 c7ef4f7..bacbf33 100644 --- a/src/main/java/de/hsel/itech/servlet/BookList.java +++ b/src/main/java/de/hsel/itech/servlet/BookList.java @@ -16,6 +16,14 @@ import java.time.Year; import java.util.ArrayList; import java.util.List; +/** + * Servlet: list of all books and remove function + * + * + * @author Julian Hinxlage + * @version 0.1 + * @since 0.1 + */ public class BookList extends HttpServlet { protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) @@ -25,12 +33,11 @@ public class BookList extends HttpServlet { //get database object Database db = Database.getInstance(); - //print parameters (debug) - /*Enumeration names = req.getParameterNames(); - while (names.hasMoreElements()) { - String name = names.nextElement(); - System.out.println(name + "=" + req.getParameter(name)); - }*/ + resp.setCharacterEncoding("utf-8"); + PrintWriter out = resp.getWriter(); + + //header template + Utillity.insertFile(out, "template_head.html"); //remove book by id String param = req.getParameter("removeid"); @@ -40,45 +47,76 @@ public class BookList extends HttpServlet { resp.sendRedirect("booklist"); } + //add book to database if (req.getParameter("title") != null) { - long isbn = Long.parseLong(req.getParameter("isbn")); - String title = req.getParameter("title"); - String authorName = req.getParameter("author"); - int price = Integer.parseInt(req.getParameter("price")); - int year = Integer.parseInt(req.getParameter("year")); - String categoryName = req.getParameter("category"); - String description = req.getParameter("description"); - String publisher = req.getParameter("publisher"); - String image = req.getParameter("image"); + try { + long isbn = Long.parseLong(req.getParameter("isbn")); + String title = req.getParameter("title"); + String authorName = req.getParameter("author"); + String priceString = req.getParameter("price"); + String[] priceParts = priceString.split(","); + int price = Integer.parseInt(priceParts[0]) * 100; + if (priceParts.length >= 2) { + if (priceParts[1].length() == 1) { + price += Integer.parseInt(priceParts[1]) * 10; + } else { + price += Integer.parseInt(priceParts[1]); + } + } + int year = Integer.parseInt(req.getParameter("year")); + String categoryName = req.getParameter("category"); + String description = req.getParameter("description"); + String publisher = req.getParameter("publisher"); + String image = req.getParameter("image"); - ArrayList authors = new ArrayList<>(); - String[] authorNames = authorName.split(","); - for (String name : authorNames) { - authors.add(new Author(name)); + //check isbn + List books = db.getBooks(); + boolean found = false; + if (books != null) { + for(Book b : books){ + if(b.getIsbn() == isbn){ + found = true; + break; + } + } + } + + if (found) { + out.println(""); + } else { + ArrayList authors = new ArrayList<>(); + String[] authorNames = authorName.split(","); + for (String name : authorNames) { + authors.add(new Author(name)); + } + Category category = new Category(); + category.setName(categoryName); + Book book = new Book(isbn, + authors, + new Publisher(publisher), + category, + title, Year.of(year), price, description, image + ); + db.insert(book); + resp.sendRedirect("booklist"); + } + } + catch (Exception e){ + e.printStackTrace(); + out.println(""); } - Category category = new Category(); - category.setName(categoryName); - Book book = new Book(isbn, - authors, - new Publisher(publisher), - category, - title, Year.of(year), price, description, image - ); - db.insert(book); - resp.sendRedirect("booklist"); } - resp.setCharacterEncoding("utf-8"); - PrintWriter out = resp.getWriter(); - - //header template - Utillity.insertFile(out, "template_head.html"); + //list all books List books = db.getBooks(); - if (books != null) { //book entries @@ -92,18 +130,16 @@ public class BookList extends HttpServlet { out.println(" "); out.println("
"); - out.println("

" + book.getTitle() + "

"); - out.println("
"); + out.println("

" + book.getTitle() + "

"); + out.println("ISBN: " + book.getIsbn() + "
"); + for (int i = 0; i < book.getAuthors().size(); i++) { - out.println("

" + book.getAuthors().get(0).getName() + "

"); + out.println("von " + book.getAuthors().get(0).getName()); } - out.println("
"); - out.print("

" + book.getPrice() / 100 + ","); - if (book.getPrice() % 100 < 10) { - out.print("0"); - } - out.println(book.getPrice() % 100 + " €

"); - out.println("
"); + + out.println("
Veröffentlicht von " + book.getPublisher().getName() + ""); + out.println("

" + book.getDescription() + ""); + out.println("
"); out.println("
"); @@ -113,6 +149,16 @@ public class BookList extends HttpServlet { out.println("Entfernen"); out.println(""); + out.println("
"); + out.println("
"); + + out.print("

" + book.getPrice() / 100 + ","); + if (book.getPrice() % 100 < 10) { + out.print("0"); + } + + out.println(book.getPrice() % 100 + " €

"); + out.println("
"); out.println(" "); out.println(" "); diff --git a/src/main/java/de/hsel/itech/servlet/Utillity.java b/src/main/java/de/hsel/itech/servlet/Utillity.java index dddceac..9521525 100644 --- a/src/main/java/de/hsel/itech/servlet/Utillity.java +++ b/src/main/java/de/hsel/itech/servlet/Utillity.java @@ -30,6 +30,15 @@ public class Utillity { out.println(""); } + static public void addValidationInput(PrintWriter out, String label, String name, String type, String value, String pattern){ + out.println("
"); + out.println("
"); + out.println(label); + out.println(""); + out.println("
"); + out.println("
"); + } + static public void addButton(PrintWriter out, String label, String url){ out.println("
"); out.println("
"); @@ -42,23 +51,10 @@ public class Utillity { out.println("
"); } - static public void addTypeButton(PrintWriter out, String label, String type){ - /* - out.println("
"); - out.println("
"); - out.println(" "); - out.println("
"); - out.println("
"); - */ - + static public void addSubmitButton(PrintWriter out, String label){ out.println("
"); out.println("
"); - out.println(" "); + out.println(" "); out.println("
"); out.println("
"); } diff --git a/src/main/resources/database.sql b/src/main/resources/database.sql index ecde032..9da8296 100644 --- a/src/main/resources/database.sql +++ b/src/main/resources/database.sql @@ -1,6 +1,6 @@ CREATE TABLE IF NOT EXISTS test(id bigint primary key auto_increment, hello varchar(25)); INSERT IGNORE INTO test(hello) values ('Welt'); -CREATE TABLE IF NOT EXISTS book (id bigint primary key auto_increment, isbn bigint unique, title varchar(50), description varchar(500), price int, year year, publisher bigint, category bigint, image varchar(11) unique); +CREATE TABLE IF NOT EXISTS book (id bigint primary key auto_increment, isbn bigint unique, title varchar(50), description varchar(500), price int, year year, publisher bigint, category bigint, image varchar(11)); CREATE TABLE IF NOT EXISTS publisher(id bigint primary key auto_increment, name varchar(50) unique); CREATE TABLE IF NOT EXISTS author(id bigint primary key auto_increment, name varchar(50) unique); CREATE TABLE IF NOT EXISTS author_book(author bigint, book bigint, primary key(author, book), foreign key (author) REFERENCES author(id), foreign key (book) references book(id));