package uk.ac.ebi.atlas.solr.cloud;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrInputDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;

// abstract so that clients are forced to use specific collections
public abstract class CollectionProxy {
    private static final Logger LOGGER = LoggerFactory.getLogger(CollectionProxy.class);

    public final SolrClient solrClient;
    public final String nameOrAlias;

    protected CollectionProxy(SolrClient solrClient, String nameOrAlias) {
        this.solrClient = solrClient;
        this.nameOrAlias = nameOrAlias;
    }

    public QueryResponse query(SolrQuery solrQuery) {
        try {
            // Change maybe to: return new QueryRequest()
            return solrClient.query(nameOrAlias, solrQuery);
        } catch (IOException e) {
            logException(e);
            throw new UncheckedIOException(e);
        } catch (SolrServerException e) {
            logException(e);
            throw new UncheckedIOException(new IOException(e));
        }
    }

    public UpdateResponse addAndCommit(Collection<SolrInputDocument> docs) {
        try {
            return new UpdateRequest().add(docs).commit(solrClient, nameOrAlias);
        } catch (IOException e) {
            logException(e);
            throw new UncheckedIOException(e);
        } catch (SolrServerException e) {
            logException(e);
            throw new UncheckedIOException(new IOException(e));
        }
    }

    public UpdateResponse deleteAllAndCommit() {
        try {
            return new UpdateRequest().deleteByQuery("*:*").commit(solrClient, nameOrAlias);
        } catch (IOException e) {
            logException(e);
            throw new UncheckedIOException(e);
        } catch (SolrServerException e) {
            logException(e);
            throw new UncheckedIOException(new IOException(e));
        }
    }

    private void logException(Exception e) {
        LOGGER.error(
                "Problem connecting to SolrCloud {} with collection {}, full stack trace follows:\n\t{}",
                solrClient.getClass().getSimpleName(),
                nameOrAlias,
                Arrays.stream(e.getStackTrace()).map(StackTraceElement::toString).collect(Collectors.joining("\n\t")));
    }
}

@@ -118,21 +118,23 @@
 lrj.Solr
-Query
+Request
 ;%0Aimport
@@ -409,24 +409,73 @@
 utDocument;%0A
+import org.apache.solr.common.params.SolrParams;%0A
 import org.s
@@ -1150,23 +1150,25 @@
 Solr
-Query solrQuery
+Params solrParams
 ) %7B%0A
@@ -1292,21 +1292,47 @@
 as, solr
-Query
+Params, SolrRequest.METHOD.POST
 );%0A     

Remove query(SolrQuery solrQuery) as it’s redundant: SolrQuery is a subclass of SolrParams