Class: Cul::Hydra::Datastreams::RelsInt

Inherits:
ActiveFedora::Datastream
  • Object
show all
Defined in:
app/models/cul/hydra/datastreams/rels_int.rb

Defined Under Namespace

Classes: UriObject

Constant Summary collapse

XSD_INT_BITLENGTH =
31

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#relationships_loadedObject

Returns the value of attribute relationships_loaded.



10
11
12
# File 'app/models/cul/hydra/datastreams/rels_int.rb', line 10

def relationships_loaded
  @relationships_loaded
end

Class Method Details

.xml_templateObject



134
135
136
# File 'app/models/cul/hydra/datastreams/rels_int.rb', line 134

def self.xml_template
  "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"></rdf:RDF>"
end

Instance Method Details

#add_relationship(datastream, predicate, target, literal = false) ⇒ Object



82
83
84
85
86
# File 'app/models/cul/hydra/datastreams/rels_int.rb', line 82

def add_relationship(datastream, predicate, target, literal=false)
  stmt = build_statement(datastream, predicate, target, literal)
  graph.insert(stmt) unless graph.has_statement? stmt
  relationships_will_change!
end

#build_statement(datastream, predicate, object, literal = false) ⇒ Object



75
76
77
78
79
80
# File 'app/models/cul/hydra/datastreams/rels_int.rb', line 75

def build_statement(datastream, predicate, object, literal=false)
  subject = to_resource(datastream)
  predicate = to_predicate(predicate)
  object = to_resource(object,literal)
  ::RDF::Statement.new(subject,predicate,object)
end

#clear_relationship(datastream, predicate) ⇒ Object



94
95
96
97
# File 'app/models/cul/hydra/datastreams/rels_int.rb', line 94

def clear_relationship(datastream, predicate)
  graph.delete [to_resource(datastream), to_predicate(predicate), nil]
  relationships_will_change!
end

#contentObject



23
24
25
26
27
28
29
# File 'app/models/cul/hydra/datastreams/rels_int.rb', line 23

def content
  if self.new? and @content.nil?
    content= RelsInt.xml_template
  else
    super
  end
end

#content=(new_content) ⇒ Object



31
32
33
34
35
# File 'app/models/cul/hydra/datastreams/rels_int.rb', line 31

def content= new_content
  super
  relationships_loaded=false
  load_relationships
end

#from_solr(solr_doc) ⇒ Object



144
145
146
147
148
# File 'app/models/cul/hydra/datastreams/rels_int.rb', line 144

def from_solr(solr_doc)
  predicate_symbol = self.profile_solr_name
  value = (solr_doc[predicate_symbol].nil? ? solr_doc[predicate_symbol.to_s]: solr_doc[predicate_symbol])
  @solr_hash = value.blank? ? nil : JSON.parse(value[0])
end

#graphObject



121
122
123
# File 'app/models/cul/hydra/datastreams/rels_int.rb', line 121

def graph
  @graph ||= load_relationships
end

#load_relationshipsObject



110
111
112
113
114
115
116
117
118
119
# File 'app/models/cul/hydra/datastreams/rels_int.rb', line 110

def load_relationships
  # load from content
  g = ::RDF::Graph.new
  ::RDF::RDFXML::Reader.new(content).each do |stmt|
    g << stmt
  end
  self.relationships_loaded = true
  clear_attribute_changes 'relationships'
  @graph = g
end

#relationships(*args) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'app/models/cul/hydra/datastreams/rels_int.rb', line 99

def relationships(*args)
  q_args = args.empty? ? [:s, :p, :o] : [to_resource(args.first), to_predicate(args[1]), (args[2] || :o)]
  query = ::RDF::Query.new do |query|
    query << q_args
  end
  query.execute(graph).map(&:to_hash).map do |hash|
    stmt = q_args.map {|k| hash[k] || k}
    ::RDF::Statement.new(*stmt)
  end
end

#relationships_will_change!Object



19
20
21
# File 'app/models/cul/hydra/datastreams/rels_int.rb', line 19

def relationships_will_change!
  changed_attributes['relationships'] = nil
end

#remove_relationship(datastream, predicate, target, literal = false) ⇒ Object



88
89
90
91
92
# File 'app/models/cul/hydra/datastreams/rels_int.rb', line 88

def remove_relationship(datastream, predicate, target, literal=false)
  stmt = build_statement(datastream, predicate, target, literal)
  graph.delete(stmt)
  relationships_will_change!
end

#serialize!Object



14
15
16
17
# File 'app/models/cul/hydra/datastreams/rels_int.rb', line 14

def serialize!
  self.content = to_rels_int() if changed_attributes.include? 'relationships'
  clear_attribute_changes 'relationships'
end

#solrize_relationships(solr_doc = Hash.new) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/models/cul/hydra/datastreams/rels_int.rb', line 150

def solrize_relationships(solr_doc=Hash.new)
  rel_hash = {} # the rels_int_profile is a hash of hashes in json
  graph.each_statement do |statement|
    predicate = ActiveFedora::Predicates.short_predicate(statement.predicate)
    literal = statement.object.kind_of?(::RDF::Literal)
    val = literal ? statement.object.value : statement.object.to_str
    rel_hash[statement.subject] ||= {}
    rel_hash[statement.subject][predicate] ||= []
    rel_hash[statement.subject][predicate] << val
  end
  solr_doc[self.class.profile_solr_name] = rel_hash.to_json unless rel_hash.blank?
  solr_doc
end

#to_predicate(arg) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'app/models/cul/hydra/datastreams/rels_int.rb', line 64

def to_predicate(arg)
  return :p if arg.nil?
  if arg.is_a? Symbol
    arg = ActiveFedora::Predicates.find_graph_predicate(arg)
  elsif arg.is_a? ::RDF::Resource
    arg
  else
    ::RDF::URI.new(arg.to_s)
  end
end

#to_rels_intObject



125
126
127
128
129
130
131
132
# File 'app/models/cul/hydra/datastreams/rels_int.rb', line 125

def to_rels_int
  xml = Cul::Hydra::RelsInt::RDFXMLWriter.buffer(:max_depth=>1) do |writer|
    graph.each_statement do |statement|
      writer << statement
    end
  end
  xml
end

#to_resource(object, literal = false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/cul/hydra/datastreams/rels_int.rb', line 37

def to_resource(object, literal=false)
  if object.is_a? ActiveFedora::Datastream
    ::RDF::URI.new("info:fedora/#{object.pid}/#{object.dsid}")
  elsif object.respond_to? :internal_uri
    ::RDF::URI.new(object.internal_uri)
  elsif object.is_a? ::RDF::Resource
    object
  elsif literal
    result = ::RDF::Literal.new(object)
    case # invalid datatypes for FCRepo 3
    when result.datatype.eql?(::RDF::XSD.integer)
      result.datatype = (signed_bit_length(result.object) > XSD_INT_BITLENGTH ? ::RDF::XSD.long : ::RDF::XSD.int)
    when result.datatype.eql?(::RDF::XSD.decimal)
      result.datatype = ::RDF::XSD.double
    when result.datatype.eql?(::RDF::XSD.boolean)
      result.datatype = nil
    when result.datatype.eql?(::RDF::XSD.date)
      result = ::RDF::Literal.new(object.to_datetime)
    when result.datatype.eql?(::RDF::XSD.time)
      result = ::RDF::Literal.new(object.to_datetime)
    end
    result
  else
    ::RDF::URI.new(object.to_s)
  end
end

#to_solr(solr_doc = Hash.new) ⇒ Object



138
139
140
141
142
# File 'app/models/cul/hydra/datastreams/rels_int.rb', line 138

def to_solr(solr_doc=Hash.new)
  result = super(solr_doc)
  result = solrize_relationships(result)
  result
end