enju_biblio 0.3.8 → 0.3.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (129) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/content_types_controller.rb +1 -1
  3. data/app/controllers/item_custom_properties_controller.rb +69 -0
  4. data/app/controllers/items_controller.rb +2 -3
  5. data/app/controllers/manifestation_custom_properties_controller.rb +69 -0
  6. data/app/controllers/manifestations_controller.rb +12 -15
  7. data/app/controllers/resource_import_results_controller.rb +6 -2
  8. data/app/models/agent.rb +6 -6
  9. data/app/models/agent_import_file.rb +7 -7
  10. data/app/models/agent_type.rb +0 -1
  11. data/app/models/carrier_type.rb +0 -1
  12. data/app/models/content_type.rb +0 -1
  13. data/app/models/form_of_work.rb +1 -2
  14. data/app/models/frequency.rb +0 -1
  15. data/app/models/identifier_type.rb +0 -1
  16. data/app/models/item.rb +15 -16
  17. data/app/models/item_custom_property.rb +18 -0
  18. data/app/models/item_custom_value.rb +17 -0
  19. data/app/models/license.rb +0 -1
  20. data/app/models/manifestation.rb +54 -22
  21. data/app/models/manifestation_custom_property.rb +18 -0
  22. data/app/models/manifestation_custom_value.rb +17 -0
  23. data/app/models/medium_of_performance.rb +1 -2
  24. data/app/models/picture_file.rb +0 -2
  25. data/app/models/resource_export_file.rb +4 -2
  26. data/app/models/resource_import_file.rb +157 -67
  27. data/app/policies/item_custom_property_policy.rb +21 -0
  28. data/app/policies/manifestation_custom_property_policy.rb +21 -0
  29. data/app/views/agent_import_results/index.txt.ruby +5 -0
  30. data/app/views/agent_relationship_types/_form.html.erb +1 -1
  31. data/app/views/agent_types/_form.html.erb +1 -1
  32. data/app/views/carrier_types/_form.html.erb +1 -1
  33. data/app/views/create_types/_form.html.erb +1 -1
  34. data/app/views/form_of_works/_form.html.erb +1 -1
  35. data/app/views/frequencies/_form.html.erb +1 -1
  36. data/app/views/item_custom_properties/_form.html.erb +32 -0
  37. data/app/views/item_custom_properties/edit.html.erb +13 -0
  38. data/app/views/item_custom_properties/index.html.erb +43 -0
  39. data/app/views/item_custom_properties/new.html.erb +12 -0
  40. data/app/views/item_custom_properties/show.html.erb +28 -0
  41. data/app/views/items/_form.html.erb +13 -10
  42. data/app/views/items/show.html.erb +3 -4
  43. data/app/views/licenses/_form.html.erb +1 -1
  44. data/app/views/manifestation_custom_properties/_form.html.erb +32 -0
  45. data/app/views/manifestation_custom_properties/edit.html.erb +13 -0
  46. data/app/views/manifestation_custom_properties/index.html.erb +43 -0
  47. data/app/views/manifestation_custom_properties/new.html.erb +12 -0
  48. data/app/views/manifestation_custom_properties/show.html.erb +28 -0
  49. data/app/views/manifestation_relationship_types/_form.html.erb +1 -1
  50. data/app/views/manifestations/_export_detail.html.erb +1 -0
  51. data/app/views/manifestations/_export_list.html.erb +1 -0
  52. data/app/views/manifestations/_form.html.erb +18 -10
  53. data/app/views/manifestations/_show_detail_librarian.html.erb +9 -3
  54. data/app/views/manifestations/_show_detail_user.html.erb +8 -0
  55. data/app/views/manifestations/show.json.jbuilder +2 -0
  56. data/app/views/medium_of_performances/_form.html.erb +1 -1
  57. data/app/views/picture_files/edit.html.erb +4 -0
  58. data/app/views/produce_types/_form.html.erb +1 -1
  59. data/app/views/realize_types/_form.html.erb +1 -1
  60. data/app/views/resource_import_results/index.txt.ruby +5 -0
  61. data/app/views/series_statement_merges/_form.html.erb +1 -1
  62. data/config/locales/translation_en.yml +22 -8
  63. data/config/locales/translation_ja.yml +22 -8
  64. data/config/routes.rb +2 -0
  65. data/db/migrate/20081027150907_create_picture_files.rb +0 -2
  66. data/db/migrate/20200425072340_create_manifestation_custom_properties.rb +12 -0
  67. data/db/migrate/20200425072349_create_item_custom_properties.rb +12 -0
  68. data/db/migrate/20200425074758_create_manifestation_custom_values.rb +12 -0
  69. data/db/migrate/20200425074822_create_item_custom_values.rb +12 -0
  70. data/lib/enju_biblio/version.rb +1 -1
  71. data/spec/cassette_library/ResourceImportFile/when_its_mode_is_create_/when_it_is_written_in_utf-8/should_import_custom_values.yml +121 -0
  72. data/spec/cassette_library/resource_import_results/index_txt_ruby/renders_a_list_of_resource_import_results.yml +121 -0
  73. data/spec/controllers/content_types_controller_spec.rb +1 -1
  74. data/spec/controllers/item_custom_properties_controller_spec.rb +449 -0
  75. data/spec/controllers/items_controller_spec.rb +22 -25
  76. data/spec/controllers/manifestation_custom_properties_controller_spec.rb +449 -0
  77. data/spec/controllers/manifestations_controller_spec.rb +20 -23
  78. data/spec/dummy/db/schema.rb +43 -12
  79. data/spec/factories/item_custom_property.rb +6 -0
  80. data/spec/factories/item_custom_value.rb +6 -0
  81. data/spec/factories/manifestation_custom_property.rb +6 -0
  82. data/spec/factories/manifestation_custom_value.rb +6 -0
  83. data/spec/fixtures/creates.yml +2 -0
  84. data/spec/fixtures/item_custom_properties.yml +24 -0
  85. data/spec/fixtures/library_groups.yml +29 -15
  86. data/spec/fixtures/manifestation_custom_properties.yml +24 -0
  87. data/spec/fixtures/picture_files.yml +0 -2
  88. data/spec/fixtures/produces.yml +2 -0
  89. data/spec/fixtures/realizes.yml +2 -0
  90. data/spec/fixtures/resource_import_results.yml +2 -2
  91. data/spec/models/item_custom_property_spec.rb +18 -0
  92. data/spec/models/item_custom_value_spec.rb +17 -0
  93. data/spec/models/manifestation_custom_property_spec.rb +18 -0
  94. data/spec/models/manifestation_custom_value_spec.rb +17 -0
  95. data/spec/models/manifestation_spec.rb +11 -21
  96. data/spec/models/picture_file_spec.rb +0 -2
  97. data/spec/models/resource_export_file_spec.rb +61 -8
  98. data/spec/models/resource_import_file_spec.rb +43 -13
  99. data/spec/requests/item_custom_properties_spec.rb +129 -0
  100. data/spec/requests/manifestation_custom_properties_spec.rb +129 -0
  101. data/spec/routing/item_custom_properties_routing_spec.rb +38 -0
  102. data/spec/routing/manifestation_custom_properties_routing_spec.rb +38 -0
  103. data/spec/system/items_spec.rb +12 -0
  104. data/spec/system/manifestations_spec.rb +12 -0
  105. data/spec/views/agent_import_results/index.html.erb_spec.rb +29 -0
  106. data/spec/views/agent_import_results/index.txt.ruby_spec.rb +20 -0
  107. data/spec/views/agent_import_results/show.html.erb_spec.rb +24 -0
  108. data/spec/views/item_custom_properties/edit.html.erb_spec.rb +21 -0
  109. data/spec/views/item_custom_properties/index.html.erb_spec.rb +25 -0
  110. data/spec/views/item_custom_properties/new.html.erb_spec.rb +21 -0
  111. data/spec/views/item_custom_properties/show.html.erb_spec.rb +16 -0
  112. data/spec/views/manifestation_custom_properties/edit.html.erb_spec.rb +21 -0
  113. data/spec/views/manifestation_custom_properties/index.html.erb_spec.rb +25 -0
  114. data/spec/views/manifestation_custom_properties/new.html.erb_spec.rb +21 -0
  115. data/spec/views/manifestation_custom_properties/show.html.erb_spec.rb +16 -0
  116. data/spec/views/manifestations/show.json.jbuilder_spec.rb +16 -0
  117. data/spec/views/manifestations/show.txt.ruby_spec.rb +1 -1
  118. data/spec/views/resource_import_results/index.txt.ruby_spec.rb +22 -0
  119. data/spec/views/resource_import_results/show.html.erb_spec.rb +1 -1
  120. metadata +118 -37
  121. data/app/models/custom_property.rb +0 -17
  122. data/app/views/agent_import_results/index.txt.erb +0 -3
  123. data/app/views/items/_custom_property_fields.html.erb +0 -7
  124. data/app/views/manifestations/_custom_property_fields.html.erb +0 -7
  125. data/app/views/resource_import_results/index.txt.erb +0 -3
  126. data/db/migrate/20191219122214_create_custom_properties.rb +0 -12
  127. data/spec/factories/custom_property.rb +0 -18
  128. data/spec/models/custom_property_spec.rb +0 -18
  129. data/spec/system/custom_properties_spec.rb +0 -84
@@ -0,0 +1,18 @@
1
+ class ItemCustomProperty < ApplicationRecord
2
+ include MasterModel
3
+ validates :name, presence: true, uniqueness: true
4
+ acts_as_list
5
+ end
6
+
7
+ # == Schema Information
8
+ #
9
+ # Table name: item_custom_properties
10
+ #
11
+ # id :integer not null, primary key
12
+ # name :string not null
13
+ # display_name :text not null
14
+ # note :text
15
+ # position :integer default(1), not null
16
+ # created_at :datetime not null
17
+ # updated_at :datetime not null
18
+ #
@@ -0,0 +1,17 @@
1
+ class ItemCustomValue < ApplicationRecord
2
+ belongs_to :item_custom_property
3
+ belongs_to :item, required: false
4
+ validates :item_custom_property, uniqueness: {scope: :item_id}
5
+ end
6
+
7
+ # == Schema Information
8
+ #
9
+ # Table name: item_custom_values
10
+ #
11
+ # id :integer not null, primary key
12
+ # item_custom_property_id :integer not null
13
+ # item_id :integer not null
14
+ # value :text
15
+ # created_at :datetime not null
16
+ # updated_at :datetime not null
17
+ #
@@ -1,6 +1,5 @@
1
1
  class License < ApplicationRecord
2
2
  include MasterModel
3
- default_scope { order('licenses.position') }
4
3
  end
5
4
 
6
5
  # == Schema Information
@@ -25,13 +25,13 @@ class Manifestation < ApplicationRecord
25
25
  belongs_to :required_role, class_name: 'Role', foreign_key: 'required_role_id'
26
26
  has_one :resource_import_result
27
27
  has_many :identifiers, dependent: :destroy
28
- has_many :custom_properties, as: :resource, dependent: :destroy
28
+ has_many :manifestation_custom_values, -> { joins(:manifestation_custom_property).order(:position) }
29
29
  accepts_nested_attributes_for :creators, allow_destroy: true, reject_if: :all_blank
30
30
  accepts_nested_attributes_for :contributors, allow_destroy: true, reject_if: :all_blank
31
31
  accepts_nested_attributes_for :publishers, allow_destroy: true, reject_if: :all_blank
32
32
  accepts_nested_attributes_for :series_statements, allow_destroy: true, reject_if: :all_blank
33
33
  accepts_nested_attributes_for :identifiers, allow_destroy: true, reject_if: :all_blank
34
- accepts_nested_attributes_for :custom_properties, allow_destroy: true, reject_if: :all_blank
34
+ accepts_nested_attributes_for :manifestation_custom_values, reject_if: :all_blank
35
35
 
36
36
  searchable do
37
37
  text :title, default_boost: 2 do
@@ -96,7 +96,7 @@ class Manifestation < ApplicationRecord
96
96
  end
97
97
  end
98
98
  string :language do
99
- language.try(:name)
99
+ language&.name
100
100
  end
101
101
  string :item_identifier, multiple: true do
102
102
  if series_master?
@@ -120,7 +120,7 @@ class Manifestation < ApplicationRecord
120
120
  end
121
121
  time :date_of_publication
122
122
  integer :pub_year do
123
- date_of_publication.try(:year)
123
+ date_of_publication&.year
124
124
  end
125
125
  integer :creator_ids, multiple: true
126
126
  integer :contributor_ids, multiple: true
@@ -357,7 +357,7 @@ class Manifestation < ApplicationRecord
357
357
  # TODO: よりよい推薦方法
358
358
  def self.pickup(keyword = nil, current_user = nil)
359
359
  return nil if self.cached_numdocs < 5
360
- if current_user.try(:role)
360
+ if current_user&.role
361
361
  current_role_id = current_user.role.id
362
362
  else
363
363
  current_role_id = 1
@@ -433,7 +433,7 @@ class Manifestation < ApplicationRecord
433
433
  end
434
434
 
435
435
  def index_series_statement
436
- series_statements.map{|s| s.index; s.root_manifestation.try(:index)}
436
+ series_statements.map{|s| s.index; s.root_manifestation&.index}
437
437
  end
438
438
 
439
439
  def acquired_at
@@ -541,16 +541,34 @@ class Manifestation < ApplicationRecord
541
541
  def to_hash(role: 'Guest')
542
542
  record = {
543
543
  manifestation_id: id,
544
- title: original_title,
544
+ original_title: original_title,
545
545
  title_alternative: title_alternative,
546
546
  title_transcription: title_transcription,
547
547
  statement_of_responsibility: statement_of_responsibility,
548
548
  serial: serial,
549
549
  manifestation_identifier: manifestation_identifier,
550
- creator: creators.pluck(:full_name).join('//'),
551
- contributor: contributors.pluck(:full_name).join('//'),
552
- publisher: publishers.pluck(:full_name).join('//'),
553
- date_of_publication: date_of_publication,
550
+ creator: creates.map{|create|
551
+ if create.create_type
552
+ "#{create.agent.full_name}||#{create.create_type.name}"
553
+ else
554
+ "#{create.agent.full_name}"
555
+ end
556
+ }.join('//'),
557
+ contributor: realizes.map{|realize|
558
+ if realize.realize_type
559
+ "#{realize.agent.full_name}||#{realize.realize_type.name}"
560
+ else
561
+ "#{realize.agent.full_name}"
562
+ end
563
+ }.join('//'),
564
+ publisher: produces.map{|produce|
565
+ if produce.produce_type
566
+ "#{produce.agent.full_name}||#{produce.produce_type.name}"
567
+ else
568
+ "#{produce.agent.full_name}"
569
+ end
570
+ }.join('//'),
571
+ pub_date: date_of_publication,
554
572
  year_of_publication: year_of_publication,
555
573
  publication_place: publication_place,
556
574
  manifestation_created_at: created_at,
@@ -577,13 +595,13 @@ class Manifestation < ApplicationRecord
577
595
  depth: depth,
578
596
  price: price,
579
597
  access_address: access_address,
580
- required_role: required_role.name,
598
+ manifestation_required_role: required_role.name,
581
599
  description: description,
582
600
  note: note
583
601
  }
584
602
 
585
603
  IdentifierType.find_each do |type|
586
- record[type.name.to_sym] = identifiers.where(identifier_type: type).pluck(:body).join('//')
604
+ record[:"identifier:#{type.name.to_sym}"] = identifiers.where(identifier_type: type).pluck(:body).join('//')
587
605
  end
588
606
 
589
607
  series = series_statements.order(:position)
@@ -608,15 +626,9 @@ class Manifestation < ApplicationRecord
608
626
  record.merge!({
609
627
  memo: memo
610
628
  })
611
-
612
- # 最もカスタム項目の多い資料について、カスタム項目の個数を取得する
613
- ActiveRecord::Base.connection.execute('SELECT max(record_count) FROM (SELECT count(*) AS record_count, resource_id, resource_type FROM custom_properties GROUP BY resource_id, resource_type) AS type_count ;').first.values[0].to_i.times do |i|
614
- property = custom_properties[i]
615
- if property
616
- record[:"manifestation_custom_property_#{i + 1}"] = "#{property.label}: #{property.value}"
617
- else
618
- record[:"manifestation_custom_property_#{i + 1}"] = nil
619
- end
629
+ ManifestationCustomProperty.order(:position).each do |custom_property|
630
+ custom_value = manifestation_custom_values.find_by(manifestation_custom_property: custom_property)
631
+ record[:"manifestation:#{custom_property.name}"] = custom_value&.value
620
632
  end
621
633
  end
622
634
 
@@ -629,6 +641,14 @@ class Manifestation < ApplicationRecord
629
641
  end
630
642
  end
631
643
 
644
+ if defined?(EnjuNdl)
645
+ record["jpno"] = identifier_contents(:jpno).first
646
+ end
647
+
648
+ if defined?(EnjuNii)
649
+ record["ncid"] = identifier_contents(:ncid).first
650
+ end
651
+
632
652
  record
633
653
  end
634
654
 
@@ -671,6 +691,18 @@ class Manifestation < ApplicationRecord
671
691
  ]
672
692
  }.flatten
673
693
  end
694
+
695
+ def set_custom_property(row)
696
+ ManifestationCustomProperty.all.each do |property|
697
+ if row[property]
698
+ custom_value = ManifestationCustomValue.new(
699
+ manifestation: self,
700
+ manifestation_custom_property: property,
701
+ value: row[property]
702
+ )
703
+ end
704
+ end
705
+ end
674
706
  end
675
707
 
676
708
  # == Schema Information
@@ -0,0 +1,18 @@
1
+ class ManifestationCustomProperty < ApplicationRecord
2
+ include MasterModel
3
+ validates :name, presence: true, uniqueness: true
4
+ acts_as_list
5
+ end
6
+
7
+ # == Schema Information
8
+ #
9
+ # Table name: manifestation_custom_properties
10
+ #
11
+ # id :integer not null, primary key
12
+ # name :string not null
13
+ # display_name :text not null
14
+ # note :text
15
+ # position :integer default(1), not null
16
+ # created_at :datetime not null
17
+ # updated_at :datetime not null
18
+ #
@@ -0,0 +1,17 @@
1
+ class ManifestationCustomValue < ApplicationRecord
2
+ belongs_to :manifestation_custom_property
3
+ belongs_to :manifestation, required: false
4
+ validates :manifestation_custom_property, uniqueness: {scope: :manifestation_id}
5
+ end
6
+
7
+ # == Schema Information
8
+ #
9
+ # Table name: manifestation_custom_values
10
+ #
11
+ # id :integer not null, primary key
12
+ # manifestation_custom_property_id :integer not null
13
+ # manifestation_id :integer not null
14
+ # value :text
15
+ # created_at :datetime not null
16
+ # updated_at :datetime not null
17
+ #
@@ -1,7 +1,6 @@
1
1
  class MediumOfPerformance < ApplicationRecord
2
2
  include MasterModel
3
- default_scope { order('medium_of_performances.position') }
4
- has_many :works
3
+ has_many :works, class_name: 'Manifestation'
5
4
  end
6
5
 
7
6
  # == Schema Information
@@ -45,9 +45,7 @@ end
45
45
  # id :integer not null, primary key
46
46
  # picture_attachable_id :integer
47
47
  # picture_attachable_type :string
48
- # content_type :string
49
48
  # title :text
50
- # thumbnail :string
51
49
  # position :integer
52
50
  # created_at :datetime
53
51
  # updated_at :datetime
@@ -31,8 +31,10 @@ class ResourceExportFile < ApplicationRecord
31
31
  transition_to!(:started)
32
32
  role_name = user.try(:role).try(:name)
33
33
  tsv = Manifestation.export(role: role_name)
34
- self.resource_export = StringIO.new(tsv)
35
- self.resource_export.instance_write(:filename, "resource_export.txt")
34
+ file = StringIO.new(tsv)
35
+ file.class.class_eval { attr_accessor :original_filename, :content_type }
36
+ file.original_filename = 'resource_export.txt'
37
+ self.resource_export = file
36
38
  save!
37
39
  transition_to!(:completed)
38
40
  mailer = ResourceExportMailer.completed(self)
@@ -76,12 +76,13 @@ class ResourceImportFile < ApplicationRecord
76
76
  #end
77
77
  row_num = 1
78
78
 
79
- ResourceImportResult.create!(resource_import_file_id: id, body: rows.headers.join("\t"))
79
+ ResourceImportResult.create!(resource_import_file_id: id, body: (%w( imported_manifestation_id imported_item_id ) + rows.headers).join("\t"))
80
80
  rows.each do |row|
81
81
  row_num += 1
82
- import_result = ResourceImportResult.create!(resource_import_file_id: id, body: row.fields.join("\t"))
82
+ import_result = ResourceImportResult.new(resource_import_file_id: id)
83
83
  if row['dummy'].to_s.strip.present?
84
84
  import_result.error_message = "line #{row_num}: #{I18n.t('import.dummy')}"
85
+ import_result.body = ([nil, nil] + row.fields).join("\t")
85
86
  import_result.save!
86
87
  next
87
88
  end
@@ -92,6 +93,7 @@ class ResourceImportFile < ApplicationRecord
92
93
  import_result.item = item
93
94
  import_result.manifestation = item.manifestation
94
95
  import_result.error_message = "line #{row_num}: #{I18n.t('import.item_found')}"
96
+ import_result.body = ([item.manifestation.id, item.id] + row.fields).join("\t")
95
97
  import_result.save!
96
98
  num[:item_found] += 1
97
99
  next
@@ -187,8 +189,13 @@ class ResourceImportFile < ApplicationRecord
187
189
  import_result.manifestation = manifestation
188
190
 
189
191
  if manifestation
192
+ ResourceImportFile.import_manifestation_custom_value(row, manifestation).each do |value|
193
+ value.update!(manifestation: manifestation)
194
+ end
195
+
196
+ item = nil
190
197
  if item_identifier.present? || row['shelf'].present? || row['call_number'].present?
191
- import_result.item = create_item(row, manifestation)
198
+ item = create_item(row, manifestation)
192
199
  else
193
200
  if manifestation.fulltext_content?
194
201
  item = create_item(row, manifestation)
@@ -200,17 +207,21 @@ class ResourceImportFile < ApplicationRecord
200
207
  end
201
208
  num[:failed] += 1
202
209
  end
210
+
211
+ if item
212
+ ResourceImportFile.import_item_custom_value(row, item).each do |value|
213
+ value.update!(item: item)
214
+ end
215
+
216
+ import_result.item = item
217
+ end
203
218
  else
204
219
  num[:failed] += 1
205
220
  end
206
221
 
222
+ import_result.body = ([manifestation.try(:id), item.try(:id)] + row.fields).join("\t")
207
223
  import_result.save!
208
- num[:item_imported] +=1 if import_result.item
209
-
210
- if row_num % 50 == 0
211
- Sunspot.commit
212
- GC.start
213
- end
224
+ num[:item_imported] += 1 if import_result.item
214
225
  end
215
226
 
216
227
  Sunspot.commit
@@ -311,56 +322,24 @@ class ResourceImportFile < ApplicationRecord
311
322
  rows.each do |row|
312
323
  row_num += 1
313
324
  import_result = ResourceImportResult.create!(resource_import_file_id: id, body: row.fields.join("\t"))
314
- item_identifier = row['item_identifier'].to_s.strip
315
- item = Item.find_by(item_identifier: item_identifier) if item_identifier.present?
325
+ item = Item.find_by(item_identifier: row['item_identifier'].to_s.strip) if row['item_identifier'].to_s.strip.present?
326
+ unless item
327
+ item = Item.find_by(id: row['item_id'].to_s.strip) if row['item_id'].to_s.strip.present?
328
+ end
329
+
316
330
  if item
317
331
  if item.manifestation
318
332
  fetch(row, edit_mode: 'update')
333
+ item = update_item(item, row)
319
334
  end
320
335
 
321
- if defined?(EnjuCirculation)
322
- circulation_status = CirculationStatus.find_by(name: row['circulation_status'])
323
- checkout_type = CheckoutType.find_by(name: row['checkout_type'])
324
- use_restriction = UseRestriction.find_by(name: row['use_restriction'].to_s.strip)
336
+ ResourceImportFile.import_item_custom_value(row, item).each do |value|
337
+ value.update!(item: item)
325
338
  end
326
-
327
- shelf = Shelf.find_by(name: row['shelf'].to_s.strip)
328
- bookstore = Bookstore.find_by(name: row['bookstore'])
329
- required_role = Role.find_by(name: row['required_role'])
330
-
331
- item.shelf = shelf if shelf
332
- item.circulation_status = circulation_status if circulation_status
333
- item.checkout_type = checkout_type if checkout_type
334
- item.bookstore = bookstore if bookstore
335
- item.required_role = required_role if required_role
336
- item.use_restriction = use_restriction if use_restriction
337
-
338
- acquired_at = Time.zone.parse(row['acquired_at']) rescue nil
339
- binded_at = Time.zone.parse(row['binded_at']) rescue nil
340
- item.acquired_at = acquired_at if acquired_at
341
- item.binded_at = binded_at if binded_at
342
-
343
- item_columns = %w(
344
- call_number
345
- binding_item_identifier binding_call_number binded_at
346
- )
347
- item_columns.each do |column|
348
- if row[column].present?
349
- item.assign_attributes(:"#{column}" => row[column])
350
- end
339
+ ResourceImportFile.import_manifestation_custom_value(row, item.manifestation).each do |value|
340
+ value.update!(manifestation: item.manifestation)
351
341
  end
352
342
 
353
- item.price = row['item_price'] if row['item_price'].present?
354
- item.note = row['item_note'].try(:gsub, /\\n/, "\n") if row['item_note'].present?
355
- item.url = row['item_url'] if row['item_url'].present?
356
-
357
- if row['include_supplements']
358
- if %w(t true).include?(row['include_supplements'].downcase.strip)
359
- item.include_supplements = true
360
- else
361
- item.include_supplements = false if item.include_supplements
362
- end
363
- end
364
343
  item.manifestation.reload
365
344
  item.save!
366
345
  import_result.item = item
@@ -372,6 +351,9 @@ class ResourceImportFile < ApplicationRecord
372
351
  end
373
352
  if manifestation
374
353
  fetch(row, edit_mode: 'update')
354
+ ResourceImportFile.import_manifestation_custom_value(row, manifestation).each do |value|
355
+ value.update!(manifestation: manifestation)
356
+ end
375
357
  import_result.manifestation = manifestation
376
358
  end
377
359
  end
@@ -397,8 +379,11 @@ class ResourceImportFile < ApplicationRecord
397
379
 
398
380
  rows.each do |row|
399
381
  row_num += 1
400
- item_identifier = row['item_identifier'].to_s.strip
401
- item = Item.find_by(item_identifier: item_identifier)
382
+ item = Item.find_by(item_identifier: row['item_identifier'].to_s.strip) if row['item_identifier'].to_s.strip.present?
383
+ unless item
384
+ item = Item.find_by(id: row['item_id'].to_s.strip) if row['item_id'].to_s.strip.present?
385
+ end
386
+
402
387
  if item
403
388
  item.destroy if item.removable?
404
389
  end
@@ -422,10 +407,9 @@ class ResourceImportFile < ApplicationRecord
422
407
  row_num = 1
423
408
 
424
409
  rows.each do |row|
425
- item_identifier = row['item_identifier'].to_s.strip
426
- item = Item.find_by(item_identifier: item_identifier)
410
+ item = Item.find_by(item_identifier: row['item_identifier'].to_s.strip) if row['item_identifier'].to_s.strip.present?
427
411
  unless item
428
- item = Item.find_by(id: row['item_id'].to_s.strip)
412
+ item = Item.find_by(id: row['item_id'].to_s.strip) if row['item_id'].to_s.strip.present?
429
413
  end
430
414
 
431
415
  manifestation_identifier = row['manifestation_identifier'].to_s.strip
@@ -482,6 +466,9 @@ class ResourceImportFile < ApplicationRecord
482
466
  use_restriction include_supplements item_note item_url
483
467
  dummy
484
468
  )
469
+ header_columns += ManifestationCustomProperty.order(:position).pluck(:name).map{|c| "manifestation:#{c}"}
470
+ header_columns += ItemCustomProperty.order(:position).pluck(:name).map{|c| "item:#{c}"}
471
+
485
472
  if defined?(EnjuSubject)
486
473
  header_columns += ClassificationType.order(:position).pluck(:name).map{|c| "classification:#{c}"}
487
474
  header_columns += SubjectHeadingType.order(:position).pluck(:name).map{|s| "subject:#{s}"}
@@ -578,12 +565,68 @@ class ResourceImportFile < ApplicationRecord
578
565
  item
579
566
  end
580
567
 
568
+ def update_item(item, row)
569
+ shelf = Shelf.find_by(name: row['shelf'].to_s.strip)
570
+ bookstore = Bookstore.find_by(name: row['bookstore'])
571
+ required_role = Role.find_by(name: row['required_role'])
572
+
573
+ item.shelf = shelf if shelf
574
+ item.bookstore = bookstore if bookstore
575
+ item.required_role = required_role if required_role
576
+
577
+ acquired_at = Time.zone.parse(row['acquired_at']) rescue nil
578
+ binded_at = Time.zone.parse(row['binded_at']) rescue nil
579
+ item.acquired_at = acquired_at if acquired_at
580
+ item.binded_at = binded_at if binded_at
581
+
582
+ if defined?(EnjuCirculation)
583
+ circulation_status = CirculationStatus.find_by(name: row['circulation_status'])
584
+ checkout_type = CheckoutType.find_by(name: row['checkout_type'])
585
+ use_restriction = UseRestriction.find_by(name: row['use_restriction'].to_s.strip)
586
+ item.circulation_status = circulation_status if circulation_status
587
+ item.checkout_type = checkout_type if checkout_type
588
+ item.use_restriction = use_restriction if use_restriction
589
+ end
590
+
591
+ item_columns = %w(
592
+ call_number
593
+ binding_item_identifier binding_call_number binded_at
594
+ )
595
+ item_columns.each do |column|
596
+ if row[column].present?
597
+ item.assign_attributes(:"#{column}" => row[column])
598
+ end
599
+ end
600
+
601
+ item.price = row['item_price'] if row['item_price'].present?
602
+ item.note = row['item_note'].try(:gsub, /\\n/, "\n") if row['item_note'].present?
603
+ item.url = row['item_url'] if row['item_url'].present?
604
+
605
+ if row['include_supplements']
606
+ if %w(t true).include?(row['include_supplements'].downcase.strip)
607
+ item.include_supplements = true
608
+ else
609
+ item.include_supplements = false if item.include_supplements
610
+ end
611
+ end
612
+
613
+ item
614
+ end
615
+
581
616
  def fetch(row, options = {edit_mode: 'create'})
582
- case options[:edit_mode]
583
- when 'create'
584
- manifestation = nil
585
- when 'update'
586
- manifestation = Item.find_by(item_identifier: row['item_identifier'].to_s.strip).try(:manifestation)
617
+ manifestation = nil
618
+ item = nil
619
+
620
+ if options[:edit_mode] == 'update'
621
+ if row['item_identifier'].to_s.strip.present?
622
+ item = Item.find_by(item_identifier: row['item_identifier'].to_s.strip)
623
+ end
624
+ if row['item_id'].to_s.strip.present?
625
+ item = Item.find_by(id: row['item_id'].to_s.strip)
626
+ end
627
+
628
+ manifestation = item.manifestation if item
629
+
587
630
  unless manifestation
588
631
  manifestation_identifier = row['manifestation_identifier'].to_s.strip
589
632
  manifestation = Manifestation.find_by(manifestation_identifier: manifestation_identifier) if manifestation_identifier
@@ -608,13 +651,13 @@ class ResourceImportFile < ApplicationRecord
608
651
  end
609
652
 
610
653
  # TODO: 小数点以下の表現
611
- language = Language.where(name: row['language'].to_s.strip.camelize).first
612
- language = Language.where(iso_639_2: row['language'].to_s.strip.downcase).first unless language
613
- language = Language.where(iso_639_1: row['language'].to_s.strip.downcase).first unless language
654
+ language = Language.find_by(name: row['language'].to_s.strip.camelize)
655
+ language = Language.find_by(iso_639_2: row['language'].to_s.strip.downcase) unless language
656
+ language = Language.find_by(iso_639_1: row['language'].to_s.strip.downcase) unless language
614
657
 
615
- carrier_type = CarrierType.where(name: row['carrier_type'].to_s.strip).first
616
- content_type = ContentType.where(name: row['content_type'].to_s.strip).first
617
- frequency = Frequency.where(name: row['frequency'].to_s.strip).first
658
+ carrier_type = CarrierType.find_by(name: row['carrier_type'].to_s.strip)
659
+ content_type = ContentType.find_by(name: row['content_type'].to_s.strip)
660
+ frequency = Frequency.find_by(name: row['frequency'].to_s.strip)
618
661
 
619
662
  fulltext_content = serial = nil
620
663
  if %w(t true).include?(row['fulltext_content'].to_s.downcase.strip)
@@ -764,6 +807,7 @@ class ResourceImportFile < ApplicationRecord
764
807
  manifestation.set_agent_role_type(publishers_list, scope: :publisher)
765
808
  end
766
809
  end
810
+
767
811
  manifestation
768
812
  end
769
813
 
@@ -790,6 +834,52 @@ class ResourceImportFile < ApplicationRecord
790
834
  end
791
835
  identifiers
792
836
  end
837
+
838
+ def self.import_manifestation_custom_value(row, manifestation)
839
+ values = []
840
+ ManifestationCustomProperty.order(:position).pluck(:name).map{|c| "manifestation:#{c}"}.each do |column_name|
841
+ value = nil
842
+ property = column_name.split(':').last
843
+ next if row[column_name].blank?
844
+ if manifestation
845
+ value = manifestation.manifestation_custom_values.find_by(manifestation_custom_property: property)
846
+ end
847
+
848
+ if value
849
+ value.value = row[column_name]
850
+ else
851
+ value = ManifestationCustomValue.new(
852
+ manifestation_custom_property: ManifestationCustomProperty.find_by(name: property),
853
+ value: row[column_name]
854
+ )
855
+ end
856
+ values << value
857
+ end
858
+ values
859
+ end
860
+
861
+ def self.import_item_custom_value(row, item)
862
+ values = []
863
+ ItemCustomProperty.order(:position).pluck(:name).map{|c| "item:#{c}"}.each do |column_name|
864
+ value = nil
865
+ property = column_name.split(':').last
866
+ next if row[column_name].blank?
867
+ if item
868
+ value = item.item_custom_values.find_by(item_custom_property: property)
869
+ end
870
+
871
+ if value
872
+ value.value = row[column_name]
873
+ else
874
+ value = ItemCustomValue.new(
875
+ item_custom_property: ItemCustomProperty.find_by(name: property),
876
+ value: row[column_name]
877
+ )
878
+ end
879
+ values << value
880
+ end
881
+ values
882
+ end
793
883
  end
794
884
 
795
885
  # == Schema Information