package net.davidvoid.thor.lightning.data.access;

import java.util.Date;
import java.util.List;

import net.davidvoid.thor.lightning.data.source.MongoDataSource;
import net.davidvoid.thor.lightning.entity.Entity;
import net.davidvoid.thor.lightning.entity.Feed;
import net.davidvoid.thor.lightning.entity.FeedRelation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
 * Created by david on 3/22/16. feed collection:
 * {_id: XX, id: long, description: string, url: string, last_update: Date}
 * index 1 : id, unique
 */
@Component
public class FeedStore extends AbstractStore {

    private static final String COLLECTION_NAME = "feed";
    @Autowired
    private MongoDataSource data_source = null;
    @Autowired
    private Counter counter = null;

    @SuppressWarnings("unchecked")
    public List<Feed> getFeeds(List<FeedRelation> list) {
        DBObject query = get_query(list);
        return (List<Feed>) (List<?>) get(query);
    }

    private DBObject get_query(List<FeedRelation> list) {
        BasicDBList id_list = get_id_list(list);
        return new BasicDBObject("id", new BasicDBObject("$in", id_list));
    }

    private BasicDBList get_id_list(List<FeedRelation> list) {
        BasicDBList id_list = new BasicDBList();

        for (FeedRelation relation : list) {
            id_list.add(relation.getFeedId());
        }

        return id_list;
    }

    @Override
    protected DBObject toDBObject(Entity entity) {
        Feed feed = (Feed) entity;

        BasicDBObject object = new BasicDBObject("id", feed.getId());
        object.put("description", feed.getDescription());
        object.put("url", feed.getUrl());
        object.put("last_update", feed.getLastUpdate());

        return object;
    }

    @Override
    protected Entity toEntity(DBObject object) {
        Feed feed = new Feed();
        feed.setId((Long) object.get("id"));
        feed.setDescription((String) object.get("description"));
        feed.setUrl((String) object.get("url"));
        feed.setLastUpdate((Date) object.get("last_update"));

        return feed;
    }

    @Override
    protected DBObject getModifyQuery(Entity entity) {
        Feed feed = (Feed) entity;
        return new BasicDBObject("id", feed.getId());
    }

    @Override
    protected String getCollectionName() {
        return COLLECTION_NAME;
    }
}

@@ -437,16 +437,72 @@
 ponent;%0D
+%0Aimport static org.springframework.util.Assert.notNull;%0D
 %0A%0D%0Aimpor
@@ -1086,32 +1086,58 @@
 ation%3E list) %7B%0D%0A
+        notNull(list);%0D%0A%0D%0A
         DBObject
@@ -1274,32 +1274,64 @@
 ation%3E list) %7B%0D%0A
+        assert list != null;%0D%0A%0D%0A
         BasicDBL
@@ -1651,32 +1651,131 @@
 .getFeedId());%0D%0A
+            assert relation.has_valid_id() : %22the relation used to find feeds must has valid id%22;%0D%0A
         %7D%0D%0A%0D%0A   
@@ -1864,32 +1864,66 @@
 tity entity) %7B%0D%0A
+        assert entity != null;%0D%0A%0D%0A
         Feed fee
@@ -1942,18 +1942,16 @@
 ntity;%0D%0A
-%0D%0A
         
@@ -2269,24 +2269,58 @@
  object) %7B%0D%0A
+        assert object != null;%0D%0A%0D%0A
         Feed
@@ -2681,33 +2681,104 @@
     
-Feed feed = (Feed) entity
+assert entity != null;%0D%0A%0D%0A        Feed feed = (Feed) entity;%0D%0A        assert feed.has_valid_id()
 ;%0D%0A 

add assertions