summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/DataFiller.java53
-rw-r--r--src/main/Main.java111
2 files changed, 98 insertions, 66 deletions
diff --git a/src/main/DataFiller.java b/src/main/DataFiller.java
index a65a032..10ed774 100644
--- a/src/main/DataFiller.java
+++ b/src/main/DataFiller.java
@@ -1,9 +1,9 @@
package main;
import data.Tweet;
-import database.DBConnection;
+import database.NamedPreparedStatement;
import database.QueryUtils;
-import java.sql.PreparedStatement;
+import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@@ -20,58 +20,52 @@ public class DataFiller {
/**
* The main database connection to fill.
*/
- private final DBConnection m_connection;
+ private final Connection m_connection;
/**
* A single insert tweet that can be used.
*/
- private PreparedStatement m_insertTweet;
+ private final NamedPreparedStatement m_insertTweet;
/**
* A single insert profiles that can be used.
*/
- private PreparedStatement m_insertProfile;
- /**
- * A single insert ispostedby that can be used.
- */
- private PreparedStatement m_insertPosted;
+ private final NamedPreparedStatement m_insertProfile;
/**
* A single insert brand that can be used.
*/
- private PreparedStatement m_insertBrand;
+ private final NamedPreparedStatement m_insertBrand;
/**
* A single insert hashtag that can be used.
*/
- private PreparedStatement m_insertHash;
+ private final NamedPreparedStatement m_insertHash;
/**
* A single insert url that can be used.
*/
- private PreparedStatement m_insertUrl;
+ private final NamedPreparedStatement m_insertUrl;
/**
* A single insert url that can be used.
*/
- private PreparedStatement m_insertMentions;
+ private final NamedPreparedStatement m_insertMentions;
/**
* Create the datafiller object.
*
* @param connection The database connection to use.
*/
- public DataFiller(DBConnection connection) {
+ public DataFiller(Connection connection) {
try {
m_connection = connection;
- m_insertTweet = m_connection.create(QueryUtils.insertTweet());
- m_insertProfile = m_connection.create(QueryUtils.insertProfile());
- m_insertPosted = m_connection.create(QueryUtils.insertPosted());
- m_insertBrand = m_connection.create(QueryUtils.insertBrand());
- m_insertHash = m_connection.create(QueryUtils.insertHash());
- m_insertUrl = m_connection.create(QueryUtils.insertUrl());
- m_insertMentions = m_connection.create(QueryUtils.insertMentions());
-
+ m_insertTweet = new NamedPreparedStatement(m_connection, QueryUtils.insertTweet);
+ m_insertProfile = new NamedPreparedStatement(m_connection, QueryUtils.insertProfile);
+ m_insertBrand = new NamedPreparedStatement(m_connection, QueryUtils.insertBrand);
+ m_insertHash = new NamedPreparedStatement(m_connection, QueryUtils.insertHash);
+ m_insertUrl = new NamedPreparedStatement(m_connection, QueryUtils.insertUrl);
+ m_insertMentions = new NamedPreparedStatement(m_connection, QueryUtils.insertMentions);
} catch (SQLException ex) {
throw new RuntimeException(ex.getMessage());
}
@@ -81,25 +75,24 @@ public class DataFiller {
try {
for (Tweet.Hashtag hashtag : tweet.entities.hashtags) {
QueryUtils.setInsertHashParams(m_insertHash, tweet.id, hashtag.text);
- m_insertHash.executeUpdate();
+ m_insertHash.getStmt().executeUpdate();
}
for (Tweet.Url url : tweet.entities.urls) {
QueryUtils.setInsertHashParams(m_insertUrl, tweet.id, url.expanded_url);
- m_insertUrl.executeUpdate();
+ m_insertUrl.getStmt().executeUpdate();
}
for (Tweet.Mention mention : tweet.entities.user_mentions) {
QueryUtils.setInsertMentionsParams(m_insertMentions, tweet.id, mention.id);
- m_insertMentions.executeUpdate();
+ m_insertMentions.getStmt().executeUpdate();
}
- QueryUtils.setInsertParams(m_insertTweet, m_insertProfile, m_insertPosted, tweet);
- m_insertTweet.executeUpdate();
- m_insertProfile.executeUpdate();
- m_insertPosted.executeUpdate();
+ QueryUtils.setInsertParams(m_insertTweet, m_insertProfile, tweet);
+ m_insertTweet.getStmt().executeUpdate();
+ m_insertProfile.getStmt().executeUpdate();
List<String> brands = getBrands(tweet);
for (String brand : brands) {
QueryUtils.setInsertBrandParams(m_insertBrand, tweet.id, brand);
- m_insertBrand.executeUpdate();
+ m_insertBrand.getStmt().executeUpdate();
}
} catch (SQLException ex) {
Logger.getLogger(DataFiller.class.getName()).log(Level.SEVERE, null, ex);
diff --git a/src/main/Main.java b/src/main/Main.java
index 67032bf..b5df23f 100644
--- a/src/main/Main.java
+++ b/src/main/Main.java
@@ -1,13 +1,16 @@
package main;
-import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import data.Tweet;
-import database.DBConnection;
+import database.ConnectionBuilder;
import io.FileTweetReader;
import io.ITweetReader;
import io.TweetReader;
import java.io.IOException;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
/**
* The main class.
@@ -20,30 +23,60 @@ public class Main {
* @param args the global arguments to pass to the program.
*/
public static void main(String[] args) {
+ Main main;
try {
- Main main = new Main(args);
+ main = new Main(args);
} catch (IllegalArgumentException ex) {
System.err.println(ex.getMessage());
System.exit(1);
+ return;
}
+ main.run();
}
- private String m_hostaddress;
-
private String m_filename;
+ private final ConnectionBuilder cb;
+ /**
+ * Whether the database should be contacted or not.
+ */
+ private boolean skipDb;
public Main(String[] args) {
+ // default connection properties
+ cb = new ConnectionBuilder()
+ .setServerName("localhost")
+ .setUsername("postgres")
+ .setPassword("2IOC02")
+ .setDbName("Twitter");
+ skipDb = false;
+
/* parse the global options. */
parseGlobalOptions(args);
+ }
- if (m_hostaddress == null) {
- throw new IllegalArgumentException("Missing --dbhost to specify the hostaddress.");
+ private void printTweets(ITweetReader reader) throws IOException {
+ Tweet tweet;
+ long tweetNo = 1;
+ while ((tweet = reader.getTweet()) != null) {
+ System.out.println("/*" + tweetNo++ + "*/ " + tweet);
}
+ }
- DBConnection connection = new DBConnection(m_hostaddress, "5432", "Twitter", "postgres", "2IOC02");
- /* create the object that fills the database */
- DataFiller filler = new DataFiller(connection);
+ private void tweetsToDb(ITweetReader reader) throws IOException {
+ Tweet tweet;
+ try (Connection connection = cb.create()) {
+ /* create the object that fills the database */
+ DataFiller filler = new DataFiller(connection);
+ while ((tweet = reader.getTweet()) != null) {
+ filler.processTweet(tweet);
+ }
+ } catch (SQLException ex) {
+ Logger.getLogger(Main.class.getName()).log(Level.SEVERE,
+ "DB error", ex);
+ }
+ }
+ public void run() {
ITweetReader reader = null;
try {
if (m_filename == null) {
@@ -51,10 +84,10 @@ public class Main {
} else {
reader = new FileTweetReader(m_filename);
}
-
- Tweet tweet;
- while ((tweet = reader.getTweet()) != null) {
- filler.processTweet(tweet);
+ if (skipDb) {
+ printTweets(reader);
+ } else {
+ tweetsToDb(reader);
}
} catch (JsonSyntaxException ex) {
System.err.println("Got an invalid tweet: " + ex);
@@ -65,42 +98,43 @@ public class Main {
reader.close();
}
}
-
- connection.close();
-
- System.out.print("exit succesfull.");
}
- private void parseGlobalOptions(String[] args) {
+ private void parseGlobalOptions(String[] args)
+ throws IllegalArgumentException {
/* parse global options */
for (int i = 0; i < args.length; i++) {
if ("--help".equals(args[i])) {
printHelp();
+ System.exit(0);
} else if ("--dbhost".equals(args[i])) {
- m_hostaddress = getParam(args, ++i);
+ cb.setServerName(getArg(args, ++i, "--dbhost"));
+ } else if ("--dbuser".equals(args[i])) {
+ cb.setUsername(getArg(args, ++i, "--dbuser"));
+ } else if ("--dbpass".equals(args[i])) {
+ cb.setPassword(getArg(args, ++i, "--dbpass"));
+ } else if ("--dbname".equals(args[i])) {
+ cb.setDbName(getArg(args, ++i, "--dbname"));
+ } else if ("--skipdb".equals(args[i])) {
+ skipDb = true;
} else if (args[i].startsWith("-")) {
throw new IllegalArgumentException("Invalid option: " + args[i]);
} else {
/* This should be the filename */
- m_filename = getParam(args, i);
+ m_filename = args[i];
}
}
-
- if (args.length == 0) {
- throw new IllegalArgumentException("No parameters specified, see --help for usage info.");
- }
}
/**
* Read an extra option for a command.
*/
- private String getParam(String[] args, Integer index) {
- if (index + 1 <= args.length) {
- index++;
- return args[index - 1];
- } else {
- throw new IllegalArgumentException("An extra option was missing.");
+ private String getArg(String[] params, int index, String name) {
+ if (index >= params.length) {
+ System.err.println("Missing argument for parameter " + name);
+ System.exit(1);
}
+ return params[index];
}
/**
@@ -113,11 +147,16 @@ public class Main {
}
private final static String[] HELP = {
+ "Usage: java -jar DataFiller.jar [options] [tweets-file]",
+ "",
"Global options:",
- " --help Print this help text.",
- " --dbhost <ip> Specify the database host ipaddress.",
- " --file <tweet-filename> Specify the tweet file to read from.",
- " ",
- " If no --file was specified, program reads from standard input."
+ " --help Print this help text.",
+ " --dbhost HOST Database host (defaults to 'localhost')",
+ " --dbuser USER Database username (defaults to 'postgres')",
+ " --dbpass PASS Database password (defaults to '2IOC02')",
+ " --dbname NAME Database name (defaults to 'Twitter')",
+ " --skipdb Do not contact the database at all, just print data.",
+ "",
+ "If no tweets file is given, data will be read from standard input."
};
}