package uk.ac.ebi.quickgo.rest.search.request.converter;

import uk.ac.ebi.quickgo.rest.controller.FilterRequestConfig;
import uk.ac.ebi.quickgo.rest.search.query.QuickGOQuery;
import uk.ac.ebi.quickgo.rest.search.request.FilterRequest;
import uk.ac.ebi.quickgo.rest.search.request.config.FilterConfig;
import uk.ac.ebi.quickgo.rest.search.request.config.FilterConfigRetrieval;

import com.google.common.base.Preconditions;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestOperations;

/**
 * <p>A generic factory to be used for performing arbitrary REST calls, whose details (including
 * address) are specified in a {@link FilterRequest}, and subsequently retrieving
 * instantiated instances that represent the response.
 *
 * <p>This class differs from {@link FilterConverterFactory} in that it can return any instance/response type,
 * whereas {@link FilterConverterFactory} returns specifically {@link QuickGOQuery} instances.
 *
 * Created 31/08/16
 * @author Edd
 */
@Component
@Import(FilterRequestConfig.class)
public class RESTFilterConverterFactory {
    private static final Logger LOGGER = LoggerFactory.getLogger(RESTFilterConverterFactory.class);
    private final FilterConfigRetrieval filterConfigRetrieval;
    private final RestOperations restOperations;

    @Autowired
    public RESTFilterConverterFactory(
            FilterConfigRetrieval globalFilterConfigRetrieval,
            RestOperations restOperations) {
        Preconditions.checkArgument(globalFilterConfigRetrieval != null, "RequestConfigRetrieval cannot be null");
        Preconditions.checkArgument(restOperations != null, "RestOperations cannot be null");

        this.filterConfigRetrieval = globalFilterConfigRetrieval;
        this.restOperations = restOperations;
    }

    public <T> ConvertedFilter<T> convert(FilterRequest request) {
        Optional<FilterConfig> configOpt = filterConfigRetrieval.getBySignature(request.getSignature());
        if (configOpt.isPresent()) {
            FilterConfig filterConfig = configOpt.get();
            switch (filterConfig.getExecution()) {
                case REST_COMM:
                    return new RESTFilterConverter<T>(filterConfig, restOperations).transform(request);
                default:
                    throw new IllegalStateException(
                            "RequestConfig execution has not been handled " +
                                    "for signature (" + request.getSignature() + ") in " + filterConfigRetrieval);
            }

        } else {
            throw new IllegalStateException(
                    "Could not find signature (" + request.getSignature() + ") in " + filterConfigRetrieval);
        }
    }

    @Override public String toString() {
        return "RESTFilterConverterFactory{" +
                "filterConfigRetrieval=" + filterConfigRetrieval +
                ", restOperations=" + restOperations +
                '}';
    }
}

@@ -448,65 +448,8 @@
 al;%0A
-import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 impo
@@ -1246,108 +1246,8 @@
 y %7B%0A
-    private static final Logger LOGGER = LoggerFactory.getLogger(RESTFilterConverterFactory.class);%0A
     

remove unused logger