+ insert(Book/Author/Publisher)

+ getBooks

Signed-off-by: Johannes Theiner <j.theiner@live.de>
This commit is contained in:
Johannes Theiner 2019-04-17 20:14:26 +02:00
parent 18efa59177
commit 59ecac912c
16 changed files with 302 additions and 103 deletions

View File

@ -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 org.jetbrains.annotations.Nullable;
import org.mariadb.jdbc.MariaDbPoolDataSource;
@ -23,8 +24,6 @@ import java.util.List;
import java.util.Map;
/**
*
*
* @author Johannes Theiner
* @version 0.1
* @since 0.1
@ -34,7 +33,7 @@ public class Database {
private static Database instance;
public static Database getInstance() {
if(instance == null)
if (instance == null)
instance = new Database();
return instance;
}
@ -53,7 +52,7 @@ public class Database {
assert connection != null;
URL file = getClass().getClassLoader().getResource("database.sql");
assert file != null;
try(BufferedReader br = new BufferedReader(new FileReader(file.getFile()))) {
try (BufferedReader br = new BufferedReader(new FileReader(file.getFile()))) {
for (String line = br.readLine(); line != null; line = br.readLine()) {
PreparedStatement statement = connection.prepareStatement(line);
statement.executeUpdate();
@ -61,8 +60,7 @@ public class Database {
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
} finally {
connection.close();
}
} catch (SQLException e) {
@ -100,52 +98,6 @@ public class Database {
}
@Nullable
private Map.Entry<ResultSet, Connection> getResultSet(long id, String table) {
Connection connection = getConnection();
try{
assert connection != null;
PreparedStatement statement = connection.prepareStatement("SELECT * FROM ? WHERE ID = ?");
statement.setString(1, table);
statement.setLong(2, id);
ResultSet resultSet = statement.executeQuery();
if(resultSet.next()) {
return new AbstractMap.SimpleEntry<>(resultSet, connection);
}
return null;
}catch (SQLException ex) {
ex.printStackTrace();
}
return null;
}
@Nullable
private Map.Entry<List<ResultSet>, Connection> getResultSets(long id, String table, String column) {
Connection connection = getConnection();
try {
assert connection != null;
PreparedStatement statement = connection.prepareStatement("SELECT * FROM ? WHERE ? = ?");
statement.setString(1, table);
statement.setString(2, column);
statement.setLong(3, id);
ResultSet resultSet = statement.executeQuery();
Map.Entry<List<ResultSet>, Connection> entry = new AbstractMap.SimpleEntry<>(new ArrayList<>(), connection);
while(resultSet.next()) {
entry.getKey().add(resultSet);
}
return entry;
}catch (SQLException ex) {
ex.printStackTrace();
}
return null;
}
@Nullable
public Author getAuthor(long id) {
Map.Entry<ResultSet, Connection> entry = getResultSet(id, "author");
@ -196,11 +148,11 @@ public class Database {
@Nullable
private List<Integer> getAuthors(long id) {
Map.Entry<List<ResultSet>, Connection> entry = getResultSets(id, "author_book", "book");
assert entry != null;
Map.Entry<List<ResultSet>, Connection> entry = getResultSets(id, "author_book", "book");
assert entry != null;
try {
List<Integer> list = new ArrayList<>();
for(ResultSet rs : entry.getKey()) {
for (ResultSet rs : entry.getKey()) {
list.add(rs.getInt("author"));
}
entry.getValue().close();
@ -224,7 +176,7 @@ public class Database {
List<Integer> authorIds = getAuthors(id);
assert authorIds != null;
List<Author> authors = new ArrayList<>();
for(int i : authorIds) {
for (int i : authorIds) {
Author author = getAuthor(i);
assert author != null;
authors.add(author);
@ -237,4 +189,210 @@ public class Database {
return book;
}
@Nullable
public List<Book> getBooks() {
Map.Entry<List<ResultSet>, Connection> entry = getResultSets("book", "id");
assert entry != null;
List<Long> ids = new ArrayList<>();
try {
for (ResultSet resultSet : entry.getKey()) {
ids.add(resultSet.getLong("id"));
}
entry.getValue().close();
} catch (SQLException ex) {
ex.printStackTrace();
}
List<Book> books = new ArrayList<>();
for(long id : ids) {
books.add(getBook(id));
}
return books;
}
@Nullable
public List<Book> getBooks(long category) {
Connection connection = getConnection();
assert connection != null;
List<Book> books = new ArrayList<>();
try {
PreparedStatement statement = connection.prepareStatement("SELECT (id) FROM book WHERE category = ?");
statement.setLong(1, category);
ResultSet resultSet = statement.executeQuery();
while(resultSet.next()) {
books.add(getBook(resultSet.getLong("id")));
}
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
return books;
}
@Nullable
public List<Book> getBooks(@NotNull Category category) {
return getBooks(category.getId());
}
/**
* Inserts book into database
*
* authors, publishers, and category have to exist
* @param book Book
* @return insert count
*/
public int insert(@NotNull Book book) {
Connection connection = getConnection();
assert connection != null;
int insertCount = 0;
try {
PreparedStatement statement = connection.prepareStatement("INSERT INTO book (title, description, price, year, publisher, category, image) VALUES (?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, book.getTitle());
statement.setString(2, book.getDescription());
statement.setInt(3, book.getPrice());
statement.setInt(4, book.getYear().getValue());
statement.setLong(5, book.getPublisher().getId());
statement.setLong(6, book.getCategory().getId());
statement.setString(7, book.getImage());
insertCount += statement.executeUpdate();
for(Author author : book.getAuthors()) {
PreparedStatement authorStatement = connection.prepareStatement("INSERT INTO author_book (author, book) VALUES (?, ?)");
statement.setLong(1, author.getId());
statement.setLong(2, book.getIsbn());
insertCount += authorStatement.executeUpdate();
}
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return insertCount;
}
/**
* inserts Category into database
* @param category Category
* @return insert count
*/
public int insert(@NotNull Category category) {
Connection connection = getConnection();
assert connection != null;
int insertCount = 0;
try{
PreparedStatement statement = connection.prepareStatement("INSERT INTO category (name, image) VALUES (?, ?)");
statement.setString(1, category.getName());
statement.setString(2, category.getImage());
insertCount += statement.executeUpdate();
connection.close();
}catch (SQLException ex) {
ex.printStackTrace();
}
return insertCount;
}
/**
* inserts Author
* @param author Author
* @return insert count
*/
public int insert(@NotNull Author author) {
Connection connection = getConnection();
assert connection != null;
int insertCount = 0;
try{
PreparedStatement statement = connection.prepareStatement("INSERT INTO author (name) VALUES (?)");
statement.setString(1, author.getName());
insertCount += statement.executeUpdate();
connection.close();
}catch (SQLException ex) {
ex.printStackTrace();
}
return insertCount;
}
@Nullable
private Map.Entry<ResultSet, Connection> getResultSet(long id, String table) {
Connection connection = getConnection();
try {
assert connection != null;
PreparedStatement statement = connection.prepareStatement("SELECT * FROM ? WHERE ID = ?");
statement.setString(1, table);
statement.setLong(2, id);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
return new AbstractMap.SimpleEntry<>(resultSet, connection);
}
return null;
} catch (SQLException ex) {
ex.printStackTrace();
}
return null;
}
@NotNull
private Map.Entry<List<ResultSet>, Connection> getListConnectionEntry(Connection connection, PreparedStatement statement) throws SQLException {
ResultSet resultSet = statement.executeQuery();
Map.Entry<List<ResultSet>, Connection> entry = new AbstractMap.SimpleEntry<>(new ArrayList<>(), connection);
while (resultSet.next()) {
entry.getKey().add(resultSet);
}
return entry;
}
@Nullable
private Map.Entry<List<ResultSet>, Connection> getResultSets(long id, String table, String column) {
Connection connection = getConnection();
try {
assert connection != null;
PreparedStatement statement = connection.prepareStatement("SELECT * FROM ? WHERE ? = ?");
statement.setString(1, table);
statement.setString(2, column);
statement.setLong(3, id);
return getListConnectionEntry(connection, statement);
} catch (SQLException ex) {
ex.printStackTrace();
}
return null;
}
@Nullable
private Map.Entry<List<ResultSet>, Connection> getResultSets(String table, String columns) {
Connection connection = getConnection();
try {
assert connection != null;
PreparedStatement statement = connection.prepareStatement("SELECT (?) FROM ?");
statement.setString(1, columns);
statement.setString(2, table);
return getListConnectionEntry(connection, statement);
} catch (SQLException ex) {
ex.printStackTrace();
}
return null;
}
}

View File

@ -2,6 +2,8 @@ package de.hsel.itech.db.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
/**
* @author Johannes Theiner
@ -11,13 +13,14 @@ import lombok.Data;
@Data
@AllArgsConstructor
@RequiredArgsConstructor
public class Address {
private Customer customer;
@NonNull private Customer customer;
private long id;
private String name;
private String street;
private String number;
private int zipCode;
private String city;
@NonNull private String name;
@NonNull private String street;
@NonNull private String number;
@NonNull private int zipCode;
@NonNull private String city;
}

View File

@ -2,6 +2,8 @@ package de.hsel.itech.db.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
/**
* @author Johannes Theiner
@ -11,9 +13,10 @@ import lombok.Data;
@Data
@AllArgsConstructor
@RequiredArgsConstructor
public class Author {
private long id;
private String name;
@NonNull private String name;
}

View File

@ -2,6 +2,8 @@ package de.hsel.itech.db.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.time.Year;
import java.util.List;
@ -14,21 +16,22 @@ import java.util.List;
@Data
@AllArgsConstructor
@RequiredArgsConstructor
public class Book {
private long isbn;
private List<Author> authors;
private Publisher publisher;
private Category category;
private String title;
private Year year;
@NonNull private List<Author> authors;
@NonNull private Publisher publisher;
@NonNull private Category category;
@NonNull private String title;
@NonNull private Year year;
/**
* saved in cents
*/
private int price;
private String description;
private String image;
@NonNull private int price;
@NonNull private String description;
@NonNull private String image;

View File

@ -2,6 +2,8 @@ package de.hsel.itech.db.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
/**
* @author Johannes Theiner
@ -11,9 +13,10 @@ import lombok.Data;
@Data
@AllArgsConstructor
@RequiredArgsConstructor
public class Category {
private long id;
private String name;
private String image;
@NonNull private String name;
@NonNull private String image;
}

View File

@ -2,6 +2,8 @@ package de.hsel.itech.db.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.time.LocalDate;
@ -13,12 +15,13 @@ import java.time.LocalDate;
@Data
@AllArgsConstructor
@RequiredArgsConstructor
public class CreditCardPayment {
private long id;
private Customer customer;
private int number;
private String owner;
private LocalDate expiration;
private int checksum;
@NonNull private Customer customer;
@NonNull private int number;
@NonNull private String owner;
@NonNull private LocalDate expiration;
@NonNull private int checksum;
}

View File

@ -2,6 +2,8 @@ package de.hsel.itech.db.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
/**
* @author Johannes Theiner
@ -11,11 +13,12 @@ import lombok.Data;
@Data
@AllArgsConstructor
@RequiredArgsConstructor
public class Customer {
private long id;
private String email;
private String name;
private String password;
private boolean admin;
@NonNull private String email;
@NonNull private String name;
@NonNull private String password;
@NonNull private boolean admin;
}

View File

@ -2,6 +2,8 @@ package de.hsel.itech.db.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
/**
* @author Johannes Theiner
@ -11,11 +13,12 @@ import lombok.Data;
@Data
@AllArgsConstructor
@RequiredArgsConstructor
public class DebitCardPayment {
private long id;
private Customer customer;
private String iban;
private String bic;
private String owner;
@NonNull private Customer customer;
@NonNull private String iban;
@NonNull private String bic;
@NonNull private String owner;
}

View File

@ -2,6 +2,8 @@ package de.hsel.itech.db.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
/**
* @author Johannes Theiner
@ -11,8 +13,9 @@ import lombok.Data;
@Data
@AllArgsConstructor
@RequiredArgsConstructor
public class InvoicePayment {
private long id;
private Customer customer;
@NonNull private Customer customer;
}

View File

@ -2,6 +2,8 @@ package de.hsel.itech.db.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.time.LocalDateTime;
@ -14,16 +16,17 @@ import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@RequiredArgsConstructor
public class Order {
private long id;
private LocalDateTime date;
private Book book;
@NonNull private LocalDateTime date;
@NonNull private Book book;
/**
* price in cents
*/
private int price;
private PaymentType paymentType;
private Address address;
@NonNull private int price;
@NonNull private PaymentType paymentType;
@NonNull private Address address;
}

View File

@ -2,6 +2,8 @@ package de.hsel.itech.db.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
/**
* @author Johannes Theiner
@ -11,10 +13,11 @@ import lombok.Data;
@Data
@AllArgsConstructor
@RequiredArgsConstructor
public class PayPalPayment {
private long id;
private Customer customer;
private String email;
private String authCode;
@NonNull private Customer customer;
@NonNull private String email;
@NonNull private String authCode;
}

View File

@ -2,6 +2,8 @@ package de.hsel.itech.db.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
/**
* @author Johannes Theiner
@ -11,8 +13,9 @@ import lombok.Data;
@Data
@AllArgsConstructor
@RequiredArgsConstructor
public class PaymentType {
private long id;
private String name;
@NonNull private String name;
}

View File

@ -2,6 +2,8 @@ package de.hsel.itech.db.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
/**
* @author Johannes Theiner
@ -11,8 +13,9 @@ import lombok.Data;
@Data
@AllArgsConstructor
@RequiredArgsConstructor
public class Publisher {
private long id;
private String name;
@NonNull private String name;
}

View File

@ -2,6 +2,8 @@ package de.hsel.itech.db.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
/**
* @author Johannes Theiner
@ -11,10 +13,11 @@ import lombok.Data;
@Data
@AllArgsConstructor
@RequiredArgsConstructor
public class ShoppingCart {
private long id;
private Customer customer;
private Book article;
private int count;
@NonNull private Customer customer;
@NonNull private Book article;
@NonNull private int count;
}

View File

@ -1,5 +1,7 @@
package de.hsel.itech.servlet;
import de.hsel.itech.db.Database;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -11,7 +13,7 @@ import java.io.PrintWriter;
* @version 0.1
* @since 0.1
**/
public class Database extends HttpServlet {
public class DBTest extends HttpServlet {
private static final long serialVersionUID = 15679036734L;
@ -33,7 +35,7 @@ public class Database extends HttpServlet {
out.println("<title>Hallo Welt!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>" + de.hsel.itech.db.Database.getInstance().getHello() + "</h1>");
out.println("<h1>" + Database.getInstance().getHello() + "</h1>");
out.println("</body>");
out.println("</html>");
}

View File

@ -12,7 +12,7 @@
<servlet>
<servlet-name>database</servlet-name>
<servlet-class>de.hsel.itech.servlet.Database</servlet-class>
<servlet-class>de.hsel.itech.servlet.DBTest</servlet-class>
</servlet>
<servlet-mapping>