package org.ringojs.repository;

import javax.servlet.ServletContext;
import java.io.File;
import java.util.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;

public class WebappRepository extends AbstractRepository {

    ServletContext context;

    private int exists = -1;

    public WebappRepository(ServletContext context, String path) {
        this.context = context;
        this.parent = null;
        if (path == null) {
            path = "/";
        } else if (!path.endsWith("/")) {
            path = path + "/";
        }
        this.path = path;
        this.name = path;
    }

    protected WebappRepository(ServletContext context, WebappRepository parent, String name) {
        this.context = context;
        this.parent = parent;
        this.name = name;
        this.path = parent.path + name + "/";
    }

    public long getChecksum() {
        return lastModified();
    }

    public long lastModified() {
        try {
            String realPath = context.getRealPath(path);
            return realPath == null ? 0 : new File(realPath).lastModified();
        } catch (Exception x) {
            return 0;
        }
    }

    public boolean exists() {
        if (exists < 0) {
            if ("/".equals(path)) {
                exists = 1;
            } else {
                Set paths = context.getResourcePaths(path);
                exists = (paths != null && !paths.isEmpty()) ? 1 : 0;
            }
        }
        return exists == 1;
    }

    public URL getUrl() throws MalformedURLException {
        return context.getResource(path);
    }

    @Override
    protected Resource lookupResource(String name) {
        AbstractResource res = resources.get(name);
        if (res == null) {
            res = new WebappResource(context, this, name);
            resources.put(name, res);
        }
        return res;
    }

    protected AbstractRepository createChildRepository(String name) {
        return new WebappRepository(context, this, name);
    }

    protected void getResources(List<Resource> list, boolean recursive)
            throws IOException {
        Set paths = context.getResourcePaths(path);

        if (paths != null) {
            for (Object obj: paths) {
                String path = (String) obj;
                if (!path.endsWith("/")) {
                    int n = path.lastIndexOf('/', path.length() - 1);
                    String name = path.substring(n + 1);
                    list.add(lookupResource(name));
                } else if (recursive) {
                    int n = path.lastIndexOf('/', path.length() - 2);
                    String name = path.substring(n + 1, path.length() - 1);
                    AbstractRepository repo = lookupRepository(name);
                    repo.getResources(list, true);
                }
            }
        }
    }

    public Repository[] getRepositories() throws IOException {
        Set paths = context.getResourcePaths(path);
        List<Repository> list = new ArrayList<Repository>();

        if (paths != null) {
            for (Object obj: paths) {
                String path = (String) obj;
                if (path.endsWith("/")) {
                    int n = path.lastIndexOf('/', path.length() - 2);
                    String name = path.substring(n + 1, path.length() - 1);
                    list.add(lookupRepository(name));
                }
            }
        }
        return list.toArray(new Repository[list.size()]);
    }

    @Override
    public String toString() {
        return "WebappRepository[" + path + "]";
    }

    @Override
    public int hashCode() {
        return 5 + path.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof WebappRepository && path.equals(((WebappRepository)obj).path);
    }

}

@@ -1344,32 +1344,55 @@
             
-Set path
+try %7B%0A                    exist
 s = context.
@@ -1406,90 +1406,135 @@
 urce
-Paths
 (path)
-;%0A                exists = (paths != null && !paths.isEmpty()) ? 1 : 0;
+ != null ? 1 : 0;%0A                %7D catch (MalformedURLException mux) %7B%0A                    exists = 0;%0A                %7D
 %0A   

Simplify WebappRepository.exists(), fixing it for empty directories.