package org.slc.sli.dal.repository;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.bson.types.ObjectId;
import org.slc.sli.domain.Entity;
import org.slc.sli.domain.MongoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

/**
 * mongodb implementation of the entity repository interface that provides basic
 * CRUD and field query methods for entities including core entities and
 * association entities
 * 
 * @author Dong Liu dliu@wgen.net
 * 
 */

@Repository
public class MongoEntityRepository implements EntityRepository {

	@Autowired
	private MongoTemplate template;

	@Override
	public Entity find(String entityType, String id) {
		return template.findById(new ObjectId(id), MongoEntity.class,
				entityType);
	}

	@Override
	public Iterable<Entity> finalAll(String entityType, int skip, int max) {

		LinkedList<Entity> entities = new LinkedList<Entity>();
		List<MongoEntity> results = template.find(
				new Query().skip(skip).limit(max), MongoEntity.class,
				entityType);
		entities.addAll(results);
		return entities;
	}

	@Override
	public void update(Entity entity) {
		// TODO Auto-generated method stub

	}

	@Override
	public Entity create(Entity entity) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delete(Entity entity) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(String entityType, String id) {
		// TODO Auto-generated method stub

	}

	@Override
	public Iterable<Entity> findByFields(String entityType,
			Map<String, String> fields, int skip, int max) {
		// TODO Auto-generated method stub
		return null;
	}

}

@@ -344,16 +344,78 @@
 plate;%0D%0A
+import org.springframework.data.mongodb.core.query.Criteria;%0D%0A
 import o
@@ -515,16 +515,57 @@
 sitory;%0D
+%0Aimport org.springframework.util.Assert;%0D
 %0A%0D%0A/**%0D%0A
@@ -1463,140 +1463,523 @@
 %0D%0A%09%09
-// TODO Auto-generated method stub%0D%0A%0D%0A%09%7D%0D%0A%0D%0A%09@Override%0D%0A%09public Entity create(Entity entity) %7B%0D%0A%09%09// TODO Auto-generated method stub
+Assert.notNull(entity, %22The given entity must not be null!%22);%0D%0A%09%09String id = entity.getId();%0D%0A%09%09String collection = entity.getType();%0D%0A%09%09if (id.equals(%22%22))%0D%0A%09%09%09return;%0D%0A%09%09Entity found = template.findOne(new Query(Criteria.where(%22_id%22).is(new ObjectId(id))), MongoEntity.class, collection);%0D%0A%09%09if(found != null)%0D%0A%09%09template.save(entity, collection);%0D%0A%09%7D%0D%0A%0D%0A%09@Override%0D%0A%09public Entity create(Entity entity) %7B%0D%0A%09%09Assert.notNull(entity, %22The given entity must not be null!%22);%0D%0A%09%09template.save(entity, entity.getType());
 %0D%0A%09%09
@@ -1977,36 +1977,38 @@
 e());%0D%0A%09%09return 
-null
+entity
 ;%0D%0A%09%7D%0D%0A%0D%0A%09@Overr
@@ -2056,42 +2056,226 @@
 %0D%0A%09%09
-// TODO Auto-generated method stub
+Assert.notNull(entity, %22The given entity must not be null!%22);%0D%0A%09%09String id = entity.getId();%0D%0A%09%09if(id.equals(%22%22))%0D%0A%09%09%09return;%0D%0A%09%09template.remove(new Query(Criteria.where(%22_id%22).is(new ObjectId(id))), entity.getType());
 %0D%0A%0D%0A

add more impl for mongo entity repository