package uk.ac.ebi.quickgo.ontology.controller;

import uk.ac.ebi.quickgo.ontology.common.coterm.CoTerm;
import uk.ac.ebi.quickgo.ontology.common.coterm.CoTermLimit;
import uk.ac.ebi.quickgo.ontology.common.coterm.CoTermType;
import uk.ac.ebi.quickgo.ontology.common.document.OntologyType;
import uk.ac.ebi.quickgo.ontology.model.GOTerm;
import uk.ac.ebi.quickgo.ontology.model.OBOTerm;
import uk.ac.ebi.quickgo.ontology.service.OntologyService;
import uk.ac.ebi.quickgo.ontology.service.search.SearchServiceConfig;
import uk.ac.ebi.quickgo.rest.search.SearchService;
import uk.ac.ebi.quickgo.rest.search.SearchableField;
import uk.ac.ebi.quickgo.rest.search.results.QueryResult;

import com.google.common.base.Preconditions;
import io.swagger.annotations.ApiOperation;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * REST controller for accessing GO related information.
 *
 * For complete list of necessary endpoints, and their behaviour:
 *  refer to https://www.ebi.ac.uk/seqdb/confluence/display/GOA/REST+API
 *
 * Created 16/11/15
 * @author Edd
 */
@RestController
@RequestMapping(value = "/QuickGO/services/go")
public class GOController extends OBOController<GOTerm> {

    private static final Pattern GO_ID_FORMAT = Pattern.compile("^GO:[0-9]{7}$");
    private static final String COTERMS_RESOURCE = "coterms";

    private CoTermLimit coTermLimit;

    @Value("${coterm.default.limit:50}")
    private int defaultLimit;

    @Autowired
    public GOController(OntologyService<GOTerm> goOntologyService,
            SearchService<OBOTerm> ontologySearchService,
            SearchableField searchableField,
            SearchServiceConfig.OntologyCompositeRetrievalConfig ontologyRetrievalConfig) {
        super(goOntologyService, ontologySearchService, searchableField, ontologyRetrievalConfig);

        coTermLimit = new CoTermLimit(defaultLimit);
    }


    /**
     * Get co-occurring term information about a list of terms in comma-separated-value (CSV) format
     *
     * @param id ontology identifier
     * @param type whether manual only or all
     * @param limit Optional:  requires a String so the user can specify 'ALL' or a number.
     * @param similarityThreshold requires a String so the user can specify 'ALL' or a number
     * @return
     * <ul>
     *     <li>all ids are valid: response consists of a 200 with the chosen information about the ontology terms</li>
     *     <li>any id is not found: response returns 404</li>
     *     <li>any id is of the an invalid format: response returns 400</li>
     * </ul>
     */
    @ApiOperation(value = "Get co-occurring term information about a (CSV) list of terms based on their ids",
            notes = "If possible, response fields include: id, name, definition, probability ratio, similarity ratio," +
                    " together, compared.")
    @RequestMapping(value = TERMS_RESOURCE + "/{id}/" + COTERMS_RESOURCE, method = RequestMethod.GET,
            produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<QueryResult<CoTerm>> findCoTerms(@PathVariable(value = "id") String id,
            @RequestParam(value = "type", defaultValue = "ALL") String type,
            @RequestParam(value = "limit") String limit,
            @RequestParam(value = "similarityThreshold") int similarityThreshold) {

        Preconditions.checkArgument(similarityThreshold <0 || similarityThreshold>100, "The value for " +
                "similarityThreshold should be an integer between 0 and 100, not {}", similarityThreshold );

        return getResultsResponse(ontologyService.findCoTermsByOntologyId(id, CoTermType.valueOf(type),
                coTermLimit.workoutLimit(limit), similarityThreshold));
    }


    @Override
    public Predicate<String> idValidator() {
        return id -> GO_ID_FORMAT.matcher(id).matches();
    }

    @Override protected OntologyType getOntologyType() {
        return OntologyType.GO;
    }
}

@@ -70,158 +70,87 @@
 kgo.
-ontology.common.coterm.CoTerm;%0Aimport uk.ac.ebi.quickgo.ontology.common.coterm.CoTermLimit;%0Aimport uk.ac.ebi.quickgo.ontology.common.coterm.CoTermType
+rest.search.SearchService;%0Aimport uk.ac.ebi.quickgo.rest.search.SearchableField
 ;%0Aim
@@ -441,261 +441,8 @@
 ig;%0A
-import uk.ac.ebi.quickgo.rest.search.SearchService;%0Aimport uk.ac.ebi.quickgo.rest.search.SearchableField;%0Aimport uk.ac.ebi.quickgo.rest.search.results.QueryResult;%0A%0Aimport com.google.common.base.Preconditions;%0Aimport io.swagger.annotations.ApiOperation;
 %0Aimp
@@ -601,129 +601,42 @@
 ork.
-beans.factory.annotation.Value;%0Aimport org.springframework.http.MediaType;%0Aimport org.springframework.http.ResponseEntity
+web.bind.annotation.RequestMapping
 ;%0Aim
@@ -680,17 +680,30 @@
 otation.
-*
+RestController
 ;%0A%0A/**%0A 
@@ -1150,180 +1150,8 @@
 $%22);
-%0A    private static final String COTERMS_RESOURCE = %22coterms%22;%0A%0A    private CoTermLimit coTermLimit;%0A%0A    @Value(%22$%7Bcoterm.default.limit:50%7D%22)%0A    private int defaultLimit;
 %0A%0A  
@@ -1528,1912 +1528,13 @@
 g);%0A
-%0A        coTermLimit = new CoTermLimit(defaultLimit);%0A    %7D%0A%0A%0A    /**%0A     * Get co-occurring term information about a list of terms in comma-separated-value (CSV) format%0A     *%0A     * @param id ontology identifier%0A     * @param type whether manual only or all%0A     * @param limit Optional:  requires a String so the user can specify 'ALL' or a number.%0A     * @param similarityThreshold requires a String so the user can specify 'ALL' or a number%0A     * @return%0A     * %3Cul%3E%0A     *     %3Cli%3Eall ids are valid: response consists of a 200 with the chosen information about the ontology terms%3C/li%3E%0A     *     %3Cli%3Eany id is not found: response returns 404%3C/li%3E%0A     *     %3Cli%3Eany id is of the an invalid format: response returns 400%3C/li%3E%0A     * %3C/ul%3E%0A     */%0A    @ApiOperation(value = %22Get co-occurring term information about a (CSV) list of terms based on their ids%22,%0A            notes = %22If possible, response fields include: id, name, definition, probability ratio, similarity ratio,%22 +%0A                    %22 together, compared.%22)%0A    @RequestMapping(value = TERMS_RESOURCE + %22/%7Bid%7D/%22 + COTERMS_RESOURCE, method = RequestMethod.GET,%0A            produces = %7BMediaType.APPLICATION_JSON_VALUE%7D)%0A    public ResponseEntity%3CQueryResult%3CCoTerm%3E%3E findCoTerms(@PathVariable(value = %22id%22) String id,%0A            @RequestParam(value = %22type%22, defaultValue = %22ALL%22) String type,%0A            @RequestParam(value = %22limit%22) String limit,%0A            @RequestParam(value = %22similarityThreshold%22) int similarityThreshold) %7B%0A%0A        Preconditions.checkArgument(similarityThreshold %3C0 %7C%7C similarityThreshold%3E100, %22The value for %22 +%0A                %22similarityThreshold should be an integer between 0 and 100, not %7B%7D%22, similarityThreshold );%0A%0A        return getResultsResponse(ontologyService.findCoTermsByOntologyId(id, CoTermType.valueOf(type),%0A                coTermLimit.workoutLimit(limit), similarityThreshold));%0A    %7D%0A
+    %7D
 %0A%0A  

Revert the changes made for Co Terms - as this will now use its own endpoint.