/*
 * Copyright 2015 Martynas Jusevičius <martynas@atomgraph.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.atomgraph.server.io;

import org.apache.jena.ontology.Ontology;
import org.apache.jena.rdf.model.Model;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriBuilder;
import com.atomgraph.server.exception.SkolemizationException;
import com.atomgraph.processor.util.Skolemizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Martynas Jusevičius <martynas@atomgraph.com>
 */
public class SkolemizingModelProvider extends ValidatingModelProvider
{
    private static final Logger log = LoggerFactory.getLogger(SkolemizingModelProvider.class);
    
    @Context private Request request;
    
    @Override
    public Model process(Model model)
    {
        if (getRequest().getMethod().equalsIgnoreCase("POST"))
            return skolemize(getOntology(), getUriInfo().getBaseUriBuilder(), getUriInfo().getAbsolutePathBuilder(),
                    super.process(model));
        
        return super.process(model);
    }
    
    public Model skolemize(Ontology ontology, UriBuilder baseUriBuilder, UriBuilder absolutePathBuilder, Model model)
    {
        try
        {
            return new Skolemizer(ontology, baseUriBuilder, absolutePathBuilder).build(model);
        }
        catch (IllegalArgumentException ex)
        {
            throw new SkolemizationException(ex, model);
        }
    }

    public Request getRequest()
    {
        return request;
    }

}

@@ -948,24 +948,329 @@
 Skolemizer;%0A
+import com.atomgraph.processor.vocabulary.DH;%0Aimport java.util.Set;%0Aimport java.util.UUID;%0Aimport org.apache.jena.ontology.OntClass;%0Aimport org.apache.jena.rdf.model.ResIterator;%0Aimport org.apache.jena.rdf.model.Resource;%0Aimport org.apache.jena.rdf.model.Statement;%0Aimport org.apache.jena.vocabulary.RDF;%0A
 import org.s
@@ -1313,16 +1313,50 @@
 Factory;
+%0Aimport org.spinrdf.util.JenaUtil;
 %0A%0A/**%0A *
@@ -1752,16 +1752,418 @@
 POST%22))%0A
+        %7B%0A            ResIterator it = model.listSubjects();%0A            try%0A            %7B%0A                while (it.hasNext())%0A                %7B%0A                    Resource resource = it.next();%0A                    process(resource); // add dh:slug to documents before skolemization%0A                %7D%0A            %7D%0A            finally%0A            %7B%0A                it.close();%0A            %7D%0A        %0A
         
@@ -2314,16 +2314,26 @@
 odel));%0A
+        %7D%0A
         
@@ -2377,24 +2377,1023 @@
 %0A    %7D%0A    %0A
+    public Resource process(Resource resource)%0A    %7B%0A        // add UUID as dh:slug value to instances of dh:Container and dh:Item, if they don't have one%0A        // unlike using STRUUID() in dh:DocumentConstructor, we can cache pages without worrying about reusing slugs%0A        if (!resource.hasProperty(DH.slug))%0A        %7B%0A            Statement typeStmt = resource.getProperty(RDF.type);%0A            if (typeStmt != null && typeStmt.getObject().isURIResource())%0A            %7B%0A                OntClass ontClass = getOntology().getOntModel().getOntClass(typeStmt.getResource().getURI());%0A                // cannot use ontClass.hasSuperClass() here as it does not traverse the chain%0A                Set%3CResource%3E superClasses = JenaUtil.getAllSuperClasses(ontClass);%0A                if (superClasses.contains(DH.Container) %7C%7C superClasses.contains(DH.Item))%0A                    resource.addLiteral(DH.slug, UUID.randomUUID().toString());%0A            %7D%0A        %7D%0A%0A        return resource;%0A    %7D%0A    %0A
     public M

Add UUID dh:slug to documents before skolemization