package gr.iti.mklab.framework.client.mongo;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.WriteConcern;

import gr.iti.mklab.framework.common.domain.feeds.AccountFeed;
import gr.iti.mklab.framework.common.domain.feeds.Feed;
import gr.iti.mklab.framework.common.domain.feeds.KeywordsFeed;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.mongodb.morphia.dao.BasicDAO;

/**
 *
 * @author Manos Schinas
 * @email  manosetro@iti.gr
 *
 */
public class DAOFactory {

    public static int ASC = 1;
    public static int DESC = -1;
    
    private static MongoClientOptions options = MongoClientOptions.builder()
            .writeConcern(WriteConcern.UNACKNOWLEDGED).build();
    
    private static Map<String, MongoClient> connections = new HashMap<String, MongoClient>();
    private static Map<String, Datastore> datastores = new HashMap<String, Datastore>();

    public <K> BasicDAO<K, String> getDAO(String hostname, String dbName, Class<K> clazz) throws Exception {
    	
        String connectionKey = hostname + "#" + dbName;
        Datastore ds = datastores.get(connectionKey);
        
        Morphia morphia = new Morphia();
        MongoClient mongoClient = connections.get(hostname);

        if (mongoClient == null) {
        	mongoClient = new MongoClient(hostname, options);
            connections.put(hostname, mongoClient);
        }
        
        if (ds == null) {
            ds = morphia.createDatastore(mongoClient, dbName);
            datastores.put(connectionKey, ds);
        }
		
        return new BasicDAO<K, String>(clazz, mongoClient, morphia, dbName);
    }

	public static void main(String...args) throws Exception {
		DAOFactory factory = new DAOFactory();
		BasicDAO<Feed, String> dao = factory.getDAO("160.40.50.207", "SocialWalls", Feed.class);
		
		Date since = new Date(System.currentTimeMillis()- 5*24*3600*1000);
		
		Feed feed1 = new AccountFeed("1", "MWC_Barcelona", since);
		feed1.setSource("Twitter");	
		
		Feed feed2 = new AccountFeed("2", "startups_bcn", since);
		feed2.setSource("Twitter");

		Feed feed3 = new AccountFeed("3", "4YFN_MWC", since);
		feed3.setSource("Twitter");
		
		Feed feed4 = new KeywordsFeed("4", "MWC15", since);
		feed4.setSource("Twitter");
	
		Feed feed5 = new KeywordsFeed("5", "#connectedbeings", since);
		feed5.setSource("Twitter");
		
		Feed feed6 = new KeywordsFeed("6", "#4YFN", since);
		feed6.setSource("Twitter");
		
		dao.save(feed1);
		dao.save(feed2);
		dao.save(feed3);
		dao.save(feed4);
		dao.save(feed5);
		dao.save(feed6);
		
		//Set<Feed> feeds = new HashSet<Feed>(dao.find().asList());	
		//Set<Feed> feeds2 = new HashSet<Feed>(dao.find().asList().subList(0, 4));		
		//feeds.removeAll(feeds2);
	}
	
}

@@ -1912,19 +1912,12 @@
 %22, %22
-SocialWalls
+test
 %22, F
@@ -1983,17 +1983,17 @@
 llis()- 
-5
+1
 *24*3600
@@ -2065,29 +2065,10 @@
 ince
-);%0A%09%09feed1.setSource(
+, 
 %22Twi
@@ -2074,17 +2074,16 @@
 itter%22);
-%09
 %0A%09%09%0A%09%09Fe
@@ -2139,29 +2139,10 @@
 ince
-);%0A%09%09feed2.setSource(
+, 
 %22Twi
@@ -2207,29 +2207,10 @@
 ince
-);%0A%09%09feed3.setSource(
+, 
 %22Twi
@@ -2275,29 +2275,10 @@
 ince
-);%0A%09%09feed4.setSource(
+, 
 %22Twi
@@ -2285,24 +2285,26 @@
 tter%22);%0A%09%0A%09%09
+//
 Feed feed5 =
@@ -2352,24 +2352,26 @@
 , since);%0A%09%09
+//
 feed5.setSou
@@ -2383,32 +2383,34 @@
 Twitter%22);%0A%09%09%0A%09%09
+//
 Feed feed6 = new
@@ -2447,16 +2447,18 @@
 nce);%0A%09%09
+//
 feed6.se
@@ -2482,16 +2482,18 @@
 );%0A%09%09%0A%09%09
+//
 dao.save
@@ -2499,24 +2499,26 @@
 e(feed1);%0A%09%09
+//
 dao.save(fee
@@ -2520,24 +2520,26 @@
 e(feed2);%0A%09%09
+//
 dao.save(fee
@@ -2560,24 +2560,26 @@
 e(feed4);%0A%09%09
+//
 dao.save(fee
@@ -2581,24 +2581,26 @@
 e(feed5);%0A%09%09
+//
 dao.save(fee

Fix feeds