summaryrefslogtreecommitdiff
path: root/src/main/DataFiller.java
blob: 6e1ba9de96ebfad6fd3bddc0f7d17b630197dcea (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main;

import data.Tweet;
import database.QueryUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Process that incoming tweets and fill the database.
 *
 * @author Maurice Laveaux
 */
public class DataFiller {

    /**
     * The main database connection to fill.
     */
    private final Connection m_connection;

    /**
     * A single insert tweet that can be used.
     */
    private final PreparedStatement m_insertTweet;

    /**
     * A single insert profiles that can be used.
     */
    private final PreparedStatement m_insertProfile;
    /**
     * A single insert ispostedby that can be used.
     */
    private final PreparedStatement m_insertPosted;

    /**
     * A single insert brand that can be used.
     */
    private final PreparedStatement m_insertBrand;

    /**
     * A single insert hashtag that can be used.
     */
    private final PreparedStatement m_insertHash;

    /**
     * A single insert url that can be used.
     */
    private final PreparedStatement m_insertUrl;

    /**
     * A single insert url that can be used.
     */
    private final PreparedStatement m_insertMentions;

    /**
     * Create the datafiller object.
     *
     * @param connection The database connection to use.
     */
    public DataFiller(Connection connection) {
        try {
            m_connection = connection;
            m_insertTweet = m_connection.prepareStatement(QueryUtils.insertTweet());
            m_insertProfile = m_connection.prepareStatement(QueryUtils.insertProfile());
            m_insertPosted = m_connection.prepareStatement(QueryUtils.insertPosted());
            m_insertBrand = m_connection.prepareStatement(QueryUtils.insertBrand());
            m_insertHash = m_connection.prepareStatement(QueryUtils.insertHash());
            m_insertUrl = m_connection.prepareStatement(QueryUtils.insertUrl());
            m_insertMentions = m_connection.prepareStatement(QueryUtils.insertMentions());

        } catch (SQLException ex) {
            throw new RuntimeException(ex.getMessage());
        }
    }

    public void processTweet(Tweet tweet) {
        try {
            for (Tweet.Hashtag hashtag : tweet.entities.hashtags) {
                QueryUtils.setInsertHashParams(m_insertHash, tweet.id, hashtag.text);
                m_insertHash.executeUpdate();
            }
            for (Tweet.Url url : tweet.entities.urls) {
                QueryUtils.setInsertHashParams(m_insertUrl, tweet.id, url.expanded_url);
                m_insertUrl.executeUpdate();
            }
            for (Tweet.Mention mention : tweet.entities.user_mentions) {
                QueryUtils.setInsertMentionsParams(m_insertMentions, tweet.id, mention.id);
                m_insertMentions.executeUpdate();
            }

            QueryUtils.setInsertParams(m_insertTweet, m_insertProfile, m_insertPosted, tweet);
            m_insertTweet.executeUpdate();
            m_insertProfile.executeUpdate();
            m_insertPosted.executeUpdate();
            List<String> brands = getBrands(tweet);
            for (String brand : brands) {
                QueryUtils.setInsertBrandParams(m_insertBrand, tweet.id, brand);
                m_insertBrand.executeUpdate();
            }
        } catch (SQLException ex) {
            Logger.getLogger(DataFiller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    ArrayList<String> getBrands(Tweet tweet) {
        ArrayList<String> result = new ArrayList<>();
        String text = tweet.text.toLowerCase();
        if (text.contains("samsung") || text.contains("galaxy")) {
            result.add("Samsung");
        }
        if (text.contains("htc") || text.contains("one")) {
            result.add("HTC");
        }
        if (text.contains("apple") || text.contains("iphone")) {
            result.add("Apple");
        }
        if (text.contains("sony") || text.contains("xperia")) {
            result.add("Sony");
        }
        if (text.contains("huawei") || text.contains("ascend")) {
            result.add("Huawei");
        }
        if (text.contains("lg")) {
            result.add("LG");
        }

        // TODO: WTF IS THIS PILE OF SHIT?!
        if (result.isEmpty()) {
            result.add("geen");
            System.out.println(text);
        }
        return result;
    }
}