summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaurice Laveaux <m.laveaux@student.tue.nl>2014-05-07 12:33:50 +0200
committerMaurice Laveaux <m.laveaux@student.tue.nl>2014-05-07 12:33:50 +0200
commita3303f1fe119f963edec13c48286cc5d8cdcc5eb (patch)
treec2fef6ff8c7662ebc868f6a7fbd81353222e9822
parent2f203acc7f28afce9e704cf27fd59d11e28ec2d5 (diff)
downloadDatafiller-a3303f1fe119f963edec13c48286cc5d8cdcc5eb.tar.gz
Two different inputs (file and stdin).
* DataFiller fills the database with given input.
-rw-r--r--src/io/DataReader.java32
-rw-r--r--src/io/InputReader.java51
-rw-r--r--src/main/DataFiller.java43
-rw-r--r--src/main/ResultListener.java16
4 files changed, 142 insertions, 0 deletions
diff --git a/src/io/DataReader.java b/src/io/DataReader.java
new file mode 100644
index 0000000..67dca2b
--- /dev/null
+++ b/src/io/DataReader.java
@@ -0,0 +1,32 @@
+package io;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import main.ResultListener;
+
+/**
+ * Create a data reader that reads the input file.
+ *
+ * @author Maurice Laveaux
+ */
+public class DataReader {
+
+ /* The listener that receives the data input. */
+ private final ResultListener m_listener;
+
+ /* The input stream for the given file. */
+ private final FileInputStream m_input;
+
+ /**
+ * @param filename The filename to read from.
+ * @param listener The listener of the read tweets.
+ */
+ public DataReader(final String filename, final ResultListener listener) {
+ try {
+ m_listener = listener;
+ m_input = new FileInputStream(filename);
+ } catch (FileNotFoundException ex) {
+ throw new IllegalArgumentException("the given filename \"" + filename + "\" doesn't exist.");
+ }
+ }
+}
diff --git a/src/io/InputReader.java b/src/io/InputReader.java
new file mode 100644
index 0000000..ef22c97
--- /dev/null
+++ b/src/io/InputReader.java
@@ -0,0 +1,51 @@
+package io;
+
+import java.util.Scanner;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import main.ResultListener;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+/**
+ * Read from standard input and inserts these tweets into the database.
+ *
+ * @author Maurice Laveaux
+ */
+public class InputReader {
+
+ /* The listener for standard input tweets. */
+ private final ResultListener m_listener;
+
+ /**
+ * @param listener The given listener for the input.
+ */
+ public InputReader(final ResultListener listener) {
+ m_listener = listener;
+ }
+
+ /* Create a reader for the standard input, read each line and tweetReceived
+ * listener when a valid JSONObject is generated.
+ */
+ public void startLoop() {
+ Scanner input = new Scanner(System.in);
+
+ while (input.hasNext()) {
+ try {
+ JSONObject tweet = new JSONObject(input.nextLine());
+
+ if (tweet.has("tweet-id")) {
+ throw new JSONException("not a valid tweet JSON object");
+ }
+
+ m_listener.tweetReceived(tweet);
+ } catch (JSONException ex) {
+ getLogger().log(Level.INFO, null, ex);
+ }
+ }
+ }
+
+ private Logger getLogger() {
+ return Logger.getLogger(InputReader.class.getName());
+ }
+}
diff --git a/src/main/DataFiller.java b/src/main/DataFiller.java
new file mode 100644
index 0000000..461bd21
--- /dev/null
+++ b/src/main/DataFiller.java
@@ -0,0 +1,43 @@
+package main;
+
+import database.DBConnection;
+import database.DBQuery;
+import database.QueryUtils;
+import org.json.JSONObject;
+
+/**
+ * Process that incoming tweets and fill the database.
+ *
+ * @author Maurice Laveaux
+ */
+public class DataFiller implements ResultListener {
+
+ /**
+ * The main database connection to fill.
+ */
+ private final DBConnection m_connection;
+
+ /**
+ * A single insert tweet that can be used often.
+ */
+ private DBQuery m_insertTweet;
+
+ /**
+ * Create the datafiller object.
+ *
+ * @param connection The database connection to use.
+ */
+ public DataFiller(DBConnection connection) {
+ m_insertTweet = QueryUtils.insertTweet();
+
+ m_connection = connection;
+ m_connection.prepare(m_insertTweet);
+ }
+
+ @Override
+ public void tweetReceived(JSONObject tweet) {
+ QueryUtils.setInsertParams(m_insertTweet, tweet);
+
+ m_insertTweet.execute();
+ }
+}
diff --git a/src/main/ResultListener.java b/src/main/ResultListener.java
new file mode 100644
index 0000000..9194a63
--- /dev/null
+++ b/src/main/ResultListener.java
@@ -0,0 +1,16 @@
+package main;
+
+import org.json.JSONObject;
+
+/**
+ * This class is used to obtain resulting JSON tweets.
+ * @author Maurice Laveaux
+ */
+public interface ResultListener {
+
+ /**
+ * Notify the result listener that a tweet was generated.
+ * @param tweet A single tweet that should be processed.
+ */
+ public abstract void tweetReceived(JSONObject tweet);
+}