summaryrefslogtreecommitdiffstats
path: root/MobiReader
diff options
context:
space:
mode:
Diffstat (limited to 'MobiReader')
-rw-r--r--MobiReader/hello.dbbin0 -> 2048 bytes
-rw-r--r--MobiReader/nbproject/project.properties6
-rw-r--r--MobiReader/sample.dbbin3072 -> 5120 bytes
-rw-r--r--MobiReader/src/mobireader/Author.java27
-rw-r--r--MobiReader/src/mobireader/SqliteDatabaseHandler.java236
-rw-r--r--MobiReader/test/tests/SqlConnectionJUnitTest.java62
6 files changed, 292 insertions, 39 deletions
diff --git a/MobiReader/hello.db b/MobiReader/hello.db
new file mode 100644
index 0000000..7b739e4
--- /dev/null
+++ b/MobiReader/hello.db
Binary files differ
diff --git a/MobiReader/nbproject/project.properties b/MobiReader/nbproject/project.properties
index 0cfd8f9..fc409f0 100644
--- a/MobiReader/nbproject/project.properties
+++ b/MobiReader/nbproject/project.properties
@@ -40,6 +40,7 @@ file.reference.commons-io-1.2.jar=/home/att/studia/semestr4/Java/resources/preon
file.reference.commons-io-1.3.1.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/commons-io-1.3.1.jar
file.reference.commons-logging-1.1.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/commons-logging-1.1.jar
file.reference.guava-14.0.1.jar=/home/att/studia/semestr4/Java/compression/guava-14.0.1.jar
+file.reference.joda-time-2.2.jar=/home/att/studia/semestr4/Java/resources/joda-time-2.2/joda-time-2.2.jar
file.reference.limbo-1.1-20081112.085229-2.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/limbo-1.1-20081112.085229-2.jar
file.reference.log4j-1.2.12.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/log4j-1.2.12.jar
file.reference.logkit-1.0.1.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/logkit-1.0.1.jar
@@ -48,6 +49,7 @@ file.reference.preon-binding-1.0-SNAPSHOT.jar=/home/att/studia/semestr4/Java/res
file.reference.preon-bitbuffer-1.0-SNAPSHOT.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/preon-bitbuffer-1.0-SNAPSHOT.jar
file.reference.preon-bitbuffer-1.0-SNAPSHOT.jar-1=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/preon-bitbuffer-1.0-SNAPSHOT.jar
file.reference.servlet-api-2.3.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/servlet-api-2.3.jar
+file.reference.sqlite-jdbc-3.7.2.jar=/home/att/studia/semestr4/Java/resources/sqlite-jdbc-3.7.2.jar
file.reference.stax-api-1.0.1.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/stax-api-1.0.1.jar
file.reference.stringtemplate-3.0.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/stringtemplate-3.0.jar
file.reference.wstx-asl-3.2.1.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/wstx-asl-3.2.1.jar
@@ -80,7 +82,9 @@ javac.classpath=\
${file.reference.servlet-api-2.3.jar}:\
${file.reference.stax-api-1.0.1.jar}:\
${file.reference.stringtemplate-3.0.jar}:\
- ${file.reference.wstx-asl-3.2.1.jar}
+ ${file.reference.wstx-asl-3.2.1.jar}:\
+ ${file.reference.joda-time-2.2.jar}:\
+ ${file.reference.sqlite-jdbc-3.7.2.jar}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
diff --git a/MobiReader/sample.db b/MobiReader/sample.db
index 0effe82..de744ad 100644
--- a/MobiReader/sample.db
+++ b/MobiReader/sample.db
Binary files differ
diff --git a/MobiReader/src/mobireader/Author.java b/MobiReader/src/mobireader/Author.java
index 0c78792..a7f5efc 100644
--- a/MobiReader/src/mobireader/Author.java
+++ b/MobiReader/src/mobireader/Author.java
@@ -22,12 +22,30 @@ public class Author {
return this.name;
}
- public Author(String name, String titleOfBook)
+ public Author(String name, String additionalInfo)
{
- writtenTitles.add(titleOfBook);
+ this.additionalInfo = additionalInfo;
this.name = name;
}
+ public Author(String name)
+ {
+ this.name = name;
+ }
+
+ public void addTitle(String title)
+ {
+ writtenTitles.add(title);
+ }
+
+ public void addTitles(ArrayList<String> titles)
+ {
+ for(String title : titles)
+ {
+ writtenTitles.add(title);
+ }
+ }
+
public Author(String name, ArrayList<String> titles)
{
writtenTitles = titles;
@@ -39,6 +57,11 @@ public class Author {
this.additionalInfo = info;
}
+ public String getAdditionalInfo()
+ {
+ return this.additionalInfo;
+ }
+
public String provideAllInfo()
{
String separator = "\n";
diff --git a/MobiReader/src/mobireader/SqliteDatabaseHandler.java b/MobiReader/src/mobireader/SqliteDatabaseHandler.java
index 82fbda4..bc5aa90 100644
--- a/MobiReader/src/mobireader/SqliteDatabaseHandler.java
+++ b/MobiReader/src/mobireader/SqliteDatabaseHandler.java
@@ -17,32 +17,31 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
-public class SqliteDatabaseHandler
+class StatementBuilder
{
- static String DATABASE_FILE = "sample.db";
- String driver;
-
- public SqliteDatabaseHandler(String driver)
+ public PreparedStatement prepareFindingAuthorByNameStatement(Connection connection) throws SQLException
{
- this.driver = driver;
+ PreparedStatement statement = connection.prepareStatement(
+ "select * from authors where name=?");
+ statement.setQueryTimeout(30); // set timeout to 30 sec.
+ return statement;
}
- public SqliteDatabaseHandler()
+ public PreparedStatement prepareAddingAuthorStatement(Connection connection)
+ throws SQLException, ClassNotFoundException
{
- this.driver = "org.sqlite.JDBC";
+ PreparedStatement statement = connection.prepareStatement(
+ "insert or replace into authors(name, additional_info) values( ?, ?)");
+ statement.setQueryTimeout(30); // set timeout to 30 sec.
+ return statement;
}
- public Connection prepareConnection() throws ClassNotFoundException, SQLException
- {
- Class.forName(this.driver);
- return DriverManager.getConnection("jdbc:sqlite:" + DATABASE_FILE);
- }
public PreparedStatement prepareAddingBookStatement(Connection connection)
throws SQLException, ClassNotFoundException
{
PreparedStatement statement = connection.prepareStatement(
- "insert into books values($next_id, ?, ?, ?)");
+ "insert or replace into books(title, author_name, path_to_content) values( ?, ?, ?)");
statement.setQueryTimeout(30); // set timeout to 30 sec.
return statement;
}
@@ -56,6 +55,42 @@ public class SqliteDatabaseHandler
return statement;
}
+ public PreparedStatement prepareFindingBookByAuthorStatement(Connection connection)
+ throws SQLException, ClassNotFoundException
+ {
+ PreparedStatement statement = connection.prepareStatement(
+ "select * from books where author_name=?");
+ statement.setQueryTimeout(30); // set timeout to 30 sec.
+ return statement;
+ }
+
+}
+
+public class SqliteDatabaseHandler
+{
+ static String DATABASE_FILE = "sample.db";
+ String driver;
+ StatementBuilder statBld = new StatementBuilder();
+ public SqliteDatabaseHandler(String driver)
+ {
+ this.driver = driver;
+ }
+
+ public SqliteDatabaseHandler()
+ {
+ this.driver = "org.sqlite.JDBC";
+ }
+
+ public Connection prepareConnection() throws ClassNotFoundException, SQLException
+ {
+ Class.forName(this.driver);
+ return DriverManager.getConnection("jdbc:sqlite:" + DATABASE_FILE);
+ }
+
+
+
+
+
public void addBooks(ArrayList<Book> books) throws ClassNotFoundException
{
for(Book book : books)
@@ -71,14 +106,16 @@ public class SqliteDatabaseHandler
Connection connection = prepareConnection();
try
{
- //TODO
- //Check if author is in db, if he is, check his id
- int authorId = 1;
+ Author author = this.findAuthor(book.getAuthor().getName());
+ if(author == null)
+ {
+ this.addAuthor(book.getAuthor());
+ }
PreparedStatement stat;
- stat= this.prepareAddingBookStatement(connection);
- stat.setString(2, book.getTitle());
- stat.setInt(3, authorId);
- stat.setString(4, book.getPathToContent());
+ stat= statBld.prepareAddingBookStatement(connection);
+ stat.setString(1, book.getTitle());
+ stat.setString(2, book.getAuthor().getName());
+ stat.setString(3, book.getPathToContent());
stat.executeUpdate();
}
catch(SQLException e) { System.err.println(e.getMessage()); }
@@ -107,14 +144,10 @@ public class SqliteDatabaseHandler
try
{
PreparedStatement stat;
- stat= this.prepareFindingBookStatement(connection);
-
+ stat= statBld.prepareFindingBookStatement(connection);
stat.setString(1, title);
-
stat.executeQuery();
-
ResultSet rs = stat.executeQuery();
- int authorId = rs.getInt("author_id");
String path = rs.getString("path_to_content");
Author author = new Author("Gepetto", title);
return new Book(title, author, path);
@@ -137,13 +170,148 @@ public class SqliteDatabaseHandler
}
return null;
}
-
- public Book getBook(Integer id)
+
+ public ArrayList<Book> findBooksByAuthor(Author author) throws ClassNotFoundException
{
- return Book.exemplaryBook();
+ try
+ {
+ Connection connection = prepareConnection();
+ try
+ {
+ PreparedStatement stat;
+ stat = statBld.prepareFindingBookByAuthorStatement(connection);
+ stat.setString(1, author.getName());
+ ResultSet rs = stat.executeQuery();
+ ArrayList<Book> books = new ArrayList<>();
+
+ while(rs.next())
+ {
+ String path = rs.getString("path_to_content");
+ String title = rs.getString("title");
+ books.add(new Book(title, author, path));
+ }
+ return books;
+ }
+ catch(SQLException e) { System.err.println(e.getMessage()); }
+ finally
+ {
+ try
+ {
+ if(connection != null)
+ connection.close();
+ }
+ catch(SQLException e) { System.err.println(e.getMessage()); }
+ }
+ }
+ catch (SQLException e)
+ {
+ System.err.println("Problem occured during making connection to db");
+ System.err.println(e.getMessage());
+ }
+ return null;
}
-
-
+
+ public void addAuthor(Author author) throws ClassNotFoundException
+ {
+ try
+ {
+ Connection connection = prepareConnection();
+ try
+ {
+ PreparedStatement stat;
+ stat= statBld.prepareAddingAuthorStatement(connection);
+ stat.setString(1, author.getName());
+ stat.setString(2, author.getAdditionalInfo());
+ stat.executeUpdate();
+ }
+ catch(SQLException e) { System.err.println(e.getMessage()); }
+ finally
+ {
+ try
+ {
+ if(connection != null)
+ connection.close();
+ }
+ catch(SQLException e) { System.err.println(e.getMessage()); }
+ }
+ }
+ catch (SQLException e)
+ {
+ System.err.println("Problem occured during making connection to db");
+ System.err.println(e.getMessage());
+ }
+ }
+
+
+
+ public Author findAuthor(String name) throws ClassNotFoundException
+ {
+ try
+ {
+ Connection connection = prepareConnection();
+ try
+ {
+ PreparedStatement stat;
+ stat= statBld.prepareFindingAuthorByNameStatement(connection);
+ stat.setString(1, name);
+ stat.executeQuery();
+ ResultSet rs = stat.executeQuery();
+ String additional_info = rs.getString("additional_info");
+ return new Author(name, additional_info);
+ }
+ catch(SQLException e) { System.err.println(e.getMessage()); }
+ finally
+ {
+ try
+ {
+ if(connection != null)
+ connection.close();
+ }
+ catch(SQLException e) { System.err.println(e.getMessage()); }
+ }
+ }
+ catch (SQLException e)
+ {
+ System.err.println("Problem occured during making connection to db");
+ System.err.println(e.getMessage());
+ }
+ return null;
+ }
+
+ public void createTablesInDB() throws SQLException, ClassNotFoundException
+ {
+ try
+ {
+ Connection connection = prepareConnection();
+ try
+ {
+ Statement stat = connection.createStatement();
+ stat.executeUpdate("drop table if exists books");
+ stat.executeUpdate("drop table if exists authors");
+ stat.executeUpdate("create table books (title string, " +
+ "author_name string, path_to_content string)");
+ stat.executeUpdate("create table authors (name string unique, " +
+ "additional_info string)");
+
+ }
+ catch(SQLException e) { System.err.println(e.getMessage()); }
+ finally
+ {
+ try
+ {
+ if(connection != null)
+ connection.close();
+ }
+ catch(SQLException e) { System.err.println(e.getMessage()); }
+ }
+ }
+ catch (SQLException e)
+ {
+ System.err.println("Problem occured during making connection to db");
+ System.err.println(e.getMessage());
+ }
+ }
+
public static void main () throws Exception
{// load the sqlite-JDBC driver using the current class loader
Class.forName("org.sqlite.JDBC");
@@ -155,10 +323,14 @@ public class SqliteDatabaseHandler
connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.
-
+
statement.executeUpdate("drop table if exists books");
+ statement.executeUpdate("drop table if exists authors");
statement.executeUpdate("create table books (id integer, title string, " +
"author_id integer, path_to_content string)");
+ statement.executeUpdate("create table authors (id integer, name string, " +
+ "additional_info string)");
+
statement.executeUpdate("insert into books values(1, 'leo', 1, '/somepath/leo.txt')");
statement.executeUpdate("insert into books values(2, 'yui', 1, '/somepath/yui.txt')");
ResultSet rs = statement.executeQuery("select * from books");
diff --git a/MobiReader/test/tests/SqlConnectionJUnitTest.java b/MobiReader/test/tests/SqlConnectionJUnitTest.java
index 909ba12..4176cc8 100644
--- a/MobiReader/test/tests/SqlConnectionJUnitTest.java
+++ b/MobiReader/test/tests/SqlConnectionJUnitTest.java
@@ -4,6 +4,7 @@
*/
package tests;
+import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import mobireader.Author;
@@ -21,6 +22,7 @@ import static org.junit.Assert.*;
* @author att
*/
public class SqlConnectionJUnitTest {
+ SqliteDatabaseHandler handler = new SqliteDatabaseHandler();
public SqlConnectionJUnitTest() {
}
@@ -34,7 +36,8 @@ public class SqlConnectionJUnitTest {
}
@Before
- public void setUp() {
+ public void setUp() throws SQLException, ClassNotFoundException {
+ handler.createTablesInDB();
}
@After
@@ -52,8 +55,6 @@ public class SqlConnectionJUnitTest {
@Test
public void testAddingBookToDb() throws Exception
{
- SqliteDatabaseHandler handler = new SqliteDatabaseHandler();
-
String title = "Title";
String authorName = "Gepetto";
Author gepetto = new Author(authorName, title);
@@ -66,9 +67,62 @@ public class SqlConnectionJUnitTest {
assertEquals(path, bookFromDB.getPathToContent());
}
+ @Test
+ public void testAddingFewBooksOfSameAuthorToDb() throws ClassNotFoundException
+ {
+ String title1 = "Title1";
+ String title2 = "Title2";
+ String title3 = "Title3";
+ String authorName = "Gepetto";
+ Author gepetto = new Author(authorName);
+ String path = "/somepath/book.txt";
+ ArrayList<Book> books = new ArrayList<>();
+ books.add(new Book(title1, gepetto, path));
+ books.add(new Book(title2, gepetto, path));
+ books.add(new Book(title3, gepetto, path));
+ handler.addBooks(books);
+ ArrayList<Book> booksFromDB = handler.findBooksByAuthor(gepetto);
+ assertEquals(books.size(), booksFromDB.size());
+ for(int i = 0; i < books.size(); i++ )
+ {
+ assertEquals(books.get(i).getTitle(), booksFromDB.get(i).getTitle());
+ assertEquals(books.get(i).getAuthor().getName(), booksFromDB.get(i).getAuthor().getName());
+ }
+
+ }
+
+ @Test
+ public void testAddingAuthorToDb() throws Exception
+ {
+ String authorName = "Geronimo";
+ String additionalInfo = "Likes wood";
+ Author authorToBeAdded = new Author(authorName, additionalInfo);
+ handler.addAuthor(authorToBeAdded);
+ Author authorFromDB = handler.findAuthor(authorName);
+ assertEquals(authorName, authorFromDB.getName());
+ assertEquals(additionalInfo, authorFromDB.getAdditionalInfo());
+ }
+
+ @Test
+ public void testAddingTheSameAuthorToDbFewTimes() throws Exception
+ {
+ String authorName = "Geronimo";
+ String additionalInfo = "Likes wood";
+ Author authorToBeAdded = new Author(authorName, additionalInfo);
+ handler.addAuthor(authorToBeAdded);
+ String newerInfo = authorToBeAdded.getAdditionalInfo() +
+ ", and trees, and art.";
+ authorToBeAdded.addAdditionalInfo(newerInfo);
+ handler.addAuthor(authorToBeAdded);
+ Author anotherAuthor = new Author("Alfredo", "Enjoy waterfalls");
+ handler.addAuthor(anotherAuthor);
+ Author authorFromDB = handler.findAuthor(authorName);
+ assertEquals(authorName, authorFromDB.getName());
+ assertEquals(newerInfo, authorFromDB.getAdditionalInfo());
+ }
+
public void testAddingFewBooksToDB() throws Exception
{
- SqliteDatabaseHandler handler = new SqliteDatabaseHandler();
ArrayList<Book> books = Book.createSomeExamples();
handler.addBooks(books);
for(Book book : books)