// Path: elasticservice/src/main/java/elasticsearch/exception/ElasticAPIException.java
// public class ElasticAPIException extends RuntimeException {
//     public ElasticAPIException(String message) {
//         super(message);
//     }
// 
//     public ElasticAPIException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }
// 
// Path: elasticservice/src/main/java/elasticsearch/exception/ElasticQueryException.java
// public class ElasticQueryException extends RuntimeException {
//     public ElasticQueryException(String message) {
//         super(message);
//     }
// 
//     public ElasticQueryException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }
// 
// Path: elasticservice/src/main/java/elasticsearch/exception/ElasticVersionConflictException.java
// public class ElasticVersionConflictException extends RuntimeException {
//     public ElasticVersionConflictException(String message) {
//         super(message);
//     }
// 
//     public ElasticVersionConflictException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }


import com.google.gson.Gson;
import com.google.gson.JsonObject;
import elasticsearch.esapi.resp.*;
import elasticsearch.exception.ElasticAPIException;
import elasticsearch.exception.ElasticQueryException;
import elasticsearch.exception.ElasticVersionConflictException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
import org.apache.log4j.Logger;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

package elasticsearch.esapi;



/**
 * Created by I311352 on 10/3/2016.
 */

@Component
public class DocumentService {
    private static final Logger logger = Logger.getLogger(DocumentService.class);

    private final RestClient client;
    private final Gson gson;
    private final IndexService indexService;

    @Autowired
    public DocumentService(RestClient client, Gson gson, IndexService indexService) {
        this.client = client;
        this.gson = gson;
        this.indexService = indexService;
    }

    public boolean isDocExist(String index, String type, Long sourceId, HashMap param) {
        Map<String, String> params = addTenantId2Param(param);
        try {
            Response response = client.performRequest("HEAD", index + "/" + type + "/" + sourceId, params);
            int statusCode = response.getStatusLine().getStatusCode();

            if (statusCode == 200) {
                return true;
            } else if (statusCode == 404) {
                return false;
            }


// Path: elasticservice/src/main/java/elasticsearch/exception/ElasticAPIException.java
// public class ElasticAPIException extends RuntimeException {
//     public ElasticAPIException(String message) {
//         super(message);
//     }
// 
//     public ElasticAPIException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }
// 
// Path: elasticservice/src/main/java/elasticsearch/exception/ElasticQueryException.java
// public class ElasticQueryException extends RuntimeException {
//     public ElasticQueryException(String message) {
//         super(message);
//     }
// 
//     public ElasticQueryException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }
// 
// Path: elasticservice/src/main/java/elasticsearch/exception/ElasticVersionConflictException.java
// public class ElasticVersionConflictException extends RuntimeException {
//     public ElasticVersionConflictException(String message) {
//         super(message);
//     }
// 
//     public ElasticVersionConflictException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }


// Path: elasticservice/src/main/java/elasticsearch/esapi/DocumentService.java
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import elasticsearch.esapi.resp.*;
import elasticsearch.exception.ElasticAPIException;
import elasticsearch.exception.ElasticQueryException;
import elasticsearch.exception.ElasticVersionConflictException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
import org.apache.log4j.Logger;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

package elasticsearch.esapi;



/**
 * Created by I311352 on 10/3/2016.
 */

@Component
public class DocumentService {
    private static final Logger logger = Logger.getLogger(DocumentService.class);

    private final RestClient client;
    private final Gson gson;
    private final IndexService indexService;

    @Autowired
    public DocumentService(RestClient client, Gson gson, IndexService indexService) {
        this.client = client;
        this.gson = gson;
        this.indexService = indexService;
    }

    public boolean isDocExist(String index, String type, Long sourceId, HashMap param) {
        Map<String, String> params = addTenantId2Param(param);
        try {
            Response response = client.performRequest("HEAD", index + "/" + type + "/" + sourceId, params);
            int statusCode = response.getStatusLine().getStatusCode();

            if (statusCode == 200) {
                return true;
            } else if (statusCode == 404) {
                return false;
            }


            throw new ElasticQueryException("Exception for Doc Exist query");
