package org.handwerkszeug.mvnhack.repository.impl;

import java.net.URL;
import java.util.HashSet;
import java.util.Set;

import org.handwerkszeug.common.exception.IORuntimeException;
import org.handwerkszeug.common.util.UrlUtil;
import org.handwerkszeug.mvnhack.Constants;
import org.handwerkszeug.mvnhack.repository.Artifact;
import org.handwerkszeug.mvnhack.repository.ArtifactBuilder;
import org.handwerkszeug.mvnhack.repository.Context;
import org.handwerkszeug.mvnhack.repository.Repository;


public class RemoteRepository implements Repository {

	protected String baseUrl;

	protected ArtifactBuilder builder;

	public RemoteRepository(String url, ArtifactBuilder builder) {
		if (url.endsWith("/") == false) {
			url += "/";
		}
		this.baseUrl = url;
		this.builder = builder;
	}

	@Override
	public Artifact load(Context context, String groupId, String artifactId,
			String version) {
		StringBuilder stb = new StringBuilder();
		stb.append(baseUrl);
		stb.append(ArtifactUtil.toPom(groupId, artifactId, version));
		URL url = UrlUtil.toURL(stb.toString());
		try {
			return builder.build(context, context.open(ArtifactUtil.create(
					groupId, artifactId, version), url));
		} catch (IORuntimeException e) {
		}
		return null;
	}

	@Override
	public Set<URL> getLocation(Artifact artifact) {
		Set<URL> urls = new HashSet<URL>();
		urls.add(toURL(ArtifactUtil.toPath(artifact)));
		urls.add(toURL(ArtifactUtil.toPath(artifact, Constants.POM)));
		urls.add(toURL(ArtifactUtil.toPath(artifact, "-sources."
				+ artifact.getType())));
		return urls;
	}

	protected URL toURL(String suffix) {
		StringBuilder stb = new StringBuilder();
		stb.append(this.baseUrl);
		stb.append(suffix);
		return UrlUtil.toURL(stb.toString());
	}
}

@@ -506,18 +506,16 @@
 ory;%0D%0A%0D%0A
-%0D%0A
 public c
@@ -1152,16 +1152,23 @@
 xt.open(
+%0D%0A%09%09%09%09%09
 Artifact
@@ -1179,23 +1179,16 @@
 .create(
-%0D%0A%09%09%09%09%09
 groupId,
@@ -1430,24 +1430,83 @@
 tifact)));%0D%0A
+%09%09urls.add(toURL(ArtifactUtil.toPath(artifact, %22.jar%22)));%0D%0A
 %09%09urls.add(t
@@ -1605,17 +1605,22 @@
 rtifact,
- 
+%0D%0A%09%09%09%09
 %22-source
@@ -1622,22 +1622,17 @@
 ources.%22
-%0D%0A%09%09%09%09
+ 
 + artifa
@@ -1645,24 +1645,91 @@
 Type())));%0D%0A
+%09%09urls.add(toURL(ArtifactUtil.toPath(artifact, %22-sources.jar%22)));%0D%0A
 %09%09return url

add ignore