Class: ArkName

Inherits:
Sequel::Model
  • Object
show all
Includes:
ASModel
Defined in:
backend/app/model/ark_name.rb

Constant Summary collapse

EXTERNAL_ARK_VERSION_KEY =
'__archivesspace_external_url'

Constants included from JSONModel

JSONModel::REFERENCE_KEY_REGEX

Class Method Summary collapse

Instance Method Summary collapse

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, check_valid_refs, 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_publish_flags!, set_repository, strict_mode, strict_mode?, validate_schema, with_repository

Class Method Details

.ark_minterObject



24
25
26
# File 'backend/app/model/ark_name.rb', line 24

def self.ark_minter
  load_minter(AppConfig[:ark_minter])
end

.check_unique(db, obj) ⇒ Object



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
# File 'backend/app/model/ark_name.rb', line 111

def self.check_unique(db, obj)
  fk_col = fk_for_class(obj.class)
  return unless fk_col

  # Make sure the value we've generated, or the user value hasn't been used elsewhere.
  db[:ark_uniq_check].filter(:record_uri => obj.uri).delete

  ark_value_query = self.filter(fk_col => obj.id).select(:ark_value).distinct

  # If external ARKs are turned off, don't count them in our uniqueness checks
  unless AppConfig[:arks_allow_external_arks]
    ark_value_query = ark_value_query.filter(:is_external_url => 0)
  end

  begin
    ark_value_query.map {|row| row[:ark_value]}.each do |value|
      db[:ark_uniq_check].insert(:record_uri => obj.uri, :value => value.sub(/^.*?ark:/i, 'ark:'))
    end
  rescue Sequel::UniqueConstraintViolation => e
    # We want to give a useful error in the case that the collision is on the external_ark_url
    if our_external_ark = db[:ark_name].filter(fk_col => obj.id).filter(:is_external_url => 1).get(:ark_value)
      if db[:ark_name].filter(Sequel.~(fk_col => obj.id)).filter(:ark_value => our_external_ark).count > 0
        # Someone else collided with our external ARK url
        raise JSONModel::ValidationException.new(:errors => {"external_ark_url" => ["external_ark_collision"]})
      end
    end

    raise JSONModel::ValidationException.new(:errors => {"ark" => ["ark_collision"]})
  end
end

.clean_ark_value(value) ⇒ Object



211
212
213
214
215
216
217
# File 'backend/app/model/ark_name.rb', line 211

def self.clean_ark_value(value)
  unless value.match(/^(.*?\/)?ark:\//)
    raise JSONModel::ValidationException.new(:errors => {"ark" => ["ark_format_error"]})
  end

  value.sub(/^(.*?\/)?ark:\//, 'ark:/')
end

.ensure_ark_for_record(obj, external_ark_url) ⇒ 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
98
99
100
101
102
103
104
105
106
107
108
109
# File 'backend/app/model/ark_name.rb', line 28

def self.ensure_ark_for_record(obj, external_ark_url)
  return false unless AppConfig[:arks_enabled]
  return false unless ArkName.require_update?(obj, external_ark_url)

  fk_col = fk_for_class(obj.class)

  return false unless fk_col

  now = Time.now

  DB.open do |db|
    # Handle the case where external ARKs are enabled
    if AppConfig[:arks_allow_external_arks]
      self.filter(fk_col => obj.id, :is_external_url => 1).delete

      # ... and we have one coming in
      if external_ark_url
        self
          .filter(fk_col => obj.id, :is_current => 1)
          .update(:is_current => 0,
                  :retired_at_epoch_ms => (now.to_f * 1000).to_i)

        self.insert(fk_col => obj.id,
                    :created_by => 'admin',
                    :last_modified_by => 'admin',
                    :create_time => now,
                    :system_mtime => now,
                    :user_mtime => now,
                    :is_current => 1,
                    :is_external_url => 1,
                    :retired_at_epoch_ms => 0,
                    :lock_version => 0,
                    :version_key => EXTERNAL_ARK_VERSION_KEY,
                    :ark_value => external_ark_url,
                   )

        check_unique(db, obj)
        return true
      end
    end

    # Look for a previously minted ARK that we can promote into current
    previous_ark = self
      .filter(fk_col => obj.id, :version_key => ark_minter.version_key_for(obj))
      .reverse(:retired_at_epoch_ms)
      .first

    if previous_ark
      self
        .filter(fk_col => obj.id, :is_current => 1)
        .update(:is_current => 0,
                :retired_at_epoch_ms => (now.to_f * 1000).to_i)

      self.filter(:id => previous_ark.id).update(:is_current => 1, :retired_at_epoch_ms => 0)
      return
    end

    # Need to mint a new ARK
    self
      .filter(fk_col => obj.id, :is_current => 1)
      .update(:is_current => 0,
              :retired_at_epoch_ms => (now.to_f * 1000).to_i)

    ark_minter.mint!(obj,
                     fk_col => obj.id,
                     :created_by => 'admin',
                     :last_modified_by => 'admin',
                     :create_time => now,
                     :system_mtime => now,
                     :user_mtime => now,
                     :is_current => 1,
                     :is_external_url => 0,
                     :retired_at_epoch_ms => 0,
                     :lock_version => 0,
                     :version_key => ark_minter.version_key_for(obj),
                    )
  end

  check_unique(db, obj)

  true
end

.handle_delete(model_clz, ids) ⇒ Object



245
246
247
248
249
250
251
252
253
# File 'backend/app/model/ark_name.rb', line 245

def self.handle_delete(model_clz, ids)
  DB.open do |db|
    db[:ark_uniq_check]
      .filter(:record_uri => ids.map {|id| model_clz.my_jsonmodel.uri_for(id, :repo_id => model_clz.active_repository)})
      .delete

    ArkName.filter(fk_for_class(model_clz) => ids).delete
  end
end

.load_minter(minter_id) ⇒ Object



16
17
18
19
20
21
22
# File 'backend/app/model/ark_name.rb', line 16

def self.load_minter(minter_id)
  clz = @minters.fetch(minter_id) do
    raise "Couldn't find a minter matching: #{minter_id}"
  end

  clz.new
end

.prefix(value) ⇒ Object



255
256
257
# File 'backend/app/model/ark_name.rb', line 255

def self.prefix(value)
  "#{AppConfig[:ark_url_prefix]}/#{value}"
end

.register_minter(minter_id, clz) ⇒ Object



11
12
13
14
# File 'backend/app/model/ark_name.rb', line 11

def self.register_minter(minter_id, clz)
  @minters[minter_id] = clz
  nil
end

.run_housekeeping!Object

Invoked when the ARKs runner job kicks off.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'backend/app/model/ark_name.rb', line 221

def self.run_housekeeping!
  DB.open do |db|
    # Delete any entries from our uniq check table whose records have since been
    # deleted/transferred.
    if DB.supports_mvcc?
      db[:ark_uniq_check]
        .join(:deleted_records, Sequel.qualify(:ark_uniq_check, :record_uri) => Sequel.qualify(:deleted_records, :uri))
        .delete
    else
      loop do
        to_delete = db[:ark_uniq_check]
                      .join(:deleted_records, Sequel.qualify(:ark_uniq_check, :record_uri) => Sequel.qualify(:deleted_records, :uri))
                      .select(Sequel.qualify(:ark_uniq_check, :record_uri))
                      .limit(256)
                      .map {|row| row[:record_uri]}

        break if to_delete.empty?

        db[:ark_uniq_check].filter(:record_uri => to_delete).delete
      end
    end
  end
end

.update_for_record(obj, ark_name) ⇒ Object

Bypass the minting process and assert values for current and previous ARKs Accepts an ASModel object for the record to apply the ARKs to and a JSONModel(:ark_name) containing the asserted ARK values. This supports the exceptional case where an admin is fixing spurious previous ARKs, or is reconstructing a record and needs to assert its current ARK.



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
# File 'backend/app/model/ark_name.rb', line 147

def self.update_for_record(obj, ark_name)
  fk_col = fk_for_class(obj.class)
  raise "Record type does not support ARKs: #{obj.class}" unless fk_col
  now = Time.now

  ark = {
    fk_col => obj.id,
    :created_by => 'admin',
    :last_modified_by => 'admin',
    :create_time => now,
    :system_mtime => now,
    :user_mtime => now,
    :lock_version => 0,
  }

  DB.open do |db|
    current_ark = ArkName.first(fk_col => obj.id, :is_current => 1)

    ArkName.filter(fk_col => obj.id).delete

    if ark_name['current']
      if ark_name['current_is_external']
        ArkName.insert(ark.merge(:ark_value => ark_name['current'],
                                 :is_external_url => 1,
                                 :is_current => 1,
                                 :retired_at_epoch_ms => 0,
                                 :version_key => EXTERNAL_ARK_VERSION_KEY))
      else
        if ark_minter.ark_recognized?(ark_name['current'])
          ArkName.insert(ark.merge(:ark_value => clean_ark_value(ark_name['current']),
                                   :is_external_url => 0,
                                   :is_current => 1,
                                   :retired_at_epoch_ms => 0,
                                   :version_key => ark_minter.version_key_for(obj)))
        elsif AppConfig[:arks_allow_external_arks]
          ArkName.insert(ark.merge(:ark_value => ark_name['current'],
                                   :is_external_url => 1,
                                   :is_current => 1,
                                   :retired_at_epoch_ms => 0,
                                   :version_key => EXTERNAL_ARK_VERSION_KEY))
        end
      end
    end

    now_i = (now.to_f * 1000).to_i

    ark_name['previous'].each_with_index do |prev, ix|
      if ark_minter.ark_recognized?(prev)
        ArkName.insert(ark.merge(:ark_value => clean_ark_value(prev),
                                 :is_current => 0,
                                 :is_external_url => 0,
                                 :retired_at_epoch_ms => (now_i - ix),
                                 :version_key => ark_minter.version_key_for(obj)))
      end
    end
  end

  check_unique(db, obj)

  obj.class.update_mtime_for_ids([obj.id])

  true
end

Instance Method Details

#valueObject



259
260
261
262
263
264
265
# File 'backend/app/model/ark_name.rb', line 259

def value
  if self.is_external_url == 1
    self.ark_value
  else
    self.class.prefix(self.ark_value)
  end
end