package main; import data.Tweet; import database.NamedPreparedStatement; import database.QueryUtils; import java.sql.Connection; 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 NamedPreparedStatement m_insertTweet; /** * A single insert profiles that can be used. */ private final NamedPreparedStatement m_insertProfile; /** * A single insert brand that can be used. */ private final NamedPreparedStatement m_insertBrand; /** * A single insert hashtag that can be used. */ private final NamedPreparedStatement m_insertHash; /** * A single insert url that can be used. */ private final NamedPreparedStatement m_insertUrl; /** * A single insert url that can be used. */ private final NamedPreparedStatement m_insertMentions; /** * Create the datafiller object. * * @param connection The database connection to use. */ public DataFiller(Connection connection) { try { m_connection = connection; m_insertTweet = new NamedPreparedStatement(m_connection, QueryUtils.insertTweet); m_insertProfile = new NamedPreparedStatement(m_connection, QueryUtils.insertProfile); m_insertBrand = new NamedPreparedStatement(m_connection, QueryUtils.insertBrand); m_insertHash = new NamedPreparedStatement(m_connection, QueryUtils.insertHash); m_insertUrl = new NamedPreparedStatement(m_connection, QueryUtils.insertUrl); m_insertMentions = new NamedPreparedStatement(m_connection, 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.getStmt().executeUpdate(); } for (Tweet.Url url : tweet.entities.urls) { QueryUtils.setInsertHashParams(m_insertUrl, tweet.id, url.expanded_url); m_insertUrl.getStmt().executeUpdate(); } for (Tweet.Mention mention : tweet.entities.user_mentions) { QueryUtils.setInsertMentionsParams(m_insertMentions, tweet.id, mention.id); m_insertMentions.getStmt().executeUpdate(); } QueryUtils.setInsertParams(m_insertTweet, m_insertProfile, tweet); m_insertTweet.getStmt().executeUpdate(); m_insertProfile.getStmt().executeUpdate(); List brands = getBrands(tweet); for (String brand : brands) { QueryUtils.setInsertBrandParams(m_insertBrand, tweet.id, brand); m_insertBrand.getStmt().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; } }