Skip to content

Commit

Permalink
Jena-based RDF wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
stain committed May 23, 2013
1 parent b5faeea commit 7201062
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 14 deletions.
5 changes: 5 additions & 0 deletions orcid-pub-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,23 @@
*/
package org.orcid.api.t1.server;

import com.yammer.metrics.Metrics;
import com.yammer.metrics.core.Counter;
import static org.orcid.api.common.OrcidApiConstants.APPLICATION_RDFXML;
import static org.orcid.api.common.OrcidApiConstants.BIO_PATH;
import static org.orcid.api.common.OrcidApiConstants.BIO_SEARCH_PATH;
import static org.orcid.api.common.OrcidApiConstants.EXTERNAL_IDENTIFIER_PATH;
import static org.orcid.api.common.OrcidApiConstants.ORCID_JSON;
import static org.orcid.api.common.OrcidApiConstants.ORCID_XML;
import static org.orcid.api.common.OrcidApiConstants.PROFILE_GET_PATH;
import static org.orcid.api.common.OrcidApiConstants.STATUS_PATH;
import static org.orcid.api.common.OrcidApiConstants.TEXT_N3;
import static org.orcid.api.common.OrcidApiConstants.TEXT_TURTLE;
import static org.orcid.api.common.OrcidApiConstants.VND_ORCID_JSON;
import static org.orcid.api.common.OrcidApiConstants.VND_ORCID_XML;
import static org.orcid.api.common.OrcidApiConstants.WORKS_PATH;

import org.orcid.api.common.OrcidApiService;
import org.orcid.api.common.delegator.OrcidApiServiceDelegator;
import org.orcid.jaxb.model.message.OrcidMessage;
import org.springframework.stereotype.Component;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.ws.rs.GET;
Expand All @@ -33,10 +43,22 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.util.List;
import java.util.Map;

import static org.orcid.api.common.OrcidApiConstants.*;
import org.orcid.api.common.OrcidApiService;
import org.orcid.api.common.delegator.OrcidApiServiceDelegator;
import org.orcid.jaxb.model.message.OrcidMessage;
import org.orcid.jaxb.model.message.OrcidProfile;
import org.springframework.stereotype.Component;

import com.hp.hpl.jena.ontology.AnnotationProperty;
import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.Ontology;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.yammer.metrics.Metrics;
import com.yammer.metrics.core.Counter;

/**
* Copyright 2011-2012 ORCID
Expand All @@ -47,6 +69,7 @@
@Path("/")
public class T1OrcidApiServiceImpl implements OrcidApiService<Response> {

private static final String FOAF_0_1 = "http://xmlns.com/foaf/0.1/";
final static Counter T1_GET_REQUESTS = Metrics.newCounter(T1OrcidApiServiceImpl.class, "T1-GET-REQUESTS");
final static Counter T1_SEARCH_REQUESTS = Metrics.newCounter(T1OrcidApiServiceImpl.class, "T1-SEARCH-REQUESTS");

Expand Down Expand Up @@ -125,7 +148,35 @@ public Response viewBioDetailsXml(@PathParam("orcid") String orcid) {
@Path(BIO_PATH)
public Response viewBioDetailsRdf(@PathParam("orcid") String orcid) {
T1_GET_REQUESTS.inc();
return serviceDelegator.findBioDetails(orcid);
Response xmlResp = serviceDelegator.findBioDetails(orcid);
return xmlToRdf(xmlResp, APPLICATION_RDFXML);
}

protected Response xmlToRdf(Response xmlResp, String mediaType) {
OrcidMessage xml = (OrcidMessage) xmlResp.getEntity();

// Create RDF model
OntModel m = ModelFactory.createOntologyModel();
// TODO: Load FOAF locally, and cached
m.setDynamicImports(true);
OrcidProfile orcidProfile = xml.getOrcidProfile();
String profileUri = orcidProfile.getOrcid().getValue();
Ontology ont = m.createOntology(profileUri + "#");
ont.addImport(m.createResource(FOAF_0_1));
m.setNsPrefix("foaf", FOAF_0_1);

Individual person = m.createIndividual(profileUri, m.getOntClass(FOAF_0_1 + "Person"));

// AnnotationProperty foafName = m.getAnnotationProperty(FOAF_0_1 + "name");
DatatypeProperty foafName = m.getDatatypeProperty(FOAF_0_1 + "name");
person.addProperty(foafName, orcidProfile.getOrcidBio().getPersonalDetails().getCreditName().getContent());

// TOOD: Do a stream to Response
StringWriter writer = new StringWriter();
m.write(writer);

String rdf = writer.toString();
return Response.ok(rdf, mediaType).build();
}

/**
Expand All @@ -141,7 +192,7 @@ public Response viewBioDetailsRdf(@PathParam("orcid") String orcid) {
@Path(BIO_PATH)
public Response viewBioDetailsTurtle(@PathParam("orcid") String orcid) {
T1_GET_REQUESTS.inc();
return serviceDelegator.findBioDetails(orcid);
return xmlToRdf(serviceDelegator.findBioDetails(orcid), TEXT_TURTLE);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public void resetVals() {
T1OrcidApiServiceImpl.T1_SEARCH_RESULTS_FOUND.clear();
}

@Ignore
@Test
public void testViewBioDetailsRdf() {
assertTrue(T1OrcidApiServiceImpl.T1_SEARCH_REQUESTS.count() == 0);
Expand All @@ -107,9 +106,9 @@ public void testViewBioDetailsRdf() {
assertEquals(200, response.getStatus());
// assertEquals(APPLICATION_RDFXML, response.getMetadata().g)
String str = response.getEntity().toString();
// System.out.println(str);
System.out.println(str);
assertTrue(str.contains("http://orcid.example.com/000-1337"));
assertTrue(str.contains("foaf:fullname>John"));
assertTrue(str.contains("foaf:name>John"));
assertTrue(str.contains("rdf:about"));
assertTrue(T1OrcidApiServiceImpl.T1_GET_REQUESTS.count() == 1);
assertTrue(T1OrcidApiServiceImpl.T1_SEARCH_REQUESTS.count() == 0);
Expand Down

0 comments on commit 7201062

Please sign in to comment.