package uk.ac.ebi.quickgo.annotation.metadata;

import uk.ac.ebi.quickgo.rest.metadata.MetaData;
import uk.ac.ebi.quickgo.rest.metadata.MetaDataProvider;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;

import static java.util.stream.Collectors.toList;

/**
 * Configure the provision of metadata for the Annotation service.
 *
 * @author Tony Wardell
 * Date: 07/03/2017
 * Time: 10:55
 * Created with IntelliJ IDEA.
 */
@Configuration
public class MetaDataConfig {
    private static final Logger LOGGER = LoggerFactory.getLogger(MetaDataConfig.class);
    private static final String SERVICE = "Annotation";
    private static final Path DEFAULT_METADATA_PATH = Paths.get("goa_uniprot.gpa-version");
    private static final Function<Path, List<MetaData>> PATH_TO_METADATA_MAPPER = (Path p) -> {
        try {
            Stream<String> stream =  Files.lines(p);
            return stream.map(s -> new MetaData(null, s.substring(s.indexOf("2"))))
            .collect(toList());
        } catch (IOException e) {
            throw new java.io.UncheckedIOException(e);
        }
    };

    private static final int NUMBER_OF_METADATA_LINES = 1;

    @Value("${annotation.metadata.source}")
    private Resource source;

    @Bean
    MetaDataProvider metaDataProvider()  {
        Path metaDataPath = DEFAULT_METADATA_PATH;
       if (source != null ) {
           try {
               metaDataPath = Paths.get(source.getURI());
           } catch (IOException e) {
               LOGGER.error("Failed to get the URI of the metadata source " + source,e);
           }
       }
        return new MetaDataProvider(SERVICE, PATH_TO_METADATA_MAPPER, metaDataPath, NUMBER_OF_METADATA_LINES);
    }
}

@@ -90,16 +90,81 @@
 taData;%0A
+import uk.ac.ebi.quickgo.rest.metadata.MetaDataConfigProperties;%0A
 import u
@@ -504,37 +504,54 @@
 rk.b
-eans.factory.annotation.Value
+oot.context.properties.ConfigurationProperties
 ;%0Aim
@@ -1352,17 +1352,16 @@
 stream =
- 
  Files.l
@@ -1453,16 +1453,29 @@
 %222%22))))%0A
+             
         
@@ -1674,181 +1674,416 @@
    @
-Value(%22$%7Bannotation.metadata.source%7D%22)%0A    private Resource source;%0A%0A    @Bean%0A    MetaDataProvider metaDataProvider()  %7B%0A        Path metaDataPath = DEFAULT_METADATA_PATH;%0A
+Bean%0A    @ConfigurationProperties(prefix = %22annotation.metadata%22)%0A    public MetaDataConfigProperties metaDataConfigProperties() %7B%0A        return new MetaDataConfigProperties();%0A    %7D%0A%0A    @Bean%0A    MetaDataProvider metaDataProvider(MetaDataConfigProperties metaDataConfigProperties) %7B%0A        Path metaDataPath = DEFAULT_METADATA_PATH;%0A        final Resource source = metaDataConfigProperties.getSource();%0A 
     
@@ -2107,13 +2107,13 @@
 null
- 
 ) %7B%0A
+ 
     
@@ -2117,32 +2117,33 @@
           try %7B%0A
+ 
                m
@@ -2180,24 +2180,25 @@
 .getURI());%0A
+ 
            %7D
@@ -2237,16 +2237,17 @@
         
+ 
 LOGGER.e
@@ -2308,16 +2308,17 @@
  source,
+ 
 e);%0A    
@@ -2320,26 +2320,28 @@
 %0A           
+ 
 %7D%0A
+ 
        %7D%0A   

Replace @Value with @ConfigurationProperties.