diff options
author | S129778 <S129778@S129778.campus.tue.nl> | 2014-05-07 16:32:55 +0200 |
---|---|---|
committer | S129778 <S129778@S129778.campus.tue.nl> | 2014-05-07 16:32:55 +0200 |
commit | 29f055f6150bd0ade14471dfeb7ebd55f6730515 (patch) | |
tree | a060314565310d257531f64b0fa36482e4390147 /src/main/DataFiller.java | |
parent | a9de546d1867c4a2f130cf55c208719eb6b27f54 (diff) | |
download | Datafiller-29f055f6150bd0ade14471dfeb7ebd55f6730515.tar.gz |
data querries into database
Diffstat (limited to 'src/main/DataFiller.java')
-rw-r--r-- | src/main/DataFiller.java | 93 |
1 files changed, 92 insertions, 1 deletions
diff --git a/src/main/DataFiller.java b/src/main/DataFiller.java index 20fcf3a..c9a5811 100644 --- a/src/main/DataFiller.java +++ b/src/main/DataFiller.java @@ -4,6 +4,7 @@ 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; @@ -30,6 +31,30 @@ public class DataFiller implements ResultListener { * 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. @@ -40,6 +65,15 @@ public class DataFiller implements ResultListener { 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()); } @@ -48,11 +82,68 @@ public class DataFiller implements ResultListener { @Override public void tweetReceived(JSONObject tweet) { try { - QueryUtils.setInsertParams(m_insertTweet, tweet); + 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; + } } |