All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lordofthejars.nosqlunit.demo.mongodb.BookManager Maven / Gradle / Ivy

There is a newer version: 1.0.0-rc.5
Show newest version
package com.lordofthejars.nosqlunit.demo.mongodb;


import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lordofthejars.nosqlunit.demo.model.Book;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

public class BookManager {

	private static final Logger LOGGER = LoggerFactory.getLogger(BookManager.class);
	
	private static final MongoDbBookConverter MONGO_DB_BOOK_CONVERTER = new MongoDbBookConverter();
	private static final DbObjectBookConverter DB_OBJECT_BOOK_CONVERTER = new DbObjectBookConverter();
	
	private DBCollection booksCollection;
	
	public BookManager(DBCollection booksCollection) {
		this.booksCollection = booksCollection;
	}
	
	public void create(Book book) {
		DBObject dbObject = MONGO_DB_BOOK_CONVERTER.convert(book);
		booksCollection.insert(dbObject);
	}
	
	public List findAll() {
		
		LOGGER.debug("Finding All Elements of Collection "+booksCollection.getName());
		
		List books = new ArrayList();
		DBCursor findAll = booksCollection.find();
		
		LOGGER.debug("Size found "+findAll.count());
		
		while(findAll.hasNext()) {
			books.add(DB_OBJECT_BOOK_CONVERTER.convert(findAll.next()));
		}
		
		return books;
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy