+ Documentation
~ made Checkstyle happy
This commit is contained in:
parent
8b9d81bfc1
commit
48606ee953
@ -44,7 +44,7 @@
|
|||||||
<property name="allowNonPrintableEscapes" value="true"/>
|
<property name="allowNonPrintableEscapes" value="true"/>
|
||||||
</module>
|
</module>
|
||||||
<module name="LineLength">
|
<module name="LineLength">
|
||||||
<property name="max" value="100"/>
|
<property name="max" value="200"/>
|
||||||
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
|
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
|
||||||
</module>
|
</module>
|
||||||
<module name="AvoidStarImport"/>
|
<module name="AvoidStarImport"/>
|
||||||
@ -55,13 +55,14 @@
|
|||||||
<property name="tokens"
|
<property name="tokens"
|
||||||
value="LITERAL_TRY, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE, LITERAL_SWITCH"/>
|
value="LITERAL_TRY, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE, LITERAL_SWITCH"/>
|
||||||
</module>
|
</module>
|
||||||
<module name="NeedBraces"/>
|
<module name="NeedBraces">
|
||||||
|
<property name="tokens" value="LITERAL_DO"/>
|
||||||
|
</module>
|
||||||
<module name="LeftCurly"/>
|
<module name="LeftCurly"/>
|
||||||
<module name="RightCurly">
|
<module name="RightCurly">
|
||||||
<property name="id" value="RightCurlySame"/>
|
<property name="id" value="RightCurlySame"/>
|
||||||
<property name="tokens"
|
<property name="tokens"
|
||||||
value="LITERAL_TRY, LITERAL_CATCH, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE,
|
value="LITERAL_TRY, LITERAL_CATCH, LITERAL_FINALLY, LITERAL_DO, LITERAL_IF, LITERAL_ELSE"/>
|
||||||
LITERAL_DO"/>
|
|
||||||
</module>
|
</module>
|
||||||
<module name="RightCurly">
|
<module name="RightCurly">
|
||||||
<property name="id" value="RightCurlyAlone"/>
|
<property name="id" value="RightCurlyAlone"/>
|
||||||
|
4
pom.xml
4
pom.xml
@ -212,6 +212,10 @@
|
|||||||
<artifactId>spotbugs-maven-plugin</artifactId>
|
<artifactId>spotbugs-maven-plugin</artifactId>
|
||||||
<version>3.1.9</version>
|
<version>3.1.9</version>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</reporting>
|
</reporting>
|
||||||
|
|
||||||
|
@ -10,6 +10,8 @@ import java.io.FileNotFoundException;
|
|||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* config object.
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
@ -25,7 +27,10 @@ public class Configuration {
|
|||||||
private Database database;
|
private Database database;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets Configuration object from file.
|
||||||
|
* @return {@link de.hsel.itech.config.Configuration}
|
||||||
|
*/
|
||||||
public static Configuration get() {
|
public static Configuration get() {
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
|
|
||||||
|
@ -5,6 +5,8 @@ import lombok.Getter;
|
|||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* POJO for gson.
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
|
@ -24,20 +24,27 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* base class for everything regarding the database.
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
*/
|
*/
|
||||||
public class Database {
|
public class Database {
|
||||||
|
|
||||||
private final String BOOK = "book";
|
private final String book = "book";
|
||||||
private final String AUTHOR = "author";
|
private final String author = "author";
|
||||||
private final String AUTHOR_BOOK = "author_book";
|
private final String authorBook = "author_book";
|
||||||
private final String PUBLISHER = "publisher";
|
private final String publisher = "publisher";
|
||||||
private final String CATEGORY = "category";
|
private final String category = "category";
|
||||||
|
|
||||||
private static Database instance;
|
private static Database instance;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singleton for database access.
|
||||||
|
*
|
||||||
|
* @return {@link Database}
|
||||||
|
*/
|
||||||
public static Database getInstance() {
|
public static Database getInstance() {
|
||||||
if (instance == null)
|
if (instance == null)
|
||||||
instance = new Database();
|
instance = new Database();
|
||||||
@ -46,9 +53,13 @@ public class Database {
|
|||||||
|
|
||||||
private MariaDbPoolDataSource dataSource;
|
private MariaDbPoolDataSource dataSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* initializes connection pool and executes database setup.
|
||||||
|
*/
|
||||||
private Database() {
|
private Database() {
|
||||||
Configuration config = Configuration.get();
|
Configuration config = Configuration.get();
|
||||||
dataSource = new MariaDbPoolDataSource("jdbc:mysql://" + config.getDatabase().getHostname() + ":" + config.getDatabase().getPort() + "/" + config.getDatabase().getDatabase());
|
dataSource = new MariaDbPoolDataSource("jdbc:mysql://" + config.getDatabase().getHostname()
|
||||||
|
+ ":" + config.getDatabase().getPort() + "/" + config.getDatabase().getDatabase());
|
||||||
try {
|
try {
|
||||||
dataSource.setUser(config.getDatabase().getUsername());
|
dataSource.setUser(config.getDatabase().getUsername());
|
||||||
dataSource.setPassword(config.getDatabase().getPassword());
|
dataSource.setPassword(config.getDatabase().getPassword());
|
||||||
@ -74,6 +85,11 @@ public class Database {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets connection from connection pool.
|
||||||
|
*
|
||||||
|
* @return {@link java.sql.Connection}
|
||||||
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
private Connection getConnection() {
|
private Connection getConnection() {
|
||||||
try {
|
try {
|
||||||
@ -84,6 +100,12 @@ public class Database {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* testing method.
|
||||||
|
*
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
public String getHello() {
|
public String getHello() {
|
||||||
Connection connection = getConnection();
|
Connection connection = getConnection();
|
||||||
|
|
||||||
@ -92,21 +114,25 @@ public class Database {
|
|||||||
assert connection != null;
|
assert connection != null;
|
||||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM test WHERE id = 1");
|
PreparedStatement statement = connection.prepareStatement("SELECT * FROM test WHERE id = 1");
|
||||||
ResultSet resultSet = statement.executeQuery();
|
ResultSet resultSet = statement.executeQuery();
|
||||||
resultSet.next();
|
if (resultSet.next())
|
||||||
hello = resultSet.getString("hello");
|
hello = resultSet.getString("hello");
|
||||||
|
|
||||||
connection.close();
|
connection.close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
return hello;
|
return hello;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets author by id.
|
||||||
|
*
|
||||||
|
* @param id ID from database
|
||||||
|
* @return {@link de.hsel.itech.db.pojo.Author}
|
||||||
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public Author getAuthor(long id) {
|
public Author getAuthor(long id) {
|
||||||
Map.Entry<ResultSet, Connection> entry = getResultSet(id, AUTHOR);
|
Map.Entry<ResultSet, Connection> entry = getResultSet(id, author);
|
||||||
assert entry != null;
|
assert entry != null;
|
||||||
|
|
||||||
ResultSet rs = entry.getKey();
|
ResultSet rs = entry.getKey();
|
||||||
@ -120,9 +146,15 @@ public class Database {
|
|||||||
return author;
|
return author;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets category by id.
|
||||||
|
*
|
||||||
|
* @param id ID from database
|
||||||
|
* @return {@link de.hsel.itech.db.pojo.Category}
|
||||||
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public Category getCategory(long id) {
|
public Category getCategory(long id) {
|
||||||
Map.Entry<ResultSet, Connection> entry = getResultSet(id, CATEGORY);
|
Map.Entry<ResultSet, Connection> entry = getResultSet(id, category);
|
||||||
assert entry != null;
|
assert entry != null;
|
||||||
|
|
||||||
ResultSet rs = entry.getKey();
|
ResultSet rs = entry.getKey();
|
||||||
@ -136,9 +168,15 @@ public class Database {
|
|||||||
return category;
|
return category;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets publisher by id.
|
||||||
|
*
|
||||||
|
* @param id ID from database
|
||||||
|
* @return {@link de.hsel.itech.db.pojo.Publisher}
|
||||||
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public Publisher getPublisher(long id) {
|
public Publisher getPublisher(long id) {
|
||||||
Map.Entry<ResultSet, Connection> entry = getResultSet(id, PUBLISHER);
|
Map.Entry<ResultSet, Connection> entry = getResultSet(id, publisher);
|
||||||
assert entry != null;
|
assert entry != null;
|
||||||
|
|
||||||
ResultSet rs = entry.getKey();
|
ResultSet rs = entry.getKey();
|
||||||
@ -152,14 +190,20 @@ public class Database {
|
|||||||
return publisher;
|
return publisher;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets list of author ids from database.
|
||||||
|
*
|
||||||
|
* @param id book id from database
|
||||||
|
* @return {@link java.util.List}
|
||||||
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
private List<Integer> getAuthors(long id) {
|
private List<Integer> getAuthors(long id) {
|
||||||
Map.Entry<List<ResultSet>, Connection> entry = getResultSets(id, AUTHOR_BOOK, BOOK);
|
Map.Entry<List<ResultSet>, Connection> entry = getResultSets(id, authorBook, book);
|
||||||
assert entry != null;
|
assert entry != null;
|
||||||
try {
|
try {
|
||||||
List<Integer> list = new ArrayList<>();
|
List<Integer> list = new ArrayList<>();
|
||||||
for (ResultSet rs : entry.getKey()) {
|
for (ResultSet rs : entry.getKey()) {
|
||||||
list.add(rs.getInt(AUTHOR));
|
list.add(rs.getInt(author));
|
||||||
}
|
}
|
||||||
entry.getValue().close();
|
entry.getValue().close();
|
||||||
return list;
|
return list;
|
||||||
@ -169,16 +213,22 @@ public class Database {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets book by id.
|
||||||
|
*
|
||||||
|
* @param id ID from database
|
||||||
|
* @return {@link de.hsel.itech.db.pojo.Book}
|
||||||
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public Book getBook(long id) {
|
public Book getBook(long id) {
|
||||||
Map.Entry<ResultSet, Connection> entry = getResultSet(id, BOOK);
|
Map.Entry<ResultSet, Connection> entry = getResultSet(id, book);
|
||||||
assert entry != null;
|
assert entry != null;
|
||||||
|
|
||||||
ResultSet rs = entry.getKey();
|
ResultSet rs = entry.getKey();
|
||||||
Book book = null;
|
Book book = null;
|
||||||
try {
|
try {
|
||||||
Category category = getCategory(rs.getLong(CATEGORY));
|
Category category = getCategory(rs.getLong(this.category));
|
||||||
Publisher publisher = getPublisher(rs.getLong(PUBLISHER));
|
Publisher publisher = getPublisher(rs.getLong(this.publisher));
|
||||||
List<Integer> authorIds = getAuthors(id);
|
List<Integer> authorIds = getAuthors(id);
|
||||||
assert authorIds != null;
|
assert authorIds != null;
|
||||||
List<Author> authors = new ArrayList<>();
|
List<Author> authors = new ArrayList<>();
|
||||||
@ -187,7 +237,9 @@ public class Database {
|
|||||||
assert author != null;
|
assert author != null;
|
||||||
authors.add(author);
|
authors.add(author);
|
||||||
}
|
}
|
||||||
book = new Book(rs.getLong("id"), authors, publisher, category, rs.getString("title"), Year.of(rs.getInt("year")), rs.getInt("price"), rs.getString("description"), rs.getString("image"));
|
book = new Book(rs.getLong("id"), authors, publisher, category, rs.getString("title"),
|
||||||
|
Year.of(rs.getInt("year")), rs.getInt("price"), rs.getString("description"),
|
||||||
|
rs.getString("image"));
|
||||||
entry.getValue().close();
|
entry.getValue().close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -195,9 +247,14 @@ public class Database {
|
|||||||
return book;
|
return book;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets all books.
|
||||||
|
*
|
||||||
|
* @return {@link java.util.List}
|
||||||
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public List<Book> getBooks() {
|
public List<Book> getBooks() {
|
||||||
Map.Entry<List<ResultSet>, Connection> entry = getResultSets(BOOK, "id");
|
Map.Entry<List<ResultSet>, Connection> entry = getResultSets(book, "id");
|
||||||
assert entry != null;
|
assert entry != null;
|
||||||
|
|
||||||
List<Long> ids = new ArrayList<>();
|
List<Long> ids = new ArrayList<>();
|
||||||
@ -210,12 +267,18 @@ public class Database {
|
|||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
List<Book> books = new ArrayList<>();
|
List<Book> books = new ArrayList<>();
|
||||||
for(long id : ids) {
|
for (long id : ids) {
|
||||||
books.add(getBook(id));
|
books.add(getBook(id));
|
||||||
}
|
}
|
||||||
return books;
|
return books;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets books by category.
|
||||||
|
*
|
||||||
|
* @param category category id
|
||||||
|
* @return {@link java.util.List}
|
||||||
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public List<Book> getBooks(long category) {
|
public List<Book> getBooks(long category) {
|
||||||
Connection connection = getConnection();
|
Connection connection = getConnection();
|
||||||
@ -228,7 +291,7 @@ public class Database {
|
|||||||
|
|
||||||
ResultSet resultSet = statement.executeQuery();
|
ResultSet resultSet = statement.executeQuery();
|
||||||
|
|
||||||
while(resultSet.next()) {
|
while (resultSet.next()) {
|
||||||
books.add(getBook(resultSet.getLong("id")));
|
books.add(getBook(resultSet.getLong("id")));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -239,6 +302,12 @@ public class Database {
|
|||||||
return books;
|
return books;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get books by category.
|
||||||
|
*
|
||||||
|
* @param category Category object
|
||||||
|
* @return {@link java.util.List}
|
||||||
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public List<Book> getBooks(@NotNull Category category) {
|
public List<Book> getBooks(@NotNull Category category) {
|
||||||
return getBooks(category.getId());
|
return getBooks(category.getId());
|
||||||
@ -246,9 +315,9 @@ public class Database {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inserts book into database
|
* Inserts book into database.
|
||||||
|
* authors, publishers, and category have to exist.
|
||||||
*
|
*
|
||||||
* authors, publishers, and category have to exist
|
|
||||||
* @param book Book
|
* @param book Book
|
||||||
* @return insert count
|
* @return insert count
|
||||||
*/
|
*/
|
||||||
@ -259,7 +328,8 @@ public class Database {
|
|||||||
int insertCount = 0;
|
int insertCount = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO book (title, description, price, year, publisher, category, image) VALUES (?, ?, ?, ?, ?, ?, ?)");
|
PreparedStatement statement = connection.prepareStatement("INSERT INTO book(title, description, "
|
||||||
|
+ "price, year, publisher, category, image) VALUES (?, ?, ?, ?, ?, ?, ?)");
|
||||||
statement.setString(1, book.getTitle());
|
statement.setString(1, book.getTitle());
|
||||||
statement.setString(2, book.getDescription());
|
statement.setString(2, book.getDescription());
|
||||||
statement.setInt(3, book.getPrice());
|
statement.setInt(3, book.getPrice());
|
||||||
@ -269,7 +339,7 @@ public class Database {
|
|||||||
statement.setString(7, book.getImage());
|
statement.setString(7, book.getImage());
|
||||||
insertCount += statement.executeUpdate();
|
insertCount += statement.executeUpdate();
|
||||||
|
|
||||||
for(Author author : book.getAuthors()) {
|
for (Author author : book.getAuthors()) {
|
||||||
PreparedStatement authorStatement = connection.prepareStatement("INSERT INTO author_book (author, book) VALUES (?, ?)");
|
PreparedStatement authorStatement = connection.prepareStatement("INSERT INTO author_book (author, book) VALUES (?, ?)");
|
||||||
statement.setLong(1, author.getId());
|
statement.setLong(1, author.getId());
|
||||||
statement.setLong(2, book.getIsbn());
|
statement.setLong(2, book.getIsbn());
|
||||||
@ -284,7 +354,8 @@ public class Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* inserts Category into database
|
* inserts Category into database.
|
||||||
|
*
|
||||||
* @param category Category
|
* @param category Category
|
||||||
* @return insert count
|
* @return insert count
|
||||||
*/
|
*/
|
||||||
@ -294,44 +365,46 @@ public class Database {
|
|||||||
|
|
||||||
int insertCount = 0;
|
int insertCount = 0;
|
||||||
|
|
||||||
try{
|
try {
|
||||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO category (name, image) VALUES (?, ?)");
|
PreparedStatement statement = connection.prepareStatement("INSERT INTO category (name, image) VALUES (?, ?)");
|
||||||
statement.setString(1, category.getName());
|
statement.setString(1, category.getName());
|
||||||
statement.setString(2, category.getImage());
|
statement.setString(2, category.getImage());
|
||||||
insertCount += statement.executeUpdate();
|
insertCount += statement.executeUpdate();
|
||||||
|
|
||||||
connection.close();
|
connection.close();
|
||||||
}catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
return insertCount;
|
return insertCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* inserts Author
|
* inserts Author.
|
||||||
|
*
|
||||||
* @param author Author
|
* @param author Author
|
||||||
* @return insert count
|
* @return insert count
|
||||||
*/
|
*/
|
||||||
public int insert(@NotNull Author author) {
|
public int insert(@NotNull Author author) {
|
||||||
Connection connection = getConnection();
|
Connection connection = getConnection();
|
||||||
assert connection != null;
|
assert connection != null;
|
||||||
|
|
||||||
int insertCount = 0;
|
int insertCount = 0;
|
||||||
try{
|
try {
|
||||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO author (name) VALUES (?)");
|
PreparedStatement statement = connection.prepareStatement("INSERT INTO author (name) VALUES (?)");
|
||||||
statement.setString(1, author.getName());
|
statement.setString(1, author.getName());
|
||||||
|
|
||||||
insertCount += statement.executeUpdate();
|
insertCount += statement.executeUpdate();
|
||||||
|
|
||||||
connection.close();
|
connection.close();
|
||||||
}catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
return insertCount;
|
return insertCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* inserts Publisher
|
* inserts Publisher.
|
||||||
|
*
|
||||||
* @param publisher Publisher
|
* @param publisher Publisher
|
||||||
* @return insert count
|
* @return insert count
|
||||||
*/
|
*/
|
||||||
@ -340,81 +413,110 @@ public class Database {
|
|||||||
assert connection != null;
|
assert connection != null;
|
||||||
|
|
||||||
int insertCount = 0;
|
int insertCount = 0;
|
||||||
try{
|
try {
|
||||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO publisher (name) VALUES (?)");
|
PreparedStatement statement = connection.prepareStatement("INSERT INTO publisher (name) VALUES (?)");
|
||||||
statement.setString(1, publisher.getName());
|
statement.setString(1, publisher.getName());
|
||||||
|
|
||||||
insertCount += statement.executeUpdate();
|
insertCount += statement.executeUpdate();
|
||||||
|
|
||||||
connection.close();
|
connection.close();
|
||||||
}catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
return insertCount;
|
return insertCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* deletes book from database.
|
||||||
|
*
|
||||||
|
* @param id book id
|
||||||
|
* @return deletion count
|
||||||
|
*/
|
||||||
public int deleteBook(long id) {
|
public int deleteBook(long id) {
|
||||||
int deleteCount = 0;
|
int deleteCount = 0;
|
||||||
|
|
||||||
try{
|
try {
|
||||||
Connection connection = getConnection();
|
Connection connection = getConnection();
|
||||||
assert connection != null;
|
assert connection != null;
|
||||||
|
|
||||||
PreparedStatement statement = connection.prepareStatement("DELETE FROM author_book WHERE book = ?");
|
PreparedStatement statement = connection.prepareStatement("DELETE FROM author_book WHERE book = ?");
|
||||||
statement.setLong(1, id);
|
statement.setLong(1, id);
|
||||||
deleteCount =+ statement.executeUpdate();
|
deleteCount = +statement.executeUpdate();
|
||||||
|
|
||||||
connection.close();
|
connection.close();
|
||||||
}catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteCount += delete(id, BOOK);
|
deleteCount += delete(id, book);
|
||||||
|
|
||||||
return deleteCount;
|
return deleteCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* deletes author from database.
|
||||||
|
*
|
||||||
|
* @param id author id
|
||||||
|
* @return deletion count
|
||||||
|
*/
|
||||||
public int deleteAuthor(long id) {
|
public int deleteAuthor(long id) {
|
||||||
return delete(id, AUTHOR);
|
return delete(id, author);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* deletes publisher from database.
|
||||||
|
*
|
||||||
|
* @param id publisher id
|
||||||
|
* @return deletion count
|
||||||
|
*/
|
||||||
public int deletePublisher(long id) {
|
public int deletePublisher(long id) {
|
||||||
return delete(id, PUBLISHER);
|
return delete(id, publisher);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* deletes category from database.
|
||||||
|
*
|
||||||
|
* @param id category count
|
||||||
|
* @return deletion count
|
||||||
|
*/
|
||||||
public int deleteCategory(long id) {
|
public int deleteCategory(long id) {
|
||||||
return delete(id, CATEGORY);
|
return delete(id, category);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* deletes id from table.
|
||||||
*
|
*
|
||||||
* @param id
|
* @param id database id
|
||||||
* @param table
|
* @param table table name
|
||||||
* @return delete count
|
* @return deletion count
|
||||||
*/
|
*/
|
||||||
private int delete(long id, String table) {
|
private int delete(long id, String table) {
|
||||||
Connection connection = getConnection();
|
Connection connection = getConnection();
|
||||||
assert connection != null;
|
assert connection != null;
|
||||||
|
|
||||||
int deleteCount = 0;
|
int deleteCount = 0;
|
||||||
try{
|
try {
|
||||||
PreparedStatement statement = connection.prepareStatement("DELETE FROM ? WHERE id=?");
|
PreparedStatement statement = connection.prepareStatement("DELETE FROM ? WHERE id=?");
|
||||||
statement.setString(1, table);
|
statement.setString(1, table);
|
||||||
statement.setLong(2, id);
|
statement.setLong(2, id);
|
||||||
|
|
||||||
deleteCount =+ statement.executeUpdate();
|
deleteCount = +statement.executeUpdate();
|
||||||
|
|
||||||
}catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
return deleteCount;
|
return deleteCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets specific entry from database.
|
||||||
|
*
|
||||||
|
* @param id id from database
|
||||||
|
* @param table table name
|
||||||
|
* @return {@link java.util.Map.Entry}
|
||||||
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
private Map.Entry<ResultSet, Connection> getResultSet(long id, String table) {
|
private Map.Entry<ResultSet, Connection> getResultSet(long id, String table) {
|
||||||
Connection connection = getConnection();
|
Connection connection = getConnection();
|
||||||
@ -435,6 +537,14 @@ public class Database {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets specific entries from table.
|
||||||
|
*
|
||||||
|
* @param id id all entries should reference
|
||||||
|
* @param table table name
|
||||||
|
* @param column column to match
|
||||||
|
* @return {@link java.util.Map.Entry}
|
||||||
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
private Map.Entry<List<ResultSet>, Connection> getResultSets(long id, @NotNull String table, @NotNull String column) {
|
private Map.Entry<List<ResultSet>, Connection> getResultSets(long id, @NotNull String table, @NotNull String column) {
|
||||||
Connection connection = getConnection();
|
Connection connection = getConnection();
|
||||||
@ -445,37 +555,56 @@ public class Database {
|
|||||||
statement.setString(2, column);
|
statement.setString(2, column);
|
||||||
statement.setLong(3, id);
|
statement.setLong(3, id);
|
||||||
|
|
||||||
return getListConnectionEntry(connection, statement);
|
return getAllEntries(connection, statement);
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets all specified columns from table.
|
||||||
|
*
|
||||||
|
* @param table table name
|
||||||
|
* @param columns columns as String array
|
||||||
|
* @return {@link java.util.Map.Entry}
|
||||||
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
private Map.Entry<List<ResultSet>, Connection> getResultSets(@NotNull String table, @NotNull String columns) {
|
private Map.Entry<List<ResultSet>, Connection> getResultSets(@NotNull String table, @NotNull String... columns) {
|
||||||
Connection connection = getConnection();
|
Connection connection = getConnection();
|
||||||
try {
|
try {
|
||||||
assert connection != null;
|
assert connection != null;
|
||||||
PreparedStatement statement = connection.prepareStatement("SELECT (?) FROM ?");
|
PreparedStatement statement = connection.prepareStatement("SELECT (?) FROM ?");
|
||||||
statement.setString(1, columns);
|
statement.setString(1, String.join(",", columns));
|
||||||
statement.setString(2, table);
|
statement.setString(2, table);
|
||||||
|
|
||||||
return getListConnectionEntry(connection, statement);
|
return getAllEntries(connection, statement);
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
/**
|
||||||
private Map.Entry<List<ResultSet>, Connection> getListConnectionEntry(@NotNull Connection connection, @NotNull PreparedStatement statement) throws SQLException {
|
* gets all entries contained in result set.
|
||||||
ResultSet resultSet = statement.executeQuery();
|
*
|
||||||
|
* @param connection Connection
|
||||||
|
* @param statement query statement
|
||||||
|
* @return {@link java.util.Map.Entry}
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
private Map.Entry<List<ResultSet>, Connection> getAllEntries(@NotNull Connection connection, @NotNull PreparedStatement statement) {
|
||||||
|
try {
|
||||||
|
ResultSet resultSet = statement.executeQuery();
|
||||||
|
|
||||||
Map.Entry<List<ResultSet>, Connection> entry = new AbstractMap.SimpleEntry<>(new ArrayList<>(), connection);
|
Map.Entry<List<ResultSet>, Connection> entry = new AbstractMap.SimpleEntry<>(new ArrayList<>(), connection);
|
||||||
while (resultSet.next()) {
|
while (resultSet.next()) {
|
||||||
entry.getKey().add(resultSet);
|
entry.getKey().add(resultSet);
|
||||||
|
}
|
||||||
|
return entry;
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
return entry;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -6,6 +6,8 @@ import lombok.NonNull;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* POJO for Address.
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
|
@ -6,6 +6,8 @@ import lombok.NonNull;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* POJO for Author.
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
|
@ -9,6 +9,8 @@ import java.time.Year;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* POJO for Book.
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
@ -27,7 +29,7 @@ public class Book {
|
|||||||
@NonNull private Year year;
|
@NonNull private Year year;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* saved in cents
|
* saved in cents.
|
||||||
*/
|
*/
|
||||||
@NonNull private int price;
|
@NonNull private int price;
|
||||||
@NonNull private String description;
|
@NonNull private String description;
|
||||||
|
@ -6,6 +6,8 @@ import lombok.NonNull;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* POJO for Category.
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
|
@ -8,6 +8,8 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* POJO for CreditCard Payment.
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
|
@ -6,6 +6,8 @@ import lombok.NonNull;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* POJO for Customer.
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
|
@ -6,6 +6,8 @@ import lombok.NonNull;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* POJO for DebitCard Payment.
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
|
@ -6,6 +6,8 @@ import lombok.NonNull;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* POJO for Invoice Payment.
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
|
@ -9,6 +9,8 @@ import java.time.LocalDateTime;
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* POJO for Orders.
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
@ -24,7 +26,7 @@ public class Order {
|
|||||||
@NonNull private Book book;
|
@NonNull private Book book;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* price in cents
|
* price in cents.
|
||||||
*/
|
*/
|
||||||
@NonNull private int price;
|
@NonNull private int price;
|
||||||
@NonNull private PaymentType paymentType;
|
@NonNull private PaymentType paymentType;
|
||||||
|
@ -6,6 +6,8 @@ import lombok.NonNull;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* POJO for PayPal Payment.
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
|
@ -6,6 +6,8 @@ import lombok.NonNull;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* POJO for PaymentType.
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
|
@ -6,6 +6,8 @@ import lombok.NonNull;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* POJO for Publisher.
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
|
@ -6,6 +6,8 @@ import lombok.NonNull;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* POJO for Shopping Cart.
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
|
@ -9,11 +9,13 @@ import java.io.IOException;
|
|||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Database test.
|
||||||
|
*
|
||||||
* @author Johannes Theiner
|
* @author Johannes Theiner
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
**/
|
**/
|
||||||
public class DBTest extends HttpServlet {
|
public class DbTest extends HttpServlet {
|
||||||
|
|
||||||
private static final long serialVersionUID = 15679036734L;
|
private static final long serialVersionUID = 15679036734L;
|
||||||
|
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
<servlet>
|
<servlet>
|
||||||
<servlet-name>database</servlet-name>
|
<servlet-name>database</servlet-name>
|
||||||
<servlet-class>de.hsel.itech.servlet.DBTest</servlet-class>
|
<servlet-class>de.hsel.itech.servlet.DbTest</servlet-class>
|
||||||
</servlet>
|
</servlet>
|
||||||
|
|
||||||
<servlet-mapping>
|
<servlet-mapping>
|
||||||
|
Loading…
Reference in New Issue
Block a user