package org.globalbioticinteractions.dataset;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.FileFileFilter;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.commons.lang.StringUtils;
import org.eol.globi.service.Dataset;
import org.eol.globi.service.DatasetFactory;
import org.eol.globi.service.DatasetFinder;
import org.eol.globi.service.DatasetFinderException;
import org.eol.globi.service.DatasetImpl;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.TreeSet;

public class DatasetFinderLocal implements DatasetFinder {
    private final String cacheDir;

    public DatasetFinderLocal(String cacheDir) {
        this.cacheDir = cacheDir;
    }

    @Override
    public Collection<String> findNamespaces() throws DatasetFinderException {
        Collection<File> accessFiles = FileUtils.listFiles(new File(cacheDir), new FileFileFilter() {
            @Override
            public boolean accept(File file) {
                return "access.tsv".endsWith(file.getName());
            }
        }, TrueFileFilter.INSTANCE);

        Collection<String> namespaces = new TreeSet<>();
        for (File accessFile : accessFiles) {
            try {
                String[] rows = IOUtils.toString(accessFile.toURI()).split("\n");
                for (String row : rows) {
                    namespaces.add(row.split("\t")[0]);
                }
            } catch (IOException e) {
                throw new DatasetFinderException("failed to read ", e);
            }
        }

        return namespaces;
    }


    @Override
    public Dataset datasetFor(String namespace) throws DatasetFinderException {
        Dataset dataset;

        try {
            final URI sourceURI = findLastCachedDatasetURI(namespace);
            dataset = sourceURI == null ? null : DatasetFactory.datasetFor(namespace, new DatasetFinder() {
                @Override
                public Collection<String> findNamespaces() throws DatasetFinderException {
                    return Arrays.asList(namespace);
                }

                @Override
                public Dataset datasetFor(String s) throws DatasetFinderException {
                    return new DatasetWithCache(new DatasetImpl(namespace, sourceURI),
                            DatasetFinderCaching.cacheFor(namespace, cacheDir));
                }
            });
        } catch (IOException e) {
            throw new DatasetFinderException("failed to access [" + namespace + "]", e);
        }

        if (dataset == null) {
            throw new DatasetFinderException("failed to retrieve/cache dataset in namespace [" + namespace + "]");
        }

        return dataset;
    }

    private URI findLastCachedDatasetURI(String namespace) throws IOException {
        URI sourceURI = null;
        File accessFile = new File(cacheDir + "/" + namespace + "/access.tsv");
        if (accessFile.exists()) {
            String[] rows = IOUtils.toString(accessFile.toURI()).split("\n");
            for (String row : rows) {
                String[] split = row.split("\t");
                if (split.length > 4
                        && StringUtils.equalsIgnoreCase(StringUtils.trim(split[0]), namespace)
                        && StringUtils.equals(StringUtils.trim(split[4]), DatasetFinderCaching.MIME_TYPE_GLOBI)) {
                    sourceURI = URI.create(split[1]);
                }
            }
        }
        return sourceURI;
    }


}

@@ -270,24 +270,109 @@
 tringUtils;%0A
+import org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A
 import org.e
@@ -397,16 +397,16 @@
 ataset;%0A
-
 import o
@@ -702,24 +702,77 @@
 Collection;%0A
+import java.util.Collections;%0Aimport java.util.List;%0A
 import java.
@@ -845,16 +845,96 @@
 inder %7B%0A
+    private final static Log LOG = LogFactory.getLog(DatasetFinderLocal.class);%0A
     priv
@@ -1136,32 +1136,494 @@
 nderException %7B%0A
+        File directory = new File(cacheDir);%0A        Collection%3CString%3E namespaces = Collections.emptyList();%0A        if (directory.exists() && directory.isDirectory()) %7B%0A            namespaces = collectNamespaces(directory);%0A        %7D%0A        else %7B%0A            LOG.warn(%22Directory %5B%22 + cacheDir + %22%5D does not exist.%22);%0A        %7D%0A        return namespaces;%0A    %7D%0A%0A    private Collection%3CString%3E collectNamespaces(File directory) throws DatasetFinderException %7B%0A
         Collecti
@@ -1665,34 +1665,25 @@
 stFiles(
-new File(cacheDir)
+directory
 , new Fi
@@ -2317,32 +2317,32 @@
 ;%0A            %7D%0A
+
         %7D%0A%0A     
@@ -2327,33 +2327,32 @@
     %7D%0A        %7D%0A
-%0A
         return n

check directiory