/*
 * 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.dao;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import de.hsbo.fbg.sm4c.mining.config.Configuration;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.bson.Document;

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

    private MongoClient mongoClient;
    private MongoDatabase database;
    private MongoCollection<Document> collection;

    public FacebookDao() {
        mongoClient = new MongoClient(
                Configuration.getConfig().getPropertyValue("db_host"),
                Integer.parseInt(Configuration.getConfig().getPropertyValue("db_port")));
        database = mongoClient.getDatabase(
                Configuration.getConfig().getPropertyValue("db_name"));
        collection = database.getCollection(
                Configuration.getConfig().getPropertyValue("db_fb_collection"));
    }

    public void storeFacebookPosts(List<String> postsJoson) {
        List<Document> documents = postsJoson.stream()
                .map(p -> Document.parse(p)).collect(Collectors.toList());
        collection.insertMany(documents);
    }

    public void storeSingleFacebookPost(String postJson) {
        Document postDoc = Document.parse(postJson);
        collection.insertOne(postDoc);
    }

    public ArrayList<String> getValues() {
        MongoCursor<Document> cursor = collection.find().iterator();
        ArrayList<String> resultList = new ArrayList<>();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            resultList.add(doc.toJson());
        }
        return resultList;
    }

}

@@ -371,16 +371,67 @@
 tabase;%0A
+import static com.mongodb.client.model.Filters.eq;%0A
 import d
@@ -486,179 +486,541 @@
 ort 
-java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.stream.Collectors;%0Aimport org.bson.Document;%0A%0A/**%0A *%0A * @author Sebastian Drost%0A */%0Apublic class FacebookDao %7B
+de.hsbo.fbg.sm4c.mining.encode.FacebookJSONEncoder;%0Aimport facebook4j.Group;%0Aimport facebook4j.Page;%0Aimport facebook4j.Post;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.stream.Collectors;%0Aimport org.apache.logging.log4j.LogManager;%0Aimport org.apache.logging.log4j.Logger;%0Aimport org.bson.Document;%0Aimport org.springframework.beans.factory.annotation.Autowired;%0A%0A/**%0A *%0A * @author Sebastian Drost%0A */%0Apublic class FacebookDao %7B%0A%0A    private static final Logger LOGGER = LogManager.getLogger(FacebookDao.class);
 %0A%0A  
@@ -1139,16 +1139,59 @@
 lection;
+%0A    private FacebookJSONEncoder fbEncoder;
 %0A%0A    pu
@@ -1207,16 +1207,37 @@
 bookDao(
+String collectionName
 ) %7B%0A    
@@ -1596,87 +1596,69 @@
 ion(
-%0A                Configuration.getConfig().getPropertyValue(%22db_fb_collection%22)
+collectionName);%0A        fbEncoder = new FacebookJSONEncoder(
 );%0A 
@@ -1684,25 +1684,25 @@
 id storeFace
-b
+B
 ookPosts(Lis
@@ -1707,26 +1707,34 @@
 ist%3C
-String
+Post
 %3E posts
-Joson
+, Object source
 ) %7B%0A
@@ -1756,24 +1756,23 @@
 cument%3E 
-document
+postDoc
 s = post
@@ -1776,13 +1776,8 @@
 osts
-Joson
 .str
@@ -1781,33 +1781,16 @@
 stream()
-%0A                
 .map(p -
@@ -1810,11 +1810,75 @@
 rse(
-p))
+fbEncoder%0A                .encodePost(p, source)))%0A                
 .col
@@ -1938,16 +1938,15 @@
 any(
-document
+postDoc
 s);%0A
@@ -1996,23 +1996,32 @@
 ost(
-String postJson
+Post post, Object source
 ) %7B%0A
@@ -2066,16 +2066,42 @@
 rse(
-postJson
+fbEncoder.encodePost(post, source)
 );%0A 
@@ -2156,21 +2156,16 @@
  public 
-Array
 List%3CStr
@@ -2473,10 +2473,328 @@
 %0A    %7D%0A%0A
+    public boolean existsPage(Page p) %7B%0A        return collection.find(eq(%22source.id%22, p.getId())).first() != null;%0A%0A    %7D%0A%0A    public boolean existsGroup(Group g) %7B%0A        return collection.find(eq(%22source.id%22, g.getId())).first() != null;%0A    %7D%0A%0A    public void dropCollection() %7B%0A        collection.drop();%0A    %7D%0A%0A
 %7D%0A

fix storing and expand dao