summaryrefslogtreecommitdiff
path: root/src/main/Main.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/Main.java')
-rw-r--r--src/main/Main.java111
1 files changed, 75 insertions, 36 deletions
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."
};
}