package net.explorviz.landscape.repository.persistence;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoDatabase;
import net.explorviz.shared.annotations.Config;

public class MongoHelper {

  private static final String LANDSCAPE_COLLECTION = "landscape";
  private static MongoClient client = null;

  @Config("mongo.host")
  private String host;

  @Config("mongo.port")
  private String port;

  @Config("mongo.db")
  private String dbName;



  /*
   * From mongo documentation: "It is important to limit the number of MongoClient instances in your
   * application, hence why we suggest a singleton - the MongoClient is effectively the connection
   * pool, so for every new MongoClient, you are opening a new pool."
   */

  private MongoHelper() {
    if (client == null) {
      MongoHelper.client = new MongoClient(new MongoClientURI(this.getUri()));
    }
  }

  /**
   * Returns a {@link MongoClient} which is connected to the database given in the configuration
   * file. This method is private in order to prevent unintentional closing of alls connections.
   *
   * @see #close()
   */
  private MongoClient getClient() {
    return MongoHelper.client;
  }

  /**
   * Returns the {@link MongoDatabase} given in the configuration file.
   *
   */
  public DB getDatabase() {
    return this.getClient().getDB(this.dbName);
  }

  /**
   * Returns a connection to the landscape collection.
   */
  public DBCollection getLandscapeCollection() {
    return this.getDatabase().getCollection(LANDSCAPE_COLLECTION);
  }

  private String getUri() {
    return this.host + ':' + this.port;
  }

  /**
   * Closes all resources associated with this instance, in particular any open network connections.
   * Once called, this instance and any databases obtained from it can no longer be used.
   */
  public void close() {
    this.getClient().close();
  }


}

@@ -261,16 +261,73 @@
 .Config;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Apublic
@@ -382,43 +382,313 @@
 ing 
-LANDSCAPE_COLLECTION = %22landscape%22;
+DEFAULT_HOST = %22localhost%22;%0A  private static final String DEFAULT_PORT = %2227018%22;%0A  private static final String DEFAULT_DB = %22explorviz%22;%0A  private static final String LANDSCAPE_COLLECTION = %22landscape%22;%0A%0A  private static final Logger LOGGER = LoggerFactory.getLogger(MongoHelper.class.getSimpleName());%0A%0A
 %0A  p
@@ -729,16 +729,18 @@
  null;%0A%0A
+%0A%0A
   @Confi
@@ -1716,24 +1716,262 @@
 atabase() %7B%0A
+%0A    String dbName = this.dbName;%0A%0A    if (dbName == null %7C%7C %22%22.equals(dbName)) %7B%0A      if (LOGGER.isWarnEnabled()) %7B%0A        LOGGER.warn(%22No database name given, falling back to %22 + DEFAULT_DB);%0A      %7D%0A      dbName = DEFAULT_DB;%0A    %7D%0A%0A
     return t
@@ -1992,21 +1992,16 @@
 ).getDB(
-this.
 dbName);
@@ -2222,32 +2222,500 @@
 Uri() %7B%0A
+%0A
     
-return this.
+String host = this.host;%0A    String port = this.port;%0A%0A    if (host == null %7C%7C %22%22.equals(host)) %7B%0A      if (LOGGER.isWarnEnabled()) %7B%0A        LOGGER.warn(%22No host configured, falling back to %22 + DEFAULT_HOST);%0A        host = DEFAULT_HOST;%0A      %7D%0A    %7D%0A%0A    if (port == null %7C%7C %22%22.equals(port)) %7B%0A      if (LOGGER.isWarnEnabled()) %7B%0A        LOGGER.warn(%22No port configured, falling back to %22 + DEFAULT_PORT);%0A        port = DEFAULT_PORT;%0A      %7D%0A    %7D%0A%0A    return %22mongodb://%22 + 
 host + '
@@ -2719,21 +2719,16 @@
 + ':' + 
-this.
 port;%0A  

Add fallback options for mongo connection