/**
 * 
 */
package awesomefb;

import java.net.UnknownHostException;
import java.util.List;

import com.mongodb.*;
import org.springframework.social.UncategorizedApiException;
import org.springframework.social.facebook.api.*;
import org.springframework.social.facebook.api.impl.FacebookTemplate;

/**
 * @author earl
 *
 */
public class Main {
  
  private static Facebook facebook;
  
  /**
   * @param args
   * @throws UnknownHostException 
   */
  public static void main(String[] args) throws UnknownHostException {
    // Topics: samsung, iphone, coffee, galaxy
    String rootPage = "KHTNCFS";
    String accessToken = "CAAJK2NXosZAABAPjvopDAeoFgmBFZB98ixFdop1sFGiBKsZB2ZAZCrPgVein9u6lxxG28fDHELyvE1Unyvmi2XSkaG5wuIflAYTc2vp7DT4xkD6j9lFN1l60la0sVsLbPRysxbOV6kmbAKpLPXbvza4Rb1bW5k9gu0ZBZARbI4ZBcUYwlb5Wat2OhZC4zaqEoXZCDSLAy6CTZBSvXtNujkGRipF";
    
    facebook = new FacebookTemplate(accessToken);
    // Connect to mongodb.
    DatabaseManager dbManager = DatabaseManager.getInstance();
    DBCollection postsCollection = dbManager.getPostsCollection();
    DBCollection usersCollection = dbManager.getUsersCollection();
    
    List<Post> feed = facebook.feedOperations().getPosts(rootPage);
    for (Post post: feed) {
      String id = post.getId();
      System.out.println("[awesomeFb] Processing post " + id);
      
      BasicDBObject doc = new BasicDBObject("message", post.getMessage())
                                            .append("creator", getCreatorInfo(usersCollection, post.getFrom()))
                                            .append("time", post.getCreatedTime())
                                            .append("comments", getComments(usersCollection, facebook.commentOperations()
                                                    .getComments(id)))
                                            .append("link", post.getLink());
      postsCollection.insert(doc);
    }
  }
    
  /**
   * Saves user data into users collection,
   * returns DBObject with newly inserted id and some basic info (FB ID, name)
   * @param usersCollection
   * @param reference
   * @return BasicDBObject
   */
  private static BasicDBObject getCreatorInfo(DBCollection usersCollection, Reference reference) {
    String id = reference.getId();
    String name = reference.getName();

    BasicDBObject userObject = new BasicDBObject("id", id).append("name", name);
    System.out.println("[awesomeFb] Inserting basic user data " + id);
    usersCollection.insert(userObject);

    // Returns DBObject with id of the object just inserted
    BasicDBObject ret = new BasicDBObject("user_id", userObject.get("_id").toString())
            .append("fb_id", id).append("name", name);
    return ret;
  }
  
  private static BasicDBObject getComments(DBCollection usersCollection, List<Comment> comments) {
    Integer count = comments.size();
    BasicDBObject ret = new BasicDBObject("count", count);
    for (Integer i = 0; i != count; i++) {
      ret.append(i.toString(), new BasicDBObject("creator", getCreatorInfo(usersCollection,
              comments.get(i).getFrom()))
              .append("message", comments.get(i).getMessage()));
    }
    return ret;
  }
}

@@ -98,202 +98,52 @@
 ort 
-com.mongodb.*;%0Aimport org.springframework.social.UncategorizedApiException;%0Aimport org.springframework.social.facebook.api.*;%0Aimport org.springframework.social.facebook.api.impl.FacebookTemplate
+org.springframework.social.facebook.api.Post
 ;%0A%0A/
@@ -188,18 +188,16 @@
  Main %7B%0A
-  
 %0A  priva
@@ -210,25 +210,40 @@
 tic 
-Facebook facebook
+DatabaseManager mDatabaseManager
 ;%0A  
@@ -462,311 +462,8 @@
 S%22;%0A
-    String accessToken = %22CAAJK2NXosZAABAPjvopDAeoFgmBFZB98ixFdop1sFGiBKsZB2ZAZCrPgVein9u6lxxG28fDHELyvE1Unyvmi2XSkaG5wuIflAYTc2vp7DT4xkD6j9lFN1l60la0sVsLbPRysxbOV6kmbAKpLPXbvza4Rb1bW5k9gu0ZBZARbI4ZBcUYwlb5Wat2OhZC4zaqEoXZCDSLAy6CTZBSvXtNujkGRipF%22;%0A    %0A    facebook = new FacebookTemplate(accessToken);
 %0A   
@@ -490,16 +490,17 @@
 db.%0A    
+m
 Database
@@ -511,18 +511,8 @@
 ger 
-dbManager 
 = Da
@@ -544,142 +544,8 @@
 ();%0A
-    DBCollection postsCollection = dbManager.getPostsCollection();%0A    DBCollection usersCollection = dbManager.getUsersCollection();%0A
     
@@ -571,31 +571,35 @@
 d = 
-f
+F
 acebook
-.feedOperations
+Manager.getInstance
 ().g
@@ -757,1853 +757,118 @@
     
-BasicDBObject doc = new BasicDBObject(%22message%22, post.getMessage())%0A                                            .append(%22creator%22, getCreatorInfo(usersCollection, post.getFrom()))%0A                                            .append(%22time%22, post.getCreatedTime())%0A                                            .append(%22comments%22, getComments(usersCollection, facebook.commentOperations()%0A                                                    .getComments(id)))%0A                                            .append(%22link%22, post.getLink());%0A      postsCollection.insert(doc);%0A    %7D%0A  %7D%0A    %0A  /**%0A   * Saves user data into users collection,%0A   * returns DBObject with newly inserted id and some basic info (FB ID, name)%0A   * @param usersCollection%0A   * @param reference%0A   * @return BasicDBObject%0A   */%0A  private static BasicDBObject getCreatorInfo(DBCollection usersCollection, Reference reference) %7B%0A    String id = reference.getId();%0A    String name = reference.getName();%0A%0A    BasicDBObject userObject = new BasicDBObject(%22id%22, id).append(%22name%22, name);%0A    System.out.println(%22%5BawesomeFb%5D Inserting basic user data %22 + id);%0A    usersCollection.insert(userObject);%0A%0A    // Returns DBObject with id of the object just inserted%0A    BasicDBObject ret = new BasicDBObject(%22user_id%22, userObject.get(%22_id%22).toString())%0A            .append(%22fb_id%22, id).append(%22name%22, name);%0A    return ret;%0A  %7D%0A  %0A  private static BasicDBObject getComments(DBCollection usersCollection, List%3CComment%3E comments) %7B%0A    Integer count = comments.size();%0A    BasicDBObject ret = new BasicDBObject(%22count%22, count);%0A    for (Integer i = 0; i != count; i++) %7B%0A      ret.append(i.toString(), new BasicDBObject(%22creator%22, getCreatorInfo(usersCollection,%0A              comments.get(i).getFrom()))%0A              .append(%22message%22, comments.get(i).getMessage()));%0A    %7D%0A    return ret;
+ExtractedPost extractedPost = new ExtractedPost(post);%0A      mDatabaseManager.insertPost(extractedPost);%0A    %7D
 %0A  %7D

Update Main method