summaryrefslogtreecommitdiff
path: root/src/main/Main.java
blob: dec057ae7faa70d025e0f6d0729e13c407ed19cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main;

import database.DBConnection;
import io.DataReader;
import io.InputReader;
import java.util.Arrays;

/**
 * The main class.
 */
public class Main {

    /**
     * The main method of the application.
     * @param args the global arguments to pass to the program.
     */
    public static void main(String[] args) {
        try {
            Main main = new Main(args);
        } catch (IllegalArgumentException ex) {
            System.err.println(ex.getMessage());
            System.exit(1);
        }
    }
    
    private String m_hostaddress;
    
    private String m_filename;
    
    public Main(String[] args) {
        /* parse the global options. */
        parseGlobalOptions(args);
        
        if (m_hostaddress == null) {
            throw new IllegalArgumentException("Missing --ip to specify the hostaddress.");
        } 
        
        DBConnection connection = new DBConnection(m_hostaddress, "5432", "Twitter", "postgres", "2IOC02");
        /* create the object that fills the database */        
        DataFiller filler = new DataFiller(connection);
        
        if (m_filename == null) {
            InputReader reader = new InputReader(filler);
            
            reader.startLoop();
        } else {
            DataReader reader = new DataReader(m_filename, filler);
        }
        
        connection.close();
    }
    
    private void parseGlobalOptions(String[] args) {
        /* parse global options */
        for (int i = 0; i < args.length; i++) {
            if ("--help".equals(args[i])) {
                printHelp();
            } else if("--dbhost".equals(args[i])) {
                m_hostaddress = getParam(args, ++i);
            } else if (args[i].startsWith("-")) {
                throw new IllegalArgumentException("Invalid option: " + args[i]);
            } else {
                /* This should be the filename */
                m_filename = getParam(args, i);
            }
        }
    }
    
    /** 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 void printHelp() {
        for (String line : HELP) {
            System.out.println(line);
        }
    }
    
    private final static String[] HELP = {
        "Global options:",
        "   --help    Print this help text.",
        "   --ip <ip> Specify the database ip address.",
        "   --file <tweet> <profile> Specify the tweet and profile filenames ",
        "                                                                    ",
        " if no --file was specified read from standard input."
    };
}