Skip to content

Instantly share code, notes, and snippets.

@flyingzumwalt
Last active August 29, 2015 14:23
Show Gist options
  • Save flyingzumwalt/68208ef6a5b74a80ecdb to your computer and use it in GitHub Desktop.
Save flyingzumwalt/68208ef6a5b74a80ecdb to your computer and use it in GitHub Desktop.
navigating LDP indirect containment relationships in ActiveFedora
<http://127.0.0.1:8983/fedora/rest/dev/df/93/78/f3/df9378f3-480a-4ad8-8ee7-7c3a100ac263> a <http://www.w3.org/ns/ldp#RDFSource>,
<http://www.w3.org/ns/ldp#Container>,
<http://www.jcp.org/jcr/nt/1.0folder>,
<http://www.jcp.org/jcr/nt/1.0hierarchyNode>,
<http://www.jcp.org/jcr/nt/1.0base>,
<http://www.jcp.org/jcr/mix/1.0created>,
<http://fedora.info/definitions/v4/repository#Container>,
<http://fedora.info/definitions/v4/repository#Resource>,
<http://www.jcp.org/jcr/mix/1.0lastModified>,
<http://www.jcp.org/jcr/mix/1.0referenceable>;
<http://fedora.info/definitions/v4/repository#created> "2015-06-17T20:14:29.735Z"^^<http://www.w3.org/2001/XMLSchema#dateTime>;
<http://fedora.info/definitions/v4/repository#createdBy> "bypassAdmin";
<http://fedora.info/definitions/v4/repository#exportsAs> <http://127.0.0.1:8983/fedora/rest/dev/df/93/78/f3/df9378f3-480a-4ad8-8ee7-7c3a100ac263/fcr:export?format=jcr/xml>;
<http://fedora.info/definitions/v4/repository#hasParent> <http://127.0.0.1:8983/fedora/rest/dev>;
<http://fedora.info/definitions/v4/repository#lastModified> "2015-06-17T20:14:30.877Z"^^<http://www.w3.org/2001/XMLSchema#dateTime>;
<http://fedora.info/definitions/v4/repository#lastModifiedBy> "bypassAdmin";
<http://fedora.info/definitions/v4/repository#mixinTypes> "fedora:Container",
"fedora:Resource";
<http://fedora.info/definitions/v4/repository#primaryType> "nt:folder";
<http://fedora.info/definitions/v4/repository#uuid> "94b3c149-166a-407d-8393-cfeccbe50a7d";
<http://fedora.info/definitions/v4/repository#writable> true;
<http://pcdm.org/models#hasMember> <http://127.0.0.1:8983/fedora/rest/dev/ba/34/67/07/ba346707-4d84-41ab-84b9-2300c31ec3f7>;
<http://www.iana.org/assignments/link-relations/first> <http://127.0.0.1:8983/fedora/rest/dev/df/93/78/f3/df9378f3-480a-4ad8-8ee7-7c3a100ac263/related_objects/c0a7d413-47a2-4ebf-aa14-3384e8af86bd>;
<http://www.iana.org/assignments/link-relations/last> <http://127.0.0.1:8983/fedora/rest/dev/df/93/78/f3/df9378f3-480a-4ad8-8ee7-7c3a100ac263/related_objects/c0a7d413-47a2-4ebf-aa14-3384e8af86bd>;
<http://www.w3.org/ns/ldp#contains> <http://127.0.0.1:8983/fedora/rest/dev/df/93/78/f3/df9378f3-480a-4ad8-8ee7-7c3a100ac263/related_objects>;
<info:fedora/fedora-system:def/model#hasModel> "FooHistory" .
<http://127.0.0.1:8983/fedora/rest/dev/df/93/78/f3/df9378f3-480a-4ad8-8ee7-7c3a100ac263/fcr:export?format=jcr/xml> <http://purl.org/dc/elements/1.1/format> <http://fedora.info/definitions/v4/repository#jcr/xml> .
<http://fedora.info/definitions/v4/repository#jcr/xml> <http://www.w3.org/2000/01/rdf-schema#label> "jcr/xml" .
require 'active-fedora'
require 'active_fedora/aggregation'
class FooHistory < ActiveFedora::Base
# aggregates is similar to calling:
# indirectly_contains :related_objects, class_name: 'ContainedObject', has_member_relation: ::RDF::URI.new('http://www.openarchives.org/ore/terms/aggregates'), inserted_content_relation: ::RDF::URI.new('http://www.openarchives.org/ore/terms/proxyFor'), through: 'ActiveFedora::Aggregation::Proxy', foreign_key: :proxy_for
aggregates :related_objects, predicate: ::RDF::URI.new('http://pcdm.org/models#hasMember'),
class_name: "ActiveFedora::Base"
end
class ContainedObject < ActiveFedora::Base
has_many :containers, class_name:'FooHistory', predicate: ::RDF::URI.new('http://www.openarchives.org/ore/terms/aggregates') #, key:'related_objects'
belongs_to :forest, class_name:'FooHistory', predicate: ::RDF::URI.new('http://www.openarchives.org/ore/terms/aggregates')
property :title, predicate: ::RDF::DC.title, multiple: false
end
container = FooHistory.new
contained_object = ContainedObject.new
container.related_objects << contained_object
container.save
container.reload
contained_object.reload
puts contained_object.uri
# => "http://localhost:8983/fedora/rest/test/7c/a7/9d/5a/7ca79d5a-1c87-4af7-aa91-5a08a8108f4d"
puts container.uri
# => "http://localhost:8983/fedora/rest/test/4d/c0/f8/69/4dc0f869-ba09-4d8b-80f4-717784463bc2"
# confirm that the info is in the contained_object's rdf info from Fedora 4
contained_object.resource.dump(:ttl)
# (among other info) "\n<http://localhost:8983/fedora/rest/test/f1/81/3b/7c/f1813b7c-dcdb-40c2-a12f-4edb6c08a1fd/related_objects/696819ae-aefd-4d73-bfba-fcd9e6aaedfe> <http://www.openarchives.org/ore/terms/proxyFor> <http://localhost:8983/fedora/rest/test/1b/80/74/3e/1b80743e-b93c-452b-9787-22afb230d6f2>"
# contained_object.resource.query(predicate: RDF::Vocab::ORE.proxyFor, object: RDF::URI(contained_object.uri)).subjects.to_a.map{|proxy_uri| ActiveFedora::Aggregation::Proxy.find(ActiveFedora::Base.uri_to_id(proxy_uri.to)s))}.map(&:target)
proxy_uris = contained_object.resource.query(predicate: RDF::Vocab::ORE.proxyFor, object: RDF::URI(contained_object.uri)).subjects.to_a
proxy_objects = proxy_uris.map{|proxy_uri| ActiveFedora::Aggregation::Proxy.find(ActiveFedora::Base.uri_to_id(proxy_uri.to_s))}
proxy_objects.map(&:containers)
# => [[]] # the proxy object can't find its container.
# (Doesn't help)
contained_object.resource.query(predicate: RDF::Vocab::ORE.proxyFor).map { |result| ActiveFedora::Base.find(ActiveFedora::Base.uri_to_id(result.object.to_s))}
# => [#<ContainedObject id: "7c/a7/9d/5a/7ca79d5a-1c87-4af7-aa91-5a08a8108f4d", title: nil, container_ids: [], forest_id: nil>]
# uses https://github.com/projecthydra/active_fedora/blob/master/lib/active_fedora/associations/contained_finder.rb#L36-L38
container.related_objects.proxy_association.send(:record_proxy_finder).send(:relation_subjects, contained_object)
#=> [#<RDF::URI:0x3fd3a515a08c URI:http://localhost:8983/fedora/rest/test/4d/c0/f8/69/4dc0f869-ba09-4d8b-80f4-717784463bc2/related_objects/f7e45700-6a34-4645-8025-99ccec69c106>]
<http://127.0.0.1:8983/fedora/rest/dev/df/93/78/f3/df9378f3-480a-4ad8-8ee7-7c3a100ac263> <http://www.iana.org/assignments/link-relations/first> <http://127.0.0.1:8983/fedora/rest/dev/df/93/78/f3/df9378f3-480a-4ad8-8ee7-7c3a100ac263/related_objects/c0a7d413-47a2-4ebf-aa14-3384e8af86bd>;
<http://www.iana.org/assignments/link-relations/last> <http://127.0.0.1:8983/fedora/rest/dev/df/93/78/f3/df9378f3-480a-4ad8-8ee7-7c3a100ac263/related_objects/c0a7d413-47a2-4ebf-aa14-3384e8af86bd> .
<http://127.0.0.1:8983/fedora/rest/dev/df/93/78/f3/df9378f3-480a-4ad8-8ee7-7c3a100ac263/related_objects/c0a7d413-47a2-4ebf-aa14-3384e8af86bd/fcr:export?format=jcr/xml> <http://purl.org/dc/elements/1.1/format> <http://fedora.info/definitions/v4/repository#jcr/xml> .
<http://fedora.info/definitions/v4/repository#jcr/xml> <http://www.w3.org/2000/01/rdf-schema#label> "jcr/xml" .
<http://127.0.0.1:8983/fedora/rest/dev/df/93/78/f3/df9378f3-480a-4ad8-8ee7-7c3a100ac263/related_objects/c0a7d413-47a2-4ebf-aa14-3384e8af86bd> a <http://www.w3.org/ns/ldp#RDFSource>,
<http://www.w3.org/ns/ldp#Container>,
<http://www.jcp.org/jcr/nt/1.0folder>,
<http://www.jcp.org/jcr/nt/1.0hierarchyNode>,
<http://www.jcp.org/jcr/nt/1.0base>,
<http://www.jcp.org/jcr/mix/1.0created>,
<http://fedora.info/definitions/v4/repository#Container>,
<http://www.openarchives.org/ore/terms/Proxy>,
<http://fedora.info/definitions/v4/repository#Resource>,
<http://www.jcp.org/jcr/mix/1.0lastModified>,
<http://www.jcp.org/jcr/mix/1.0referenceable>;
<http://fedora.info/definitions/v4/repository#created> "2015-06-17T20:14:30.352Z"^^<http://www.w3.org/2001/XMLSchema#dateTime>;
<http://fedora.info/definitions/v4/repository#createdBy> "bypassAdmin";
<http://fedora.info/definitions/v4/repository#exportsAs> <http://127.0.0.1:8983/fedora/rest/dev/df/93/78/f3/df9378f3-480a-4ad8-8ee7-7c3a100ac263/related_objects/c0a7d413-47a2-4ebf-aa14-3384e8af86bd/fcr:export?format=jcr/xml>;
<http://fedora.info/definitions/v4/repository#hasParent> <http://127.0.0.1:8983/fedora/rest/dev/df/93/78/f3/df9378f3-480a-4ad8-8ee7-7c3a100ac263/related_objects>;
<http://fedora.info/definitions/v4/repository#lastModified> "2015-06-17T20:14:30.877Z"^^<http://www.w3.org/2001/XMLSchema#dateTime>;
<http://fedora.info/definitions/v4/repository#lastModifiedBy> "bypassAdmin";
<http://fedora.info/definitions/v4/repository#mixinTypes> "fedora:Container",
"ns011:Proxy",
"fedora:Resource";
<http://fedora.info/definitions/v4/repository#primaryType> "nt:folder";
<http://fedora.info/definitions/v4/repository#uuid> "55e05d68-5e13-4072-a639-b8ae4f9992c4";
<http://fedora.info/definitions/v4/repository#writable> true;
<http://www.openarchives.org/ore/terms/proxyFor> <http://127.0.0.1:8983/fedora/rest/dev/ba/34/67/07/ba346707-4d84-41ab-84b9-2300c31ec3f7>;
<info:fedora/fedora-system:def/model#hasModel> "ActiveFedora::Aggregation::Proxy" .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment