package de.uni_koeln.spinfo.drc.mongodb.repository;

import java.util.List;

import javax.inject.Inject;

import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import de.uni_koeln.spinfo.drc.mongodb.data.document.Page;

@Repository
public class PageRepositoryImpl implements PageRepository {

	private MongoOperations operations;

	@Inject
	public PageRepositoryImpl(MongoOperations operations) {
		this.operations = operations;
	}

	@Override
	public Page findByUrl(String url) {
		return operations.findOne(new Query(Criteria.where("url").is(url)),
				Page.class);
	}

	@Override
	public List<Page> findByUserId(String userId) {
		return operations.find(new Query(Criteria.where("userId").is(userId)),
				Page.class);
	}

	@Override
	public List<Page> findByVolumeId(String volumeId) {
		return operations.find(new Query(Criteria.where("volumeId")
				.is(volumeId)), Page.class);
	}

	@Override
	public List<Page> findByChapterIds(List<String> chapterIds) {
		return operations.find(
				new Query(Criteria.where("chapterIds").is(chapterIds)),
				Page.class);
	}

	@Override
	public List<Page> findByLanguageIds(List<String> LanguageIds) {
		return operations.find(
				new Query(Criteria.where("LanguageIds").is(LanguageIds)),
				Page.class);
	}

	@Override
	public List<Page> findAll() {
		return operations.find(new Query(), Page.class);
	}

	@Override
	public Page save(Page page) {
		operations.save(page);
		return page;
	}

	@Override
	public String findOne() {
		return operations.findOne(new Query(), String.class, "pages");
	}

	@Override
	public Page findByPageId(String pageId) {
		return operations.findOne(new Query(Criteria.where("_id").is(pageId)),
				Page.class);
	}

	@Override
	public String findOne(String pageId) {
		return operations.findOne(new Query(Criteria.where("_id").is(pageId)),
				String.class, "pages");
	}
	
	@Override
	public List<Page> findByRange(int from, int to) {
		List<Page> wordRange = operations.find(new Query(Criteria.where("index").gte(from)
				.andOperator(Criteria.where("index").lte(to))), Page.class);
		return wordRange;
	}

}

@@ -2096,25 +2096,21 @@
 t%3CPage%3E 
-wordRan
+pa
 ge
+s
  = opera
@@ -2146,21 +2146,21 @@
 .where(%22
-index
+start
 %22).gte(f
@@ -2201,13 +2201,11 @@
 re(%22
-i
+e
 nd
-ex
 %22).l
@@ -2240,17 +2240,13 @@
 urn 
-wordRan
+pa
 ge
+s
 ;%0A%09%7D

Fix range query for page objects