diff options
-rw-r--r-- | src/io/DataReader.java | 32 | ||||
-rw-r--r-- | src/io/InputReader.java | 51 | ||||
-rw-r--r-- | src/main/DataFiller.java | 43 | ||||
-rw-r--r-- | src/main/ResultListener.java | 16 |
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); +} |