package org.obd.ws.resources;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.json.JSONException;
import org.json.JSONObject;
import org.nescent.informatics.OBDQuery;
import org.obd.model.LiteralStatement;
import org.obd.model.Node;
import org.obd.model.Statement;
import org.obd.query.Shard;
import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Reference;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.Variant;

public class AnatomyResource extends Resource {

	private final String termId;
	private JSONObject jObjs;
	private Shard obdsql;

	public AnatomyResource(Context context, Request request, Response response) {
		super(context, request, response);
		this.obdsql = (Shard) this.getContext().getAttributes().get("shard");
		this.getVariants().add(new Variant(MediaType.APPLICATION_JSON));
		// this.getVariants().add(new Variant(MediaType.TEXT_HTML));
		this.termId = Reference.decode((String) (request.getAttributes()
				.get("termID")));
		// System.out.println(termId);
	}

	public Representation getRepresentation(Variant variant) {

		Representation rep = null;

		try {
			this.jObjs = getAnatomyTermInfo(this.termId);
			if (this.jObjs.get("name").toString().equals("not found")) {
				getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND,
						"The search term was not found");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		rep = new JsonRepresentation(this.jObjs);
		return rep;

	}

	private JSONObject getAnatomyTermInfo(String termId) throws IOException,
			SQLException, ClassNotFoundException, JSONException {

		OBDQuery obdq = new OBDQuery(obdsql);
		JSONObject jsonObj = new JSONObject();
		
		Set<Statement> stmts = obdq.genericSearch(null, termId, null);	
		String def = "";
		Collection<LiteralStatement> lstmts = obdsql.getLiteralStatementsByNode(termId);
		for (LiteralStatement lstmt : lstmts) {
			if (lstmt.getRelationId().toLowerCase().contains("definition")) {
				def = lstmt.getTargetId();
			}
		}

		if (obdsql.getNode(termId) != null) {
			jsonObj.put("id", termId);
			jsonObj.put("name", obdsql.getNode(termId).getLabel());
			if (def.length() > 0)
				jsonObj.put("definition", def);

			for(Statement stmt : stmts){
				System.out.println(stmt);
			}
		}
		else{
			jsonObj.put("name", "not found");
		}

		return jsonObj;
	}
}

@@ -2214,16 +2214,69 @@
 tion %7B%0A%0A
+%09%09int totalAnnotationCount = 0, totalTaxonCount = 0;%0A
 %09%09OBDQue
@@ -2392,35 +2392,433 @@
 eric
+Term
 Search(
-null, termId, null);
+termId); //find everything about this node%0A%09%09for(Statement stmt : stmts)%7B%0A%09%09%09System.out.println(stmt);%0A%09%09%09if(stmt.getRelationId().contains(%22is_a%22) && %0A%09%09%09%09%09stmt.getTargetId().equals(termId))%7B  //looking for children of this node%0A%09%09%09%09String childNodeId = stmt.getNodeId();%0A%09%09%09%7D%0A%09%09%7D%0A%09%09totalAnnotationCount += stmts.size();%0A%09%09for(Statement t : stmts)%7B%0A%09%09%09if(t.getNodeId().contains(%22TTO:%22))%0A%09%09%09%09++totalTaxonCount;%0A%09%09%7D%0A
 %09%0A%09%09
@@ -2917,16 +2917,57 @@
 ermId);%0A
+%09%09totalAnnotationCount += lstmts.size();%0A
 %09%09for (L
@@ -3308,39 +3308,44 @@
 %0A%09%09%09
-for(Statement stmt : stmts)%7B
+%0A%09%09%7D%0A%09%09else%7B%0A%09%09%09jsonObj = null;
 %0A%09%09
+%7D%0A
 %09%09Sy
@@ -3365,73 +3365,98 @@
 tln(
-stmt);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09else%7B%0A%09%09%09jsonObj.put(%22name%22, %22not found%22);%0A%09%09%7D%0A
+%22annotations: %22 + totalAnnotationCount);%0A%09%09System.out.println(%22taxa: %22 + totalTaxonCount);
 %0A%09%09r

Work in progress...getAnatomyTermInfo should recursively find annotations for child concepts as well