package edu.isi.karma.mapreduce.driver;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Iterator;

import org.apache.hadoop.io.Text;
import org.json.JSONArray;
import org.json.JSONObject;

import edu.isi.karma.kr2rml.ContextIdentifier;
import edu.isi.karma.kr2rml.writer.JSONKR2RMLRDFWriter;
import edu.isi.karma.kr2rml.writer.KR2RMLRDFWriter;

public class JSONMapper extends BaseRDFMapper {
	private Text reusableOutputValue = new Text("");
	private Text reusableOutputKey = new Text("");
	private String atId = "@id";
	@Override
	protected KR2RMLRDFWriter configureRDFWriter(StringWriter sw) {
		PrintWriter pw = new PrintWriter(sw);
		KR2RMLRDFWriter outWriter = new JSONKR2RMLRDFWriter(pw, karma.getBaseURI());
		ContextIdentifier contextId = karma.getContextId();
		try {
			atId = getAtId(karma.getGenerator().loadContext(contextId).getJSONObject(("@context")));
		} catch(Exception e)
		{}
		return outWriter;
	}

	@Override
	protected void writeRDFToContext(Context context, String results)
			throws IOException, InterruptedException {
		JSONArray generatedObjects = new JSONArray(results);
		for(int i = 0; i < generatedObjects.length(); i++)
		{
			if (generatedObjects.getJSONObject(i).has(atId)) {
				reusableOutputKey.set(generatedObjects.getJSONObject(i).getString(atId));
			}
			else {
				reusableOutputKey.set(generatedObjects.getJSONObject(i).toString());
			}
			reusableOutputValue.set(generatedObjects.getJSONObject(i).toString());
			context.write(reusableOutputKey, new Text(reusableOutputValue));
		}
	}

	private String getAtId(JSONObject c) {
		@SuppressWarnings("rawtypes")
		Iterator itr = c.keys();
		while (itr.hasNext()) {
			String key = itr.next().toString();
			try {
				if (c.get(key).toString().equals("@id")) {
					return key;
				}
			}
			catch(Exception e) 
			{

			}
		}
		return atId;
	}
}

@@ -235,16 +235,73 @@
 NObject;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -626,16 +626,92 @@
  %22@id%22;%0A
+%09%0A%09private static Logger LOG = LoggerFactory.getLogger(JSONMapper.class);%0A%09%0A
 %09@Overri
@@ -1228,16 +1228,19 @@
 tion %7B%0A%09
+%0A%09%09
 %09JSONArr
@@ -1285,16 +1285,17 @@
 sults);%0A
+%09
 %09%09for(in
@@ -1341,21 +1341,33 @@
  i++)%0A%09%09
+%09%7B%0A%09%09%09%09try
 %7B%0A%09%09%09
+%09%09
 if (gene
@@ -1405,24 +1405,26 @@
 as(atId)) %7B%0A
+%09%09
 %09%09%09%09reusable
@@ -1496,20 +1496,26 @@
 %0A%09%09%09
+%09%09
 %7D%0A%09%09%09
+%09
+%09
 else %7B%0A
+%09%09
 %09%09%09%09
@@ -1586,18 +1586,22 @@
 ());%0A%09%09%09
-%7D%0A
+%09%09%7D%0A%09%09
 %09%09%09reusa
@@ -1669,16 +1669,18 @@
 ());%0A%09%09%09
+%09%09
 context.
@@ -1738,17 +1738,208 @@
 ue));%0A%09%09
-%7D
+%09%09%7Dcatch(ArrayIndexOutOfBoundsException ae)%7B%0A%09%09%09%09%09LOG.error(%22************ARRAYEXCEPTION*********:%22 + ae.getMessage() + %22SOURCE: %22 + generatedObjects.getJSONObject(i).toString());%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09
 %0A%09%7D%0A%0A%09pr

add debugging to JSONMapper