/**
*   Copyright 2014 Torben Werner, Bridget Ryan
*
*   Licensed under the Apache License, Version 2.0 (the "License");
*   you may not use this file except in compliance with the License.
*   You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*/

package org.imirp.imirp.db;

import java.net.UnknownHostException;

import javax.net.ssl.SSLSocketFactory;

import org.apache.log4j.Logger;
import org.jongo.Jongo;

import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;
import com.typesafe.config.Config;

public class _DbModule extends AbstractModule {
	private static final Logger logger = Logger.getLogger(_DbModule.class);
	
	@Override
	protected void configure() {
		bind(ImirpDataStore.class).to(MongoDataStoreImpl.class);
	}

	@Provides @Singleton
	Mongo provideMongo(Config config) throws UnknownHostException{
		// Read the db connection properties
		String dbHost = config.getString("imirp.db.host");
		int dbPort = config.getInt("imirp.db.port");
		logger.info("Connecting to db at host[" + dbHost + "] and port[" + dbPort + "]");		
		
		// Setup connection options
		MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder();
		if(config.getBoolean("imirp.db.ssl_enabled")){
			logger.debug("Mongo SSL enabled.");
			optionsBuilder = optionsBuilder.socketFactory(SSLSocketFactory.getDefault());
		}
        MongoClientOptions o = optionsBuilder.build();
		MongoClient mongo = new MongoClient(new ServerAddress(dbHost, dbPort), o);
		
		return mongo;
	}
	
	@SuppressWarnings("deprecation")
	@Provides @Singleton
	Jongo provideJongo(Mongo mongo, Config config){
		String dbName = config.getString("imirp.db.name");
		logger.debug("Using database[" + dbName + "]");
		DB db = mongo.getDB(dbName);
		logger.debug("Authenticating...");
		if(config.hasPath("imirp.db.user")){
			if(!db.authenticate(config.getString("imirp.db.user"), config.getString("imirp.db.password").toCharArray())){
				logger.error("Unable to authenticate to mongo database.");
				return null;
			}
		}
		return new Jongo(db);
	}
}

@@ -704,50 +704,8 @@
 %0D%0A%0D%0A
-import javax.net.ssl.SSLSocketFactory;%0D%0A%0D%0A
 impo
@@ -879,59 +879,8 @@
 n;%0D%0A
-import com.mongodb.DB;%0D%0Aimport com.mongodb.Mongo;%0D%0A
 impo
@@ -942,50 +942,11 @@
 ient
-Options;%0D%0Aimport com.mongodb.ServerAddress
+URI
 ;%0D%0Ai
@@ -1242,24 +1242,33 @@
 eton%0D%0A%09Mongo
+ClientURI
  provideMong
@@ -1268,16 +1268,19 @@
 ideMongo
+URI
 (Config 
@@ -1319,119 +1319,59 @@
 ion%7B
+%09
 %0D%0A
-%09%09// Read the db connection properties%0D%0A%09%09String dbHost = config.getString(%22imirp.db.host%22);%0D%0A%09%09int dbPort = 
+        MongoClientURI uri = new MongoClientURI(
 conf
@@ -1380,11 +1380,14 @@
 .get
-Int
+String
 (%22im
@@ -1397,140 +1397,62 @@
 .db.
-port
+uri
 %22)
+)
 ;%0D%0A%09%09
-logger.info(%22Connecting to db at host%5B%22 + dbHost + %22%5D and port%5B%22 + dbPort + %22%5D%22);%09%09%0D%0A%09%09%0D%0A%09%09// Setup connection options
+return uri;%0D%0A%09%7D%0D%0A%09%0D%0A%09@Provides @Singleton
 %0D%0A
-%09
 %09Mon
@@ -1463,448 +1463,147 @@
 ient
-Options.Builder optionsBuilder = new MongoClientOptions.Builder();%0D%0A%09%09if(config.getBoolean(%22imirp.db.ssl_enabled%22))%7B%0D%0A%09%09%09logger.debug(%22Mongo SSL enabled.%22);%0D%0A%09%09%09optionsBuilder = optionsBuilder.socketFactory(SSLSocketFactory.getDefault());%0D%0A%09%09%7D%0D%0A        MongoClientOptions o = optionsBuilder.build();%0D%0A%09%09MongoClient mongo = new MongoClient(new ServerAddress(dbHost, dbPort), o);%0D%0A%09%09%0D%0A%09%09return mongo;%0D%0A%09%7D%0D%0A%09%0D%0A%09@SuppressWarnings(%22deprecation%22)
+ provideMongo(MongoClientURI uri) throws UnknownHostException %7B%09%0D%0A        MongoClient mongo = new MongoClient(uri);%0D%0A%09%09return mongo;%0D%0A%09%7D%0D%0A%09
 %0D%0A%09@
@@ -1652,16 +1652,22 @@
 ongo
+Client
  mongo, 
 Conf
@@ -1662,29 +1662,34 @@
  mongo, 
-Config config
+MongoClientURI uri
 )%7B%0D%0A%09%09St
@@ -1706,40 +1706,24 @@
 e = 
-config.getString(%22imirp.db.name%22
+uri.getDatabase(
 );%0D%0A
@@ -1779,344 +1779,44 @@
 %0D%0A%09%09
-DB db = mongo.getDB(dbName);%0D%0A%09%09logger.debug(%22Authenticating...%22);%0D%0A%09%09if(config.hasPath(%22imirp.db.user%22))%7B%0D%0A%09%09%09if(!db.authenticate(config.getString(%22imirp.db.user%22), config.getString(%22imirp.db.password%22).toCharArray()))%7B%0D%0A%09%09%09%09logger.error(%22Unable to authenticate to mongo database.%22);%0D%0A%09%09%09%09return null;%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09return new Jongo(db
+return new Jongo(mongo.getDB(dbName)
 );%0D%0A
@@ -1820,10 +1820,8 @@
 ;%0D%0A%09%7D%0D%0A%7D
-%0D%0A

Use more conventional method for connecting to Mongo.