/trunk/influence-parent/twitter-cache/src/main/java/it/cybion/influencers/cache/persistance/TweetsPersistenceFacade.java

https://github.com/edoven/TwiCinf · Java · 166 lines · 148 code · 16 blank · 2 comment · 21 complexity · 555a950f5c2961c9d53e4193940d926e MD5 · raw file

  1. package it.cybion.influencers.cache.persistance;
  2. import com.google.gson.FieldNamingPolicy;
  3. import com.google.gson.Gson;
  4. import com.google.gson.GsonBuilder;
  5. import com.mongodb.BasicDBObject;
  6. import com.mongodb.DBCollection;
  7. import com.mongodb.DBCursor;
  8. import com.mongodb.DBObject;
  9. import com.mongodb.util.JSON;
  10. import it.cybion.influencers.cache.model.Tweet;
  11. import it.cybion.influencers.cache.persistance.exceptions.DataRangeNotCoveredException;
  12. import it.cybion.influencers.cache.persistance.exceptions.UserWithNoTweetsException;
  13. import org.apache.log4j.Logger;
  14. import java.util.ArrayList;
  15. import java.util.Collections;
  16. import java.util.Date;
  17. import java.util.List;
  18. public class TweetsPersistenceFacade
  19. {
  20. private static final Logger LOGGER = Logger.getLogger(TweetsPersistenceFacade.class);
  21. private DBCollection tweetsCollection;
  22. public TweetsPersistenceFacade(DBCollection tweetsCollection)
  23. {
  24. this.tweetsCollection = tweetsCollection;
  25. }
  26. public String getTweet(long tweetId)
  27. {
  28. BasicDBObject key = new BasicDBObject();
  29. key.put("id", tweetId);
  30. DBObject tweet = tweetsCollection.findOne(key);
  31. if (tweet == null)
  32. return null;
  33. else
  34. return tweet.toString();
  35. }
  36. public List<String> getUpTo200Tweets(long userId) throws UserWithNoTweetsException
  37. {
  38. BasicDBObject key = new BasicDBObject();
  39. key.put("user.id", userId);
  40. DBCursor cursor = tweetsCollection.find(key);
  41. List<String> tweets = new ArrayList<String>();
  42. while (cursor.hasNext() && tweets.size() < 200)
  43. {
  44. DBObject retrivedTweet = cursor.next();
  45. String tweetJson = retrivedTweet.toString();
  46. tweets.add(tweetJson);
  47. }
  48. if (tweets.size() == 0) {
  49. throw new UserWithNoTweetsException("userId " + userId + " has no tweets in mongodb collection");
  50. }
  51. return tweets;
  52. }
  53. public List<String> getTweets(long userId) throws UserWithNoTweetsException
  54. {
  55. BasicDBObject key = new BasicDBObject();
  56. key.put("user.id", userId);
  57. DBCursor cursor = tweetsCollection.find(key);
  58. List<String> tweets = new ArrayList<String>();
  59. while (cursor.hasNext())
  60. {
  61. DBObject retrivedTweet = cursor.next();
  62. String tweetJson = retrivedTweet.toString();
  63. tweets.add(tweetJson);
  64. }
  65. if (tweets.size() == 0)
  66. throw new UserWithNoTweetsException("userId " + userId + " has no tweets in mongodb collection");
  67. return tweets;
  68. }
  69. public void putTweetIfNotPresent(String tweetToInsertJson)
  70. {
  71. DBObject tweetToInsert = (DBObject) JSON.parse(tweetToInsertJson);
  72. long tweetId = -1;
  73. try
  74. {
  75. Object id = tweetToInsert.get("id");
  76. if (id instanceof Long)
  77. tweetId = (Long) id;
  78. else
  79. if (id instanceof Integer) {
  80. tweetId = new Long((Integer) id);
  81. }
  82. else
  83. {
  84. LOGGER.error("can't cast " + id + " to integer or long");
  85. // System.exit(0);
  86. }
  87. } catch (ClassCastException e)
  88. {
  89. LOGGER.error(
  90. "ERROR: problem extracting id from " + tweetToInsertJson + " " + e.getMessage());
  91. return;
  92. }
  93. String tweetJson = getTweet(tweetId);
  94. if (tweetJson == null)
  95. tweetsCollection.insert(tweetToInsert);
  96. }
  97. public void putTweets(List<String> tweets)
  98. {
  99. for (String tweet : tweets)
  100. putTweetIfNotPresent(tweet);
  101. }
  102. public void removeTweet(Long tweetId)
  103. {
  104. BasicDBObject key = new BasicDBObject();
  105. key.put("id", tweetId);
  106. DBCursor cursor = tweetsCollection.find(key);
  107. if (cursor.hasNext())
  108. {
  109. DBObject json = cursor.next();
  110. tweetsCollection.remove(json);
  111. }
  112. }
  113. public List<String> getTweetsByDate(long userId, Date fromDate, Date toDate ) throws UserWithNoTweetsException, DataRangeNotCoveredException
  114. {
  115. List<String> tweetsJsons = getTweets(userId);
  116. List<Tweet> tweets = getTweetsFromJsons(tweetsJsons);
  117. Collections.sort(tweets);
  118. Date oldestTweetDate = tweets.get(0).getCreatedAt();
  119. Date mostRecentTweetDate = tweets.get(tweets.size()-1).getCreatedAt();
  120. if (fromDate.compareTo(oldestTweetDate)<=0 || toDate.compareTo(mostRecentTweetDate)>=0) {
  121. throw new DataRangeNotCoveredException("range specified are external to current cached tweets");
  122. }
  123. List<String> matchingTweets = new ArrayList<String>();
  124. Date tweetDate;
  125. for (Tweet tweet : tweets)
  126. {
  127. tweetDate = tweet.getCreatedAt();
  128. if (fromDate.compareTo(tweetDate)<=0 && toDate.compareTo(tweetDate)>0) {
  129. matchingTweets.add(tweet.getOriginalJson());
  130. }
  131. }
  132. return matchingTweets;
  133. }
  134. private List<Tweet> getTweetsFromJsons(List<String> tweetsJsons)
  135. {
  136. List<Tweet> tweets = new ArrayList<Tweet>();
  137. Gson gson = new GsonBuilder()
  138. .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
  139. // "Wed Oct 17 19:59:40 +0000 2012"
  140. .setDateFormat("EEE MMM dd HH:mm:ss ZZZZZ yyyy").create();
  141. Tweet tweet;
  142. for (String tweetjson : tweetsJsons)
  143. {
  144. tweet = gson.fromJson(tweetjson, Tweet.class);
  145. tweet.setOriginalJson(tweetjson);
  146. tweets.add(tweet);
  147. }
  148. return tweets;
  149. }
  150. }