package main.java.org.wikidata.analyzer.Fetcher;

import org.wikidata.wdtk.dumpfiles.MwDumpFile;
import org.wikidata.wdtk.dumpfiles.MwLocalDumpFile;
import org.wikidata.wdtk.dumpfiles.wmf.JsonOnlineDumpFile;
import org.wikidata.wdtk.util.DirectoryManager;
import org.wikidata.wdtk.util.DirectoryManagerImpl;
import org.wikidata.wdtk.util.WebResourceFetcher;
import org.wikidata.wdtk.util.WebResourceFetcherImpl;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

/**
 * @author Addshore
 */
public class DumpFetcher {

    protected File dataDirectory;

    public DumpFetcher(File dataDirectory) {
        this.dataDirectory = dataDirectory;
    }

    /**
     * Look for the most recent dump date online and try to retrieve as dump object with fallback:
     * 1 - Look for dumps in location expected on labs
     * 2 - Look for dumps in the dump directory for this tool
     * 3 - Look online & download dumps
     *
     * @return MwDumpFile
     * @throws IOException
     */
    public MwDumpFile getMostRecentDump() throws IOException {
        DumpDateFetcher dateFetcher = new DumpDateFetcher();
        String latestDumpDate = dateFetcher.getLatestOnlineDumpDate();
        System.out.println("Latest dump date stamp is " + latestDumpDate);

        // 1) Try to process the file from labs storage
        MwLocalDumpFile labsDumpFile = new MwLocalDumpFile(
                "/public/dumps/public/wikidatawiki/entities/" + latestDumpDate + "/wikidata-" + latestDumpDate + "-all.json.gz"
        );
        if (labsDumpFile.isAvailable()) {
            System.out.println("Using dump file from labs storage");
            return labsDumpFile;
        }

        // 2) Try to use our local storage directory
        MwLocalDumpFile localDumpFile = new MwLocalDumpFile(
                this.dataDirectory + "/dumpfiles/json-" + latestDumpDate + "/" + latestDumpDate + "-all.json.gz"
        );
        if (localDumpFile.isAvailable()) {
            System.out.println("Using dump file from local storage");
            return localDumpFile;
        }

        // 3) Fallback to downloading the dump ourselves
        DirectoryManager localDirectoryManager = new DirectoryManagerImpl(
                Paths.get(this.dataDirectory.getAbsolutePath() + File.separator + "dumpfiles"),
                true
        );
        WebResourceFetcher fetcher = new WebResourceFetcherImpl();
        JsonOnlineDumpFile onlineDumpFile = new JsonOnlineDumpFile(
                latestDumpDate,
                "wikidatawiki",
                fetcher,
                localDirectoryManager
        );
        if (onlineDumpFile.isAvailable()) {
            System.out.println("Using online dump file");
            return onlineDumpFile;
        }

        throw new IOException("Failed to get most recent dump from any sources");
    }
}

@@ -482,16 +482,67 @@
 e.Paths;
+%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0A/**%0A *
@@ -853,112 +853,64 @@
 for 
-dumps in location expected on labs%0A     * 2 - Look for dump
+local dump copie
 s 
+(
 in 
-the dump directory for this tool
+a collection of locations)
 %0A     * 
 3 - 
@@ -905,17 +905,17 @@
 %0A     * 
-3
+2
  - Look 
@@ -1292,52 +1292,118 @@
  // 
-1) Try to process the file from labs storage
+Look for the dump in a list of possible local locations%0A        List%3CString%3E locationList = new ArrayList%3C%3E();
 %0A   
@@ -1411,76 +1411,212 @@
     
-Mw
+//
 Local
-DumpFile labsDumpFile = new MwLocalDumpFile(%0A                
+ data dir location%0A        locationList.add(this.dataDirectory + %22/dumpfiles/json-%22 + latestDumpDate + %22/%22 + latestDumpDate + %22-all.json.gz%22);%0A        //Labs dump location%0A        locationList.add(
 %22/pu
@@ -1718,33 +1718,24 @@
 all.json.gz%22
-%0A        
 );%0A        i
@@ -1737,405 +1737,328 @@
     
-if (labsDumpFile.isAvailable()) %7B%0A            System.out.println(%22Using dump file from labs storage%22);%0A            return labsDumpFile;%0A        %7D%0A%0A        // 2) Try to use our local storage directory%0A        MwLocalDumpFile localDumpFile = new MwLocalDumpFile(%0A                this.dataDirectory + %22/dumpfiles/json-%22 + latestDumpDate + %22/%22 + latestDumpDate + %22-all.json.gz%22%0A        );%0A        if (
+//Stat1002 dump location%0A        locationList.add(%22/mnt/data/xmldatadumps/public/wikidatawiki/entities/%22 + latestDumpDate + %22/wikidata-%22 + latestDumpDate + %22-all.json.gz%22);%0A        for (String dumpLocation: locationList) %7B%0A            MwLocalDumpFile localDumpFile = new MwLocalDumpFile( dumpLocation );%0A            if( 
 loca
@@ -2072,32 +2072,33 @@
 le.isAvailable()
+ 
 ) %7B%0A            
@@ -2089,32 +2089,36 @@
 ) %7B%0A            
+    
 System.out.print
@@ -2145,26 +2145,33 @@
 from
- local storage%22);%0A
+: %22 + dumpLocation);%0A    
     
@@ -2192,32 +2192,46 @@
  localDumpFile;%0A
+            %7D%0A
         %7D%0A%0A     
@@ -2239,11 +2239,8 @@
   //
- 3)
  Fal

Refactor selecting dumps from local locations