package ch.tkuhn.nanopub.server;

import java.net.URL;

import net.trustyuri.TrustyUriUtils;

import org.nanopub.NanopubImpl;

public class CollectNanopubs implements Runnable {

	private static  NanopubDb db = NanopubDb.get();

	private String serverUri;
	private String artifactCodeStart;

	public CollectNanopubs(String serverUri, String artifactCodeStart) {
		this.serverUri = serverUri;
		this.artifactCodeStart = artifactCodeStart;
	}

	@Override
	public void run() {
		try {
			ServerInfo si = ServerInfo.load(serverUri);
			for (String nanopubUri : Utils.loadNanopubUriList(si, artifactCodeStart)) {
				if ("...".equals(nanopubUri)) {
					processDeeperLevel();
					break;
				}
				String ac = TrustyUriUtils.getArtifactCode(nanopubUri);
				if (!db.hasNanopub(ac)) {
					db.loadNanopub(new NanopubImpl(new URL(serverUri + ac)));
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private void processDeeperLevel() {
		if (artifactCodeStart.length() > 44) {
			throw new RuntimeException("Unexcepted search depth: something went wrong");
		}
		for (char ch : Utils.base64Alphabet.toCharArray()) {
			CollectNanopubs obj = new CollectNanopubs(serverUri, artifactCodeStart + ch);
			obj.run();
		}
	}

}

@@ -43,15 +43,22 @@
 ava.
-net.URL
+io.InputStream
 ;%0A%0Ai
@@ -109,27 +109,162 @@
 org.
-nanopub.NanopubImpl
+apache.http.client.methods.HttpGet;%0Aimport org.apache.http.impl.client.HttpClientBuilder;%0Aimport org.nanopub.NanopubImpl;%0Aimport org.openrdf.rio.RDFFormat
 ;%0A%0Ap
@@ -892,16 +892,30 @@
 %09%09%09%09if (
+ac != null && 
 !db.hasN
@@ -924,24 +924,227 @@
 opub(ac)) %7B%0A
+%09%09%09%09%09HttpGet get = new HttpGet(serverUri + ac);%0A%09%09%09%09%09get.setHeader(%22Content-Type%22, %22application/trig%22);%0A%09%09%09%09    InputStream in = HttpClientBuilder.create().build().execute(get).getEntity().getContent();%0A
 %09%09%09%09%09db.load
@@ -1171,31 +1171,26 @@
 mpl(
-new URL(serverUri + ac)
+in, RDFFormat.TRIG
 ));%0A

Fix small bugs with collecting nanopubs