Skip to content

Commit

Permalink
Update admin SPARQL search to support rdf:type assertions about mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Dec 2, 2013
1 parent ceae23e commit 61aaed0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
1 change: 1 addition & 0 deletions fcrepo-kernel/src/main/resources/fedora-node-types.cnd
Expand Up @@ -83,6 +83,7 @@
*/
[fedora:resource] > fedora:relations, mix:created, mix:lastModified, mix:lockable, mix:versionable, dc:describable mixin

- rdf:type (URI) multiple
- * (undefined) multiple
- * (undefined)

Expand Down
Expand Up @@ -120,6 +120,7 @@ public class JQLQueryVisitor implements QueryVisitor, ElementVisitor, ExprVisito
private boolean distinct;
private boolean inOptional;
private Map<String, Source> joins;
private Map<String, String> joinTypes;
private Map<String, JoinCondition> joinConditions;

private NodePropertiesTools propertiesTools = new NodePropertiesTools();
Expand All @@ -140,6 +141,7 @@ public JQLQueryVisitor(final Session session,
this.constraint = null;
this.variables = new HashMap<String, Column>();
this.joins = new HashMap<String, Source>();
this.joinTypes = new HashMap<>();
this.joinConditions = new HashMap<String, JoinCondition>();
}

Expand Down Expand Up @@ -211,9 +213,17 @@ private Source getSource() {
final Map.Entry<String, Source> next = iterator.next();

if (next.getValue() != parentSource) {
final String joinType;

if (joinTypes.containsKey(next.getKey())) {
joinType = QueryObjectModelConstants.JCR_JOIN_TYPE_INNER;
} else {
joinType = QueryObjectModelConstants.JCR_JOIN_TYPE_LEFT_OUTER;
}

this.source = queryFactory.join(this.source,
next.getValue(),
QueryObjectModelConstants.JCR_JOIN_TYPE_LEFT_OUTER,
joinType,
joinConditions.get(next.getKey()));
}
}
Expand Down Expand Up @@ -453,6 +463,24 @@ public void visit(final ElementPathBlock el) {

final String propertyName = jcrTools.getPropertyNameFromPredicate(defaultModel.createProperty(
predicate.getURI()));

if (propertyName.equals("rdf:type") && object.isURI()) {
final String mixinName =
jcrTools.getPropertyNameFromPredicate(defaultModel.createProperty(object.getURI()));

if (session.getWorkspace().getNodeTypeManager().hasNodeType(mixinName)) {
final String selectorName = "ref_type_" + mixinName.replace(":", "_");

this.joins.put(selectorName,
queryFactory.selector(mixinName, selectorName));

joinTypes.put(selectorName, QueryObjectModelConstants.JCR_JOIN_TYPE_INNER);
joinConditions.put(selectorName,
queryFactory.sameNodeJoinCondition(c.getSelectorName(), selectorName, "."));
continue;
}
}

final int propertyType = jcrTools.getPropertyType(FEDORA_RESOURCE, propertyName);

if (object.isVariable()) {
Expand Down
Expand Up @@ -64,6 +64,24 @@ public void testSimpleFilterReturningJcrSubject() throws RepositoryException {

}

@Test
public void testSimpleMixinRdfTypeFilter() throws RepositoryException {

final String sparql = "SELECT ?subject WHERE { ?subject a <http://fedora.info/definitions/v4/rest-api#datastream>}";
JQLConverter testObj = new JQLConverter(session, subjects, sparql);
assertEquals("SELECT [fedoraResource_subject].[jcr:path] AS subject FROM [fedora:resource] AS [fedoraResource_subject] INNER JOIN [fedora:datastream] AS [ref_type_fedora_datastream] ON ISSAMENODE([fedoraResource_subject],[ref_type_fedora_datastream],'.')", testObj.getStatement());

}

@Test
public void testSimplePropertyRdfTypeFilter() throws RepositoryException {

final String sparql = "SELECT ?subject WHERE { ?subject a <http://some/other/uri>}";
JQLConverter testObj = new JQLConverter(session, subjects, sparql);
assertEquals("SELECT [fedoraResource_subject].[jcr:path] AS subject FROM [fedora:resource] AS [fedoraResource_subject] WHERE [fedoraResource_subject].[rdf:type] = CAST('http://some/other/uri' AS URI)", testObj.getStatement());

}

@Test
public void testDistinctFilterReturningJcrSubject() throws RepositoryException {
final String sparql = "PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT DISTINCT ?subject WHERE { ?subject dc:title \"xyz\"}";
Expand Down
Expand Up @@ -66,6 +66,7 @@ public void setUpTestData() throws RepositoryException {
fedoraResource.getNode().setProperty("fedorarelsext:hasPart", new Value[] { valueFactory.createValue(fedoraResource2.getNode()) });
fedoraResource2.getNode().setProperty("fedorarelsext:isPartOf", new Value[] { valueFactory.createValue(fedoraResource.getNode()) });
fedoraResource3.getNode().setProperty("zz:name", new Value[] { valueFactory.createValue("junk") });
fedoraResource3.getNode().setProperty("rdf:type", new Value[] { valueFactory.createValue("info:some-type") });
session.save();

}
Expand Down Expand Up @@ -102,6 +103,39 @@ public void itShouldWorkWithSimpleProperties() throws IOException, RepositoryExc
assertEquals(serverAddress + "/abc", resultSet.next().get("subject").toString());
}

@Test
public void itShouldWorkWithRdfTypeMixins() throws IOException, RepositoryException {

final String sparql = "PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?subject WHERE { ?subject a <http://fedora.info/definitions/v4/rest-api#resource> . ?subject dc:title \"xyz\"}";

String content = getResponseContent(sparql);
final ResultSet resultSet = ResultSetFactory.fromTSV(IOUtils.toInputStream(content));


assertTrue(resultSet.hasNext());

assertEquals("subject", resultSet.getResultVars().get(0));

assertEquals(serverAddress + "/abc", resultSet.next().get("subject").toString());

}

@Test
public void itShouldWorkWithRdfTypeProperties() throws IOException, RepositoryException {

final String sparql = "SELECT ?subject WHERE { ?subject a <info:some-type> }";

String content = getResponseContent(sparql);
final ResultSet resultSet = ResultSetFactory.fromTSV(IOUtils.toInputStream(content));

assertTrue(resultSet.hasNext());

assertEquals("subject", resultSet.getResultVars().get(0));

assertEquals(serverAddress + "/anobject", resultSet.next().get("subject").toString());

}

@Test
public void itShouldWorkWithReferenceProperties() throws IOException, RepositoryException {

Expand Down

0 comments on commit 61aaed0

Please sign in to comment.