STREAMS-105 | Code review feedback
Project: http://git-wip-us.apache.org/repos/asf/incubator-streams/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-streams/commit/ce6e3276 Tree: http://git-wip-us.apache.org/repos/asf/incubator-streams/tree/ce6e3276 Diff: http://git-wip-us.apache.org/repos/asf/incubator-streams/diff/ce6e3276 Branch: refs/heads/STREAMS-46 Commit: ce6e3276e113251b15c4655335a6887082e13315 Parents: c35c2ef Author: Robert Douglas <[email protected]> Authored: Mon Jun 30 11:38:32 2014 -0500 Committer: Robert Douglas <[email protected]> Committed: Mon Jun 30 11:38:32 2014 -0500 ---------------------------------------------------------------------- .../DatasiftTweetActivitySerializer.java | 141 +++++++++++-------- .../main/jsonschema/com/datasift/Datasift.json | 2 +- 2 files changed, 86 insertions(+), 57 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/ce6e3276/streams-contrib/streams-provider-datasift/src/main/java/org/apache/streams/datasift/serializer/DatasiftTweetActivitySerializer.java ---------------------------------------------------------------------- diff --git a/streams-contrib/streams-provider-datasift/src/main/java/org/apache/streams/datasift/serializer/DatasiftTweetActivitySerializer.java b/streams-contrib/streams-provider-datasift/src/main/java/org/apache/streams/datasift/serializer/DatasiftTweetActivitySerializer.java index 465196f..a9514ed 100644 --- a/streams-contrib/streams-provider-datasift/src/main/java/org/apache/streams/datasift/serializer/DatasiftTweetActivitySerializer.java +++ b/streams-contrib/streams-provider-datasift/src/main/java/org/apache/streams/datasift/serializer/DatasiftTweetActivitySerializer.java @@ -81,58 +81,32 @@ public class DatasiftTweetActivitySerializer extends DatasiftDefaultActivitySeri return activity; } + /** + * Returns the actor created from this particular event and Twitter object + * + * @param event + * @param twitter + * @return + */ public Actor buildActor(Datasift event, Twitter twitter) { User user = twitter.getUser(); Actor actor = super.buildActor(event.getInteraction()); - if(user == null) { - return retweetBuildActor(actor, twitter.getRetweet().getUser()); - } - - actor.setDisplayName(user.getName()); - actor.setId(formatId(Optional.fromNullable( - user.getIdStr()) - .or(Optional.of(user.getId().toString())) - .orNull())); - actor.setSummary(user.getDescription()); - try { - actor.setPublished(RFC3339Utils.parseToUTC(user.getCreatedAt())); - } catch (Exception e) { - LOGGER.warn("Exception trying to parse date : {}", e); - } - if(user.getUrl() != null) { - actor.setUrl(user.getUrl()); - } - - Map<String, Object> extensions = new HashMap<String,Object>(); - extensions.put("location", user.getLocation()); - extensions.put("posts", user.getStatusesCount()); - extensions.put("followers", user.getFollowersCount()); - extensions.put("screenName", user.getScreenName()); - if(user.getAdditionalProperties() != null) { - extensions.put("favorites", user.getAdditionalProperties().get("favourites_count")); - } - - Image profileImage = new Image(); - String profileUrl = null; - profileUrl = event.getInteraction().getAuthor().getAvatar(); - if(profileUrl == null && user.getAdditionalProperties() != null) { - Object url = user.getAdditionalProperties().get("profile_image_url_https"); - if(url instanceof String) - profileUrl = (String) url; - } - if(profileUrl == null) { - profileUrl = user.getProfileImageUrl(); + if (user == null) { + return userBuildActor(actor, twitter.getRetweet().getUser()); + } else { + return userBuildActor(actor, user); } - profileImage.setUrl(profileUrl); - actor.setImage(profileImage); - - actor.setAdditionalProperty("extensions", extensions); - return actor; } - //Need to make retweet user and tweet user the same object. - public Actor retweetBuildActor(Actor actor, org.apache.streams.datasift.twitter.User user) { + /** + * Build an actor object given a User + * + * @param actor + * @param user + * @return + */ + private Actor userBuildActor(Actor actor, User user) { actor.setDisplayName(user.getName()); actor.setId(formatId(Optional.fromNullable( @@ -169,13 +143,22 @@ public class DatasiftTweetActivitySerializer extends DatasiftDefaultActivitySeri if(actor.getImage() == null && profileUrl == null) { profileUrl = user.getProfileImageUrl(); } - profileImage.setUrl(profileUrl); - actor.setImage(profileImage); + + if(profileUrl != null) { + profileImage.setUrl(profileUrl); + actor.setImage(profileImage); + } actor.setAdditionalProperty("extensions", extensions); return actor; } + /** + * Adds location attributes from the given Twitter object + * + * @param activity + * @param twitter + */ public void addLocationExtension(Activity activity, Twitter twitter) { Map<String, Object> extensions = ensureExtensions(activity); Map<String, Object> location = Maps.newHashMap(); @@ -187,6 +170,13 @@ public class DatasiftTweetActivitySerializer extends DatasiftDefaultActivitySeri extensions.put("location", location); } + /** + * Creates and adds Twitter Extensions from given interaction + * + * @param activity + * @param twitter + * @param interaction + */ public void addTwitterExtensions(Activity activity, Twitter twitter, Interaction interaction) { Retweet retweet = twitter.getRetweet(); Map<String, Object> extensions = ensureExtensions(activity); @@ -217,24 +207,63 @@ public class DatasiftTweetActivitySerializer extends DatasiftDefaultActivitySeri } if(interaction.getAdditionalProperties() != null) { - ArrayList<String> mentions = (ArrayList<String>) interaction.getAdditionalProperties().get("mentions"); - ArrayList<Long> mentionIds = (ArrayList<Long>) interaction.getAdditionalProperties().get("mention_ids"); - ArrayList<UserMentions> userMentions = new ArrayList<UserMentions>(); + ArrayList<UserMentions> userMentions = createUserMentions(interaction); + + if(userMentions.size() > 0) + extensions.put("user_mentions", userMentions); + } + + extensions.put("keywords", interaction.getContent()); + } + + /** + * Returns an ArrayList of all UserMentions in this interaction + * Note: The ID list and the handle lists do not necessarily correspond 1:1 for this provider + * If those lists are the same size, then they will be merged into individual UserMention + * objects. However, if they are not the same size, a new UserMention object will be created + * for each entry in both lists. + * + * @param interaction + * @return + */ + private ArrayList<UserMentions> createUserMentions(Interaction interaction) { + ArrayList<String> mentions = (ArrayList<String>) interaction.getAdditionalProperties().get("mentions"); + ArrayList<Long> mentionIds = (ArrayList<Long>) interaction.getAdditionalProperties().get("mention_ids"); + ArrayList<UserMentions> userMentions = new ArrayList<UserMentions>(); - if(mentions != null && mentionIds != null && (mentionIds.size() == mentions.size()) && !mentions.isEmpty() && !mentionIds.isEmpty()) { + if(mentions != null && mentionIds != null && (mentionIds.size() == mentions.size()) && !mentions.isEmpty() && !mentionIds.isEmpty()) { + for(int x = 0; x < mentions.size(); x ++) { + UserMentions mention = new UserMentions(); + + mention.setIdStr("id:twitter:" + mentionIds.get(x)); + mention.setId(Long.parseLong(String.valueOf(mentionIds.get(x)))); + mention.setName(mentions.get(x)); + mention.setScreenName(mentions.get(x)); + + userMentions.add(mention); + } + } else if((mentions != null && !mentions.isEmpty()) || (mentionIds != null && !mentionIds.isEmpty())) { + if(mentions != null && !mentions.isEmpty()) { for(int x = 0; x < mentions.size(); x ++) { UserMentions mention = new UserMentions(); - - mention.setIdStr("id:twitter:" + mentionIds.get(x)); - mention.setId(mentionIds.get(x)); mention.setName(mentions.get(x)); mention.setScreenName(mentions.get(x)); userMentions.add(mention); } - extensions.put("user_mentions", userMentions); + } + if(mentionIds != null && !mentionIds.isEmpty()) { + for(int x = 0; x < mentionIds.size(); x ++) { + UserMentions mention = new UserMentions(); + + mention.setIdStr("id:twitter:" + mentionIds.get(x)); + mention.setId(Long.parseLong(String.valueOf(mentionIds.get(x)))); + + userMentions.add(mention); + } } } - extensions.put("keywords", interaction.getContent()); + + return userMentions; } } http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/ce6e3276/streams-contrib/streams-provider-datasift/src/main/jsonschema/com/datasift/Datasift.json ---------------------------------------------------------------------- diff --git a/streams-contrib/streams-provider-datasift/src/main/jsonschema/com/datasift/Datasift.json b/streams-contrib/streams-provider-datasift/src/main/jsonschema/com/datasift/Datasift.json index e7b664a..c30fa8f 100644 --- a/streams-contrib/streams-provider-datasift/src/main/jsonschema/com/datasift/Datasift.json +++ b/streams-contrib/streams-provider-datasift/src/main/jsonschema/com/datasift/Datasift.json @@ -738,7 +738,7 @@ "type": "array", "items": [ { - "type": "integer" + "type": "string" } ] },
