package uk.ac.ebi.atlas.search.analyticsindex;

import com.google.common.collect.ImmutableSet;
import org.apache.log4j.Logger;
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.response.QueryResponse;
import org.apache.solr.common.SolrException;
import org.springframework.beans.factory.annotation.Qualifier;
import uk.ac.ebi.atlas.solr.SolrUtil;
import uk.ac.ebi.atlas.web.GeneQuery;

import javax.inject.Inject;
import javax.inject.Named;
import java.io.IOException;

@Named
public class AnalyticsSearchDao {
    public static final String ABOVE_CUTOFF = "(((experimentType:rnaseq_mrna_baseline OR experimentType:proteomics_baseline) AND expressionLevel:[0.5 TO *]) OR ((experimentType:rnaseq_mrna_differential OR experimentType:microarray_1colour_mrna_differential or experimentType:microarray_2colour_mrna_differential or experimentType:microarray_1colour_microrna_differential) AND foldChange:[1.0 TO *]))";

    private static final Logger LOGGER = Logger.getLogger(AnalyticsSearchDao.class);

    private SolrClient analyticsSolrClient;

    @Inject
    public AnalyticsSearchDao(@Qualifier("analyticsSolrClient") SolrClient analyticsSolrClient) {
        this.analyticsSolrClient = analyticsSolrClient;
    }


    public ImmutableSet<String> fetchExperimentTypes(GeneQuery geneQuery) {

        QueryResponse queryResponse = query(buildQuery(geneQuery));

        ImmutableSet<String> experimentTypes = SolrUtil.extractFirstFacetValues(queryResponse);

        return experimentTypes;
    }

    public QueryResponse query(SolrQuery solrQuery) {
        try {
            QueryResponse queryResponse = analyticsSolrClient.query(solrQuery);
            //LOGGER.debug("<query> Solr query time: " + queryResponse.getQTime() + "ms, status code: " + queryResponse.getStatus());
            return queryResponse;
        } catch (SolrServerException | IOException e) {
            LOGGER.error(e.getMessage(), e);
            throw new SolrException(SolrException.ErrorCode.UNKNOWN, e);
        }
    }

    private SolrQuery buildQuery(GeneQuery geneQuery) {
        String identifierSearch = geneQuery.asString(); //TODO: support multiple gene query terms
        SolrQuery solrQuery = new SolrQuery("identifierSearch:" + identifierSearch);
        solrQuery.setRows(0);
        solrQuery.setFilterQueries(ABOVE_CUTOFF);
        solrQuery.setFacet(true);
        solrQuery.addFacetField("experimentType");
        solrQuery.setFacetMinCount(1);
        return solrQuery;
    }

}

@@ -690,17 +690,68 @@
 OFF = %22(
-(
+%22 +%0A                                               %22
 (experim
@@ -750,32 +750,33 @@
 (experimentType:
+(
 rnaseq_mrna_base
@@ -783,31 +783,16 @@
 line OR 
-experimentType:
 proteomi
@@ -839,17 +839,68 @@
  *%5D) OR 
-(
+%22 +%0A                                               %22
 (experim
@@ -907,16 +907,17 @@
 entType:
+(
 rnaseq_m
@@ -936,31 +936,16 @@
 tial OR 
-experimentType:
 microarr
@@ -973,34 +973,19 @@
 rential 
-or experimentType:
+OR 
 microarr
@@ -1017,26 +1017,11 @@
 ial 
-or experimentType:
+OR 
 micr
@@ -1084,16 +1084,67 @@
 0 TO *%5D)
+%22 +%0A                                              %22
 )%22;%0A%0A   
@@ -1603,46 +1603,14 @@
     
-ImmutableSet%3CString%3E experimentTypes =
+return
  Sol
@@ -1658,41 +1658,8 @@
 se);
-%0A%0A        return experimentTypes;
 %0A   
@@ -1746,37 +1746,14 @@
     
-QueryResponse queryResponse =
+return
  ana
@@ -1791,176 +1791,8 @@
 y);%0A
-            //LOGGER.debug(%22%3Cquery%3E Solr query time: %22 + queryResponse.getQTime() + %22ms, status code: %22 + queryResponse.getStatus());%0A            return queryResponse;%0A
     

Clean up default search query syntax and remove unneeded local variables