/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.hsbo.fbg.sm4c.mining.encode;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.gson.Gson;
import facebook4j.Group;
import facebook4j.Page;
import facebook4j.Post;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 *
 * @author Sebastian Drost
 */
public class FacebookJSONEncoder {

    private Gson gson;
    ObjectMapper mapper;

    public FacebookJSONEncoder() {
        gson = new Gson();
        mapper = new ObjectMapper();
    }

    public String encodePost(Post post, Object group) {
        String result = "";
        ObjectNode postNode = createPostNode(post, group);
        try {
            result = mapper.writeValueAsString(postNode);
        } catch (IOException ex) {
            Logger.getLogger(FacebookJSONEncoder.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public ObjectNode createPostNode(Post post, Object source) {
        String postJson = gson.toJson(post);
        ObjectNode root = mapper.createObjectNode();

        try {
            JsonNode fbPostNode = mapper.readTree(postJson);
            String createdTime = fbPostNode.get("createdTime").asText();
            DateTime dt = DateTime.parse(createdTime, DateTimeFormat.forPattern("MMM dd, yyyy hh:mm:ss a"));
            ((ObjectNode) fbPostNode).put("createdTime", dt.toDateTime(DateTimeZone.UTC).toString());
            root.put("id", fbPostNode.get("id").asText());
            ObjectNode sourceNode = mapper.createObjectNode();

            if (source != null) {
                if (source instanceof Group) {
                    sourceNode.put("type", "group");
                    sourceNode.put("id", ((Group) source).getId());
                    sourceNode.put("name", ((Group) source).getName());
                } else if (source instanceof Page) {
                    sourceNode.put("type", "page");
                    sourceNode.put("id", ((Page) source).getId());
                    sourceNode.put("name", ((Page) source).getName());
                } else {
                    sourceNode.put("type", "none");
                    sourceNode.put("id", "");
                    sourceNode.put("name", "");
                }
            }
            root.set("source", sourceNode);
            root.put("label", "");
            root.set("fb_post", fbPostNode);
        } catch (IOException ex) {
            Logger.getLogger(FacebookJSONEncoder.class.getName()).log(Level.SEVERE, null, ex);
        }
        return root;
    }

    public ArrayNode createPostArrayNode(List<Post> posts, Object source) {
        ArrayNode postArray = mapper.createArrayNode();
        posts.forEach(p -> {
            ObjectNode node = createPostNode(p, source);
            postArray.add(node);
        });
        return postArray;
    }

    public ArrayNode createPostArrayNodeForGroup(List<Post> posts, Group group) {
        ArrayNode postArray = mapper.createArrayNode();
        posts.forEach(p -> {
            ObjectNode node = createPostNode(p, group);
            postArray.add(node);
        });
        return postArray;
    }
}

@@ -219,16 +219,75 @@
 ncode;%0A%0A
+import com.fasterxml.jackson.core.JsonProcessingException;%0A
 import c
@@ -651,49 +651,62 @@
 ort 
-java.util.logging.Level;%0Aimport java.util
+org.apache.logging.log4j.LogManager;%0Aimport org.apache
 .log
@@ -710,16 +710,22 @@
 logging.
+log4j.
 Logger;%0A
@@ -849,42 +849,54 @@
 org.
-joda.time.format.DateTimeFormatter
+springframework.beans.factory.InitializingBean
 ;%0A%0A/
@@ -968,16 +968,107 @@
 oder %7B%0A%0A
+    private static final Logger LOGGER = LogManager.getLogger(FacebookJSONEncoder.class);%0A%0A
     priv
@@ -1082,24 +1082,32 @@
 n gson;%0A    
+private 
 ObjectMapper
@@ -1446,34 +1446,46 @@
        %7D catch (
-IO
+JsonProcessing
 Exception ex) %7B%0A
@@ -1501,83 +1501,595 @@
    L
-ogger.getLogger(FacebookJSONEncoder.class.getName()).log(Level.SEVERE, null
+OGGER.error(%22Could not encode post%22, ex);%0A        %7D%0A        return result;%0A    %7D%0A%0A    public String encodePostArray(List%3CPost%3E posts, Object source) %7B%0A        ObjectNode root = mapper.createObjectNode();%0A        ArrayNode postArray = mapper.createArrayNode();%0A        posts.forEach(p -%3E %7B%0A            ObjectNode node = createPostNode(p, source);%0A            postArray.add(node);%0A        %7D);%0A        String result = %22%22;%0A        try %7B%0A            result = mapper.writeValueAsString(postArray);%0A        %7D catch (JsonProcessingException ex) %7B%0A            LOGGER.error(%22Could not encode post%22
 , ex
@@ -3673,83 +3673,47 @@
    L
-ogger.getLogger(FacebookJSONEncoder.class.getName()).log(Level.SEVERE, null
+OGGER.error(%22Could not parse JSON post%22
 , ex

expand encoding