Class: Manifestation

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/manifestation.rb,
app/models2/manifestation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#during_importObject

Returns the value of attribute during_import.



257
258
259
# File 'app/models/manifestation.rb', line 257

def during_import
  @during_import
end

#parent_idObject

Returns the value of attribute parent_id.



257
258
259
# File 'app/models/manifestation.rb', line 257

def parent_id
  @parent_id
end

Class Method Details

.cached_numdocsObject



302
303
304
# File 'app/models/manifestation.rb', line 302

def self.cached_numdocs
  Rails.cache.fetch("manifestation_search_total"){Manifestation.search.total}
end

.csv_header(role: 'Guest') ⇒ Object

CSVのヘッダ

Parameters:

  • role (String) (defaults to: 'Guest')

    権限



526
527
528
# File 'app/models/manifestation.rb', line 526

def self.csv_header(role: 'Guest')
  Manifestation.new.to_hash(role: role).keys
end

.export(role: 'Guest', col_sep: "\t") ⇒ Object

TSVでのエクスポート

Parameters:

  • role (String) (defaults to: 'Guest')

    権限

  • col_sep (String) (defaults to: "\t")

    区切り文字



650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'app/models/manifestation.rb', line 650

def self.export(role: 'Guest', col_sep: "\t")
  file = Tempfile.create('manifestation_export') do |f|
    f.write (Manifestation.csv_header(role: role) + Item.csv_header(role: role)).to_csv(col_sep: col_sep)
    Manifestation.find_each do |manifestation|
      if manifestation.items.exists?
        manifestation.items.each do |item|
          f.write (manifestation.to_hash(role: role).values + item.to_hash(role: role).values).to_csv(col_sep: col_sep)
        end
      else
        f.write manifestation.to_hash(role: role).values.to_csv(col_sep: col_sep)
      end
    end

    f.rewind
    f.read
  end

  file
end

.find_by_isbn(isbn) ⇒ Object



429
430
431
432
433
# File 'app/models/manifestation.rb', line 429

def self.find_by_isbn(isbn)
  identifier_type = IdentifierType.find_by(name: 'isbn')
  return nil unless identifier_type
  Manifestation.includes(identifiers: :identifier_type).where("identifiers.body": isbn, "identifier_types.name": 'isbn')
end

.pickup(keyword = nil, current_user = nil) ⇒ Object

TODO: よりよい推薦方法



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'app/models/manifestation.rb', line 358

def self.pickup(keyword = nil, current_user = nil)
  return nil if self.cached_numdocs < 5
  if current_user&.role
    current_role_id = current_user.role.id
  else
    current_role_id = 1
  end

  # TODO: ヒット件数が0件のキーワードがあるときに指摘する
  response = Manifestation.search do
    fulltext keyword if keyword
    with(:required_role_id).less_than_or_equal_to current_role_id
    order_by(:random)
    paginate page: 1, per_page: 1
  end
  response.results.first
end

Instance Method Details

#acquired_atObject



439
440
441
# File 'app/models/manifestation.rb', line 439

def acquired_at
  items.order(:acquired_at).first.try(:acquired_at)
end

#clear_cached_numdocsObject



306
307
308
# File 'app/models/manifestation.rb', line 306

def clear_cached_numdocs
  Rails.cache.delete("manifestation_search_total")
end

#contributorObject



345
346
347
# File 'app/models/manifestation.rb', line 345

def contributor
  contributors.collect(&:name).flatten
end

#created(agent) ⇒ Object



401
402
403
# File 'app/models/manifestation.rb', line 401

def created(agent)
  creates.find_by(agent_id: agent.id)
end

#creatorObject



341
342
343
# File 'app/models/manifestation.rb', line 341

def creator
  creators.collect(&:name).flatten
end

#extract_textObject



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'app/models/manifestation.rb', line 376

def extract_text
  return nil if attachment.path.nil?
  return nil unless ENV['ENJU_EXTRACT_TEXT'] == 'true'
  if ENV['ENJU_STORAGE'] == 's3'
    body = Faraday.get(attachment.expiring_url(10)).body.force_encoding('UTF-8')
  else
    body = File.open(attachment.path).read
  end
  client = Faraday.new(url: ENV['SOLR_URL'] || Sunspot.config.solr.url) do |conn|
    conn.request :multipart
    conn.adapter :net_http
  end
  response = client.post('update/extract?extractOnly=true&wt=json&extractFormat=text') do |req|
    req.headers['Content-type'] = 'text/html'
    req.body = body
  end
  update_column(:fulltext, JSON.parse(response.body)[""])
end

#extract_text!Object



395
396
397
398
399
# File 'app/models/manifestation.rb', line 395

def extract_text!
  extract_text
  index
  Sunspot.commit
end

#first_issueObject



514
515
516
517
518
# File 'app/models/manifestation.rb', line 514

def first_issue
  if series_master?
    derived_manifestations.where('date_of_publication IS NOT NULL').order('date_of_publication DESC').first
  end
end

#identifier_contents(name) ⇒ Object



520
521
522
# File 'app/models/manifestation.rb', line 520

def identifier_contents(name)
  identifiers.id_type(name).order(:position).pluck(:body)
end

#index_series_statementObject



435
436
437
# File 'app/models/manifestation.rb', line 435

def index_series_statement
  series_statements.map{|s| s.index; s.root_manifestation&.index}
end

#isbn_charactersObject



674
675
676
677
678
679
680
681
682
683
684
685
# File 'app/models/manifestation.rb', line 674

def isbn_characters
  identifier_contents(:isbn).map{|i|
    isbn10 = isbn13 = isbn10_dash = isbn13_dash = nil
    isbn10 = Lisbn.new(i).isbn10
    isbn13 =  Lisbn.new(i).isbn13
    isbn10_dash = Lisbn.new(isbn10).isbn_with_dash if isbn10
    isbn13_dash = Lisbn.new(isbn13).isbn_with_dash if isbn13
    [
      isbn10, isbn13, isbn10_dash, isbn13_dash
    ]
  }.flatten
end

#latest_issueObject



508
509
510
511
512
# File 'app/models/manifestation.rb', line 508

def latest_issue
  if series_master?
    derived_manifestations.where('date_of_publication IS NOT NULL').order('date_of_publication DESC').first
  end
end

#number_of_pagesObject



314
315
316
317
318
# File 'app/models/manifestation.rb', line 314

def number_of_pages
  if start_page && end_page
    end_page.to_i - start_page.to_i + 1
  end
end

#parent_of_seriesObject



310
311
312
# File 'app/models/manifestation.rb', line 310

def parent_of_series
  original_manifestations
end

#produced(agent) ⇒ Object



409
410
411
# File 'app/models/manifestation.rb', line 409

def produced(agent)
  produces.find_by(agent_id: agent.id)
end

#pub_datesObject



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'app/models/manifestation.rb', line 490

def pub_dates
  return [] unless pub_date
  pub_date_array = pub_date.split(';')
  pub_date_array.map{|pub_date_string|
    date = nil
    while date.nil? do
      pub_date_string += '-01'
      break if pub_date_string =~ /-01-01-01$/
      begin
        date = Time.zone.parse(pub_date_string)
      rescue ArgumentError
      rescue TZInfo::AmbiguousTime
      end
    end
    date
  }.compact
end

#publisherObject



349
350
351
# File 'app/models/manifestation.rb', line 349

def publisher
  publishers.collect(&:name).flatten
end

#realized(agent) ⇒ Object



405
406
407
# File 'app/models/manifestation.rb', line 405

def realized(agent)
  realizes.find_by(agent_id: agent.id)
end

#root_series_statementObject



670
671
672
# File 'app/models/manifestation.rb', line 670

def root_series_statement
  series_statements.find_by(root_manifestation_id: id)
end

#series_master?Boolean

Returns:

  • (Boolean)


443
444
445
446
# File 'app/models/manifestation.rb', line 443

def series_master?
  return true if root_series_statement
  false
end

#set_agent_role_type(agent_lists, options = {scope: :creator}) ⇒ Object



452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'app/models/manifestation.rb', line 452

def set_agent_role_type(agent_lists, options = {scope: :creator})
  agent_lists.each do |agent_list|
    name_and_role = agent_list[:full_name].split('||')
    if agent_list[:agent_identifier].present?
      agent = Agent.find_by(agent_identifier: agent_list[:agent_identifier])
    end
    agent = Agent.find_by(full_name: name_and_role[0]) unless agent
    next unless agent
    type = name_and_role[1].to_s.strip

    case options[:scope]
    when :creator
      type = 'author' if type.blank?
      role_type = CreateType.find_by(name: type)
      create = Create.find_by(work_id: id, agent_id: agent.id)
      if create
        create.create_type = role_type
        create.save(validate: false)
      end
    when :publisher
      type = 'publisher' if role_type.blank?
      produce = Produce.find_by(manifestation_id: id, agent_id: agent.id)
      if produce
        produce.produce_type = ProduceType.find_by(name: type)
        produce.save(validate: false)
      end
    else
      raise "#{options[:scope]} is not supported!"
    end
  end
end

#set_custom_property(row) ⇒ Object



687
688
689
690
691
692
693
694
695
696
697
# File 'app/models/manifestation.rb', line 687

def set_custom_property(row)
  ManifestationCustomProperty.all.each do |property|
    if row[property]
      custom_value = ManifestationCustomValue.new(
        manifestation: self,
        manifestation_custom_property: property,
        value: row[property]
      )
    end
  end
end

#set_date_of_publicationObject



259
260
261
262
263
264
265
266
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
300
# File 'app/models/manifestation.rb', line 259

def set_date_of_publication
  return if pub_date.blank?
  year = Time.utc(pub_date.rjust(4, "0")).year rescue nil
  begin
    date = Time.zone.parse(pub_date.rjust(4, "0"))
    if date.year != year
      raise ArgumentError
    end
  rescue ArgumentError, TZInfo::AmbiguousTime
    date = nil
  end

  pub_date_string = pub_date.rjust(4, "0").split(';').first.gsub(/[\[\]]/, '')
  if pub_date_string.length == 4
    date = Time.zone.parse(Time.utc(pub_date_string).to_s).beginning_of_day
  else
    while date.nil? do
      pub_date_string += '-01'
      break if pub_date_string =~ /-01-01-01$/
      begin
        date = Time.zone.parse(pub_date_string)
        if date.year != year
          raise ArgumentError
        end
      rescue ArgumentError
        date = nil
      rescue TZInfo::AmbiguousTime
        date = nil
        self.year_of_publication = pub_date_string.to_i if pub_date_string =~ /^\d+$/
        break
      end
    end
  end

  if date
    self.year_of_publication = date.year
    self.month_of_publication = date.month
    if date.year > 0
      self.date_of_publication = date
    end
  end
end

#set_numberObject



484
485
486
487
488
# File 'app/models/manifestation.rb', line 484

def set_number
  self.volume_number = volume_number_string.scan(/\d*/).map{|s| s.to_i if s =~ /\d/}.compact.first if volume_number_string && !volume_number?
  self.issue_number = issue_number_string.scan(/\d*/).map{|s| s.to_i if s =~ /\d/}.compact.first if issue_number_string && !issue_number?
  self.edition = edition_string.scan(/\d*/).map{|s| s.to_i if s =~ /\d/}.compact.first if edition_string && !edition?
end

#sort_titleObject



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'app/models/manifestation.rb', line 413

def sort_title
  if series_master?
    if root_series_statement.title_transcription?
      NKF.nkf('-w --katakana', root_series_statement.title_transcription)
    else
      root_series_statement.original_title
    end
  else
    if title_transcription?
      NKF.nkf('-w --katakana', title_transcription)
    else
      original_title
    end
  end
end

#titleObject



353
354
355
# File 'app/models/manifestation.rb', line 353

def title
  titles
end

#titlesObject



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'app/models/manifestation.rb', line 320

def titles
  title = []
  title << original_title.to_s.strip
  title << title_transcription.to_s.strip
  title << title_alternative.to_s.strip
  title << volume_number_string
  title << issue_number_string
  title << serial_number.to_s
  title << edition_string
  title << series_statements.map{|s| s.titles}
  #title << original_title.wakati
  #title << title_transcription.wakati rescue nil
  #title << title_alternative.wakati rescue nil
  title.flatten
end

#to_hash(role: 'Guest') ⇒ Object

CSV出力用のハッシュ

Parameters:

  • role (String) (defaults to: 'Guest')

    権限



532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'app/models/manifestation.rb', line 532

def to_hash(role: 'Guest')
  record = {
    manifestation_id: id,
    original_title: original_title,
    title_alternative: title_alternative,
    title_transcription: title_transcription,
    statement_of_responsibility: statement_of_responsibility,
    serial: serial,
    manifestation_identifier: manifestation_identifier,
    creator: creates.map{|create|
      if create.create_type
        "#{create.agent.full_name}||#{create.create_type.name}"
      else
        "#{create.agent.full_name}"
      end
    }.join('//'),
    contributor: realizes.map{|realize|
      if realize.realize_type
        "#{realize.agent.full_name}||#{realize.realize_type.name}"
      else
        "#{realize.agent.full_name}"
      end
    }.join('//'),
    publisher: produces.map{|produce|
      if produce.produce_type
        "#{produce.agent.full_name}||#{produce.produce_type.name}"
      else
        "#{produce.agent.full_name}"
      end
    }.join('//'),
    pub_date: date_of_publication,
    year_of_publication: year_of_publication,
    publication_place: publication_place,
    manifestation_created_at: created_at,
    manifestation_updated_at: updated_at,
    carrier_type: carrier_type.name,
    content_type: manifestation_content_type.name,
    frequency: frequency.name,
    language: language.name,
    isbn: identifier_contents(:isbn).join('//'),
    issn: identifier_contents(:issn).join('//'),
    volume_number: volume_number,
    volume_number_string: volume_number_string,
    edition: edition,
    edition_string: edition_string,
    issue_number: issue_number,
    issue_number_string: issue_number_string,
    serial_number: serial_number,
    extent: extent,
    start_page: start_page,
    end_page: end_page,
    dimensions: dimensions,
    height: height,
    width: width,
    depth: depth,
    manifestation_price: price,
    access_address: access_address,
    manifestation_required_role: required_role.name,
    abstract: abstract,
    description: description,
    note: note
  }

  IdentifierType.find_each do |type|
    record[:"identifier:#{type.name.to_sym}"] = identifiers.where(identifier_type: type).pluck(:body).join('//')
  end

  series = series_statements.order(:position)
  record.merge!(
    series_statement_id: series.pluck(:id).join('//'),
    series_statement_original_title: series.pluck(:original_title).join('.//'),
    series_statement_title_subseries: series.pluck(:title_subseries).join('//'),
    series_statement_title_subseries_transcription: series.pluck(:title_subseries_transcription).join('//'),
    series_statement_title_transcription: series.pluck(:title_transcription).join('//'),
    series_statement_creator: series.pluck(:creator_string).join('//'),
    series_statement_volume_number: series.pluck(:volume_number_string).join('//'),
    series_statement_series_master: series.pluck(:series_master).join('//'),
    series_statement_root_manifestation_id: series.pluck(:root_manifestation_id).join('//'),
    series_statement_manifestation_id: series.pluck(:manifestation_id).join('//'),
    series_statement_position: series.pluck(:position).join('//'),
    series_statement_note: series.pluck(:note).join('//'),
    series_statement_created_at: series.pluck(:created_at).join('//'),
    series_statement_updated_at: series.pluck(:updated_at).join('//')
  )

  if ['Administrator', 'Librarian'].include?(role)
    record.merge!({
      manifestation_memo: memo
    })
    ManifestationCustomProperty.order(:position).each do |custom_property|
      custom_value = manifestation_custom_values.find_by(manifestation_custom_property: custom_property)
      record[:"manifestation:#{custom_property.name}"] = custom_value&.value
    end
  end

  if defined?(EnjuSubject)
    SubjectHeadingType.find_each do |type|
      record[:"subject:#{type.name}"] = subjects.where(subject_heading_type: type).pluck(:term).join('//')
    end
    ClassificationType.find_each do |type|
      record[:"classification:#{type.name}"] = classifications.where(classification_type: type).pluck(:category).join('//')
    end
  end

  if defined?(EnjuNdl)
    record["jpno"] = identifier_contents(:jpno).first
  end

  if defined?(EnjuNii)
    record["ncid"] = identifier_contents(:ncid).first
  end

  record
end

#urlObject



336
337
338
339
# File 'app/models/manifestation.rb', line 336

def url
  #access_address
  "#{LibraryGroup.site_config.url}#{self.class.to_s.tableize}/#{self.id}"
end

#web_itemObject



448
449
450
# File 'app/models/manifestation.rb', line 448

def web_item
  items.find_by(shelf_id: Shelf.web.id)
end