package edu.isi.karma.kr2rml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.json.JSONObject;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;

import edu.isi.karma.modeling.Uris;

public class ContextGenerator {
	private Model model;
	Map<String, List<ContextObject>> contextMapping = new HashMap<String, List<ContextObject>>();
	private class ContextObject {
		public String prefix;
		public String URI;
		public ContextObject(String prefix, String URI) {
			this.prefix = prefix;
			this.URI = URI;
		}
	}
	public ContextGenerator(Model model) {
		this.model = model;
	}
	public JSONObject generateContext() {
		StmtIterator itr = model.listStatements();
        JSONObject obj = new JSONObject();
        while(itr.hasNext()) {
        	Statement stmt = itr.next();
        	if (stmt.getPredicate().getURI().equals(Uris.RR_CLASS_URI)) {
        		if (stmt.getObject().isURIResource()) {
        			String shortForm = model.shortForm(stmt.getObject().toString());
        			String fullURI = stmt.getObject().toString();
        			if (!shortForm.equals(fullURI)) {
        				String postfix = shortForm.substring(shortForm.lastIndexOf(":") + 1);
        				String prefix = shortForm.substring(0, shortForm.lastIndexOf(":"));
        				List<ContextObject> existPrefixes = contextMapping.get(postfix);
        				if (existPrefixes == null) {
        					existPrefixes = new ArrayList<ContextObject>();
        				}
        				existPrefixes.add(new ContextObject(prefix, fullURI));
        				contextMapping.put(postfix, existPrefixes);
        			}
        		}
        	}
        	if (stmt.getPredicate().getURI().equals(Uris.RR_PREDICATE_URI)) {
        		if (stmt.getObject().isURIResource()) {
        			String shortForm = model.shortForm(stmt.getObject().toString());
        			String fullURI = stmt.getObject().toString();
        			if (!shortForm.equals(fullURI)) {
        				String postfix = shortForm.substring(shortForm.lastIndexOf(":") + 1);
        				String prefix = shortForm.substring(0, shortForm.lastIndexOf(":"));
        				List<ContextObject> existPrefixes = contextMapping.get(postfix);
        				if (existPrefixes == null) {
        					existPrefixes = new ArrayList<ContextObject>();
        				}
        				existPrefixes.add(new ContextObject(prefix, fullURI));
        				contextMapping.put(postfix, existPrefixes);
        			}
        		}
        	}
        }
        for (Entry<String, List<ContextObject>> entry : contextMapping.entrySet()) {
        	List<ContextObject> prefixes = entry.getValue();
        	if (prefixes.size() > 1) {
        		for (ContextObject prefix : prefixes) {
        			obj.put(entry.getKey() + "_" + prefix.prefix, new JSONObject().put("@id", prefix.URI));
        		}
        	}
        	else {
        		for (ContextObject prefix : prefixes) {
        			obj.put(entry.getKey(), new JSONObject().put("@id", prefix.URI));
        		}
        	}
        }
        JSONObject top = new JSONObject();
        top.put("@context", obj);
        return top;
	}
}

@@ -28,36 +28,8 @@
 l;%0A%0A
-import java.util.ArrayList;%0A
 impo
@@ -67,19 +67,22 @@
 va.util.
-Lis
+HashSe
 t;%0Aimpor
@@ -125,16 +125,38 @@
 p.Entry;
+%0Aimport java.util.Set;
 %0A%0Aimport
@@ -406,35 +406,34 @@
 l;%0A%09Map%3CString, 
-Lis
+Se
 t%3CContextObject%3E
@@ -463,35 +463,34 @@
 HashMap%3CString, 
-Lis
+Se
 t%3CContextObject%3E
@@ -670,16 +670,242 @@
 RI;%0A%09%09%7D%0A
+%09%09%0A%09%09public int hashCode() %7B%0A%09%09%09return prefix.hashCode() * URI.hashCode();%09%09%0A%09%09%7D%0A%09%09%0A%09%09public boolean equals(Object o) %7B%0A%09%09%09if (o instanceof ContextObject) %7B%0A%09%09%09%09return ((ContextObject) o).URI == URI;%0A%09%09%09%7D%0A%09%09%09return false;%0A%09%09%7D%0A
 %09%7D%0A%09publ
@@ -1625,35 +1625,34 @@
 ));%0A        %09%09%09%09
-Lis
+Se
 t%3CContextObject%3E
@@ -1767,32 +1767,30 @@
 fixes = new 
-ArrayLis
+HashSe
 t%3CContextObj
@@ -2447,19 +2447,18 @@
     %09%09%09%09
-Lis
+Se
 t%3CContex
@@ -2589,16 +2589,14 @@
 new 
-ArrayLis
+HashSe
 t%3CCo
@@ -2821,19 +2821,18 @@
 String, 
-Lis
+Se
 t%3CContex
@@ -2891,11 +2891,10 @@
    %09
-Lis
+Se
 t%3CCo
@@ -3036,88 +3036,107 @@
  %09%09%09
-obj.put(entry.getKey() + %22_%22 + prefix.prefix, new JSONObject().put(%22@id%22
+String prefixURI = prefix.URI.replace(entry.getKey(), %22%22);%0A        %09%09%09obj.put(prefix.prefix
 , prefix
 .URI
@@ -3123,38 +3123,36 @@
 x.prefix, prefix
-.
 URI)
-)
 ;%0A        %09%09%7D%0A  

Fix WK-363