Class: ActiveFedora::RelsExtDatastream

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

Instance Attribute Summary collapse

Attributes inherited from Datastream

#digital_object, #last_modified

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Datastream

#create, #initialize, #inspect, #label, #profile_from_hash, #realLabel, #save, #solrize_profile, #to_param, #to_solr, #validate_content_present

Constructor Details

This class inherits a constructor from ActiveFedora::Datastream

Instance Attribute Details

#modelObject

Returns the value of attribute model.



8
9
10
# File 'lib/active_fedora/rels_ext_datastream.rb', line 8

def model
  @model
end

Class Method Details

.default_attributesObject



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

def self.default_attributes
  super.merge(:controlGroup => 'X', :mimeType => 'application/rdf+xml')
end

.ensure_predicates_exist!(xml) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/active_fedora/rels_ext_datastream.rb', line 76

def self.ensure_predicates_exist!(xml)
  statements = Nokogiri::XML(xml).xpath('//rdf:Description/*', 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
  predicates = statements.collect { |e| { :prefix => e.namespace.prefix, :uri => e.namespace.href, :predicate => e.name } }.uniq
  predicates.each do |pred|
    unless Predicates.predicate_mappings[pred[:uri]]
      Predicates.predicate_mappings[pred[:uri]] = {}
      if pred[:prefix] and not Predicates.predicate_namespaces.has_value?(pred[:uri])
        Predicates.predicate_namespaces[pred[:prefix].to_sym] = pred[:uri]
      end
    end
    ns = Predicates.predicate_mappings[pred[:uri]]
    unless ns.invert[pred[:predicate]]
      ns["#{pred[:prefix]}_#{pred[:predicate].underscore}".to_sym] = pred[:predicate]
    end
  end
end

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/active_fedora/rels_ext_datastream.rb', line 47

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

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


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

def changed?
  relationships_are_dirty? or super
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::FieldMapper) to map solr key to relationship predicate.

Warning

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


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/active_fedora/rels_ext_datastream.rb', line 102

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

#metadata?Boolean

Returns:

  • (Boolean)


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

def metadata?
  true
end

#relationshipsObject



39
40
41
# File 'lib/active_fedora/rels_ext_datastream.rb', line 39

def relationships
  model.relationships
end

#relationships_are_dirty!Object



31
32
33
# File 'lib/active_fedora/rels_ext_datastream.rb', line 31

def relationships_are_dirty!
  model.relationships_are_dirty = true
end

#relationships_are_dirty?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/active_fedora/rels_ext_datastream.rb', line 27

def relationships_are_dirty?
  model.relationships_are_dirty if model
end

#relationships_are_not_dirty!Object



35
36
37
# File 'lib/active_fedora/rels_ext_datastream.rb', line 35

def relationships_are_not_dirty!
  model.relationships_are_dirty = false
end

#serialize!Object



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

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

#to_rels_extObject

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



66
67
68
69
70
71
72
73
74
# File 'lib/active_fedora/rels_ext_datastream.rb', line 66

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