package org.openengsb.loom.java;

import java.io.File;

import org.apache.maven.repository.internal.MavenRepositorySystemSession;
import org.codehaus.plexus.DefaultPlexusContainer;
import org.sonatype.aether.RepositorySystem;
import org.sonatype.aether.RepositorySystemSession;
import org.sonatype.aether.collection.CollectRequest;
import org.sonatype.aether.collection.CollectResult;
import org.sonatype.aether.graph.Dependency;
import org.sonatype.aether.graph.DependencyNode;
import org.sonatype.aether.repository.LocalRepository;
import org.sonatype.aether.repository.RemoteRepository;
import org.sonatype.aether.resolution.DependencyRequest;
import org.sonatype.aether.util.artifact.DefaultArtifact;

public class AetherUtil {

    private static RepositorySystem newRepositorySystem() throws Exception {
        return new DefaultPlexusContainer().lookup(RepositorySystem.class);
    }

    private static RepositorySystemSession newSession(RepositorySystem system) {
        MavenRepositorySystemSession session = new MavenRepositorySystemSession();
        String homedir = System.getProperty("user.home");
        LocalRepository localRepo = new LocalRepository(new File(homedir, ".m2/repository"));
        session.setLocalRepositoryManager(system.newLocalRepositoryManager(localRepo));
        return session;
    }

    public static File downloadArtifact(String openengsbVersion) throws Exception {
        RepositorySystem repoSystem = newRepositorySystem();
        RepositorySystemSession session = newSession(repoSystem);
        DefaultArtifact artifact =
            new DefaultArtifact("org.openengsb.framework", "openengsb-framework", "zip", openengsbVersion);
        Dependency dependency =
            new Dependency(artifact, "test");
        RemoteRepository central = new RemoteRepository("central", "default", "http://repo1.maven.org/maven2/");
        RemoteRepository sonatypeSnapshots =
            new RemoteRepository("sonatype-snapshots", "default",
                "https://oss.sonatype.org/content/repositories/snapshots/");

        CollectRequest collectRequest = new CollectRequest();
        collectRequest.setRoot(dependency);
        collectRequest.addRepository(sonatypeSnapshots);
        collectRequest.addRepository(central);

        System.out.println("collecting");
        CollectResult collectDependencies = repoSystem.collectDependencies(session, collectRequest);
        DependencyNode node = collectDependencies.getRoot();
        DependencyRequest dependencyRequest = new DependencyRequest(node, null);
        repoSystem.resolveDependencies(session, dependencyRequest);

        return node.getDependency().getArtifact().getFile();
    }

}

@@ -47,16 +47,48 @@
 io.File;
+%0Aimport java.text.DecimalFormat;
 %0A%0Aimport
@@ -664,32 +664,209 @@
 endencyRequest;%0A
+import org.sonatype.aether.transfer.AbstractTransferListener;%0Aimport org.sonatype.aether.transfer.TransferCancelledException;%0Aimport org.sonatype.aether.transfer.TransferEvent;%0A
 import org.sonat
@@ -1176,16 +1176,16 @@
 stem) %7B%0A
-
         
@@ -1255,24 +1255,653 @@
 mSession();%0A
+        final DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance();%0A        formatter.setGroupingUsed(true);%0A        session.setTransferListener(new AbstractTransferListener() %7B%0A            @Override%0A            public void transferProgressed(TransferEvent event) throws TransferCancelledException %7B%0A                System.out.println(%22Downloading %22%0A                        + event.getResource().getFile().getName() + %22: %22%0A                        + formatter.format(event.getTransferredBytes()) + %22/%22%0A                        + formatter.format(event.getResource().getContentLength()));%0A            %7D%0A        %7D);%0A
         Stri

add progress-indicator to aether-download