package org.devdom.tracker.model.dao;

import facebook4j.Comment;
import facebook4j.Facebook;
import facebook4j.FacebookException;
import facebook4j.FacebookFactory;
import facebook4j.Like;
import facebook4j.PagableList;
import facebook4j.Post;
import facebook4j.Reading;
import facebook4j.ResponseList;
import facebook4j.conf.ConfigurationBuilder;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.http.HttpServletRequest;
import org.devdom.tracker.model.dto.FacebookComment;
import org.devdom.tracker.model.dto.FacebookPost;
import org.devdom.tracker.util.Configuration;

/**
 *
 * @author Carlos Vásquez Polanco
 */
public class FacebookDao {

    private static final long serialVersionUID = 1L;
    private final EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa");
    private static final ConfigurationBuilder cb = Configuration.getFacebookConfig();
    //Facebook facebook = (Facebook) request.getSession().getAttribute("facebook");
    Facebook facebook = new FacebookFactory(cb.build()).getInstance();
    public FacebookDao(){ }

    public EntityManager getEntityManager(){
        return emf.createEntityManager(Configuration.JPAConfig());
    }
    
    public void syncMessageInformation(EntityManager em, Post post){

        PagableList<Comment> comments = post.getComments();
        comments.parallelStream().forEach((comment) -> {
            if( (comment.getId()!=null) && (post.getId()!=null) && (comment.getFrom()!=null) ){
                int likes = this.getCommentLikes(comment);
                FacebookComment newComment = new FacebookComment();            
                newComment.setCreateTime(comment.getCreatedTime());
                newComment.setLikeCount(likes);
                newComment.setFromId(comment.getFrom().getId());
                newComment.setMessage(comment.getMessage());
                newComment.setMessageId(comment.getId());
                newComment.setPostId(post.getId().split("_")[1]);
                em.merge(newComment);
            }
        });
    } 
    
    /**
    * Sincroniza la información de facebook para cada developer
    *
     * @param request request from servlet
    * @param groupId identificador del grupo que se desea revisar
    */
    public void syncGroupInformation(HttpServletRequest request, String groupId){
        EntityManager em = getEntityManager();

        try {
            em.getTransaction().begin();
            Reading reading = new Reading();
            reading.limit(Configuration.POSTS_LIMIT);
            ResponseList<Post> group = facebook.getGroupFeed(groupId,reading);

            List<Post> posts = group.subList(1,group.size());
            posts.stream().forEach((Post post) -> {
                if(post.getFrom()!=null){
                    int likes = getPostLikes(post);
                    FacebookPost newPost = new FacebookPost();
                    newPost.setPostId(post.getId().split("_")[1]);
                    newPost.setFromId(post.getFrom().getId());
                    newPost.setCreationDate(post.getCreatedTime());
                    newPost.setLikeCount(likes);
                    newPost.setMessage(post.getMessage());
                    
                    em.merge(newPost);
                    
                    syncMessageInformation(em,post);
                }
            });
            em.getTransaction().commit();
        } catch (FacebookException | IllegalArgumentException ex) {
            Logger.getLogger(FacebookDao.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            if(em!=null|em.isOpen())
                em.close();
        }
    }

    /**
    * Retorna la cantidad total de likes que contiene un post
    *
    * @param post con información del post que se encuentra recorriendo en ese momento
    * @return retorna la cantidad total de likes para un post
    */ 
    public int getPostLikes(Post post){
        Reading reading = new Reading();
        reading.limit(Configuration.POSTS_LIMIT);

        try {            
            ResponseList<Like> likes = facebook.getPostLikes(post.getId(),reading);
            return likes.size();
        } catch (FacebookException ex) {
            Logger.getLogger(FacebookDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }
    /**
    * Retorna la cantidad total de likes que contiene un comentario
    *
    * @param comment con información del comentario que se encuentra recorriendo en ese momento
    * @return retorna la cantidad total de likes para un comentario
    */
    public int getCommentLikes(Comment comment){
        Reading reading = new Reading();
        reading.limit(Configuration.LIKES_LIMIT);

        try {
            ResponseList<Like> likes = facebook.getCommentLikes(comment.getId());
            return likes.size();
        } catch (FacebookException ex) {
            Logger.getLogger(FacebookDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }
}
@@ -1059,92 +1059,8 @@
 ();%0A
-    //Facebook facebook = (Facebook) request.getSession().getAttribute(%22facebook%22);%0A
     
@@ -1122,24 +1122,24 @@
 Instance();%0A
+
     public F
@@ -1457,32 +1457,67 @@
 ((comment) -%3E %7B%0A
+            if(comment!=null)%7B%0A    
             if( 
@@ -1604,32 +1604,36 @@
                 
+    
 int likes = this
@@ -1667,32 +1667,36 @@
                 
+ 
+   
 FacebookComment 
@@ -1751,32 +1751,36 @@
                 
+  
+  
 newComment.setCr
@@ -1807,32 +1807,36 @@
 CreatedTime());%0A
+    
                 
@@ -1875,32 +1875,36 @@
                 
+    
 newComment.setFr
@@ -1944,32 +1944,36 @@
                 
+ 
+   
 newComment.setMe
@@ -2009,32 +2009,36 @@
                 
+    
 newComment.setMe
@@ -2055,32 +2055,36 @@
 mment.getId());%0A
+    
                 
@@ -2141,32 +2141,36 @@
                 
+    
 em.merge(newComm
@@ -2167,32 +2167,50 @@
 ge(newComment);%0A
+                %7D%0A
             %7D%0A  

Fix al recibir objetos nulos