package main; import data.Tweet; import database.DBConnection; import database.QueryUtils; 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 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()); } } 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 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 getBrands(Tweet tweet) { ArrayList 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; } }