PageRenderTime 233ms CodeModel.GetById 120ms app.highlight 13ms RepoModel.GetById 98ms app.codeStats 0ms

/iks-ri/tags/apache-stanbol-incubation-20101129/kres/eu.iksproject.kres.jersey/src/main/java/eu/iksproject/kres/jersey/resource/GraphsResource.java

http://iks-project.googlecode.com/
Java | 159 lines | 128 code | 30 blank | 1 comment | 6 complexity | f49adf9661cc4d729473ce9bea5f353a MD5 | raw file
  1package eu.iksproject.kres.jersey.resource;
  2
  3import java.io.InputStream;
  4import java.util.ArrayList;
  5import java.util.Collection;
  6import java.util.List;
  7import java.util.Set;
  8
  9import javax.servlet.ServletContext;
 10import javax.ws.rs.Consumes;
 11import javax.ws.rs.FormParam;
 12import javax.ws.rs.GET;
 13import javax.ws.rs.POST;
 14import javax.ws.rs.Path;
 15import javax.ws.rs.PathParam;
 16import javax.ws.rs.Produces;
 17import javax.ws.rs.core.Context;
 18import javax.ws.rs.core.HttpHeaders;
 19import javax.ws.rs.core.MediaType;
 20import javax.ws.rs.core.Response;
 21import javax.ws.rs.core.UriInfo;
 22
 23import org.apache.clerezza.rdf.core.access.TcManager;
 24import org.semanticweb.owlapi.apibinding.OWLManager;
 25import org.semanticweb.owlapi.model.AddAxiom;
 26import org.semanticweb.owlapi.model.IRI;
 27import org.semanticweb.owlapi.model.OWLDataFactory;
 28import org.semanticweb.owlapi.model.OWLNamedIndividual;
 29import org.semanticweb.owlapi.model.OWLObjectProperty;
 30import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom;
 31import org.semanticweb.owlapi.model.OWLOntologyCreationException;
 32import org.semanticweb.owlapi.model.OWLOntologyManager;
 33import org.slf4j.Logger;
 34import org.slf4j.LoggerFactory;
 35
 36import eu.iksproject.kres.api.format.KReSFormat;
 37import eu.iksproject.kres.api.manager.KReSONManager;
 38import eu.iksproject.kres.api.storage.NoSuchStoreException;
 39import eu.iksproject.kres.api.storage.OntologyStorage;
 40import eu.iksproject.kres.api.storage.OntologyStoreProvider;
 41import eu.iksproject.kres.shared.transformation.JenaToOwlConvert;
 42
 43import org.semanticweb.owlapi.model.OWLOntology;
 44
 45import com.hp.hpl.jena.rdf.model.Model;
 46import com.hp.hpl.jena.rdf.model.ModelFactory;
 47import com.hp.hpl.jena.rdf.model.Bag;
 48import com.sun.jersey.api.view.ImplicitProduces;
 49
 50@Path("/graphs")
 51@ImplicitProduces(MediaType.TEXT_HTML + ";qs=2")
 52public class GraphsResource extends NavigationMixin {
 53
 54	private OntologyStoreProvider ontologyStoreProvider;
 55	protected TcManager tcManager;
 56	protected KReSONManager onManager;
 57	protected OntologyStorage storage;
 58	
 59	private final Logger log = LoggerFactory.getLogger(getClass());
 60	
 61	public GraphsResource(@Context ServletContext servletContext) {
 62		ontologyStoreProvider  = (OntologyStoreProvider) (servletContext.getAttribute(OntologyStoreProvider.class.getName()));
 63		tcManager = (TcManager) servletContext.getAttribute(TcManager.class.getName());
 64		onManager  = (KReSONManager) (servletContext.getAttribute(KReSONManager.class.getName()));
 65        if (onManager == null) {
 66            throw new IllegalStateException(
 67                    "OntologyStorage missing in ServletContext");
 68        }
 69        else{
 70        	storage = onManager.getOntologyStore();
 71        }
 72    }
 73	
 74	@GET
 75	@Path("/resume")
 76	@Produces( {KReSFormat.FUNCTIONAL_OWL, 
 77				KReSFormat.MANCHERSTER_OWL, 
 78				KReSFormat.OWL_XML, 
 79				KReSFormat.RDF_XML,
 80				KReSFormat.TURTLE,
 81				KReSFormat.RDF_JSON})
 82	public Response graphs(@Context HttpHeaders headers, @Context ServletContext servletContext){
 83		Set<IRI> iris = storage.listGraphs();
 84		if(iris != null){
 85			
 86			OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
 87			OWLDataFactory factory = OWLManager.getOWLDataFactory();
 88			
 89			OWLOntology ontology;
 90			try {
 91				ontology = manager.createOntology();
 92				
 93				String ns = onManager.getKReSNamespace();
 94				
 95				OWLNamedIndividual storage = factory.getOWLNamedIndividual(IRI.create(ns+"Storage"));
 96				
 97				OWLObjectProperty p = factory.getOWLObjectProperty(IRI.create(ns+"hasGraph"));
 98				
 99				for(IRI iri : iris){
100					iri = IRI.create(iri.toString().replace("<", "").replace(">", ""));
101					OWLNamedIndividual graph = factory.getOWLNamedIndividual(iri);
102					OWLObjectPropertyAssertionAxiom axiom = factory.getOWLObjectPropertyAssertionAxiom(p, storage, graph);
103					manager.applyChange(new AddAxiom(ontology, axiom));
104				}
105				
106				return Response.ok(ontology).build();
107			} catch (OWLOntologyCreationException e) {
108				return Response.status(500).build();
109			}
110			
111			
112		}
113		
114		return Response.status(404).build();
115	}
116	
117	@GET
118	@Path("/{graphid:.+}")
119	public Response getGraph(@PathParam("graphid") String graphid, @Context UriInfo uriInfo, @Context HttpHeaders headers){
120		OntologyStorage ontologyStorage = ontologyStoreProvider.getActiveOntologyStorage();
121		
122		IRI ontologyID = IRI.create(graphid);
123		
124		//return Response.ok(tcManager.getMGraph(new UriRef(graphid))).build();
125		try {
126			return Response.ok(ontologyStorage.getGraph(ontologyID)).build();
127		} catch (NoSuchStoreException e) {
128			return Response.status(204).build();
129		}
130		
131	}
132	
133	@POST
134	@Consumes(MediaType.MULTIPART_FORM_DATA)
135	public Response storeGraph(@FormParam("graph") InputStream graph, @FormParam("id") String id){
136		try {
137			OWLOntology ontology = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(graph);
138			ontologyStoreProvider.getActiveOntologyStorage().store(ontology, IRI.create(id));
139			return Response.ok().build();
140		} catch (OWLOntologyCreationException e) {
141			return Response.status(500).build();
142		}
143	}
144	
145	public String getNamespace(){
146		return onManager.getKReSNamespace();
147	}
148	
149	
150	public List<String> getStoredGraphs(){
151		Set<IRI> iris = ontologyStoreProvider.getActiveOntologyStorage().listGraphs();
152		
153		ArrayList<String> graphs = new ArrayList<String>();
154		for(IRI iri : iris){
155			graphs.add(iri.toString());
156		}
157		return graphs;
158	}
159}