Module: ReindexTopContainers

Included in:
Accession, ArchivalObject, Resource
Defined in:
backend/app/model/mixins/reindex_top_containers.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) delete



67
68
69
70
# File 'backend/app/model/mixins/reindex_top_containers.rb', line 67

def delete
  reindex_top_containers
  super
end

- (Object) reindex_top_containers(extra_ids = [])



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
# File 'backend/app/model/mixins/reindex_top_containers.rb', line 3

def reindex_top_containers(extra_ids = [])

  if !DB.respond_to?(:supports_join_updates?) || !DB.supports_join_updates?
    Log.warn("Invoking slow path for reindexing top containers")
    return reindex_top_containers_by_any_means_necessary(extra_ids)
  end

  # Find any relationships between a top container and any instance within the current tree.
  root_record = if self.class == ArchivalObject
                  self.root_record_id ? self.class.root_model[self.root_record_id] : self.topmost_archival_object
                else
                  self
                end

  if !extra_ids.empty?
    TopContainer.filter(:id => extra_ids).update(:system_mtime => Time.now)
  end

  if root_record.is_a?(Resource)
    TopContainer.linked_instance_ds.
      join(:archival_object, :archival_object__id => :instance__archival_object_id).
      filter(:archival_object__root_record_id => root_record.id).
      update(:top_container__system_mtime => Time.now)
  elsif root_record.is_a?(ArchivalObject)
    Log.warn("Invoking slow path for reindexing top containers")
    reindex_top_containers_by_any_means_necessary(extra_ids)
  elsif root_record.is_a?(Accession)
    TopContainer.linked_instance_ds.
      join(:accession, :accession__id => :instance__accession_id).
      filter(:accession__id => root_record.id).
      update(:top_container__system_mtime => Time.now)
  end

end

- (Object) reindex_top_containers_by_any_means_necessary(extra_ids)

Slow path for weird data or DBs that don’t support updates on joins (like derby/h2)



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'backend/app/model/mixins/reindex_top_containers.rb', line 40

def reindex_top_containers_by_any_means_necessary(extra_ids)
  # Find any relationships between a top container and any instance within the current tree.
  root_record = if self.class == ArchivalObject
                  self.root_record_id ? self.class.root_model[self.root_record_id] : self.topmost_archival_object
                else
                  self
                end
  tree_object_graph = root_record.object_graph
  top_container_link_rlshp = SubContainer.find_relationship(:top_container_link)
  relationship_ids = tree_object_graph.ids_for(top_container_link_rlshp)

  # Update the mtimes of each top container
  DB.open do |db|
    top_container_ids = db[:top_container_link_rlshp].filter(:id => relationship_ids).map(:top_container_id)
    top_container_ids.concat(extra_ids)
    TopContainer.filter(:id => top_container_ids).update(:system_mtime => Time.now)
  end
end

- (Object) update_from_json(json, opts = {}, apply_nested_records = true)



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'backend/app/model/mixins/reindex_top_containers.rb', line 73

def update_from_json(json, opts = {}, apply_nested_records = true)
  # we need to reindex top containers for instances about to be zapped
  # so remember the top containers we currently link to ...
  top_container_ids = instance.map {|instance|
                        # don't assume a sub_container - it might be a digital object instance
                        instance.sub_container.map {|sc| sc.related_records(:top_container_link).id}
                      }.flatten.compact

  result = super

  # ... and pass them in as extras
  reindex_top_containers(top_container_ids) unless opts[:skip_reindex_top_containers]

  result
end

- (Object) update_position_only(*)

not defined in accession or resource



61
62
63
64
# File 'backend/app/model/mixins/reindex_top_containers.rb', line 61

def update_position_only(*)
  super
  reindex_top_containers
end