Class: Manifestation

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/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.



260
261
262
# File 'app/models/manifestation.rb', line 260

def during_import
  @during_import
end

#parent_idObject

Returns the value of attribute parent_id.



260
261
262
# File 'app/models/manifestation.rb', line 260

def parent_id
  @parent_id
end

Class Method Details

.cached_numdocsObject



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

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

.export(options = {format: :txt}) ⇒ Object



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

def self.export(options = {format: :txt})
  header = %w(
    manifestation_id
    original_title
    creator
    contributor
    publisher
    pub_date
    manifestation_price
    manifestation_created_at
    manifestation_updated_at
    manifestation_identifier
  )
  identifiers = {}
  Identifier.find_each do |identifier|
    identifiers[identifier.identifier_type.name] = true
  end
  identifiers = identifiers.keys.sort
  header += identifiers
  header += %w(
    item_id
    item_identifier
    call_number
    item_price
    acquired_at
    accepted_at
    bookstore
    budget_type
    circulation_status
    shelf
    library
    item_created_at
    item_updated_at
  )
  lines = []
  lines << header
  Manifestation.includes(:items, :identifiers => :identifier_type).find_each do |m|
    if m.items.exists?
      m.items.includes(:shelf => :library).each do |i|
        item_lines = []
        item_lines << m.id
        item_lines << m.original_title
        item_lines << m.creators.pluck(:full_name).join("//")
        item_lines << m.contributors.pluck(:full_name).join("//")
        item_lines << m.publishers.pluck(:full_name).join("//")
        item_lines << m.pub_date
        item_lines << m.price
        item_lines << m.created_at
        item_lines << m.updated_at
        item_lines << m.manifestation_identifier
        identifiers.each do |identifier_type|
          item_lines << m.identifier_contents(identifier_type.to_sym).first
        end
        item_lines << i.id
        item_lines << i.item_identifier
        item_lines << i.call_number
        item_lines << i.price
        item_lines << i.acquired_at
        item_lines << i.accept.try(:created_at)
        item_lines << i.bookstore.try(:name)
        item_lines << i.budget_type.try(:name)
        item_lines << i.circulation_status.try(:name)
        item_lines << i.shelf.name
        item_lines << i.shelf.library.name
        item_lines << i.created_at
        item_lines << i.updated_at
        lines << item_lines
      end
    else
      line = []
      line << m.id
      line << m.original_title
      line << m.creators.pluck(:full_name).join("//")
      line << m.contributors.pluck(:full_name).join("//")
      line << m.publishers.pluck(:full_name).join("//")
      line << m.pub_date
      line << m.price
      line << m.created_at
      line << m.updated_at
      line << m.manifestation_identifier
      identifiers.each do |identifier_type|
        line << m.identifier_contents(identifier_type.to_sym).first
      end
      lines << line
    end
  end
  if options[:format] == :txt
    lines.map{|i| i.to_csv(col_sep: "\t")}.join
  else
    lines
  end
end

.find_by_isbn(isbn) ⇒ Object



431
432
433
434
435
# File 'app/models/manifestation.rb', line 431

def self.find_by_isbn(isbn)
  identifier_type = IdentifierType.where(name: 'isbn').first
  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: よりよい推薦方法



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

def self.pickup(keyword = nil, current_user = nil)
  return nil if self.cached_numdocs < 5
  if current_user.try(: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



441
442
443
# File 'app/models/manifestation.rb', line 441

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

#clear_cached_numdocsObject



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

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

#contributorObject



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

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

#created(agent) ⇒ Object



403
404
405
# File 'app/models/manifestation.rb', line 403

def created(agent)
  creates.where(agent_id: agent.id).first
end

#creatorObject



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

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

#extract_textObject



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

def extract_text
  return nil if attachment.path.nil?
  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



397
398
399
400
401
# File 'app/models/manifestation.rb', line 397

def extract_text!
  extract_text
  index
  Sunspot.commit
end

#first_issueObject



516
517
518
519
520
# File 'app/models/manifestation.rb', line 516

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



522
523
524
525
526
527
528
529
530
531
532
533
# File 'app/models/manifestation.rb', line 522

def identifier_contents(name)
  if Rails::VERSION::MAJOR > 3
    identifiers.id_type(name).order(:position).pluck(:body)
  else
    identifier_type = IdentifierType.where(name: name).first
    if identifier_type
      identifiers.where(identifier_type_id: identifier_type.id).order(:position).pluck(:body)
    else
      []
    end
  end
end

#index_series_statementObject



437
438
439
# File 'app/models/manifestation.rb', line 437

def index_series_statement
  series_statements.map{|s| s.index; s.root_manifestation.try(:index)}
end

#latest_issueObject



510
511
512
513
514
# File 'app/models/manifestation.rb', line 510

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



317
318
319
320
321
# File 'app/models/manifestation.rb', line 317

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

#parent_of_seriesObject



313
314
315
# File 'app/models/manifestation.rb', line 313

def parent_of_series
  original_manifestations
end

#produced(agent) ⇒ Object



411
412
413
# File 'app/models/manifestation.rb', line 411

def produced(agent)
  produces.where(agent_id: agent.id).first
end

#pub_datesObject



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

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



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

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

#realized(agent) ⇒ Object



407
408
409
# File 'app/models/manifestation.rb', line 407

def realized(agent)
  realizes.where(agent_id: agent.id).first
end

#root_series_statementObject



628
629
630
# File 'app/models/manifestation.rb', line 628

def root_series_statement
  series_statements.where(root_manifestation_id: id).first
end

#series_master?Boolean

Returns:

  • (Boolean)


445
446
447
448
# File 'app/models/manifestation.rb', line 445

def series_master?
  return true if root_series_statement
  false
end

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



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
483
484
# File 'app/models/manifestation.rb', line 454

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.where(agent_identifier: agent_list[:agent_identifier]).first
    end
    agent = Agent.where(full_name: name_and_role[0]).first 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.where(name: type).first
      create = Create.where(work_id: id, agent_id: agent.id).first
      if create
        create.create_type = role_type
        create.save(validate: false)
      end
    when :publisher
      type = 'publisher' if role_type.blank?
      produce = Produce.where(manifestation_id: id, agent_id: agent.id).first
      if produce
        produce.produce_type = ProduceType.where(name: type).first
        produce.save(validate: false)
      end
    else
      raise "#{options[:scope]} is not supported!"
    end
  end
end

#set_date_of_publicationObject



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
301
302
303
# File 'app/models/manifestation.rb', line 262

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



486
487
488
489
490
# File 'app/models/manifestation.rb', line 486

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



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

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



356
357
358
# File 'app/models/manifestation.rb', line 356

def title
  titles
end

#titlesObject



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'app/models/manifestation.rb', line 323

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

#urlObject



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

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

#web_itemObject



450
451
452
# File 'app/models/manifestation.rb', line 450

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