package org.dataone.annotator.matcher.esor;

import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.dataone.annotator.matcher.ConceptItem;
import org.dataone.annotator.matcher.ConceptMatcher;
import org.dataone.annotator.matcher.QueryItem;
import org.json.JSONArray;
import org.json.JSONObject;

public class EsorService implements ConceptMatcher {

	private static Log log = LogFactory.getLog(EsorService.class);

	private static final String REST_URL = "http://127.0.0.1:9100/search";


	@Override
	public List<ConceptItem> getConcepts(String fullText) throws Exception {
		return getConcepts(new QueryItem(fullText));
	}

	@Override
	public List<ConceptItem> getConcepts(QueryItem queryItem) throws Exception {
		return lookupEsor(queryItem);
	}

	private static List<ConceptItem> lookupEsor(QueryItem queryItem) throws Exception  {

		HttpClient client = HttpClients.createDefault();
		String uriStr = REST_URL + "?query=" + queryItem.toString();
		//System.out.println(uriStr);

		HttpGet method = new HttpGet(uriStr);
		method.setHeader("Accept", "application/json");
		HttpResponse response = client.execute(method);
		int code = response.getStatusLine().getStatusCode();
		if (2 != code / 100) {
				throw new Exception("response code " + code + " for resource at " + uriStr);
			}
		InputStream body = response.getEntity().getContent();
		String jsonStr = convertStreamToString(body);
		//System.out.println(jsonStr);

		JSONObject json = new JSONObject(jsonStr);
		String query = json.getString("query");
		JSONArray results = json.getJSONArray("results");


		//analysis the result and return
		ArrayList<ConceptItem> concepts = new ArrayList<ConceptItem>();
		for(int i = 0; i < results.length(); i++){
			JSONObject r = results.getJSONObject(i);
			String url = r.getString("url");
			String score = r.getString("score");


			ConceptItem c = new ConceptItem(new URI(url.substring(1,url.length()-1)), Double.parseDouble(score));
			concepts.add(c);
		}

		return concepts;
	}

	static String convertStreamToString(java.io.InputStream is) {
		java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
		return s.hasNext() ? s.next() : "";
	}
}

@@ -797,16 +797,141 @@
 arch%22;%0A%0A
+%09//temporary server for testing%0A%09//private static final String REST_URL = %22http://dataonetwc.tw.rpi.edu/linkipedia/search%22;%0A%0A
 %0A%09@Overr

add a temporaty server address for testing for EsorService