// Path: src/main/java/com/atomgraph/server/exception/SPINConstraintViolationException.java
// public class SPINConstraintViolationException extends ModelException
// {
//     private final List<ConstraintViolation> cvs;
//     
//     public SPINConstraintViolationException(List<ConstraintViolation> cvs, Model model, String graphURI)
//     {
//         super(model);
//         this.cvs = cvs;
//     }
// 
//     public SPINConstraintViolationException(List<ConstraintViolation> cvs, Model model)
//     {
//         this(cvs, model, null);
//     }
//     
//     public List<ConstraintViolation> getConstraintViolations()
//     {
//         return cvs;
//     }
//     
// }
// 
// Path: src/main/java/com/atomgraph/processor/util/Validator.java
// public class Validator
// {
//     private static final Logger log = LoggerFactory.getLogger(Validator.class);
//     
//     private final OntModel ontModel;
//         
//     public Validator(OntModel ontModel)
//     {
//         if (ontModel == null) throw new IllegalArgumentException("OntModel cannot be null");
//         this.ontModel = ontModel;
//     }
// 
//     public List<ConstraintViolation> validate(Model model)
//     {
//         if (model == null) throw new IllegalArgumentException("Model cannot be null");
// 
//         return SPINConstraints.check(model, getOntModel());
//     }
// 
//     public OntModel getOntModel()
//     {
//         return ontModel;
//     }
// 
// }
// 
// Path: src/main/java/com/atomgraph/server/exception/SHACLConstraintViolationException.java
// public class SHACLConstraintViolationException extends ModelException
// {
// 
//     private final ValidationReport report;
// 
//     public SHACLConstraintViolationException(ValidationReport report, Model model)
//     {
//         super(model);
//         this.report = report;
//     }
// 
//     public ValidationReport getValidationReport()
//     {
//         return report;
//     }
//     
// }


import org.apache.jena.ontology.Ontology;
import org.apache.jena.rdf.model.Model;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.List;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Providers;
import com.atomgraph.server.exception.SPINConstraintViolationException;
import com.atomgraph.processor.util.Validator;
import com.atomgraph.server.exception.SHACLConstraintViolationException;
import javax.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.atomgraph.spinrdf.constraints.ConstraintViolation;
import java.util.Optional;
import org.apache.jena.shacl.ShaclValidator;
import org.apache.jena.shacl.Shapes;
import org.apache.jena.shacl.ValidationReport;
/*
 * 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;


/**
 * Model provider that validates read triples against SPIN constraints in an ontology.
 * 
 * @author Martynas Jusevičius {@literal <martynas@atomgraph.com>}
 */
public class ValidatingModelProvider extends BasedModelProvider
{
    private static final Logger log = LoggerFactory.getLogger(ValidatingModelProvider.class);
    
    @Context private Providers providers;
    
    @Inject javax.inject.Provider<Optional<Ontology>> ontology;

    @Override
    public Model readFrom(Class<Model> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException
    {
        return process(super.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream));
    }

    public Model process(Model model)
    {
// Path: src/main/java/com/atomgraph/server/exception/SPINConstraintViolationException.java
// public class SPINConstraintViolationException extends ModelException
// {
//     private final List<ConstraintViolation> cvs;
//     
//     public SPINConstraintViolationException(List<ConstraintViolation> cvs, Model model, String graphURI)
//     {
//         super(model);
//         this.cvs = cvs;
//     }
// 
//     public SPINConstraintViolationException(List<ConstraintViolation> cvs, Model model)
//     {
//         this(cvs, model, null);
//     }
//     
//     public List<ConstraintViolation> getConstraintViolations()
//     {
//         return cvs;
//     }
//     
// }
// 
// Path: src/main/java/com/atomgraph/processor/util/Validator.java
// public class Validator
// {
//     private static final Logger log = LoggerFactory.getLogger(Validator.class);
//     
//     private final OntModel ontModel;
//         
//     public Validator(OntModel ontModel)
//     {
//         if (ontModel == null) throw new IllegalArgumentException("OntModel cannot be null");
//         this.ontModel = ontModel;
//     }
// 
//     public List<ConstraintViolation> validate(Model model)
//     {
//         if (model == null) throw new IllegalArgumentException("Model cannot be null");
// 
//         return SPINConstraints.check(model, getOntModel());
//     }
// 
//     public OntModel getOntModel()
//     {
//         return ontModel;
//     }
// 
// }
// 
// Path: src/main/java/com/atomgraph/server/exception/SHACLConstraintViolationException.java
// public class SHACLConstraintViolationException extends ModelException
// {
// 
//     private final ValidationReport report;
// 
//     public SHACLConstraintViolationException(ValidationReport report, Model model)
//     {
//         super(model);
//         this.report = report;
//     }
// 
//     public ValidationReport getValidationReport()
//     {
//         return report;
//     }
//     
// }


// Path: src/main/java/com/atomgraph/server/io/ValidatingModelProvider.java
import org.apache.jena.ontology.Ontology;
import org.apache.jena.rdf.model.Model;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.List;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Providers;
import com.atomgraph.server.exception.SPINConstraintViolationException;
import com.atomgraph.processor.util.Validator;
import com.atomgraph.server.exception.SHACLConstraintViolationException;
import javax.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.atomgraph.spinrdf.constraints.ConstraintViolation;
import java.util.Optional;
import org.apache.jena.shacl.ShaclValidator;
import org.apache.jena.shacl.Shapes;
import org.apache.jena.shacl.ValidationReport;

/*
 * 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;


/**
 * Model provider that validates read triples against SPIN constraints in an ontology.
 * 
 * @author Martynas Jusevičius {@literal <martynas@atomgraph.com>}
 */
public class ValidatingModelProvider extends BasedModelProvider
{
    private static final Logger log = LoggerFactory.getLogger(ValidatingModelProvider.class);
    
    @Context private Providers providers;
    
    @Inject javax.inject.Provider<Optional<Ontology>> ontology;

    @Override
    public Model readFrom(Class<Model> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException
    {
        return process(super.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream));
    }

    public Model process(Model model)
    {
        return validate(model);