...
 
Commits (2)
......@@ -10,6 +10,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.List;
import org.codecoop.iceflower.gutenbergutils.database.entity.Book;
......@@ -77,7 +78,7 @@ public class InsertOperation extends AbstractDbOperation {
if (personId == null) {
try (PreparedStatement ps = getConnection().prepareStatement(SqlCommands.INSERT_PERSON);) {
ps.setString(1, author.getName());
ps.setInt(2, author.getDeathYear());
setIntOnPreparedStatement(2, author.getDeathYear(), ps);
ps.execute();
}
personId = fetchId(author);
......@@ -93,9 +94,11 @@ public class InsertOperation extends AbstractDbOperation {
}
private Long fetchId(Person author) throws SQLException {
try (PreparedStatement ps = getConnection().prepareStatement(SqlCommands.SELECT_PERSON_ID);) {
String query = (author.getDeathYear() != null) ? SqlCommands.SELECT_PERSON_ID
: SqlCommands.SELECT_PERSON_ID2;
try (PreparedStatement ps = getConnection().prepareStatement(query);) {
ps.setString(1, author.getName());
ps.setInt(2, author.getDeathYear());
setIntOnPreparedStatement(2, author.getDeathYear(), ps);
try (ResultSet rs = ps.executeQuery();) {
while (rs.next()) {
return rs.getLong(1);
......@@ -105,6 +108,15 @@ public class InsertOperation extends AbstractDbOperation {
return null;
}
private static void setIntOnPreparedStatement(int index, Integer value, PreparedStatement ps) throws SQLException {
if (value != null) {
ps.setInt(index, value);
} else {
// special case: null value
ps.setNull(index, Types.INTEGER);
}
}
private void endBatch() throws SQLException {
getPreparedStatement().executeBatch();
getPreparedStatement().clearBatch();
......
......@@ -70,6 +70,7 @@ public interface SqlCommands {
// selects
String SELECT_PERSON_ID="SELECT id FROM person WHERE name = ? AND death_year = ? ;";
String SELECT_PERSON_ID2 = "SELECT id FROM person WHERE name = ? AND death_year IS ? ;";
String SELECT_BOOKS_WHERE_LANGUAGE =
"SELECT " +
......@@ -92,7 +93,7 @@ public interface SqlCommands {
"WHERE "+
"book.id = book_author.book_id "+
"AND book_author.person_id = author.id "+
"AND (author.death_year = -1 "+
"AND (author.death_year IS NULL "+
"OR (YEAR(CURRENT_DATE()) - author.death_year) <= 70 )"+
") "+
// no translator as copyright holder
......@@ -103,7 +104,7 @@ public interface SqlCommands {
"WHERE "+
"book.id = book_translator.book_id "+
"AND book_translator.person_id = translator.id "+
"AND (translator.death_year = -1 "+
"AND (translator.death_year IS NULL "+
"OR (YEAR(CURRENT_DATE()) - translator.death_year) <= 70)"+
") "+
"ORDER BY "+
......@@ -131,7 +132,7 @@ public interface SqlCommands {
"WHERE "+
"book.id = book_author.book_id "+
"AND book_author.person_id = author.id "+
"AND (author.death_year = -1 "+
"AND (author.death_year IS NULL "+
"OR (YEAR(CURRENT_DATE()) - author.death_year) <= 70)"+
") "+
// no translator as copyright holder
......@@ -142,7 +143,7 @@ public interface SqlCommands {
"WHERE "+
"book.id = book_translator.book_id "+
"AND book_translator.person_id = translator.id "+
"AND (translator.death_year = -1 "+
"AND (translator.death_year IS NULL "+
"OR (YEAR(CURRENT_DATE()) - translator.death_year) <= 70)"+
") "+
"ORDER BY "+
......
......@@ -8,7 +8,7 @@ package org.codecoop.iceflower.gutenbergutils.database.entity;
public class Person {
private String name = "";
private int deathYear = -1;
private Integer deathYear = null;
public String getName() {
return name;
......@@ -18,11 +18,11 @@ public class Person {
this.name = name;
}
public int getDeathYear() {
public Integer getDeathYear() {
return deathYear;
}
public void setDeathYear(int deathYear) {
public void setDeathYear(Integer deathYear) {
this.deathYear = deathYear;
}
......@@ -30,7 +30,7 @@ public class Person {
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(name);
builder.append("(");
builder.append(" (");
builder.append("died ");
builder.append(deathYear);
builder.append(")");
......
......@@ -26,7 +26,7 @@ public class RdfParser {
private Pattern personNamePattern = Pattern.compile("\\<pgterms\\:name\\>(.*)\\<\\/pgterms\\:name\\>");
private Pattern deathYearPattern = Pattern
.compile("\\<pgterms\\:deathdate .*\\>(\\d+)\\<\\/pgterms\\:deathdate\\>");
.compile("\\<pgterms\\:deathdate .*\\>(\\-?\\d+)\\<\\/pgterms\\:deathdate\\>");
private Pattern translatorPattern = Pattern.compile("\\<marcrel\\:trl\\>(.+?)\\<\\/marcrel\\:trl\\>",
Pattern.DOTALL);
......@@ -98,7 +98,6 @@ public class RdfParser {
Matcher creatorMatcher = personPattern.matcher(rdfString);
while (creatorMatcher.find()) {
String creator = creatorMatcher.group(1);
// extract author name
Matcher personNameMatcher = personNamePattern.matcher(creator);
......@@ -108,7 +107,7 @@ public class RdfParser {
// extract death year of the author
Matcher deathYearMatcher = deathYearPattern.matcher(creator);
int deathYear = -1;
Integer deathYear = null;
if (deathYearMatcher.find()) {
deathYear = Integer.parseInt(deathYearMatcher.group(1));
}
......
......@@ -24,7 +24,7 @@ class RdfParserTest {
assertEquals("Bilder aus einem Landgängerdorfe", ebook.getSubtitle());
assertEquals("de", ebook.getLanguage());
assertEquals("Schupp, Ottokar", ebook.getAuthors().get(0).getName());
assertEquals(-1, ebook.getAuthors().get(0).getDeathYear());
assertEquals(null, ebook.getAuthors().get(0).getDeathYear());
}
@Test
......@@ -40,7 +40,7 @@ class RdfParserTest {
+ "United Brethren, to convert them to Christianity", ebook.getSubtitle());
assertEquals("en", ebook.getLanguage());
assertEquals("Haensel, Johann Gottfried", ebook.getAuthors().get(0).getName());
assertEquals(-1, ebook.getAuthors().get(0).getDeathYear());
assertEquals(null, ebook.getAuthors().get(0).getDeathYear());
}
@Test
......@@ -88,4 +88,11 @@ class RdfParserTest {
assertEquals(1937, ebook.getTranslators().get(0).getDeathYear());
}
@Test
void testNegativeDeathYear() {
RdfParser rdfParser = new RdfParser();
Book ebook = rdfParser.parse(TestConstants.RDF_WITH_NEGATIVE_DEATH_YEAR);
assertEquals(-348, ebook.getAuthors().get(0).getDeathYear());
}
}
GutenbergUtils is a command-line tool which extracts book information from [Project Gutenberg](http://www.gutenberg.org/) and generates static HTML pages which list the available books in a clear way. The HTML pages link to a mirror where you can download the books.
- License: DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
- Source language: Java (tested on Java 8 and 11 JVM)
- Example: [Bücherbucht.de](http://www.bücherbucht.de)
- Tested on Java 8 and 11 JVM
## Command-line interface
**--parse-rdf RDF_DIR DATABASE_FILE**
......