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

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

import org.springframework.stereotype.Component;

import net.davidvoid.thor.lightning.entity.Entity;
import net.davidvoid.thor.lightning.entity.Feed;
import net.davidvoid.thor.lightning.entity.Item;

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


/**
 * Created by david on 3/21/16.
 * collection name: item
 * { _id: XX, id: long, name: string, author: string, content: string,
 * url: string, is_saved: boolean, is_read: boolean, last_update; Date}
 */
@Component
public class ItemStore extends AbstractStore {

    private static final String COLLECTION_NAME = "item";
    
    public List<Item> getItemsFromFeed(Feed feed) {
        assert(feed.has_valid_id());
        
        return null;
    }

    @Override
    protected DBObject toDBObject(Entity entity) {
        Item item = (Item) entity;
        
        BasicDBObject object = new BasicDBObject("id", item.getId());
        object.put("name", item.getName());
        object.put("author", item.getAuthor());
        object.put("content", item.getContent());
        object.put("url", item.getUrl());
        object.put("is_saved", item.isSaved());
        object.put("is_read", item.isRead());
        object.put("last_update", item.getLastUpdate());

        return object;
    }

    @Override
    protected Entity toEntity(DBObject object) {
        Item item = new Item();
        item.setId((Long)object.get("id"));
        item.setName((String)object.get("name"));
        item.setAuthor((String)object.get("author"));
        item.setContent((String)object.get("content"));
        item.setUrl((String)object.get("url"));
        item.setIsSaved((boolean)object.get("is_saved"));
        item.setIsRead((boolean)object.get("is_read"));
        item.setLastUpdate((Date)object.get("last_update"));
        
        return item;
    }

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

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

@@ -55,55 +55,62 @@
 ort 
-java.util.Date;%0Aimport java.util.List;%0A
+static org.springframework.util.Assert.isTrue;
 %0Aimport 
 org.
@@ -105,16 +105,23 @@
 %0Aimport 
+static 
 org.spri
@@ -136,27 +136,73 @@
 ork.
-stereotype.Componen
+util.Assert.notNull;%0A%0Aimport java.util.Date;%0Aimport java.util.Lis
 t;%0A%0A
@@ -351,16 +351,66 @@
 .Item;%0A%0A
+import org.springframework.stereotype.Component;%0A%0A
 import c
@@ -540,16 +540,31 @@
 _id: XX,
+ feed_id: long,
  id: lon
@@ -612,16 +612,17 @@
  string,
+ 
 %0A * url:
@@ -686,16 +686,168 @@
 ; Date%7D%0A
+ * index 1: id%0A * index 2: feed_id, name%0A * index 3: feed_id, last_update%0A * index 4: feed_id, is_read, last_update%0A * index 4: feed_id, is_read, name %0A
  */%0A@Com
@@ -960,24 +960,59 @@
 item%22;%0A    %0A
+    @SuppressWarnings(%22unchecked%22)%0A
     public L
@@ -1063,14 +1063,37 @@
     
-assert
+notNull(feed);%0A        isTrue
 (fee
@@ -1132,19 +1132,117 @@
     
-return null
+BasicDBObject query = new BasicDBObject(%22id%22, feed.getId());%0A        return (List%3CItem%3E) (List%3C?%3E) get(query)
 ;%0A  
@@ -1303,32 +1303,63 @@
 ntity entity) %7B%0A
+        assert entity != null;%0A
         Item ite
@@ -1369,32 +1369,68 @@
  (Item) entity;%0A
+        assert item.has_valid_id();%0A
         %0A       
@@ -1917,24 +1917,64 @@
 t object) %7B%0A
+        assert object != null;%0A        %0A
         Item
@@ -2538,33 +2538,109 @@
     
-Item item = (Item) entity
+assert entity != null;%0A        %0A        Item item = (Item) entity;%0A        assert item.has_valid_id()
 ;%0A  

add assertions