package com.rtr.alchemy.example.config;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.net.HostAndPort;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.rtr.alchemy.db.ExperimentsStoreProvider;
import com.rtr.alchemy.service.config.StoreProviderConfiguration;

import javax.validation.constraints.NotNull;
import java.net.UnknownHostException;
import java.util.List;

/**
 * Configuration object for creating a MongoDB provider with given parameters
 */
public class MongoStoreProvider extends StoreProviderConfiguration {
    @NotNull
    private final List<HostAndPort> hosts;

    @NotNull
    private final String db;

    private final String username;

    private final String pasword;

    @JsonCreator
    public MongoStoreProvider(@JsonProperty("hosts") List<HostAndPort> hosts,
                              @JsonProperty("db") String db,
                              @JsonProperty("username") String username,
                              @JsonProperty("password") String password) {
        this.hosts = hosts;
        this.db = db;
        this.username = username;
        this.pasword = password;
    }

    public List<HostAndPort> getHosts() {
        return hosts;
    }

    public String getDb() {
        return db;
    }

    public String getUsername() {
        return username;
    }

    public String getPasword() {
        return pasword;
    }

    @Override
    public ExperimentsStoreProvider createProvider() throws UnknownHostException {
        final com.rtr.alchemy.db.mongo.MongoStoreProvider.Builder builder = com.rtr.alchemy.db.mongo.MongoStoreProvider.newBuilder();
        for (final HostAndPort host : hosts) {
            if (!host.hasPort()) {
                builder.addHost(new ServerAddress(host.getHostText()));
            } else {
                builder.addHost(new ServerAddress(host.getHostText(), host.getPort()));
            }
        }

        if (username != null) {
            builder.addCredential(MongoCredential.createPlainCredential(username, db, pasword.toCharArray()));
        }

        builder.setDatabase(db);

        return builder.build();
    }
}
@@ -798,22 +798,23 @@
 ring pas
+s
 word;%0A
-
 %0A    @Js
@@ -1210,16 +1210,17 @@
 this.pas
+s
 word = p
@@ -1450,16 +1450,17 @@
 g getPas
+s
 word() %7B
@@ -1474,24 +1474,25 @@
   return pas
+s
 word;%0A    %7D%0A
@@ -2039,24 +2039,24 @@
  != null) %7B%0A
-
             
@@ -2132,16 +2132,17 @@
  db, pas
+s
 word.toC

fix typo