package step.core.accessors;

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

import org.jongo.Jongo;
import org.jongo.MongoCollection;
import org.jongo.marshall.jackson.JacksonMapper;

import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
import com.fasterxml.jackson.datatype.jsr353.JSR353Module;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;

import step.commons.conf.Configuration;

public class MongoClientSession implements Closeable {

	protected MongoClient mongoClient;
	
	protected String db;
	
	protected Configuration configuration;

	public MongoClientSession(Configuration configuration) {
		super();
		this.configuration = configuration;
		
		initMongoClient();
	}
	
	protected void initMongoClient() {
		String host = configuration.getProperty("db.host");
		Integer port = configuration.getPropertyAsInteger("db.port",27017);
		String user = configuration.getProperty("db.username");
		String pwd = configuration.getProperty("db.password");
		
		db = configuration.getProperty("db.database","step");

		ServerAddress address = new ServerAddress(host, port);
		List<MongoCredential> credentials = new ArrayList<>();
		if(user!=null) {
			MongoCredential credential = MongoCredential.createCredential(user, db, pwd.toCharArray());
			credentials.add(credential);
		}
		
		mongoClient = new MongoClient(address, credentials);
	}
	
	public MongoDatabase getMongoDatabase() {
		return mongoClient.getDatabase(db);
	}
	
	public org.jongo.MongoCollection getJongoCollection(String collectionName) {
		@SuppressWarnings("deprecation")
		DB db = mongoClient.getDB(this.db);
		
		Jongo jongo = new Jongo(db,new JacksonMapper.Builder()
			      .registerModule(new JSR353Module())
			      .registerModule(new JsonOrgModule())
			      .registerModule(new DefaultAccessorModule())
			      .build());
		MongoCollection collection = jongo.getCollection(collectionName);
		
		return collection;
	}

	@Override
	public void close() throws IOException {
		mongoClient.close();
	}
	
}

@@ -423,24 +423,64 @@
 ngoClient;%0D%0A
+import com.mongodb.MongoClientOptions;%0D%0A
 import com.m
@@ -1214,24 +1214,110 @@
 sword%22);%0D%0A%09%09
+int maxConnections = configuration.getPropertyAsInteger(%22db.maxConnections%22, 10);%0D%0A%09%09%09
 %0D%0A%09%09db = con
@@ -1633,24 +1633,190 @@
 l);%0D%0A%09%09%7D%0D%0A%09%09
+MongoClientOptions.Builder clientOptions = new MongoClientOptions.Builder();%0D%0A%09%09MongoClientOptions options = clientOptions.connectionsPerHost(maxConnections).build();
 %0D%0A%09%09mongoCli
@@ -1857,18 +1857,30 @@
 dentials
-);
+,options);%0D%0A%09%09
 %0D%0A%09%7D%0D%0A%09%0D

Add max connectionsPerHost settings for Mongo DB client