package edu.isi.karma.rdf;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;

import edu.isi.karma.imp.json.JsonImport;
import edu.isi.karma.kr2rml.ErrorReport;
import edu.isi.karma.kr2rml.KR2RMLMapping;
import edu.isi.karma.kr2rml.KR2RMLWorksheetRDFGenerator;
import edu.isi.karma.kr2rml.R2RMLMappingIdentifier;
import edu.isi.karma.kr2rml.WorksheetR2RMLJenaModelParser;
import edu.isi.karma.rep.Worksheet;
import edu.isi.karma.rep.Workspace;
import edu.isi.karma.rep.WorkspaceManager;
import edu.isi.karma.util.JSONUtil;
import edu.isi.karma.webserver.ExecutionController;
import edu.isi.karma.webserver.KarmaException;
import edu.isi.karma.webserver.WorkspaceRegistry;

public class JSONRDFGenerator {
	
	private HashMap<String, R2RMLMappingIdentifier> modelIdentifiers;
	private HashMap<String, WorksheetR2RMLJenaModelParser> readModelParsers;
	
	private Workspace workspace;
	
	private JSONRDFGenerator(List<R2RMLMappingIdentifier> modelIdentifiers) {
		this.modelIdentifiers = new HashMap<>();
		for(R2RMLMappingIdentifier mi : modelIdentifiers) {
			this.modelIdentifiers.put(mi.getName(), mi);
		}
		this.readModelParsers = new HashMap<String, WorksheetR2RMLJenaModelParser>();
		this.workspace = WorkspaceManager.getInstance().createWorkspace();
		WorkspaceRegistry.getInstance().register(new ExecutionController(this.workspace));
	}
	
	private static JSONRDFGenerator instance = null;
	public static JSONRDFGenerator getInstance() {
		return instance;
	}
	public static JSONRDFGenerator createInstance(List<R2RMLMappingIdentifier> modelIdentifiers) {
		instance = new JSONRDFGenerator(modelIdentifiers);
		return getInstance();
	}
	
	public void generateRDF(String sourceName, String jsonData, boolean addProvenance, PrintWriter pw) throws KarmaException, JSONException, IOException {
		R2RMLMappingIdentifier id = this.modelIdentifiers.get(sourceName);
		if(id == null) {
			throw new KarmaException("Cannot generate RDF. Model named " + sourceName + " does not exist");
		}
		
		//Generate worksheet from the json data
		Object json = JSONUtil.createJson(jsonData);
        JsonImport imp = new JsonImport(json, sourceName, workspace, "utf-8", -1);
        Worksheet worksheet = imp.generateWorksheet();
        
		//Check if the parser for this model exists, else create one
		WorksheetR2RMLJenaModelParser modelParser = readModelParsers.get(sourceName);
		if(modelParser == null) {
			modelParser = loadModel(id);
		}
		
        //Generate mappping data for the worksheet using the model parser
		KR2RMLMapping mapping = modelParser.parse(worksheet, workspace);
		

		//Generate RDF using the mapping data
		ErrorReport errorReport = new ErrorReport();
		KR2RMLWorksheetRDFGenerator rdfGen = new KR2RMLWorksheetRDFGenerator(worksheet,
		        workspace.getFactory(), workspace.getOntologyManager(), pw,
		        mapping.getAuxInfo(), errorReport, addProvenance);
		rdfGen.generateRDF(false);
	}
	
	private WorksheetR2RMLJenaModelParser loadModel(R2RMLMappingIdentifier modelIdentifier) throws JSONException, KarmaException {
		WorksheetR2RMLJenaModelParser parser = new WorksheetR2RMLJenaModelParser(modelIdentifier);
		this.readModelParsers.put(modelIdentifier.getName(), parser);
		return parser;
	}
}

@@ -73,24 +73,52 @@
 rintWriter;%0A
+import java.util.ArrayList;%0A
 import java.
@@ -1021,53 +1021,8 @@
 tor(
-List%3CR2RMLMappingIdentifier%3E modelIdentifiers
 ) %7B%0A
@@ -1063,19 +1063,16 @@
 Map%3C
-%3E();%0A%09%09for(
+String, 
 R2RM
@@ -1093,85 +1093,12 @@
 fier
- mi : modelIdentifiers) %7B%0A%09%09%09this.modelIdentifiers.put(mi.getName(), mi);%0A%09%09%7D
+%3E();
 %0A%09%09t
@@ -1437,23 +1437,19 @@
 %7B%0A%09%09
-return 
+if(
 instance
 ;%0A%09%7D
@@ -1448,65 +1448,108 @@
 ance
-;%0A%09%7D%0A%09public static JSONRDFGenerator createInstance(List%3C
+ == null) %7B%0A%09%09%09instance = new JSONRDFGenerator();%0A%09%09%7D%0A%09%09return instance;%0A%09%7D%0A%09%0A%09public void addModel(
 R2RM
@@ -1558,33 +1558,32 @@
 appingIdentifier
-%3E
  modelIdentifier
@@ -1586,87 +1586,82 @@
 fier
-s
 ) %7B%0A%09%09
-instance = new JSONRDFGenerator(modelIdentifiers);%0A%09%09return getInstance(
+this.modelIdentifiers.put(modelIdentifier.getName(), modelIdentifier
 );%0A%09

Change so getInstance creates instance if null, added method to add models