package io.bdrc.xmltoldmigration;

import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.jena.datatypes.xsd.XSDDatatype;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.ontology.OntProperty;
import org.apache.jena.ontology.OntResource;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.riot.system.PrefixMap;
import org.apache.jena.util.iterator.ExtendedIterator;
import org.apache.jena.vocabulary.OWL;
import org.apache.jena.vocabulary.RDF;

// takes the BDRC owl file and generates one context per document type

public class GenerateContext {
    
    public static boolean datatypeNeedsContext(String datatype) {
        if (datatype.equals(XSDDatatype.XSDstring.getURI())
                || datatype.equals(RDF.langString.getURI()))
                return false;
        return true;
    }
    
    public static Map<String, Object> generateCompleteContext(OntModel m, PrefixMap prefixMap) {
        SortedMap<String, Object> context = new TreeMap<>();
        context.putAll(prefixMap.getMappingCopyStr());
        Map<String,String> typemap = new HashMap<>();
        typemap.put("@type", "@id");
        // we iterate on all properties and figure out the type
        // with the range, as some annotation properties 
        ExtendedIterator<OntProperty> ppi = m.listAllOntProperties();
        while (ppi.hasNext()) {
            OntProperty p = ppi.next();
            System.out.println(p.toString());
            String abbr = prefixMap.abbreviate(p.getURI());
            if (abbr == null || abbr.startsWith("owl:"))
                continue;
            if (p.isObjectProperty()) {
                context.put(abbr, typemap);
                continue;
            }
            OntResource range = p.getRange();
            if (range != null && range.isURIResource()) {
                Resource r = range.getPropertyResourceValue(RDF.type);
                if (r != null) {
                    if (r.equals(OWL.Class)) {
                        context.put(abbr, typemap);
                    } else {
                        if (datatypeNeedsContext(r.getURI())) {
                            Map<String,String> pm = new HashMap<>();
                            pm.put("@type", range.getURI());
                            context.put(abbr, pm);
                        }
                    }
                }
            }
        }
        return context;
    }
}

@@ -305,32 +305,74 @@
 gy.OntResource;%0A
+import org.apache.jena.rdf.model.RDFNode;%0A
 import org.apach
@@ -871,16 +871,533 @@
 etURI())
+%0A                %7C%7C datatype.equals(RDF.getURI()+%22PlainLiteral%22)%0A                // messes with the useNativeTypes if these are not here, %0A                // certainly a bug in jsonld-java, but cannot test before%0A                // https://github.com/jsonld-java/jsonld-java/issues/208%0A                // gets fixed%0A                %7C%7C datatype.equals(XSDDatatype.XSDboolean.getURI())%0A                %7C%7C datatype.equals(XSDDatatype.XSDinteger.getURI())%0A                %7C%7C datatype.equals(XSDDatatype.XSDfloat.getURI())
 )%0A      
@@ -1661,24 +1661,156 @@
 CopyStr());%0A
+        context.put(%22@version%22, %221.1%22);%0A        context.put(%22@vocab%22, CommonMigration.ONTOLOGY_PREFIX);%0A        context.remove(%22%22);%0A
         Map%3C
@@ -2156,54 +2156,8 @@
 ();%0A
-            System.out.println(p.toString());%0A
     
@@ -2243,16 +2243,34 @@
  null %7C%7C
+ abbr.isEmpty() %7C%7C
  abbr.st
@@ -2305,32 +2305,181 @@
       continue;%0A
+            // we replace ':property' by 'property'%0A            if (abbr.charAt(0) == ':') %7B%0A                abbr = abbr.substring(1);%0A            %7D%0A
             if (
@@ -2794,36 +2794,12 @@
 null
-) %7B%0A                    if (
+ && 
 r.eq
@@ -2813,28 +2813,24 @@
 L.Class)) %7B%0A
-    
             
@@ -2889,26 +2889,85 @@
     
-%7D else %7B%0A         
+continue;%0A                %7D%0A                String typeUri = range.getURI();%0A
     
@@ -2969,32 +2969,33 @@
 %0A               
+ 
 if (datatypeNeed
@@ -3007,31 +3007,20 @@
 ext(
-r.getURI())) %7B%0A        
+typeUri)) %7B%0A
     
@@ -3096,24 +3096,16 @@
         
-    
-    
 pm.put(%22
@@ -3116,30 +3116,18 @@
 e%22, 
-range.getURI());%0A     
+typeUri);%0A
     
@@ -3134,35 +3134,32 @@
                 
-   
 context.put(abbr
@@ -3169,56 +3169,8 @@
 m);%0A
-                        %7D%0A                    %7D%0A
     

fix context generation