summaryrefslogtreecommitdiff
path: root/src/main/DataFiller.java
blob: c9a5811dc926e95243de529b7f3aeb8741e474ac (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
139
140
141
142
143
144
145
146
147
148
149
package main;

import database.DBConnection;
import database.QueryUtils;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
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.
     */
    private PreparedStatement m_insertTweet;
    
    /**
     * A single insert profiles that can be used.
     */
    private PreparedStatement m_insertProfile;
    /**
     * A single insert ispostedby that can be used.
     */
    private PreparedStatement m_insertPosted;
       
    /**
     * A single insert brand that can be used.
     */
    private PreparedStatement m_insertBrand;

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

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

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

    /**
     * Create the datafiller object.
     *
     * @param connection The database connection to use.
     */
    public DataFiller(DBConnection connection) {
        try {
            m_connection  = connection;
            m_insertTweet = m_connection.create(QueryUtils.insertTweet());
            m_insertProfile = m_connection.create(QueryUtils.insertProfile());
            m_insertPosted = m_connection.create(QueryUtils.insertPosted());
            m_insertBrand = m_connection.create(QueryUtils.insertBrand());
            m_insertHash = m_connection.create(QueryUtils.insertHash());
            m_insertUrl = m_connection.create(QueryUtils.insertUrl());
            m_insertMentions = m_connection.create(QueryUtils.insertMentions());
            
            
            
        } catch (SQLException ex) {
            throw new RuntimeException(ex.getMessage());
        }
    }

    @Override
    public void tweetReceived(JSONObject tweet) {
        try {
            
            for(int i=0;i<tweet.getJSONObject("entities").getJSONArray("hashtags").length();i++){
                String text = tweet.getJSONObject("entities").getJSONArray("hashtags").getJSONObject(i).getString("text");
                QueryUtils.setInsertHashParams(m_insertHash, tweet.getLong("id"), text);
                m_insertHash.executeUpdate();
            }
            for(int i=0;i<tweet.getJSONObject("entities").getJSONArray("urls").length();i++){
                String text = tweet.getJSONObject("entities").getJSONArray("urls").getJSONObject(i).getString("expanded_url");
                QueryUtils.setInsertHashParams(m_insertUrl, tweet.getLong("id"), text);
                m_insertUrl.executeUpdate();
            }
            for(int i=0;i<tweet.getJSONObject("entities").getJSONArray("user_mentions").length();i++){
                Long id = tweet.getJSONObject("entities").getJSONArray("user_mentions").getJSONObject(i).getLong("id");
                QueryUtils.setInsertMentionsParams(m_insertMentions, tweet.getLong("id"), id);
                m_insertMentions.executeUpdate();
            }
            
            QueryUtils.setInsertParams(m_insertTweet,m_insertProfile,m_insertPosted, tweet);
            m_insertTweet.executeUpdate();
            m_insertProfile.executeUpdate();
            m_insertPosted.executeUpdate();
            ArrayList<String> brands=getBrands(tweet);
            for(String brand : brands){
                QueryUtils.setInsertBrandParams(m_insertBrand, tweet.getLong("id"), brand);
                m_insertBrand.executeUpdate();
            }
        } catch (SQLException | JSONException ex) {
            Logger.getLogger(DataFiller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    ArrayList<String> getBrands(JSONObject tweet){
            ArrayList<String> result= new ArrayList<String>();
            String text=null;
        try {
            text = tweet.getString("text");
            text = text.toLowerCase();
        } catch (JSONException ex) {
            
        }
            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");
            }
            
            if(result.isEmpty()) {
                result.add("geen");
                System.out.println(text);
            }
            return result;
        }
}