This repository has been archived by the owner on Jul 21, 2020. It is now read-only.
/
asset.rb
453 lines (394 loc) · 17.7 KB
/
asset.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
class Asset < ActiveRecord::Base
include PbcoreXmlElement
before_create :generate_uuid
after_save :save_version
attr_protected :uuid
# IMPORTANT: This has to be kept in sync with ANY SCHEMA CHANGES!
ALL_INCLUDES = [{:identifiers => [:identifier_source]},
{:titles => [:title_type]},
{:asset_dates => [:asset_date_type]},
:subjects,
{:descriptions => [:description_type]},
:genres,
{:relations => [:relation_type]},
:coverages,
:audience_levels,
:audience_ratings,
{:creators => [:creator_role]},
{:contributors => [:contributor_role]},
{:publishers => [:publisher_role]},
:rights_summaries,
:annotations,
:extensions,
{:instantiations => [{:format_ids => :format_identifier_source},
:format,
{:instantiation_dates => [:instantiation_date_type]},
:instantiation_dimensions,
{:instantiation_relations => [:relation_type]},
:instantiation_media_type,
:instantiation_generations,
:instantiation_color,
:instantiation_rights_summaries,
{:essence_tracks => [:essence_track_type,
:annotations,
{:essence_track_identifiers => :essence_track_identifier_source}]},
:annotations]}
]
UUID_REGEX = /[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/
# Intellectual Content
has_many :identifiers, :dependent => :destroy
has_many :titles, :dependent => :destroy
has_many :asset_dates, :dependent => :destroy
has_and_belongs_to_many :subjects
has_many :descriptions, :dependent => :destroy
has_and_belongs_to_many :genres
has_many :relations, :dependent => :destroy
has_many :coverages, :dependent => :destroy
has_and_belongs_to_many :audience_levels
has_and_belongs_to_many :audience_ratings
# Intellectual Property
has_many :creators, :dependent => :destroy
has_many :contributors, :dependent => :destroy
has_many :publishers, :dependent => :destroy
has_many :rights_summaries, :dependent => :destroy
# Instantiations
has_many :instantiations, :dependent => :destroy
# Annotations
has_many :annotations, :as => :container, :dependent => :destroy
# Extensions
has_many :extensions, :dependent => :destroy
has_many :versions, :dependent => :delete_all
stampable
accepts_nested_attributes_for :identifiers, :allow_destroy => true
accepts_nested_attributes_for :titles, :allow_destroy => true
accepts_nested_attributes_for :descriptions, :allow_destroy => true
accepts_nested_attributes_for :relations, :allow_destroy => true
accepts_nested_attributes_for :coverages, :allow_destroy => true
accepts_nested_attributes_for :creators, :allow_destroy => true
accepts_nested_attributes_for :contributors, :allow_destroy => true
accepts_nested_attributes_for :publishers, :allow_destroy => true
accepts_nested_attributes_for :rights_summaries, :allow_destroy => true
accepts_nested_attributes_for :annotations, :allow_destroy => true
accepts_nested_attributes_for :extensions, :allow_destroy => true
accepts_nested_attributes_for :asset_dates, :allow_destroy => true
validates_size_of :identifiers, :minimum => 1, :message => "must have at least one entry"
validates_size_of :titles, :minimum => 1, :message => "must have at least one entry"
# IMPORTANT: This has to be kept in sync with ANY SCHEMA CHANGES also order
# matters here.
# PbcoreXmlElement Declarations
# Intellectual Content
xml_subelements "pbcoreAssetDate", :asset_dates
xml_subelements "pbcoreIdentifier", :identifiers
to_xml_elt do |obj|
xml = obj._working_xml
identif = XML::Node.new("pbcoreIdentifier", obj.uuid)
identif["source"] = "pbcore XML database UUID"
xml << identif
end
xml_subelements "pbcoreTitle", :titles
xml_subelements "pbcoreSubject", :subjects
xml_subelements "pbcoreDescription", :descriptions
xml_subelements "pbcoreGenre", :genres
xml_subelements "pbcoreRelation", :relations
xml_subelements "pbcoreCoverage", :coverages
xml_subelements_picklist "pbcoreAudienceLevel", "audienceLevel", :audience_levels
xml_subelements_picklist "pbcoreAudienceRating", "audienceRating", :audience_ratings
xml_subelements "pbcoreAnnotation", :annotations
# Intellectual Property
xml_subelements "pbcoreCreator", :creators
xml_subelements "pbcoreContributor", :contributors
xml_subelements "pbcorePublisher", :publishers
xml_subelements "pbcoreRightsSummary", :rights_summaries
# Extensions
xml_subelements "pbcoreExtension", :extensions
# Instantiation
xml_subelements "pbcoreInstantiation", :instantiations
# Sunspot/Solr definitions
searchable do
text :identifier do
identifiers.map{|a| "#{a.identifier} #{a.identifier_source.name}"} +
(instantiations.map{|a| a.format_ids.map(&:format_identifier)}.flatten)
end
text(:title, :boost => 1.2) { titles.map(&:title) }
text(:subject) { subjects.map(&:subject) }
text(:description) { descriptions.map(&:description) }
text(:genre) { genres.map(&:name) }
text(:relation) { relations.map(&:relation_identifier) }
text(:coverage) { coverages.map(&:coverage) }
text(:audience_level) { audience_levels.map(&:name) }
text(:audience_rating) { audience_ratings.map(&:name) }
text(:creator) { creators.map(&:creator) }
text(:contributor) { contributors.map(&:contributor) }
text(:publisher) { publishers.map(&:publisher) }
text(:rights) { rights_summaries.map(&:rights_summary) }
text(:extension) { extensions.map{|a| "#{a.extension} #{a.extension_authority_used}"} }
text(:location) { instantiations.map{|a| a.format_location} }
text(:annotation) do
(
instantiations.map{|a| a.annotations.map{|b| b.annotation}} +
instantiations.map{|a| a.essence_tracks.map{|b| b.annotation}}
).flatten
end
text(:date) do
(
asset_dates.map(&:asset_date) +
instantiations.map{|a| a.instantiation_dates.map{|b| b.date}}
).flatten
end
text(:format) { instantiations.map{|a| a.format.nil? ? '' : a.format.name} }
time :created_at
time :updated_at
string :uuid
boolean(:online_asset) { self.online? }
dynamic_string :facets, :multiple => true do
PBCore.config['facets'].inject({}) do |hash, facet|
key = facet[0].to_sym
value = []
case facet[1]
when "map"
value = send(facet[2].to_sym).map(&facet[3].to_sym)
when "simple_map"
value = send(facet[0].downcase.pluralize.to_sym).map(&facet[0].downcase.to_sym)
when "if_map"
value = send(facet[2].to_sym).select{|p|
subelt = p.send(facet[4].to_sym)
!(subelt.nil?) && ((subelt.respond_to?(:name) ? subelt.name : subelt) == facet[5])
}.map(&facet[3].to_sym)
when "format_location"
value = instantiations.map{|a| a.format_location}.select{|fml| !(fml.index('/') || fml.match(UUID_REGEX))}
end
hash.merge(key => value)
end
end
end
# This is used by the new form to do subject adding/removing autocompletion
# using jquery token input plugin
attr_reader :subject_tokens
def subject_tokens=(ids)
self.subject_ids = ids.split(",")
end
attr_reader :genre_tokens
def genre_tokens=(ids)
self.genre_ids = ids.split(",")
end
def to_xml
doc = XML::Document.new
root = XML::Node.new("pbcoreDescriptionDocument")
PbcoreXmlElement::Util.set_pbcore_ns(root)
xsins = XML::Namespace.new(root, "xsi", "http://www.w3.org/2001/XMLSchema-instance")
schemaloc = XML::Attr.new(root, "schemaLocation", "http://www.pbcore.org/PBCore/PBCoreNamespace.html http://pbcore.org/xsd/pbcore-2.0.xsd")
schemaloc.namespaces.namespace = xsins
doc.root = root
root << XML::Node.new_comment("XML Generated at #{Time.new} by rails pbcore database")
root << XML::Node.new_comment(created_string) if respond_to?(:created_at)
root << XML::Node.new_comment(updated_string) if respond_to?(:updated_at)
build_xml(root)
doc.to_s
end
def title
titles.map{|t| t.title}.join("; ")
end
def identifier
result = identifiers.select{|id| id.identifier_source.show_in_index?}
(result.empty? ? identifiers : result).map{|id| id.identifier}.join(" / ")
end
def online?
instantiations.any?(&:online?)
end
def has_thumbnail?
instantiations.any?(&:thumbnail?)
end
def thumbnail
instantiations.detect(&:thumbnail?)
end
# import some XML
# returns an array. the first elt is an array of UUIDs of successful
# imports; the second is an array of error messages.
def self.import_xml(stream_or_string, fn = "input")
begin
if (stream_or_string.respond_to?(:read))
parser = XML::Parser.io(stream_or_string)
else
parser = XML::Parser.string(stream_or_string)
end
xmldoc = parser.parse
rescue Exception => ex
return [[], [ex.to_s]]
end
docs = xmldoc.find("/pbcore:pbcoreDescriptionDocument|/pbcore:PBCoreDescriptionDocument|/pbcore:pbcoreCollection/pbcore:pbcoreDescriptionDocument", PbcoreXmlElement::PBCORE_NAMESPACE)
if docs.size == 0
[[], ["no PBCore Description Documents found"]]
else
count = 0
successes = []
errors = []
docs.each do |doc|
count += 1
Asset.transaction do
asset = Asset.from_xml(doc)
if asset.valid?
# If the imported record contains a UUID, attempt to find the
# existing record and delete it.
asset.destroy_existing
# Attempt to merge existing records, save if the merge_existing
# returns nil
asset.save unless asset.merge_existing
successes << asset.uuid
else
errors << "document #{count} of #{fn} is not valid: #{asset.errors.full_messages.join("; ")}"
raise ActiveRecord::Rollback
end
end
end
[successes, errors]
end
end
def destroy_existing
# Only do this with new records
return nil unless new_record?
# If the new record contains a UUID
uuid = identifiers.detect{|s| s.identifier_source_id == IdentifierSource::OUR_UUID_SOURCE.id}
if uuid
# Find an existing record with this UUID
existing = Asset.find_by_uuid(uuid.identifier)
# Remove the UUID from the identifiers collection
self.identifiers -= [uuid]
# Assign the asset the UUID
self.uuid = uuid.identifier
# Delete the already existing record
existing.destroy if existing
end
end
def merge_existing
# If the new record comes with an identifier that has its auto_merge
# flag set and an already existing identifier is found, get the asset
# with that existing identifier and merge the new data into the existing
# record.
# Only do this with new records
return nil unless new_record?
existing_asset = nil
self.identifiers.each do |identifier|
if identifier.identifier_source.auto_merge &&
(existing_identifier = Identifier.find_by_identifier_and_identifier_source_id(identifier.identifier, identifier.identifier_source_id))
existing_asset = Asset.find(existing_identifier.asset_id, :include => ALL_INCLUDES)
break
end
end
if existing_asset
existing_asset.merge(self)
existing_asset.save
end
existing_asset
end
# Copies the stuff from a new asset object into an existing asset.
# IMPORTANT: Any changes to models or the schema might affect this code.
# CHECK THIS WHEN THE SCHEMA CHANGES!
def merge(new_asset)
[:identifiers, :titles, :asset_dates, :descriptions, :relations, :coverages, :creators, :contributors,
:publishers, :rights_summaries, :instantiations, :annotations, :extensions].each do |field|
current_fields = self.send(field)
new_fields = new_asset.send(field)
current_attrs = current_fields.map { |o| clean_attributes(o.attributes) }
# this is O(n²), but hopefully n is small enough that this isn't a huge problem
new_fields.each do |fields|
if field == :instantiations
# Check if there is format identifier source that has auto merge set
if format_id = fields.format_ids.detect { |format_id| format_id.format_identifier_source.auto_merge == true }
# Does the existing record have a format identifier source that matches
# if current_format_id = current_fields.format_ids.detect { |current_format_id| format_id.format_identifier_source.name == format_id.format_identifier_source.name }
if current_instantiation = self.send(field).find(:first, :include => :format_ids, :conditions => ["format_ids.format_identifier_source_id = ? and format_ids.format_identifier = ?", format_id.format_identifier_source_id, format_id.format_identifier])
# Destroy the current instantiation and import the new one
current_instantiation.destroy
end
# Just import the new instantiation
current_fields << fields
end
else
current_fields << fields unless current_attrs.include?(clean_attributes(fields.attributes))
end
end
end
[:genre_ids, :subject_ids, :audience_level_ids, :audience_rating_ids].each do |field|
self.send("#{field}=".to_sym, self.send(field) | new_asset.send(field))
end
end
def self.xmlify_import_results(results)
successes, failures = results
doc = XML::Document.new
doc.root = root = XML::Node.new("results")
successes_node = XML::Node.new("successes")
root << successes_node
successes.each do |succ|
succ_text = block_given? ? yield(succ) : succ
successes_node << XML::Node.new("success", succ_text)
end
failures_node = XML::Node.new("failures")
root << failures_node
failures.each do |failure|
failures_node << XML::Node.new("failure", failure)
end
doc.to_s(:indent => false)
end
# IMPORTANT: This needs to be checked when the SCHEMA or MODEL changes!
def self.full_text_fields
# there must be a DRY way to ask sunspot for this
[
:identifier, :title, :subject, :description, :genre, :relation, :coverage,
:audience_level, :audience_rating, :creator, :contributor, :publisher,
:rights, :extension, :location, :annotation, :date, :format
]
end
# IMPORTANT: This code needs to be checked when models or the schema
# changes.
def self.dedupe
dedupe_field(:titles, :title, :title_type_id, :asset_id)
dedupe_field(:identifiers, :identifier, :identifier_source_id, :asset_id)
dedupe_field(:descriptions, :description, :description_type_id, :asset_id)
dedupe_field(:creators, :creator, :creator_role_id, :asset_id)
dedupe_field(:contributors, :contributor, :contributor_role_id, :asset_id)
dedupe_field(:publishers, :publisher, :publisher_role_id, :asset_id)
dedupe_field(:format_ids, :instantiation_id, :format_identifier, :format_identifier_source_id)
dedupe_field(:annotations, :container_id, :container_type, :annotation, :annotation_type)
dedupe_field(:asset_dates, :asset_date_type_id, :asset_date, :asset_id)
dedupe_trivial_field(:assets_subjects, :asset_id, :subject_id)
dedupe_trivial_field(:assets_genres, :asset_id, :genre_id)
end
protected
# Protected Class methods
def self.dedupe_field(table, *fields)
connection.execute("create temporary table tmp_#{table}_ids select distinct b.id from #{table} a, #{table} b where " + fields.map{|f| "a.#{f} = b.#{f}"}.join(" and ") + " and a.id < b.id")
connection.execute("delete from #{table} where id in (select * from tmp_#{table}_ids)")
$stdout.puts("#{count_by_sql("SELECT COUNT(*) FROM tmp_#{table}_ids")} #{table} deleted")
connection.execute("drop temporary table tmp_#{table}_ids")
end
def self.dedupe_trivial_field(table, *fields)
fieldlist = fields.join(", ")
connection.execute("CREATE TEMPORARY TABLE tmp_#{table}_t1 SELECT *, COUNT(*) AS c FROM #{table} GROUP BY #{fieldlist}")
connection.execute("CREATE TEMPORARY TABLE tmp_#{table}_t2 SELECT #{fieldlist} FROM tmp_#{table}_t1 WHERE c > 1")
connection.execute("DELETE FROM #{table} WHERE (#{fieldlist}) IN (SELECT * FROM tmp_#{table}_t2)")
connection.execute("INSERT INTO #{table} (SELECT * FROM tmp_#{table}_t2)")
$stdout.puts("#{count_by_sql("SELECT COUNT(*) FROM tmp_#{table}_t2")} duplicate #{table} cleaned up")
connection.execute("DROP TEMPORARY TABLE tmp_#{table}_t1")
connection.execute("DROP TEMPORARY TABLE tmp_#{table}_t2")
end
# Protected Instance methods
def generate_uuid
self.uuid = UUID.random_create.to_s unless (self.uuid && !self.uuid.empty?)
end
def clean_attributes(hash)
hash.delete("asset_id")
hash.delete("id")
hash.delete("uuid")
hash.delete("created_at")
hash.delete("updated_at")
hash.delete("creator_id")
hash.delete("updater_id")
hash.each do |key, value|
hash[key] = nil if value.blank?
end
hash
end
def save_version
Version.create(:asset => self, :body => self.to_xml)
end
end