package uk.ac.ebi.quickgo.rest.search.results.transformer;

import uk.ac.ebi.quickgo.rest.comm.FilterContext;
import uk.ac.ebi.quickgo.rest.search.request.converter.RESTFilterConverterFactory;
import uk.ac.ebi.quickgo.rest.search.results.QueryResult;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.checkArgument;

/**
 * The purpose of this class is to insert information available externally via
 * RESTful services, into a {@link QueryResult} of where the results have type, {@link R}.
 *
 * Created 06/04/17
 * @author Edd
 */
public class ExternalServiceResultsTransformer<R> implements ResultTransformer<QueryResult<R>> {

    private static final ResultTransformationRequests EMPTY_TRANSFORMATION_REQUESTS =
            new ResultTransformationRequests();
    private final RESTFilterConverterFactory restFilterConverterFactory;
    private final List<ResponseValueInjector<R>> fieldInjectors;
    private final List<String> fieldsToAdd;

    public ExternalServiceResultsTransformer(RESTFilterConverterFactory restFilterConverterFactory,
            List<ResponseValueInjector<R>> fieldInjectors) {
        checkArgument(restFilterConverterFactory != null,
                "RESTFilterConverterFactory cannot be null");
        checkArgument(fieldInjectors != null, "Supplied list of ResponseValueInjectors cannot be null");

        this.restFilterConverterFactory = restFilterConverterFactory;
        this.fieldInjectors = fieldInjectors;
        this.fieldsToAdd = fieldInjectors.stream().map(ResponseValueInjector::getId).collect(Collectors.toList());
    }

    @Override public QueryResult<R> transform(QueryResult<R> result, FilterContext filterContext) {
        ResultTransformationRequests transformationRequests =
                filterContext
                        .get(ResultTransformationRequests.class)
                        .orElse(EMPTY_TRANSFORMATION_REQUESTS);

        Set<String> requiredRequests = transformationRequests.getRequests().stream().map(
                ResultTransformationRequest::getId).collect(Collectors.toSet());
        requiredRequests.retainAll(fieldsToAdd);

        if (!requiredRequests.isEmpty()) {
            List<ResponseValueInjector<R>> requiredInjectors =
                    fieldInjectors.stream()
                            .filter(injector -> requiredRequests.contains(injector.getId()))
                            .collect(Collectors.toList());

            result.getResults().forEach(annotation ->
                    requiredInjectors.forEach(valueInjector ->
                            valueInjector.inject(restFilterConverterFactory, annotation))
            );
        }

        return result;
    }
}

@@ -325,16 +325,73 @@
 lectors;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -763,16 +763,102 @@
 lt%3CR%3E%3E %7B
+%0A    Logger LOGGER = LoggerFactory.getLogger(ExternalServiceResultsTransformer.class);
 %0A%0A    pr
@@ -1905,221 +1905,416 @@
     
-ResultTransformationRequests transformationRequests =%0A                filterContext%0A                        .get(ResultTransformationRequests.class)%0A                        .orElse(EMPTY_TRANSFORMATION_REQUESTS);%0A
+LOGGER.info(%22ExternalServiceResultsTransformer.transform results.%22);%0A%0A        ResultTransformationRequests transformationRequests = filterContext.get(ResultTransformationRequests.class)%0A                                                                           .orElse(EMPTY_TRANSFORMATION_REQUESTS);%0A%0A        LOGGER.info(%22ExternalServiceResultsTransformer.transform build collection of required requests.%22);
 %0A   
@@ -2385,16 +2385,78 @@
 quests()
+%0A                                                             
 .stream(
@@ -2456,21 +2456,16 @@
 stream()
-.map(
 %0A       
@@ -2477,43 +2477,155 @@
     
-ResultTransformationRequest::getId)
+                                             .map(ResultTransformationRequest::getId)%0A                                                             
 .col
@@ -2999,24 +2999,136 @@
 toList());%0A%0A
+            LOGGER.info(%22ExternalServiceResultsTransformer.transform build collection of required injectors.%22);%0A
             
@@ -3343,24 +3343,119 @@
 ;%0A        %7D%0A
+        LOGGER.info(%22ExternalServiceResultsTransformer.transform return transformed results.%22);
 %0A        ret

Add logging.