Class: ActiveFedora::RelsExtDatastream

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

Instance Attribute Summary collapse

Attributes inherited from Datastream

#digital_object, #dirty, #fields, #last_modified

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Datastream

#add_ds_location, #create, #dirty?, #initialize, #inspect, #new_object?, #profile_from_hash, #save, #size, #solrize_profile, #to_param, #validate_content_present

Constructor Details

This class inherits a constructor from ActiveFedora::Datastream

Instance Attribute Details

#modelObject

Returns the value of attribute model.



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

def model
  @model
end

Class Method Details

.from_xml(xml, tmpl) ⇒ Object

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

Parameters:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/active_fedora/rels_ext_datastream.rb', line 32

def self.from_xml(xml, tmpl) 
  if (xml.nil?)
    ### maybe put the template here?
  else
    RDF::RDFXML::Reader.new(xml) do |reader|
      reader.each_statement do |statement|
        literal = statement.object.kind_of?(RDF::Literal)
        predicate = self.short_predicate(statement.predicate)
        object = literal ? statement.object.value : statement.object.to_str
        tmpl.model.add_relationship(predicate, object, literal)
      end
    end
    tmpl.model.relationships_are_dirty = false
    tmpl
  end
end

.short_predicate(predicate) ⇒ Object



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

def self.short_predicate(predicate)
  # for this regex to short-circuit correctly, namespaces must be sorted into descending order by length
  if match = /^(#{Predicates.predicate_mappings.keys.sort.reverse.join('|')})(.+)$/.match(predicate.to_str)
    namespace = match[1]
    predicate = match[2]
    pred = Predicates.predicate_mappings[namespace].invert[predicate]
    pred
  else
    raise "Unable to parse predicate: #{predicate}"
  end
end

Instance Method Details

#add_mime_typeObject



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

def add_mime_type
  self.mimeType= 'application/rdf+xml'
end

#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.


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

def from_solr(solr_doc)
  #cycle through all possible predicates
  model.relationships_loaded = true
  Predicates.predicate_mappings.each_pair do |namespace,predicates|
    predicates.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|
            model.add_relationship(predicate, obj)
          end
        else
          model.add_relationship(predicate, value)
        end
      end
    end
  end
  @load_from_solr = true
end

#serialize!Object



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

def serialize!
  self.content = to_rels_ext() if model.relationships_are_dirty
  model.relationships_are_dirty = false
end

#to_rels_extObject

Creates a RELS-EXT datastream for insertion into a Fedora Object



50
51
52
53
54
55
56
57
58
# File 'lib/active_fedora/rels_ext_datastream.rb', line 50

def to_rels_ext()
  xml = ActiveFedora::RDFXMLWriter.buffer do |writer|
    writer.prefixes.merge! ActiveFedora::Predicates.predicate_namespaces
    model.relationships.each_statement do |statement|
      writer << statement
    end
  end
  xml
end

#to_xml(fields_xml) ⇒ Object



23
24
25
26
# File 'lib/active_fedora/rels_ext_datastream.rb', line 23

def to_xml(fields_xml) 
  ActiveSupport::Deprecation.warn("to_xml is deprecated, use to_rels_ext")
  to_rels_ext() 
end