// Path: elasticservice/src/main/java/elasticsearch/searchservice/ESService.java
// @Service
// public class ESService {
//     private static final Logger logger = LoggerFactory.getLogger(ESService.class);
// 
//     private static String baseURL;
//     private static String searchProductURL;
//     private static String countProductURL;
// 
//     @Autowired
//     ElasticRestClient client;
// 
//     public ESService(String url) {
//         this.setBaseURL(url);
//         this.setSearchProductURL(url+"/storessearch/product/_search");
//         this.setCountProductURL(url+"/storessearch/product/_count");
//         logger.info("Initial ESService done, url: " + url);
//     }
// 
//     public void setBaseURL(String url) {
//         this.baseURL = url;
//     }
// 
//     public void setSearchProductURL(String searchURL) {
//         ESService.searchProductURL = searchURL;
//     }
// 
//     public static void setCountProductURL(String countProductURL) {
//         ESService.countProductURL = countProductURL;
//     }
// 
// //    public RestResponse postSearch(byte[] body) {
// //        // only return ids
// //        map.put("_source", "id");
// //        try {
// //           return client.post(searchProductURL, body, map);
// //        } catch (RestClientException e) {
// //            //logger.warn("DO search failed " + e);
// //            return null;
// //        }
// //    }
// //
// //    public RestResponse postSearchCount(byte[] body) {
// //        try {
// //            return client.post(countProductURL, body, map);
// //        } catch (RestClientException e) {
// //            //logger.warn("DO search failed " + e);
// //            return null;
// //        }
// //    }
// }


import elasticsearch.searchservice.ESService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

package elasticsearch.searchservice.config;




/**
 * Created by I311352 on 10/18/2016.
 */
@Configuration
public class ElasticServiceConfig extends WebMvcConfigurerAdapter {
    public static final String LOCAL_CONFIG_PATH = "META-INF/resources/es.properties";
    public static final String PRODUCTION_CONFIG_PATH = "/etc/secrets/es/es.properties";
    private static final Logger logger = LoggerFactory.getLogger(ElasticServiceConfig.class);

    @Bean
    public ESConnectionConfig esConnectionConfig(){
        Properties properties = loadProperties(LOCAL_CONFIG_PATH, PRODUCTION_CONFIG_PATH);

        ESConnectionConfig config = new ESConnectionConfig();
        config.setEsDatabase(StringUtils.trimToNull(properties.getProperty("ES_DATABASE")));
        config.setEsHost(StringUtils.trimToNull(properties.getProperty("ES_HOST")));
        config.setEsPort(Integer.parseInt(StringUtils.trimToNull(properties.getProperty("ES_PORT"))));
        return config;
    }

    @Bean(name = {"ESService"})
    @Primary
    public ESService elasticRestClient(ESConnectionConfig config) {

        String host = "http://" + config.getEsHost() + ":" + config.getEsPort();

// Path: elasticservice/src/main/java/elasticsearch/searchservice/ESService.java
// @Service
// public class ESService {
//     private static final Logger logger = LoggerFactory.getLogger(ESService.class);
// 
//     private static String baseURL;
//     private static String searchProductURL;
//     private static String countProductURL;
// 
//     @Autowired
//     ElasticRestClient client;
// 
//     public ESService(String url) {
//         this.setBaseURL(url);
//         this.setSearchProductURL(url+"/storessearch/product/_search");
//         this.setCountProductURL(url+"/storessearch/product/_count");
//         logger.info("Initial ESService done, url: " + url);
//     }
// 
//     public void setBaseURL(String url) {
//         this.baseURL = url;
//     }
// 
//     public void setSearchProductURL(String searchURL) {
//         ESService.searchProductURL = searchURL;
//     }
// 
//     public static void setCountProductURL(String countProductURL) {
//         ESService.countProductURL = countProductURL;
//     }
// 
// //    public RestResponse postSearch(byte[] body) {
// //        // only return ids
// //        map.put("_source", "id");
// //        try {
// //           return client.post(searchProductURL, body, map);
// //        } catch (RestClientException e) {
// //            //logger.warn("DO search failed " + e);
// //            return null;
// //        }
// //    }
// //
// //    public RestResponse postSearchCount(byte[] body) {
// //        try {
// //            return client.post(countProductURL, body, map);
// //        } catch (RestClientException e) {
// //            //logger.warn("DO search failed " + e);
// //            return null;
// //        }
// //    }
// }


// Path: elasticservice/src/main/java/elasticsearch/searchservice/config/ElasticServiceConfig.java
import elasticsearch.searchservice.ESService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

package elasticsearch.searchservice.config;




/**
 * Created by I311352 on 10/18/2016.
 */
@Configuration
public class ElasticServiceConfig extends WebMvcConfigurerAdapter {
    public static final String LOCAL_CONFIG_PATH = "META-INF/resources/es.properties";
    public static final String PRODUCTION_CONFIG_PATH = "/etc/secrets/es/es.properties";
    private static final Logger logger = LoggerFactory.getLogger(ElasticServiceConfig.class);

    @Bean
    public ESConnectionConfig esConnectionConfig(){
        Properties properties = loadProperties(LOCAL_CONFIG_PATH, PRODUCTION_CONFIG_PATH);

        ESConnectionConfig config = new ESConnectionConfig();
        config.setEsDatabase(StringUtils.trimToNull(properties.getProperty("ES_DATABASE")));
        config.setEsHost(StringUtils.trimToNull(properties.getProperty("ES_HOST")));
        config.setEsPort(Integer.parseInt(StringUtils.trimToNull(properties.getProperty("ES_PORT"))));
        return config;
    }

    @Bean(name = {"ESService"})
    @Primary
    public ESService elasticRestClient(ESConnectionConfig config) {

        String host = "http://" + config.getEsHost() + ":" + config.getEsPort();

        ESService esService = new ESService(host);
