package uk.ac.ebi.atlas.utils;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import javax.inject.Inject;
import javax.inject.Named;
import java.text.MessageFormat;

/**
 * Created by barrera on 28/07/2014.
 *
 */

@Named
@Scope("prototype")
public class ReactomeClient {

    private static final Logger LOGGER = LoggerFactory.getLogger(ReactomeClient.class);

    private RestTemplate restTemplate;

    private String reactomeURL;

    @Inject
    public ReactomeClient(RestTemplate restTemplate, @Value("#{configuration['reactome.restful.query.url']}") String reactomeURL) {
        this.restTemplate = restTemplate;
        this.reactomeURL = reactomeURL;
    }

    /**
     * @return pathway name, or if there is an error then log it but continue by returning the reactomeId
     */
    public String fetchPathwayNameFailSafe(String reactomeId) {
        String url = MessageFormat.format(reactomeURL, reactomeId);

        try {
            String result = restTemplate.getForObject(url, String.class);

            return StringUtils.trimToEmpty(result);

        } catch (RestClientException e) {
            LOGGER.error(e.getMessage(), e);
            return reactomeId;
        }

    }

}

@@ -442,57 +442,8 @@
 t;%0A%0A
-/**%0A * Created by barrera on 28/07/2014.%0A *%0A */%0A%0A
 @Nam
@@ -624,17 +624,16 @@
 mplate;%0A
-%0A
     priv
@@ -894,130 +894,8 @@
  %7D%0A%0A
-    /**%0A     * @return pathway name, or if there is an error then log it but continue by returning the reactomeId%0A     */%0A
     
@@ -1107,25 +1107,24 @@
 ing.class);%0A
-%0A
             
@@ -1163,17 +1163,16 @@
 esult);%0A
-%0A
         

Clean up code