Class: UserDefinedFieldMigrator

Inherits:
Object
  • Object
show all
Defined in:
backend/app/lib/user_defined_field_migrator.rb

Constant Summary collapse

EXTERNAL_ID_MAPPING =
[
  {
    :source => "Archivists Toolkit Database::RESOURCE",
    :aspace_column => :resource_id,
    :at_column => :resourceId
  },
  {
    :source => "Archivists Toolkit Database::RESOURCE_COMPONENT",
    :aspace_column => :archival_object_id,
    :at_column => :resourceComponentId
  },
]

Instance Method Summary collapse

Instance Method Details

#apply_update(top_container, container_data) ⇒ Object



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
# File 'backend/app/lib/user_defined_field_migrator.rb', line 28

def apply_update(top_container, container_data)
  Log.info("Applying additional container data for top container: #{top_container.values}: #{container_data}")

  update_values = {
    :ils_holding_id => container_data.voyager_holding_id,
    :exported_to_ils => Time.now,
  }

  profile = container_data.container_profile


  if dry_run?
    Log.info("DRY RUN: Update values #{update_values.inspect}")
    Log.info("DRY RUN: Using container profile: #{profile.values}") if profile
    if container_data.voyager_bib_id
      Log.info("DRY RUN: Linked records needed user-defined fields: #{find_records_needing_user_defined_fields(top_container).inspect}")
    end
  else
    update_values.each do |property, value|
      top_container[property] = value
    end

    top_container.save

    if profile
      TopContainer.find_relationship(:top_container_profile).relate(top_container, profile,
                                                                    {
                                                                      :aspace_relationship_position => 0,
                                                                      :system_mtime => Time.now,
                                                                      :user_mtime => Time.now
                                                                    })
    end

    if container_data.voyager_bib_id
      models_to_update = find_records_needing_user_defined_fields(top_container)

      models_to_update.each do |model, ids|
        userdefined_link_column = model.association_reflection(:user_defined)[:key]

        ids.each do |id|
          udf = UserDefined[userdefined_link_column => id]

          if udf

            if udf[:string_2]
              if udf[:string_2] == container_data.voyager_bib_id
                next
              else
                Log.warn("Mismatch in Bib ID for #{model} #{id} linked to Top Container #{top_container.id}")
              end
            end

            udf[:string_2] = container_data.voyager_bib_id
            udf.save
          else
            now = Time.now
            UserDefined.create(userdefined_link_column => id,
                               :string_2 => container_data.voyager_bib_id,
                               :json_schema_version => 1,
                               :system_mtime => now,
                               :user_mtime => now,
                               :created_by => 'admin',
                               :last_modified_by => 'admin',
                               :create_time => now)
          end
        end
      end
    end
  end
end

#dry_run?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'backend/app/lib/user_defined_field_migrator.rb', line 23

def dry_run?
  !AppConfig.has_key?(:user_defined_field_migrate_dry_run) || AppConfig[:user_defined_field_migrate_dry_run]
end

#migrateObject



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
# File 'backend/app/lib/user_defined_field_migrator.rb', line 100

def migrate
  raise "No target ArchivesSpace repository was specified.  You will need to set AppConfig[:at_target_aspace_repo]" unless AppConfig.has_key?(:at_target_aspace_repo)

  repo = Repository[:repo_code => AppConfig[:at_target_aspace_repo]]

  raise "No repository found for repo_code '#{AppConfig[:at_target_aspace_repo]}'" if !repo


  set_up_temp_tables
  begin
    Log.info("Preloading top container mappings")
    preload_top_container_mappings
    Log.info("Done!")

    Sequel.connect(AppConfig[:at_db_url]) do |db|
      at = ArchivistsToolkit.new(db)

      RequestContext.open(:repo_id => repo.id,
                          :is_high_priority => false,
                          :current_username => "admin") do

        total_count = TopContainer.filter(:repo_id => repo.id).count
        count = 0
        TopContainer.filter(:repo_id => repo.id).each do |top_container|
          count += 1

          if (count % 100) == 0
            Log.info("Processing top container #{count} of #{total_count}")
          end

          DB.open do |db|

            relationship = TopContainer.find_relationship(:top_container_link)

            subcontainers = relationship.filter(:top_container_id => top_container.id).select(:sub_container_id)
            instances = Instance.filter(:id => SubContainer.filter(:id => subcontainers).select(:instance_id))

            got_a_match = false
            container_data = nil

            EXTERNAL_ID_MAPPING.each do |mapping|

              at_record_ids = db[:udf_mig_tc_to_extid].
                              filter(:top_container_id => top_container.id).
                              select(mapping[:aspace_column]).
                              map {|row| row[mapping[:aspace_column]]}

              next if at_record_ids.empty?

              at_containers = at.find_matching_containers(mapping[:at_column], at_record_ids, top_container)

              if at_containers.empty?
                true # No match, but maybe we'll get it in the next round.
              elsif (at_containers - [:no_data]).length > 1
                Log.error("Ambiguous containers for #{top_container.values.inspect}: #{at_containers.inspect}")

                # Still counts as getting a match for logging purposes, but it's a lousy match!
                got_a_match = true
              else
                # puts "#{top_container.id}: Good!"
                got_a_match = true
                container_data = (at_containers - [:no_data]).first
              end
            end

            if got_a_match
              if container_data
                apply_update(top_container, container_data)
              end
            else
              if db[:udf_mig_tc_to_extid].
                  filter(:top_container_id => top_container.id).
                  where { Sequel.~(:accession_id => nil) }.
                  count > 0

                # That's OK.  The top container is linked to an accession, so
                # not a huge problem that we didn't find a match.
                true
              else
                Log.error("No matching containers for #{top_container}: #{top_container.values.inspect}")
              end
            end
          end
        end
      end
    end
  ensure
    tear_down_temp_tables
  end
end