package uk.ac.ebi.spot.goci.component;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import uk.ac.ebi.spot.goci.model.SnpLookupJson;

import javax.validation.constraints.NotNull;

/**
 * Created by Emma on 22/04/16.
 *
 * @author emma
 *         <p>
 *         Checks a SNP identifier is valid using standard Spring mechanism to consume a RESTful service
 */
@Service
public class SnpValidationChecks {

    @NotNull @Value("${mapping.snp_lookup_endpoint}")
    private String endpoint;

    @NotNull @Value("${ensembl.server}")
    private String server;

    private final Logger log = LoggerFactory.getLogger(getClass());

    protected Logger getLog() {
        return log;
    }

    /**
     * Check gene name returns a response
     *
     * @param snp Snp identifier to check
     * @return Error message
     */
    public String checkSnpIdentifierIsValid(String snp) {

        String error = null;
        String url = getServer().concat(getEndpoint()).concat(snp);
        getLog().info("Checking url: " + url);

        // Create a new RestTemplate instance
        RestTemplate restTemplate = new RestTemplate();

        // Add the Jackson message converter
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

        try {
            String response = restTemplate.getForObject(url, String.class);
        }
        // The query returns a 400 error if response returns an error
        catch (Exception e) {
            error = "SNP identifier ".concat(snp).concat(" is not valid");
            getLog().error("Checking SNP identifier failed", e);
        }
        return error;
    }

    public void getSnpLocations(String snp) {

        String error = null;
        String url = getServer().concat(getEndpoint()).concat(snp);
        getLog().info("Checking url: " + url);

        // Create a new RestTemplate instance
        RestTemplate restTemplate = new RestTemplate();

        // Add the Jackson message converter
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

        SnpLookupJson snpLookupJson =  new SnpLookupJson();
        try {
            snpLookupJson = restTemplate.getForObject(url, SnpLookupJson.class);
        }
        // The query returns a 400 error if response returns an error
        catch (Exception e) {
            getLog().error("Checking SNP identifier failed", e);
        }


    }


    public String getEndpoint() {
        return endpoint;
    }

    public String getServer() {
        return server;
    }
}
@@ -425,16 +425,64 @@
 NotNull;
+%0Aimport java.util.HashSet;%0Aimport java.util.Set;
 %0A%0A/**%0A *
@@ -1207,123 +1207,8 @@
 ull;
-%0A        String url = getServer().concat(getEndpoint()).concat(snp);%0A        getLog().info(%22Checking url: %22 + url);
 %0A%0A  
@@ -1516,19 +1516,30 @@
 rObject(
-url
+createUrl(snp)
 , String
@@ -1844,147 +1844,194 @@
     
-public void getSnpLocations(String snp) %7B%0A%0A        String error = null;%0A        String url = getServer().concat(getEndpoint()).concat(
+/**%0A     * Get the chromosome a SNP resides on%0A     *%0A     * @param snp Snp identifier to check%0A     * @return Error message%0A     */%0A    public Set%3CString%3E getSnpLocations(String 
 snp)
-;
+ %7B%0A
 %0A   
@@ -2039,44 +2039,55 @@
     
-getLog().info(%22Checking url: %22 + url
+Set%3CString%3E snpChromosomeNames = new HashSet%3C%3E(
 );%0A%0A
@@ -2364,17 +2364,16 @@
 upJson =
- 
  new Snp
@@ -2458,11 +2458,33 @@
 ect(
-url
+createUrl(createUrl(snp))
 , Sn
@@ -2515,186 +2515,597 @@
     
-%7D%0A        // The query returns a 400 error if response returns an error%0A        catch (Exception e) %7B%0A            getLog().error(%22Checking SNP identifier failed%22, e);%0A        %7D%0A%0A
+    snpLookupJson.getMappings().forEach(snpMappingsJson -%3E %7B%0A                snpChromosomeNames.add(snpMappingsJson.getSeq_region_name());%0A            %7D);%0A        %7D%0A        // The query returns a 400 error if response returns an error%0A        catch (Exception e) %7B%0A            getLog().error(%22Getting locations for SNP %22.concat(snp).concat(%22failed%22), e);%0A        %7D%0A%0A        return snpChromosomeNames;%0A    %7D%0A%0A    private String createUrl(String query) %7B%0A        String url = getServer().concat(getEndpoint()).concat(query);%0A        getLog().info(%22Checking url: %22 + url);%0A        return url;
 %0A   

Add logic to get a snps location