Class: ActiveFedora::RelsExtDatastream

Inherits:
Datastream show all
Includes:
SemanticNode, Solrizer::FieldNameMapper
Defined in:
lib/active_fedora/rels_ext_datastream.rb

Instance Attribute Summary

Attributes included from SemanticNode

#internal_uri, #load_from_solr, #named_relationship_desc, #relationships_are_dirty

Attributes inherited from Datastream

#dirty, #fields, #last_modified

Attributes inherited from Fedora::BaseObject

#attributes, #blob, #errors, #new_object, #uri

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SemanticNode

#add_named_relationship, #add_relationship, #assert_kind_of, #assert_kind_of_model, #class_from_name, #inbound_named_relationship_predicates, #inbound_relationship_names, #inbound_relationships, included, #is_named_relationship?, #kind_of_model?, #named_inbound_relationships, #named_outbound_relationships, #named_relationship, #named_relationship_predicates, #named_relationship_predicates_from_class, #named_relationship_type, #named_relationships, #named_relationships_desc, #named_relationships_desc_from_class, #named_relationships_from_class, #outbound_named_relationship_predicates, #outbound_relationship_names, #outbound_relationships, #register_named_relationship, #register_named_subject, #register_predicate, #register_subject, #register_triple, #relationship_exists?, #relationship_names, #relationships, #relationships_from_class, #remove_named_relationship, #remove_relationship, #to_rels_ext, #unregister_triple

Methods included from MediaShelfClassLevelInheritableAttributes

included

Methods inherited from Datastream

#after_save, #before_save, #check_concurrency, #content, #content=, #delete, delete, #dirty?, #dsid=, #last_modified_in_repository, #pid, #size, #to_param

Methods inherited from Fedora::Datastream

#control_group, #control_group=, #dsid, #label, #label=, #mime_type, #mime_type=, #pid, #uri, #url

Methods inherited from Fedora::BaseObject

#[], #new_object?

Constructor Details

#initialize(attrs = nil) ⇒ RelsExtDatastream

Returns a new instance of RelsExtDatastream.



11
12
13
14
# File 'lib/active_fedora/rels_ext_datastream.rb', line 11

def initialize(attrs=nil)
  super
  self.dsid = "RELS-EXT"
end

Class Method Details

.from_xml(tmpl, node) ⇒ Object

Populate a RelsExtDatastream object based on the “datastream” node from a FOXML file Assumes that the datastream contains RDF XML from a Fedora RELS-EXT datastream

Parameters:

  • tmpl (ActiveFedora::MetadataDatastream)

    the Datastream object that you are populating

  • node (Nokogiri::XML::Node)

    the “foxml:datastream” node from a FOXML file



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/active_fedora/rels_ext_datastream.rb', line 37

def self.from_xml(tmpl, node) 
  # node.xpath("./foxml:datastreamVersion[last()]/foxml:xmlContent/rdf:RDF/rdf:Description/*").each do |f|
  node.xpath("./foxml:datastreamVersion[last()]/foxml:xmlContent/rdf:RDF/rdf:Description/*", {"rdf"=>"http://www.w3.org/1999/02/22-rdf-syntax-ns#", "foxml"=>"info:fedora/fedora-system:def/foxml#"}).each do |f|
      if f.namespace
        ns_mapping = self.predicate_mappings[f.namespace.href]
        predicate = ns_mapping ?  ns_mapping.invert[f.name] : nil
        predicate = "#{f.namespace.prefix}_#{f.name}" if predicate.nil?
      else
        logger.warn "You have a predicate without a namespace #{f.name}. Verify your rels-ext is correct."
        predicate = "#{f.name}"
      end
      is_obj = f["resource"]
      object = is_obj ? f["resource"] : f.inner_text
      r = ActiveFedora::Relationship.new(:subject=>:self, :predicate=>predicate, :object=>object, :is_literal=>!is_obj)
      tmpl.add_relationship(r)
  end
  tmpl.send(:dirty=, false)
  tmpl
end

Instance Method Details

#from_solr(solr_doc) ⇒ Object

** EXPERIMENTAL **

This is utilized by ActiveFedora::Base.load_instance_from_solr to load the relationships hash using the Solr document passed in instead of from the RELS-EXT datastream in Fedora. Utilizes solr_name method (provided by Solrizer::FieldNameMapper) to map solr key to relationship predicate.

Warning

Solr must be synchronized with RELS-EXT data in Fedora.


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/active_fedora/rels_ext_datastream.rb', line 81

def from_solr(solr_doc)
  #cycle through all possible predicates
  self.class.predicate_mappings[self.class.default_predicate_namespace].keys.each do |predicate|
    predicate_symbol = ActiveFedora::SolrService.solr_name(predicate, :symbol)
    value = (solr_doc[predicate_symbol].nil? ? solr_doc[predicate_symbol.to_s]: solr_doc[predicate_symbol]) 
    unless value.nil? 
      if value.is_a? Array
        value.each do |obj|
          o_uri = URI.parse(obj)
          literal = o_uri.scheme.nil?
          r = ActiveFedora::Relationship.new(:subject=>:self, :predicate=>predicate, :object=>obj, :is_literal=>literal)
          add_relationship(r)
        end
      else
        o_uri = URI.parse(value)
        literal = o_uri.scheme.nil?
        r = ActiveFedora::Relationship.new(:subject=>:self, :predicate=>predicate, :object=>value, :is_literal=>literal)
        add_relationship(r)
      end
    end
  end
  @load_from_solr = true
end

#pid=(pid) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/active_fedora/rels_ext_datastream.rb', line 23

def pid=(pid)
  super
  self.blob = <<-EOL
  <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <rdf:Description rdf:about="info:fedora/#{pid}">
    </rdf:Description>
  </rdf:RDF>
  EOL
end

#saveObject



16
17
18
19
20
21
# File 'lib/active_fedora/rels_ext_datastream.rb', line 16

def save
  if @dirty == true
    self.content = to_rels_ext(self.pid)
  end
  super
end

#to_solr(solr_doc = Hash.new) ⇒ Object

Serialize the datastream’s RDF relationships to solr

Parameters:

  • solr_doc (Hash) (defaults to: Hash.new)

    @deafult an empty Hash



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/active_fedora/rels_ext_datastream.rb', line 59

def to_solr(solr_doc = Hash.new)
  self.relationships.each_pair do |subject, predicates|
    if subject == :self || subject == "info:fedora/#{self.pid}"
      predicates.each_pair do |predicate, values|
        values.each do |val|
          ::Solrizer::Extractor.insert_solr_field_value(solr_doc, solr_name(predicate, :symbol), val )
        end
      end
    end
  end
  return solr_doc
end