Skip to content

Commit

Permalink
Merge pull request #147 from futures/sparql
Browse files Browse the repository at this point in the history
SPARQL endpoint for Admin Search

Resolves: https://www.pivotaltracker.com/story/show/60713796
  • Loading branch information
Andrew Woods committed Nov 18, 2013
2 parents ecbf380 + b4d4496 commit 7564c9b
Show file tree
Hide file tree
Showing 34 changed files with 2,764 additions and 86 deletions.
Expand Up @@ -46,7 +46,7 @@ public void testJcrPropertiesBasedOaiDc() throws Exception {
final BasicHttpEntity entity = new BasicHttpEntity();
final String subjectURI = serverAddress + "DublinCoreTest1";
entity.setContent(new ByteArrayInputStream(
("INSERT { <" + subjectURI + "> <http://purl.org/dc/terms/identifier> \"this is an identifier\" } WHERE {}")
("INSERT { <" + subjectURI + "> <http://purl.org/dc/elements/1.1/identifier> \"this is an identifier\" } WHERE {}")
.getBytes()));
post.setEntity(entity);
assertEquals(204, getStatus(post));
Expand Down
17 changes: 17 additions & 0 deletions fcrepo-http-api/src/main/resources/views/common.js
Expand Up @@ -101,6 +101,7 @@ $(function() {
$('#action_commit_transaction').submit(submitAndRedirectToBase);
$('#action_import').submit(sendImport);
$('#action_cnd_update').submit(sendCndUpdate);
$('#action_sparql_select').submit(sendSparqlQuery);

});

Expand Down Expand Up @@ -144,6 +145,22 @@ function registerNamespace() {
return false;
}


function sendSparqlQuery() {
var $form = $(this);
var postURI = $form.attr('action');

$.ajax({url: postURI, type: "POST", contentType: "application/sparql-query", data: $("#sparql_select_query").val(), success: function(data, textStatus, request) {

$('#errorLabel').text("RESULT");
$('#errorText').html("<pre></pre>");
$('#errorText pre').text(request.responseText);
$('#errorModal').modal('show');
}, error: ajaxErrorHandler});

return false;
}

function sendSparqlUpdate() {
var postURI = $('#main').attr('resource');

Expand Down
28 changes: 0 additions & 28 deletions fcrepo-http-api/src/main/resources/views/search-form.vsl

This file was deleted.

Expand Up @@ -25,6 +25,7 @@
<button type="submit" class="btn">Search</button>
</div>
</form>
<a class="btn" href="$uriInfo.baseUriBuilder.build()fcr:sparql">SPARQL</a>
</div>

## output triples for the topic node
Expand Down
Expand Up @@ -90,7 +90,7 @@ public void testSearchRdf() throws Exception {
final String updateString =
"INSERT { <"
+ serverAddress
+ "testobj> <http://purl.org/dc/terms/title> \"testobj\" } WHERE { }";
+ "testobj> <http://purl.org/dc/elements/1.1/title> \"testobj\" } WHERE { }";
postDc.setEntity(new StringEntity(updateString));
final HttpResponse dcResp = execute(postDc);
assertEquals(dcResp.getStatusLine().toString(), 204, dcResp
Expand Down
Expand Up @@ -129,7 +129,7 @@ public void testIngestWithNewAndSparqlQuery() throws Exception {
method.addHeader("Content-Type", "application/sparql-update");
final BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(new ByteArrayInputStream(
("INSERT { <> <http://purl.org/dc/terms/title> \"this is a title\" } WHERE {}")
("INSERT { <> <http://purl.org/dc/elements/1.1/title> \"this is a title\" } WHERE {}")
.getBytes()));
method.setEntity(entity);
final HttpResponse response = client.execute(method);
Expand All @@ -153,7 +153,7 @@ public void testIngestWithNewAndGraph() throws Exception {
final HttpPost method = postObjMethod("");
method.addHeader("Content-Type", "application/n3");
final BasicHttpEntity entity = new BasicHttpEntity();
final String rdf = "<> <http://purl.org/dc/terms/title> \"this is a title\".";
final String rdf = "<> <http://purl.org/dc/elements/1.1/title> \"this is a title\".";
entity.setContent(new ByteArrayInputStream(rdf.getBytes()));
method.setEntity(entity);
final HttpResponse response = client.execute(method);
Expand Down Expand Up @@ -405,7 +405,7 @@ public void testUpdateObjectGraph() throws Exception {
"application/sparql-update");
final BasicHttpEntity e = new BasicHttpEntity();
e.setContent(new ByteArrayInputStream(
("INSERT { <" + serverAddress + "FedoraDescribeTestGraphUpdate> <http://purl.org/dc/terms/identifier> \"this is an identifier\" } WHERE {}")
("INSERT { <" + serverAddress + "FedoraDescribeTestGraphUpdate> <http://purl.org/dc/elements/1.1/identifier> \"this is an identifier\" } WHERE {}")
.getBytes()));
updateObjectGraphMethod.setEntity(e);
final HttpResponse response = client.execute(updateObjectGraphMethod);
Expand Down
Expand Up @@ -24,6 +24,13 @@
import static org.apache.jena.riot.WebContent.contentTypeNTriples;
import static org.apache.jena.riot.WebContent.contentTypeRDFJSON;
import static org.apache.jena.riot.WebContent.contentTypeRDFXML;
import static org.apache.jena.riot.WebContent.contentTypeResultsBIO;
import static org.apache.jena.riot.WebContent.contentTypeResultsJSON;
import static org.apache.jena.riot.WebContent.contentTypeResultsXML;
import static org.apache.jena.riot.WebContent.contentTypeSSE;
import static org.apache.jena.riot.WebContent.contentTypeTextCSV;
import static org.apache.jena.riot.WebContent.contentTypeTextPlain;
import static org.apache.jena.riot.WebContent.contentTypeTextTSV;
import static org.apache.jena.riot.WebContent.contentTypeTriG;
import static org.apache.jena.riot.WebContent.contentTypeTurtle;

Expand Down Expand Up @@ -81,6 +88,40 @@ public abstract class RDFMediaType extends MediaType {
RDF_XML_TYPE, NTRIPLES_TYPE, TURTLE_TYPE, RDF_JSON_TYPE, N3_TYPE,
N3_ALT1_TYPE, N3_ALT2_TYPE, TRI_G_TYPE, NQUADS_TYPE).add().build();


public static final String TSV = contentTypeTextTSV;

public static final MediaType TSV_TYPE = typeFromString(TSV);

public static final String CSV = contentTypeTextCSV;

public static final MediaType CSV_TYPE = typeFromString(CSV);

public static final String SSE = contentTypeSSE;

public static final MediaType SSE_TYPE = typeFromString(SSE);

public static final String PLAIN = contentTypeTextPlain;

public static final MediaType PLAIN_TYPE = typeFromString(PLAIN);

public static final String RESULTS_JSON = contentTypeResultsJSON;

public static final MediaType RESULTS_JSON_TYPE = typeFromString(RESULTS_JSON);

public static final String RESULTS_XML = contentTypeResultsXML;

public static final MediaType RESULTS_XML_TYPE = typeFromString(RESULTS_XML);

public static final String RESULTS_BIO = contentTypeResultsBIO;

public static final MediaType RESULTS_BIO_TYPE = typeFromString(RESULTS_BIO);

public static final List<Variant> POSSIBLE_SPARQL_RDF_VARIANTS = mediaTypes(
TSV_TYPE, CSV_TYPE, SSE_TYPE, PLAIN_TYPE, RESULTS_JSON_TYPE,
RESULTS_XML_TYPE, RESULTS_BIO_TYPE, RDF_XML_TYPE, NTRIPLES_TYPE,
N3_TYPE, N3_ALT1_TYPE, N3_ALT2_TYPE, TURTLE_TYPE).add().build();

private static MediaType typeFromString(final String type) {
return new MediaType(type.split("/")[0], type.split("/")[1]);
}
Expand Down
Expand Up @@ -203,6 +203,8 @@ public final class RdfLexicon {
createProperty("http://microformats.org/wiki/rel-subscription");
public static final Property NOT_IMPLEMENTED =
createProperty(REPOSITORY_NAMESPACE + "notImplemented");
public static final Property HAS_SPARQL_ENDPOINT =
createProperty(RESTAPI_NAMESPACE + "sparql");

public static final Set<Property> otherServiceProperties = of(
HAS_SERIALIZATION, HAS_VERSION_HISTORY, HAS_FIXITY_SERVICE,
Expand Down Expand Up @@ -264,7 +266,7 @@ public final class RdfLexicon {
public static final Property RDFS_LABEL =
createProperty("http://www.w3.org/2000/01/rdf-schema#label");
public static final Property DC_TITLE =
createProperty("http://purl.org/dc/terms/title");
createProperty("http://purl.org/dc/elements/1.1/title");

public static final Set<Property> managedProperties;

Expand Down
Expand Up @@ -146,7 +146,7 @@ public void addedStatement(final Statement s) {
jcrRdfTools.createValue(subjectNode, s.getObject(),
NodePropertiesTools.getPropertyType(subjectNode,
propertyName));
NodePropertiesTools.appendOrReplaceNodeProperty(subjectNode,
NodePropertiesTools.appendOrReplaceNodeProperty(subjects, subjectNode,
propertyName,
v);
}
Expand Down Expand Up @@ -220,7 +220,7 @@ public void removedStatement(final Statement s) {
jcrRdfTools.createValue(subjectNode, s.getObject(),
NodePropertiesTools.getPropertyType(subjectNode,
propertyName));
NodePropertiesTools.removeNodeProperty(subjectNode,
NodePropertiesTools.removeNodeProperty(subjects, subjectNode,
propertyName,
v);
}
Expand Down
121 changes: 118 additions & 3 deletions fcrepo-kernel/src/main/java/org/fcrepo/kernel/utils/JcrRdfTools.java
Expand Up @@ -41,6 +41,7 @@
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.PropertyDefinition;

import org.fcrepo.kernel.RdfLexicon;
import org.fcrepo.kernel.rdf.GraphSubjects;
Expand Down Expand Up @@ -366,6 +367,41 @@ public boolean isInternalProperty(final Node subjectNode,
public Value createValue(final Node node, final RDFNode data, final int type)
throws RepositoryException {
final ValueFactory valueFactory = node.getSession().getValueFactory();
return createValue(valueFactory, data, type);

}

/**
* Create a JCR value (with an undefined type) from a RDFNode
* @param data
* @return
* @throws RepositoryException
*/
public Value createValue(final RDFNode data) throws RepositoryException {
return createValue(data, UNDEFINED);
}

/**
* Create a JCR value from an RDFNode with the given JCR type
* @param data
* @param type
* @return
* @throws RepositoryException
*/
public Value createValue(final RDFNode data, final int type) throws RepositoryException {
return createValue(session.getValueFactory(), data, type);
}

/**
* Create a JCR value from an RDF node with the given JCR type
* @param valueFactory
* @param data
* @param type
* @return
* @throws RepositoryException
*/
public Value createValue(final ValueFactory valueFactory, final RDFNode data, final int type)
throws RepositoryException {
assert (valueFactory != null);

if (data.isURIResource()
Expand Down Expand Up @@ -446,17 +482,61 @@ public String getPropertyNameFromPredicate(final Node node,
* @return the JCR property name
* @throws RepositoryException
*/

public String getPropertyNameFromPredicate(final Node node, final com.hp.hpl.jena.rdf.model.Property predicate,
final Map<String, String> namespaceMapping) throws RepositoryException {

final NamespaceRegistry namespaceRegistry =
getNamespaceRegistry.apply(node);

return getPropertyNameFromPredicate(namespaceRegistry, predicate, namespaceMapping);
}

/**
* Get the property name for an RDF predicate
* @param predicate
* @param namespaceMapping
* @return
* @throws RepositoryException
*/
public String getPropertyNameFromPredicate(final com.hp.hpl.jena.rdf.model.Property predicate,
final Map<String, String> namespaceMapping) throws RepositoryException {

final NamespaceRegistry namespaceRegistry = (org.modeshape.jcr.api.NamespaceRegistry)session.getWorkspace().getNamespaceRegistry();

return getPropertyNameFromPredicate(namespaceRegistry, predicate, namespaceMapping);
}

/**
* Get a property name for an RDF predicate
* @param predicate
* @return
* @throws RepositoryException
*/
public String getPropertyNameFromPredicate(final com.hp.hpl.jena.rdf.model.Property predicate) throws RepositoryException {


Map<String, String> emptyNamespaceMapping = emptyMap();
return getPropertyNameFromPredicate(predicate, emptyNamespaceMapping);
}


/**
* Get the JCR property name for an RDF predicate
* @param namespaceRegistry
* @param predicate
* @param namespaceMapping
* @return
* @throws RepositoryException
*/
public String getPropertyNameFromPredicate(final NamespaceRegistry namespaceRegistry, final com.hp.hpl.jena.rdf.model.Property predicate,
final Map<String, String> namespaceMapping) throws RepositoryException {

final String prefix;

final String namespace =
getJcrNamespaceForRDFNamespace(predicate.getNameSpace());

final NamespaceRegistry namespaceRegistry =
getNamespaceRegistry.apply(node);

assert (namespaceRegistry != null);

if (namespaceRegistry.isRegisteredUri(namespace)) {
Expand Down Expand Up @@ -502,4 +582,39 @@ public void setLlstore(final LowLevelStorageService lowLevelStorageService) {
llstore = lowLevelStorageService;
}

/**
* Given a node type and a property name, figure out an appropriate jcr value type
* @param nodeType
* @param propertyName
* @return
* @throws RepositoryException
*/
public int getPropertyType(final String nodeType, final String propertyName) throws RepositoryException {
return getPropertyType(session.getWorkspace().getNodeTypeManager().getNodeType(nodeType), propertyName);

}

/**
* Given a node type and a property name, figure out an appropraite jcr value type
* @param nodeType
* @param propertyName
* @return
* @throws RepositoryException
*/
public int getPropertyType(final NodeType nodeType, final String propertyName) throws RepositoryException {
final PropertyDefinition[] propertyDefinitions = nodeType.getPropertyDefinitions();
int type = UNDEFINED;
for (PropertyDefinition propertyDefinition : propertyDefinitions) {
if (propertyDefinition.getName().equals(propertyName)) {
if (type != UNDEFINED) {
return UNDEFINED;
}

type = propertyDefinition.getRequiredType();
}
}

return type;
}

}

0 comments on commit 7564c9b

Please sign in to comment.