Class: TopContainer
  
  
  
  
    - Inherits:
 
    - 
      Sequel::Model
      
        
          - Object
 
          
            - Sequel::Model
 
          
            - TopContainer
 
          
        
        show all
      
       
    
  
  
    
  
    
      - Includes:
 
      - ASModel, Relationships, RestrictionCalculator
 
      
    
  
  
  
    - Defined in:
 
    - backend/app/model/top_container.rb
 
  
  Instance Attribute Summary
  
  
  #cached_relationships
  
    
      Class Method Summary
      (collapse)
    
    
  
    
      Instance Method Summary
      (collapse)
    
    
  
  
  
  
  
  
  
  
  
  
  #active_restrictions, included, #restrictions
  
  
  
  
  
  
  
  
  
  #assimilate, #cache_relationships, included, #my_relationships, #related_records, #transfer_to_repository, #trigger_reindex_of_dependants
  
  
  
  
  
  
  
  
  Methods included from ASModel
  all_models, included, update_publish_flag, update_suppressed_flag
  
  
  
  
  
  
  
  
  Methods included from JSONModel
  JSONModel, #JSONModel, add_error_handler, all, allow_unmapped_enum_value, backend_url, client_mode?, custom_validations, destroy_model, enum_default_value, enum_values, handle_error, init, load_schema, #models, models, parse_jsonmodel_ref, parse_reference, repository, repository_for, schema_src, set_repository, strict_mode, strict_mode?, with_repository
  
  
    Class Method Details
    
      
  
  
    + (Object) batch_update(ids, fields) 
  
  
  
  
    
      
262
263
264
265
266
267
268
269
270
271 
     | 
    
      # File 'backend/app/model/top_container.rb', line 262
def self.batch_update(ids, fields)
  out = {}
  begin
    n = self.filter(:id => ids).update(fields.merge({:system_mtime => Time.now, :user_mtime => Time.now}))
    out[:records_updated] = n
  rescue
    out[:error] = $!
  end
  out
end
     | 
  
 
    
      
  
  
    + (Object) bulk_update_barcodes(barcode_data) 
  
  
  
  
    
      
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372 
     | 
    
      # File 'backend/app/model/top_container.rb', line 354
def self.bulk_update_barcodes(barcode_data)
  updated = []
  
  TopContainer.filter(:id => barcode_data.map{|uri,_| my_jsonmodel.id_for(uri)}).update(:barcode => nil)
  barcode_data.each do |uri, barcode|
    id = my_jsonmodel.id_for(uri)
    top_container = TopContainer[id]
    top_container.barcode = barcode
    top_container.system_mtime = Time.now
    top_container.save(:columns => [:barcode, :system_mtime])
    updated << id
  end
  updated
end
     | 
  
 
    
      
  
  
    + (Object) bulk_update_container_profile(ids, container_profile_uri) 
  
  
  
  
    
      
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 
     | 
    
      # File 'backend/app/model/top_container.rb', line 274
def self.bulk_update_container_profile(ids, container_profile_uri)
  out = {:records_updated => ids.length}
  relationship = TopContainer.find_relationship(:top_container_profile)
  begin
    
    relationship.handle_delete(relationship.find_by_participant_ids(TopContainer, ids).map(&:id))
    unless container_profile_uri.empty?
      container_profile = ContainerProfile[JSONModel(:container_profile).id_for(container_profile_uri)]
      raise "Container profile not found: #{container_profile_uri}" if !container_profile
      now = Time.now
      ids.each do |id|
        top_container = TopContainer[id]
        relationship.relate(top_container, container_profile, {
                              :aspace_relationship_position => 1,
                              :system_mtime => now,
                              :user_mtime => now
                            })
      end
    end
    TopContainer.update_mtime_for_ids(ids)
  rescue
    Log.exception($!)
    
    out[:records_updated] = 0
    out[:error] = $!
  end
  out
end
     | 
  
 
    
      
  
  
    + (Object) bulk_update_location(ids, location_uri) 
  
  
  
  
    
      
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 
     | 
    
      # File 'backend/app/model/top_container.rb', line 315
def self.bulk_update_location(ids, location_uri)
  out = {:records_updated => ids.length}
  relationship = TopContainer.find_relationship(:top_container_housed_at)
  begin
    relationship.handle_delete(relationship.find_by_participant_ids(TopContainer, ids).select{|v| v.status == 'current'}.map(&:id))
    location = Location[JSONModel(:location).id_for(location_uri)]
    raise "Location not found: #{location_uri}" if !location
    now = Time.now
    ids.each do |id|
      top_container = TopContainer[id]
      relationship.relate(top_container, location, {
                            :status => 'current',
                            :start_date => now.iso8601,
                            :aspace_relationship_position => 0,
                            :system_mtime => now,
                            :user_mtime => now
                          })
    end
    TopContainer.update_mtime_for_ids(ids)
  rescue
    Log.exception($!)
    out[:records_updated] = 0
    out[:error] = $!
  end
  out
end
     | 
  
 
    
      
  
  
    + (Object) find_title_for(series) 
  
  
  
  
    
      
109
110
111 
     | 
    
      # File 'backend/app/model/top_container.rb', line 109
def self.find_title_for(series)
  series.respond_to?(:display_string) ? series.display_string : series.title
end
 
     | 
  
 
    
      
  
  
    + (Object) for_barcode(barcode) 
  
  
  
  
    
      
375
376
377 
     | 
    
      # File 'backend/app/model/top_container.rb', line 375
def self.for_barcode(barcode)
  TopContainer[:barcode => barcode, :repo_id => self.active_repository]
end
 
     | 
  
 
    
      
  
  
    + (Object) for_indicator(indicator) 
  
  
  
  
    
      
379
380
381 
     | 
    
      # File 'backend/app/model/top_container.rb', line 379
def self.for_indicator(indicator)
  TopContainer[:indicator => indicator, :repo_id => self.active_repository]
end
 
     | 
  
 
    
      
  
  
    + (Object) linked_instance_ds 
  
  
  
  
    
      
57
58
59
60
61
62 
     | 
    
      # File 'backend/app/model/top_container.rb', line 57
def self.linked_instance_ds
  db[:instance].
    join(:sub_container, :instance_id => :instance__id).
    join(:top_container_link_rlshp, :sub_container_id => :sub_container__id).
    join(:top_container, :id => :top_container_link_rlshp__top_container_id)
end
     | 
  
 
    
      
  
  
    + (Object) search_stream(params, repo_id, &block) 
  
  
  
  
    
      
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 
     | 
    
      # File 'backend/app/model/top_container.rb', line 228
def self.search_stream(params, repo_id, &block)
  query = if params[:q]
            Solr::Query.create_keyword_search(params[:q])
          else
            Solr::Query.create_match_all_query
          end
  max_results = AppConfig.has_key?(:max_top_container_results) ? AppConfig[:max_top_container_results] : 10000
  query.(1, max_results).
    set_repo_id(repo_id).
    set_record_types(params[:type]).
    set_filter_terms(params[:filter_term]).
    set_facets(params[:facet])
  query.add_solr_param(:qf, "series_identifier_u_stext collection_identifier_u_stext")
  url = query.to_solr_url
  req = Net::HTTP::Get.new(url.request_uri)
  Net::HTTP.start(url.host, url.port) do |http|
    http.request(req, nil) do |response|
      if response.code =~ /^4/
        raise response.body
      end
      block.call(response)
    end
  end
end
     | 
  
 
    
      
  
  
    + (Object) sequel_to_jsonmodel(objs, opts = {}) 
  
  
  
  
    
      
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 
     | 
    
      # File 'backend/app/model/top_container.rb', line 143
def self.sequel_to_jsonmodel(objs, opts = {})
  jsons = super
  jsons.zip(objs).each do |json, obj|
    json['display_string'] = obj.display_string
    json['long_display_string'] = obj.long_display_string
    obj.series.each do |series|
      json['series'] ||= []
      json['series'] << {
        'ref' => series.uri,
        'identifier' => series.component_id,
        'display_string' => find_title_for(series),
        'level_display_string' => obj.level_display_string(series)
      }
    end
    obj.collections.each do |collection|
      json['collection'] ||= []
      json['collection'] << {
        'ref' => collection.uri,
        'identifier' => Identifiers.format(Identifiers.parse(collection.identifier)),
        'display_string' => find_title_for(collection)
      }
    end
    if json['exported_to_ils']
      json['exported_to_ils'] = json['exported_to_ils'].getlocal.iso8601
    end
  end
  jsons
end
     | 
  
 
    
   
  
    Instance Method Details
    
      
  
  
    - (Object) collections 
  
  
  
  
    
      
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 
     | 
    
      # File 'backend/app/model/top_container.rb', line 86
def collections
  linked_archival_records.map {|obj|
    if obj.respond_to?(:series)
      
      if obj.root_record_id
        obj.class.root_model[obj.root_record_id]
      else
        
        
        nil
      end
    else
      obj
    end
  }.compact.uniq {|obj| obj.uri}
end
     | 
  
 
    
      
  
  
    - (Object) delete 
  
  
  
  
    When deleting a top_container, delete all related instances and their subcontainers
   
 
  
  
    
      
209
210
211
212 
     | 
    
      # File 'backend/app/model/top_container.rb', line 209
def delete
  related_records(:top_container_link).map {|sub| Instance[sub.instance_id].delete }
  super
end
     | 
  
 
    
      
  
  
    - (Object) display_string 
  
  
  
  
    
      
127
128
129 
     | 
    
      # File 'backend/app/model/top_container.rb', line 127
def display_string
  ["#{type ? type.capitalize : ''}", "#{indicator}:", series_label, format_barcode].compact.join(" ").gsub(/:\Z/,'')
end
     | 
  
 
    
      
  
  
    
      
32
33
34
35
36 
     | 
    
      # File 'backend/app/model/top_container.rb', line 32
def format_barcode
  if self.barcode
    "[#{self.barcode}]"
  end
end
     | 
  
 
    
      
  
  
    - (Object) level_display_string(series) 
  
  
  
  
    
      
114
115
116 
     | 
    
      # File 'backend/app/model/top_container.rb', line 114
def level_display_string(series)
  series.other_level || I18n.t("enumerations.archival_record_level.#{series.level}", series.level)
end
     | 
  
 
    
      
  
  
    - (Object) linked_archival_record_for(subcontainer) 
  
  
  
  
    
      
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 
     | 
    
      # File 'backend/app/model/top_container.rb', line 65
def linked_archival_record_for(subcontainer)
  
  instance = Instance[subcontainer.instance_id]
  return nil unless instance
  
  ASModel.all_models.each do |model|
    next unless model.associations.include?(:instance)
    association = model.association_reflection(:instance)
    key = association[:key]
    if instance[key]
      return model[instance[key]]
    end
  end
end
     | 
  
 
    
      
  
  
    - (Object) linked_archival_records 
  
  
  
  
    return all archival records linked to this top container
   
 
  
  
    
      
50
51
52
53
54 
     | 
    
      # File 'backend/app/model/top_container.rb', line 50
def linked_archival_records
  related_records(:top_container_link).map {|subcontainer|
    linked_archival_record_for(subcontainer)
  }.compact.uniq {|obj| obj.uri}
end
     | 
  
 
    
      
  
  
    - (Object) long_display_string 
  
  
  
  
    
      
132
133
134
135
136
137
138
139
140 
     | 
    
      # File 'backend/app/model/top_container.rb', line 132
def long_display_string
  resource = collections.first
  resource &&= Identifiers.format(Identifiers.parse(resource.identifier))
  container_profile = related_records(:top_container_profile)
  container_profile &&= container_profile.name
  container_bit = ["#{type ? type.capitalize : ''}", "#{indicator}", format_barcode].compact.join(" ")
  [resource, series_label, container_bit, container_profile].compact.join(", ")
end
     | 
  
 
    
      
  
  
    - (Object) reindex_linked_records 
  
  
  
  
    
      
221
222
223
224
225 
     | 
    
      # File 'backend/app/model/top_container.rb', line 221
def reindex_linked_records
  linked_archival_records.group_by(&:class).each do |clz, records|
    clz.update_mtime_for_ids(records.map(&:id))
  end
end
     | 
  
 
    
      
  
  
    - (Object) series 
  
  
  
  
    
      
104
105
106 
     | 
    
      # File 'backend/app/model/top_container.rb', line 104
def series
  linked_archival_records.map {|record| tree_top(record)}.compact.uniq {|obj| obj.uri}
end
     | 
  
 
    
      
  
  
    - (Object) series_label 
  
  
  
  
    
      
118
119
120
121
122
123
124
125 
     | 
    
      # File 'backend/app/model/top_container.rb', line 118
def series_label
  attached_series = self.series
  if attached_series.empty?
    nil
  else
    attached_series.map {|s| "#{level_display_string(s)} #{s.component_id}" }.join("; ")
  end
end
     | 
  
 
    
      
  
  
    - (Object) tree_top(obj) 
  
  
  
  
    For Archival Objects, the series is the topmost record in the tree.
   
 
  
  
    
      
40
41
42
43
44
45
46 
     | 
    
      # File 'backend/app/model/top_container.rb', line 40
def tree_top(obj)
  if obj.respond_to?(:series)
    obj.series
  else
    nil
  end
end
     | 
  
 
    
      
  
  
    - (Object) update_from_json(json, opts = {}, apply_nested_records = true) 
  
  
  
  
    
      
214
215
216
217
218 
     | 
    
      # File 'backend/app/model/top_container.rb', line 214
def update_from_json(json, opts = {}, apply_nested_records = true)
  result = super
  reindex_linked_records
  result
end
     | 
  
 
    
      
  
  
    - (Object) validate 
  
  
  
  
    
      
17
18
19
20
21
22
23
24
25
26
27
28
29 
     | 
    
      # File 'backend/app/model/top_container.rb', line 17
def validate
  validates_unique([:repo_id, :barcode],
                   :message => "A barcode must be unique within a repository")
  map_validation_to_json_property([:repo_id, :barcode], :barcode)
  check = BarcodeCheck.new(Repository[self.class.active_repository].repo_code)
  if !check.valid?(self[:barcode])
    errors.add(:barcode, "Length must be within the range set in configuration")
  end
  super
end
     |