Class: ResourceImportFile

Inherits:
ApplicationRecord
  • Object
show all
Includes:
ImportFile, Statesman::Adapters::ActiveRecordQueries
Defined in:
app/models/resource_import_file.rb,
app/models2/resource_import_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#library_idObject

Returns the value of attribute library_id.



37
38
39
# File 'app/models/resource_import_file.rb', line 37

def library_id
  @library_id
end

#modeObject

Returns the value of attribute mode.



37
38
39
# File 'app/models/resource_import_file.rb', line 37

def mode
  @mode
end

Class Method Details

.importObject



301
302
303
304
305
306
307
# File 'app/models/resource_import_file.rb', line 301

def self.import
  ResourceImportFile.not_imported.each do |file|
    file.import_start
  end
rescue
  Rails.logger.info "#{Time.zone.now} importing resources failed!"
end

.import_expression(work, agents, options = {edit_mode: 'create'}) ⇒ Object



250
251
252
253
254
255
# File 'app/models/resource_import_file.rb', line 250

def self.import_expression(work, agents, options = {edit_mode: 'create'})
  expression = work
  expression.save
  expression.contributors = agents.uniq unless agents.empty?
  expression
end

.import_manifestation(expression, agents, options = {}, edit_options = {edit_mode: 'create'}) ⇒ Object



257
258
259
260
261
262
263
264
265
# File 'app/models/resource_import_file.rb', line 257

def self.import_manifestation(expression, agents, options = {}, edit_options = {edit_mode: 'create'})
  manifestation = expression
  manifestation.during_import = true
  manifestation.reload
  manifestation.update!(options)
  manifestation.publishers = agents.uniq unless agents.empty?
  manifestation.reload
  manifestation
end

.import_work(title, agents, options = {edit_mode: 'create'}) ⇒ Object



243
244
245
246
247
248
# File 'app/models/resource_import_file.rb', line 243

def self.import_work(title, agents, options = {edit_mode: 'create'})
  work = Manifestation.new(title)
  work.save
  work.creators = agents.uniq unless agents.empty?
  work
end

Instance Method Details

#importObject



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
# File 'app/models/resource_import_file.rb', line 61

def import
  transition_to!(:started)
  num = {
    manifestation_imported: 0,
    item_imported: 0,
    manifestation_found: 0,
    item_found: 0,
    failed: 0
  }
  rows = open_import_file(create_import_temp_file(resource_import))
  rows.shift
  #if [field['manifestation_id'], field['manifestation_identifier'], field['isbn'], field['original_title']].reject{|f|
  #  f.to_s.strip == ''
  #}.empty?
  #  raise "You should specify isbn or original_title in the first line"
  #end
  row_num = 1

  ResourceImportResult.create!(resource_import_file_id: id, body: (%w( imported_manifestation_id imported_item_id ) + rows.headers).join("\t"))
  rows.each do |row|
    row_num += 1
    import_result = ResourceImportResult.new(resource_import_file_id: id)
    if row['dummy'].to_s.strip.present?
      import_result.error_message = "line #{row_num}: #{I18n.t('import.dummy')}"
      import_result.body = ([nil, nil] + row.fields).join("\t")
      import_result.save!
      next
    end

    item_identifier = row['item_identifier'].to_s.strip
    item = Item.find_by(item_identifier: item_identifier)
    if item
      import_result.item = item
      import_result.manifestation = item.manifestation
      import_result.error_message = "line #{row_num}: #{I18n.t('import.item_found')}"
      import_result.body = ([item.manifestation.id, item.id] + row.fields).join("\t")
      import_result.save!
      num[:item_found] += 1
      next
    end

    if row['manifestation_identifier'].present?
      manifestation = Manifestation.find_by(manifestation_identifier: row['manifestation_identifier'].to_s.strip)
    end

    unless manifestation
      if row['manifestation_id'].present?
        manifestation = Manifestation.find_by(id: row['manifestation_id'].to_s.strip)
      end
    end

    unless manifestation
      if row['doi'].present?
        doi = URI.parse(row['doi']).path.gsub(/^\//, "")
        identifier_type_doi = IdentifierType.find_by(name: 'doi')
        identifier_type_doi = IdentifierType.create!(name: 'doi') unless identifier_type_doi
        manifestation = Identifier.find_by(body: doi, identifier_type_id: identifier_type_doi.id).try(:manifestation)
      end
    end

    unless manifestation
      if row['jpno'].present?
        jpno = row['jpno'].to_s.strip
        identifier_type_jpno = IdentifierType.find_by(name: 'jpno')
        identifier_type_jpno = IdentifierType.create!(name: 'jpno') unless identifier_type_jpno
        manifestation = Identifier.find_by(body: jpno, identifier_type_id: identifier_type_jpno.id).try(:manifestation)
      end
    end

    unless manifestation
      if row['ncid'].present?
        ncid = row['ncid'].to_s.strip
        identifier_type_ncid = IdentifierType.find_by(name: 'ncid')
        identifier_type_ncid = IdentifierType.where(name: 'ncid').create! unless identifier_type_ncid
        manifestation = Identifier.find_by(body: ncid, identifier_type_id: identifier_type_ncid.id).try(:manifestation)
      end
    end

    unless manifestation
      if row['isbn'].present?
        if StdNum::ISBN.valid?(row['isbn'])
          isbn = StdNum::ISBN.normalize(row['isbn'])
          identifier_type_isbn = IdentifierType.find_by(name: 'isbn')
          identifier_type_isbn = IdentifierType.where(name: 'isbn').create! unless identifier_type_isbn
          m = Identifier.find_by(body: isbn, identifier_type_id: identifier_type_isbn.id).try(:manifestation)
          if m
            if m.series_statements.exists?
              manifestation = m
            end
          end
        else
          import_result.error_message = "line #{row_num}: #{I18n.t('import.isbn_invalid')}"
        end
      end
    end

    if manifestation
      import_result.error_message = "line #{row_num}: #{I18n.t('import.manifestation_found')}"
      num[:manifestation_found] += 1
    end

    if row['original_title'].blank?
      unless manifestation
        begin
          manifestation = Manifestation.import_isbn(isbn) if isbn
          if manifestation
            num[:manifestation_imported] += 1
          end
        rescue EnjuNdl::InvalidIsbn
          manifestation = nil
          import_result.error_message = "line #{row_num}: #{I18n.t('import.isbn_invalid')}"
        rescue EnjuNdl::RecordNotFound
          manifestation = nil
          import_result.error_message = "line #{row_num}: #{I18n.t('import.isbn_record_not_found')}"
        end
      end
      if manifestation.nil? and row['ndl_bib_id']
        manifestation = Manifestation.import_ndl_bib_id(row['ndl_bib_id'])
        if manifestation
          num[:manifestation_imported] += 1
        end
      end
    end

    unless manifestation
      manifestation = fetch(row)
      num[:manifestation_imported] += 1 if manifestation
    end
    import_result.manifestation = manifestation

    if manifestation
      ResourceImportFile.import_manifestation_custom_value(row, manifestation).each do |value|
        value.update!(manifestation: manifestation)
      end

      item = nil
      if item_identifier.present? || row['shelf'].present? || row['call_number'].present?
        item = create_item(row, manifestation)
      else
        if manifestation.fulltext_content?
          item = create_item(row, manifestation)
          item.circulation_status = CirculationStatus.find_by(name: 'Available On Shelf')
          begin
            item.acquired_at = Time.zone.parse(row['acquired_at'].to_s.strip)
          rescue ArgumentError
          end
        end
        num[:failed] += 1
      end

      if item
        ResourceImportFile.import_item_custom_value(row, item).each do |value|
          value.update!(item: item)
        end

        import_result.item = item
      end
    else
      num[:failed] += 1
    end

    import_result.body = ([manifestation.try(:id), item.try(:id)] + row.fields).join("\t")
    import_result.save!
    num[:item_imported] += 1 if import_result.item
  end

  Sunspot.commit
  rows.close
  transition_to!(:completed)
  mailer = ResourceImportMailer.completed(self)
  send_message(mailer)
  Rails.cache.write("manifestation_search_total", Manifestation.search.total)
  num
rescue => e
  self.error_message = "line #{row_num}: #{e.message}"
  save
  transition_to!(:failed)
  mailer = ResourceImportMailer.failed(self)
  send_message(mailer)
  raise e
end

#import_marc(marc_type) ⇒ Object



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
# File 'app/models/resource_import_file.rb', line 267

def import_marc(marc_type)
  file = File.open(resource_import.path)
  case marc_type
  when 'marcxml'
    reader = MARC::XMLReader.new(file)
  else
    reader = MARC::Reader.new(file)
  end
  file.close

  #when 'marc_xml_url'
  #  url = URI(params[:marc_xml_url])
  #  xml = open(url).read
  #  reader = MARC::XMLReader.new(StringIO.new(xml))
  #end

  # TODO
  for record in reader
    manifestation = Manifestation.new(original_title: expression.original_title)
    manifestation.carrier_type = CarrierType.find(1)
    manifestation.frequency = Frequency.find(1)
    manifestation.language = Language.find(1)
    manifestation.save

    full_name = record['700']['a']
    publisher = Agent.find_by(full_name: record['700']['a'])
    unless publisher
      publisher = Agent.new(full_name: full_name)
      publisher.save
    end
    manifestation.publishers << publisher
  end
end

#import_startObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/resource_import_file.rb', line 46

def import_start
  case edit_mode
  when 'create'
    import
  when 'update'
    modify
  when 'destroy'
    remove
  when 'update_relationship'
    update_relationship
  else
    import
  end
end

#modifyObject

def import_jpmarc

marc = NKF::nkf('-wc', self.db_file.data)
marc.split("\r\n").each do |record|
end

end



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
# File 'app/models/resource_import_file.rb', line 315

def modify
  transition_to!(:started)
  rows = open_import_file(create_import_temp_file(resource_import))
  rows.shift
  row_num = 1

  ResourceImportResult.create!(resource_import_file_id: id, body: rows.headers.join("\t"))
  rows.each do |row|
    row_num += 1
    import_result = ResourceImportResult.create!(resource_import_file_id: id, body: row.fields.join("\t"))
    item = Item.find_by(item_identifier: row['item_identifier'].to_s.strip) if row['item_identifier'].to_s.strip.present?
    unless item
      item = Item.find_by(id: row['item_id'].to_s.strip) if row['item_id'].to_s.strip.present?
    end

    if item
      if item.manifestation
        fetch(row, edit_mode: 'update')
        item = update_item(item, row)
      end

      ResourceImportFile.import_item_custom_value(row, item).each do |value|
        value.update!(item: item)
      end
      ResourceImportFile.import_manifestation_custom_value(row, item.manifestation).each do |value|
        value.update!(manifestation: item.manifestation)
      end

      item.manifestation.reload
      item.save!
      import_result.item = item
    else
      manifestation_identifier = row['manifestation_identifier'].to_s.strip
      manifestation = Manifestation.find_by(manifestation_identifier: manifestation_identifier) if manifestation_identifier.present?
      unless manifestation
        manifestation = Manifestation.find_by(id: row['manifestation_id'])
      end
      if manifestation
        fetch(row, edit_mode: 'update')
        ResourceImportFile.import_manifestation_custom_value(row, manifestation).each do |value|
          value.update!(manifestation: manifestation)
        end
        import_result.manifestation = manifestation
      end
    end
    import_result.save!
  end
  transition_to!(:completed)
  mailer = ResourceImportMailer.completed(self)
  send_message(mailer)
rescue => e
  self.error_message = "line #{row_num}: #{e.message}"
  save
  transition_to!(:failed)
  mailer = ResourceImportMailer.failed(self)
  send_message(mailer)
  raise e
end

#removeObject



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
# File 'app/models/resource_import_file.rb', line 374

def remove
  transition_to!(:started)
  rows = open_import_file(create_import_temp_file(resource_import))
  rows.shift
  row_num = 1

  rows.each do |row|
    row_num += 1
    item = Item.find_by(item_identifier: row['item_identifier'].to_s.strip) if row['item_identifier'].to_s.strip.present?
    unless item
      item = Item.find_by(id: row['item_id'].to_s.strip) if row['item_id'].to_s.strip.present?
    end

    if item
      item.destroy if item.removable?
    end
  end
  transition_to!(:completed)
  mailer = ResourceImportMailer.completed(self)
  send_message(mailer)
rescue => e
  self.error_message = "line #{row_num}: #{e.message}"
  save
  transition_to!(:failed)
  mailer = ResourceImportMailer.failed(self)
  send_message(mailer)
  raise e
end

#state_machineObject



39
40
41
# File 'app/models/resource_import_file.rb', line 39

def state_machine
  ResourceImportFileStateMachine.new(self, transition_class: ResourceImportFileTransition)
end

#update_relationshipObject



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
# File 'app/models/resource_import_file.rb', line 403

def update_relationship
  transition_to!(:started)
  rows = open_import_file(create_import_temp_file(resource_import))
  rows.shift
  row_num = 1

  rows.each do |row|
    item = Item.find_by(item_identifier: row['item_identifier'].to_s.strip) if row['item_identifier'].to_s.strip.present?
    unless item
      item = Item.find_by(id: row['item_id'].to_s.strip) if row['item_id'].to_s.strip.present?
    end

    manifestation_identifier = row['manifestation_identifier'].to_s.strip
    manifestation = Manifestation.find_by(manifestation_identifier: manifestation_identifier)
    unless manifestation
      manifestation = Manifestation.find_by(id: row['manifestation_id'].to_s.strip)
    end

    if item && manifestation
      item.manifestation = manifestation
      item.save!
    end

    import_result = ResourceImportResult.create!(resource_import_file_id: id, body: row.fields.join("\t"))
    import_result.item = item
    import_result.manifestation = manifestation
    import_result.save!
    row_num += 1
  end
  transition_to!(:completed)
  mailer = ResourceImportMailer.completed(self)
  send_message(mailer)
rescue => e
  self.error_message = "line #{row_num}: #{e.message}"
  save
  transition_to!(:failed)
  mailer = ResourceImportMailer.failed(self)
  send_message(mailer)
  raise e
end