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

import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.context.annotation.Scope;
import uk.ac.ebi.atlas.web.GeneQuery;
import uk.ac.ebi.atlas.web.SemanticQueryTerm;

import javax.inject.Named;

import java.util.ArrayList;

import static org.apache.commons.lang3.StringUtils.wrap;

@Named
@Scope("prototype")
public class AnalyticsQueryBuilder {
    private static final String BASELINE_ABOVE_CUTOFF = "(experimentType:(rnaseq_mrna_baseline OR proteomics_baseline))";
    private static final String DIFFERENTIAL_ABOVE_CUTOFF =
            "(experimentType:(rnaseq_mrna_differential OR microarray_1colour_mrna_differential OR microarray_2colour_mrna_differential OR microarray_1colour_microrna_differential) " +
            "AND pValue:[* TO 0.05])";

    private static final String IDENTIFIER_SEARCH_VALUE_TEMPLATE = "%s:{%s}";

    private static final String EXPERIMENT_TYPE_FIELD = "experimentType";
    private static final String BIOENTITY_IDENTIFIER_FIELD = "bioentityIdentifier";
    private static final String IDENTIFIER_SEARCH_FIELD = "identifierSearch";
    private static final String SPECIES_FIELD = "species";

    private ArrayList<String> identifierSearchTerms = new ArrayList<>();
    private ArrayList<String> bioentityIdentifierTerms = new ArrayList<>();
    private ArrayList<String> speciesTerms = new ArrayList<>();

    private SolrQuery solrQuery = new SolrQuery();

    public AnalyticsQueryBuilder facetByExperimentType() {
        solrQuery.setFacet(true);
        solrQuery.setFacetMinCount(1);
        solrQuery.addFacetField(EXPERIMENT_TYPE_FIELD);
        return this;
    }


    public AnalyticsQueryBuilder facetByBioentityIdentifier() {
        solrQuery.setFacet(true);
        solrQuery.setFacetMinCount(1);
        solrQuery.addFacetField(BIOENTITY_IDENTIFIER_FIELD);
        return this;
    }


    public AnalyticsQueryBuilder setRows(int rows) {
        solrQuery.setRows(rows);
        return this;
    }


    public AnalyticsQueryBuilder queryIdentifierSearch(GeneQuery geneQuery) {
        ImmutableList.Builder<String> builder = ImmutableList.builder();
        for (SemanticQueryTerm queryTerm : geneQuery) {
            builder.add(queryTerm.toString());
        }
        identifierSearchTerms.addAll(builder.build());

        return this;
    }


//    public AnalyticsQueryBuilder queryIdentifierSearch(GeneQuery semanticQuery) {
//        ImmutableList.Builder<String> builder = ImmutableList.builder();
//        for (SemanticQueryTerm term : semanticQuery) {
//            if (term.hasValue()) {
//                if (term.hasNoCategory()) {
//                    builder.add(wrap(term.value(), '"'));
//                } else {
//                    builder.add(wrap(String.format(IDENTIFIER_SEARCH_VALUE_TEMPLATE, term.category(), term.value()), '"'));
//                }
//            }
//        }
//        identifierSearchTerms.addAll(builder.build());
//
//        return this;
//    }


    public AnalyticsQueryBuilder queryBioentityIdentifier(String identifier) {
        if (!Strings.isNullOrEmpty(identifier)) {
            bioentityIdentifierTerms.add(wrap(identifier, '"'));
        }
        return this;
    }


    public AnalyticsQueryBuilder ofSpecies(String species) {
        if (!Strings.isNullOrEmpty(species)) {
            speciesTerms.add(wrap(species, '"'));
        }
        return this;
    }


    public AnalyticsQueryBuilder filterAboveDefaultCutoff() {
        solrQuery.addFilterQuery(BASELINE_ABOVE_CUTOFF + " OR " + DIFFERENTIAL_ABOVE_CUTOFF);
        return this;
    }


    public SolrQuery build() {
        StringBuilder stringBuilder = new StringBuilder();
        Joiner joinerOr = Joiner.on(" OR ");

        if (identifierSearchTerms.size() == 0 && bioentityIdentifierTerms.size() == 0) {
            stringBuilder.append(BIOENTITY_IDENTIFIER_FIELD).append(":*");
        }

        if (identifierSearchTerms.size() > 0) {
            stringBuilder.append(IDENTIFIER_SEARCH_FIELD).append(":(").append(joinerOr.join(identifierSearchTerms)).append(")");
        }

        if (bioentityIdentifierTerms.size() > 0) {
            if (stringBuilder.length() > 0) {
                stringBuilder.append(" OR ");
            }
            stringBuilder.append(BIOENTITY_IDENTIFIER_FIELD).append(":(").append(joinerOr.join(bioentityIdentifierTerms)).append(")");
        }

        if (speciesTerms.size() > 0) {
            if (stringBuilder.length() > 0) {
                stringBuilder.append(" AND ");
            }
            stringBuilder.append(SPECIES_FIELD).append(":(").append(joinerOr.join(speciesTerms)).append(")");
        }

        solrQuery.setQuery(stringBuilder.toString());
        solrQuery.setFacetLimit(-1);
        return solrQuery;
    }
}

@@ -1332,65 +1332,46 @@
 ate 
-ArrayList%3CString%3E identifierSearchTerms = new ArrayList%3C%3E
+GeneQuery geneQuery = GeneQuery.create
 ();%0A
@@ -1559,24 +1559,25 @@
 lrQuery();%0A%0A
+%0A
     public A
@@ -2213,893 +2213,62 @@
     
-ImmutableList.Builder%3CString%3E builder = ImmutableList.builder();%0A        for (SemanticQueryTerm queryTerm : geneQuery) %7B%0A            builder.add(queryTerm.toString());%0A        %7D%0A        identifierSearchTerms.addAll(builder.build());%0A%0A        return this;%0A    %7D%0A%0A%0A//    public AnalyticsQueryBuilder queryIdentifierSearch(GeneQuery semanticQuery) %7B%0A//        ImmutableList.Builder%3CString%3E builder = ImmutableList.builder();%0A//        for (SemanticQueryTerm term : semanticQuery) %7B%0A//            if (term.hasValue()) %7B%0A//                if (term.hasNoCategory()) %7B%0A//                    builder.add(wrap(term.value(), '%22'));%0A//                %7D else %7B%0A//                    builder.add(wrap(String.format(IDENTIFIER_SEARCH_VALUE_TEMPLATE, term.category(), term.value()), '%22'));%0A//                %7D%0A//            %7D%0A//        %7D%0A//        identifierSearchTerms.addAll(builder.build());%0A//%0A//
+this.geneQuery = GeneQuery.create(geneQuery.terms());%0A
     
@@ -2288,10 +2288,8 @@
 is;%0A
-//
     
@@ -3059,41 +3059,27 @@
 if (
-identifierSearchTerms.size() == 0
+geneQuery.isEmpty()
  && 
@@ -3216,37 +3216,25 @@
     if (
-identifierSearchTerms
+geneQuery
 .size() 
@@ -3322,43 +3322,29 @@
 end(
-joinerOr.join(identifierSearchTerms
+geneQuery.asSolr1DNF(
 )).a

Use GeneQueyr’s own method to create the Solr query