// Path: connector-elasticsearch/src/main/java/com/stratio/connector/elasticsearch/core/configuration/ConfigurationOptions.java
// public enum ConfigurationOptions {
// 
//     /**
//      * The elasticserach node type property.
//      */
//     NODE_TYPE("node_type", "node_type", Constants.FALSE),
//     /**
//      * The elasticserach node data property.
//      */
//     NODE_DATA("node.data", "node.data",Constants.FALSE),
//     /**
//      * The elasticserach node master property.
//      */
//     NODE_MASTER("node.master", "node.master",Constants.FALSE),
//     /**
//      * The elasticserach transfer sniff type property.
//      */
//     TRANSPORT_SNIFF("client.transport.sniff", "client.transport.sniff",Constants.TRUE),
//     /**
//      * The elastichseach cluser name.
//      */
//     CLUSTER_NAME("Cluster Name","cluster.name","", "clusterName"),
//     /**
//      * The hosts ip.
//      */
//     HOST("Hosts","Hosts", new String[] { "localhost" }),
//     /**
//      * The hosts ports.
//      */
//     PORT("Native Ports", "Native Ports",new String[] { "c" }),
//     /**
//      * The elasticsearch coerce property.
//      */
//     COERCE("index.mapping.coerce","index.mapping.coerce", Constants.FALSE),
//     /**
//      * The elasticsearch mapper dynamic property.
//      */
//     DYNAMIC("index.mapper.dynamic", "index.mapper.dynamic",Constants.FALSE);
// 
//     /**
//      * The name of the option in crossdata manifest.
//      */
//     private final String manifestOption;
//     /**
//      * The default value of the options.
//      */
//     private final String[] defaultValue;
//     /**
//      * The option name in elasticsearch.
//      */
//     private final String elasticSearchOption;
// 
//     /**
//      * Constructor.
//      *
//      * @param manifestOption   the name of the option.
//      * @param defaultValue the default value of the option.
//      */
//     ConfigurationOptions(String manifestOption, String elasticSearchOption, String... defaultValue) {
//         this.manifestOption = manifestOption;
//         this.defaultValue = defaultValue;
//         this.elasticSearchOption = elasticSearchOption;
// 
//     }
// 
//     /**
//      * return the default value.
//      *
//      * @return the default value.
//      */
//     public String[] getDefaultValue() {
//         return defaultValue.clone();
//     }
// 
//     /**
//      * Return the option name defined in the manifest.
//      *
//      * @return the option name.
//      */
//     public String getManifestOption() {
//         return manifestOption;
//     }
// 
// 
// 
//     /**
//      * Return the option name to ElasticSearch.
//      *
//      * @return the option name.
//      */
//     public String getElasticSearchOption() {
//         return elasticSearchOption;
//     }
// 
// }


import com.stratio.connector.commons.TimerJ;
import com.stratio.connector.commons.connection.Connection;
import com.stratio.connector.commons.connection.ConnectionHandler;
import com.stratio.connector.elasticsearch.core.configuration.ConfigurationOptions;
import com.stratio.crossdata.common.connector.ConnectorClusterConfig;
import com.stratio.crossdata.common.connector.IConfiguration;
import com.stratio.crossdata.common.exceptions.ConnectionException;
import com.stratio.crossdata.common.exceptions.ExecutionException;
import com.stratio.crossdata.common.security.ICredentials;
/*
 * Licensed to STRATIO (C) under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.  The STRATIO (C) licenses this file
 * to you 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 com.stratio.connector.elasticsearch.core.connection;


/**
 * This class represents a elasticsearchs connetions handler.
 * Created by jmgomez on 28/08/14.
 */
public class ElasticSearchConnectionHandler extends ConnectionHandler {

    /**
     * Constructor.
     *
     * @param configuration the configuration.
     */
    public ElasticSearchConnectionHandler(IConfiguration configuration) {
        super(configuration);
    }

    /**
     * This method creates a elasticsearch connection.
     *
     * @param iCredentials           the credentials to connect with the database.
     * @param connectorClusterConfig the cluster configuration.
     * @throws ConnectionException if the connection is not established.
     * @return a elasticsearch connection.
     */
    @Override
    @TimerJ
    protected Connection createNativeConnection(ICredentials iCredentials,
            ConnectorClusterConfig connectorClusterConfig) throws ConnectionException {
        Connection connection;
        if (isNodeClient(connectorClusterConfig)) {
            connection = new NodeConnection(iCredentials, connectorClusterConfig);
        }
        else {
            try {connection = new TransportConnection(iCredentials, connectorClusterConfig);}
            catch (ExecutionException exception){
                throw new ConnectionException("The connection could not be established", exception);
            }
        }
        if (!connection.isConnected()) {
             throw new ConnectionException("The connection could not be established");}
        return connection;
    }

    /**
     * Return true if the config says that the connection is nodeClient. false in other case.
     *
     * @param config the configuration.
     * @return true if is configure to be a node connection. False in other case.
     */
    @TimerJ
    private boolean isNodeClient(ConnectorClusterConfig config) {
// Path: connector-elasticsearch/src/main/java/com/stratio/connector/elasticsearch/core/configuration/ConfigurationOptions.java
// public enum ConfigurationOptions {
// 
//     /**
//      * The elasticserach node type property.
//      */
//     NODE_TYPE("node_type", "node_type", Constants.FALSE),
//     /**
//      * The elasticserach node data property.
//      */
//     NODE_DATA("node.data", "node.data",Constants.FALSE),
//     /**
//      * The elasticserach node master property.
//      */
//     NODE_MASTER("node.master", "node.master",Constants.FALSE),
//     /**
//      * The elasticserach transfer sniff type property.
//      */
//     TRANSPORT_SNIFF("client.transport.sniff", "client.transport.sniff",Constants.TRUE),
//     /**
//      * The elastichseach cluser name.
//      */
//     CLUSTER_NAME("Cluster Name","cluster.name","", "clusterName"),
//     /**
//      * The hosts ip.
//      */
//     HOST("Hosts","Hosts", new String[] { "localhost" }),
//     /**
//      * The hosts ports.
//      */
//     PORT("Native Ports", "Native Ports",new String[] { "c" }),
//     /**
//      * The elasticsearch coerce property.
//      */
//     COERCE("index.mapping.coerce","index.mapping.coerce", Constants.FALSE),
//     /**
//      * The elasticsearch mapper dynamic property.
//      */
//     DYNAMIC("index.mapper.dynamic", "index.mapper.dynamic",Constants.FALSE);
// 
//     /**
//      * The name of the option in crossdata manifest.
//      */
//     private final String manifestOption;
//     /**
//      * The default value of the options.
//      */
//     private final String[] defaultValue;
//     /**
//      * The option name in elasticsearch.
//      */
//     private final String elasticSearchOption;
// 
//     /**
//      * Constructor.
//      *
//      * @param manifestOption   the name of the option.
//      * @param defaultValue the default value of the option.
//      */
//     ConfigurationOptions(String manifestOption, String elasticSearchOption, String... defaultValue) {
//         this.manifestOption = manifestOption;
//         this.defaultValue = defaultValue;
//         this.elasticSearchOption = elasticSearchOption;
// 
//     }
// 
//     /**
//      * return the default value.
//      *
//      * @return the default value.
//      */
//     public String[] getDefaultValue() {
//         return defaultValue.clone();
//     }
// 
//     /**
//      * Return the option name defined in the manifest.
//      *
//      * @return the option name.
//      */
//     public String getManifestOption() {
//         return manifestOption;
//     }
// 
// 
// 
//     /**
//      * Return the option name to ElasticSearch.
//      *
//      * @return the option name.
//      */
//     public String getElasticSearchOption() {
//         return elasticSearchOption;
//     }
// 
// }


// Path: connector-elasticsearch/src/main/java/com/stratio/connector/elasticsearch/core/connection/ElasticSearchConnectionHandler.java
import com.stratio.connector.commons.TimerJ;
import com.stratio.connector.commons.connection.Connection;
import com.stratio.connector.commons.connection.ConnectionHandler;
import com.stratio.connector.elasticsearch.core.configuration.ConfigurationOptions;
import com.stratio.crossdata.common.connector.ConnectorClusterConfig;
import com.stratio.crossdata.common.connector.IConfiguration;
import com.stratio.crossdata.common.exceptions.ConnectionException;
import com.stratio.crossdata.common.exceptions.ExecutionException;
import com.stratio.crossdata.common.security.ICredentials;

/*
 * Licensed to STRATIO (C) under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.  The STRATIO (C) licenses this file
 * to you 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 com.stratio.connector.elasticsearch.core.connection;


/**
 * This class represents a elasticsearchs connetions handler.
 * Created by jmgomez on 28/08/14.
 */
public class ElasticSearchConnectionHandler extends ConnectionHandler {

    /**
     * Constructor.
     *
     * @param configuration the configuration.
     */
    public ElasticSearchConnectionHandler(IConfiguration configuration) {
        super(configuration);
    }

    /**
     * This method creates a elasticsearch connection.
     *
     * @param iCredentials           the credentials to connect with the database.
     * @param connectorClusterConfig the cluster configuration.
     * @throws ConnectionException if the connection is not established.
     * @return a elasticsearch connection.
     */
    @Override
    @TimerJ
    protected Connection createNativeConnection(ICredentials iCredentials,
            ConnectorClusterConfig connectorClusterConfig) throws ConnectionException {
        Connection connection;
        if (isNodeClient(connectorClusterConfig)) {
            connection = new NodeConnection(iCredentials, connectorClusterConfig);
        }
        else {
            try {connection = new TransportConnection(iCredentials, connectorClusterConfig);}
            catch (ExecutionException exception){
                throw new ConnectionException("The connection could not be established", exception);
            }
        }
        if (!connection.isConnected()) {
             throw new ConnectionException("The connection could not be established");}
        return connection;
    }

    /**
     * Return true if the config says that the connection is nodeClient. false in other case.
     *
     * @param config the configuration.
     * @return true if is configure to be a node connection. False in other case.
     */
    @TimerJ
    private boolean isNodeClient(ConnectorClusterConfig config) {
        return Boolean.parseBoolean(config.getConnectorOptions().get(ConfigurationOptions.NODE_TYPE.getManifestOption()));