<commit_before>package com.github.maximilientyc.conversations.domain.repositories.mongodb;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.github.maximilientyc.conversations.domain.Conversation;
import com.github.maximilientyc.conversations.domain.ConversationSearchCriteria;
import com.github.maximilientyc.conversations.domain.Participant;
import com.github.maximilientyc.conversations.domain.User;
import com.github.maximilientyc.conversations.domain.repositories.ConversationRepository;
import com.github.maximilientyc.conversations.domain.repositories.mongodb.serializers.*;
import com.github.maximilientyc.conversations.infrastructure.searches.PaginatedList;
import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.mongodb.client.model.Filters.eq;

/**
 * Created by @maximilientyc on 10/02/2016.
 */
public class MongoDbConversationRepository implements ConversationRepository {

	private final MongoCollection<Document> conversationCollection;

	private final ObjectMapper conversationObjectMapper;

	public MongoDbConversationRepository(MongoDatabase mongoDatabase) {
		this.conversationCollection = mongoDatabase.getCollection("conversations");

		ObjectMapper objectMapper = new ObjectMapper();
		objectMapper.enable(SerializationFeature.INDENT_OUTPUT);

		SimpleModule customSerializerModule = new SimpleModule();
		customSerializerModule.addSerializer(Conversation.class, new ConversationSerializer());
		customSerializerModule.addSerializer(Participant.class, new ParticipantSerializer());
		customSerializerModule.addSerializer(User.class, new UserSerializer());
		customSerializerModule.addDeserializer(Conversation.class, new ConversationDeserializer());
		customSerializerModule.addDeserializer(Participant.class, new ParticipantDeserializer());
		customSerializerModule.addDeserializer(User.class, new UserDeserializer());
		objectMapper.registerModule(customSerializerModule);

		this.conversationObjectMapper = objectMapper;
	}

	@Override
	public void add(Conversation conversation) {
		try {
			insertOneConversation(conversation);
		} catch (JsonProcessingException e) {
			throw new IllegalArgumentException(e);
		}
	}

	private void insertOneConversation(Conversation conversation) throws JsonProcessingException {
		String conversationAsJson = conversationObjectMapper.writeValueAsString(conversation);
		Document document = Document.parse(conversationAsJson);
		conversationCollection.insertOne(document);
	}

	@Override
	public void update(Conversation conversation) {
		try {
			updateOneConversation(conversation);
		} catch (JsonProcessingException e) {

		}
	}

	private void updateOneConversation(Conversation conversation) throws JsonProcessingException {
		String conversationAsJson = conversationObjectMapper.writeValueAsString(conversation);
		Document document = Document.parse(conversationAsJson);

		String conversationId = conversation.getConversationId();
		conversationCollection.findOneAndReplace(eq("conversationId", conversationId), document);
	}

	@Override
	public boolean exists(String conversationId) {
		long conversationCount = conversationCollection.count(eq("conversationId", conversationId));
		return conversationCount > 0;
	}

	@Override
	public Conversation get(String conversationId) {
		if (conversationId == null) {
			throw new IllegalArgumentException("Conversation Id cannot be empty.");
		}
		try {
			return findOneConversation(conversationId);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	private Conversation findOneConversation(String conversationId) throws IOException {
		FindIterable<Document> documents = conversationCollection.find(eq("conversationId", conversationId));
		Document document = documents.first();
		return conversationObjectMapper.readValue(document.toJson(), Conversation.class);
	}

	private PaginatedList<Conversation> findConversations(ConversationSearchCriteria conversationSearchCriteria) throws IOException {
		int sortDirection = (conversationSearchCriteria.getSortDirection().toLowerCase().equals("asc")) ? 1 : -1;
		FindIterable<Document> documents = conversationCollection.find(eq("participants.user.userId", conversationSearchCriteria.getUserId())).sort(
				new BasicDBObject("lastActiveOn", sortDirection)
		);

		List<Conversation> foundConversationList = new ArrayList<Conversation>();
		for (Document document : documents) {
			Conversation conversation = conversation = conversationObjectMapper.readValue(document.toJson(), Conversation.class);
			foundConversationList.add(conversation);
		}

		PaginatedList<Conversation> conversationPaginatedList = new PaginatedList<Conversation>(foundConversationList.size(), foundConversationList);
		return conversationPaginatedList;
	}

	@Override
	public PaginatedList<Conversation> find(ConversationSearchCriteria conversationSearchCriteria) {
		try {
			return findConversations(conversationSearchCriteria);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public long count(ConversationSearchCriteria criteria) {<commit_msg>quick renaming on mongodb repository<commit_after>package com.github.maximilientyc.conversations.domain.repositories.mongodb;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.github.maximilientyc.conversations.domain.Conversation;
import com.github.maximilientyc.conversations.domain.ConversationSearchCriteria;
import com.github.maximilientyc.conversations.domain.Participant;
import com.github.maximilientyc.conversations.domain.User;
import com.github.maximilientyc.conversations.domain.repositories.ConversationRepository;
import com.github.maximilientyc.conversations.domain.repositories.mongodb.serializers.*;
import com.github.maximilientyc.conversations.infrastructure.searches.PaginatedList;
import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.mongodb.client.model.Filters.eq;

/**
 * Created by @maximilientyc on 10/02/2016.
 */
public class MongoDbConversationRepository implements ConversationRepository {

	private final MongoCollection<Document> conversationCollection;

	private final ObjectMapper conversationObjectMapper;

	public MongoDbConversationRepository(MongoDatabase mongoDatabase) {
		this.conversationCollection = mongoDatabase.getCollection("conversations");

		ObjectMapper objectMapper = new ObjectMapper();
		objectMapper.enable(SerializationFeature.INDENT_OUTPUT);

		SimpleModule customSerializerModule = new SimpleModule();
		customSerializerModule.addSerializer(Conversation.class, new ConversationSerializer());
		customSerializerModule.addSerializer(Participant.class, new ParticipantSerializer());
		customSerializerModule.addSerializer(User.class, new UserSerializer());
		customSerializerModule.addDeserializer(Conversation.class, new ConversationDeserializer());
		customSerializerModule.addDeserializer(Participant.class, new ParticipantDeserializer());
		customSerializerModule.addDeserializer(User.class, new UserDeserializer());
		objectMapper.registerModule(customSerializerModule);

		this.conversationObjectMapper = objectMapper;
	}

	@Override
	public void add(Conversation conversation) {
		try {
			insertOneConversation(conversation);
		} catch (JsonProcessingException e) {
			throw new IllegalArgumentException(e);
		}
	}

	private void insertOneConversation(Conversation conversation) throws JsonProcessingException {
		String conversationAsJson = conversationObjectMapper.writeValueAsString(conversation);
		Document document = Document.parse(conversationAsJson);
		conversationCollection.insertOne(document);
	}

	@Override
	public void update(Conversation conversation) {
		try {
			updateOneConversation(conversation);
		} catch (JsonProcessingException e) {

		}
	}

	private void updateOneConversation(Conversation conversation) throws JsonProcessingException {
		String conversationAsJson = conversationObjectMapper.writeValueAsString(conversation);
		Document document = Document.parse(conversationAsJson);

		String conversationId = conversation.getConversationId();
		conversationCollection.findOneAndReplace(eq("conversationId", conversationId), document);
	}

	@Override
	public boolean exists(String conversationId) {
		long conversationCount = conversationCollection.count(eq("conversationId", conversationId));
		return conversationCount > 0;
	}

	@Override
	public Conversation get(String conversationId) {
		if (conversationId == null) {
			throw new IllegalArgumentException("Conversation Id cannot be empty.");
		}
		try {
			return findOneConversation(conversationId);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	private Conversation findOneConversation(String conversationId) throws IOException {
		FindIterable<Document> documents = conversationCollection.find(eq("conversationId", conversationId));
		Document document = documents.first();
		return conversationObjectMapper.readValue(document.toJson(), Conversation.class);
	}

	private PaginatedList<Conversation> findConversations(ConversationSearchCriteria searchCriteria) throws IOException {
		int mongoDbSortDirection = (searchCriteria.getSortDirection().toLowerCase().equals("asc")) ? 1 : -1;
		FindIterable<Document> documents = conversationCollection.find(eq("participants.user.userId", searchCriteria.getUserId())).sort(
				new BasicDBObject(searchCriteria.getSortCriteria(), mongoDbSortDirection)
		);

		List<Conversation> foundConversationList = new ArrayList<Conversation>();
		for (Document document : documents) {
			Conversation conversation = conversation = conversationObjectMapper.readValue(document.toJson(), Conversation.class);
			foundConversationList.add(conversation);
		}

		PaginatedList<Conversation> conversationPaginatedList = new PaginatedList<Conversation>(foundConversationList.size(), foundConversationList);
		return conversationPaginatedList;
	}

	@Override
	public PaginatedList<Conversation> find(ConversationSearchCriteria conversationSearchCriteria) {
		try {
			return findConversations(conversationSearchCriteria);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public long count(ConversationSearchCriteria criteria) {
{'fraction_non_alphanumeric': 0.08680142687277051, 'fraction_numerical': 0.002012256471233879, 'mean_word_length': 7.4107692307692306, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7327566', 'n_tokens_mistral': 3047, 'n_tokens_neox': 2815, 'n_words': 526}