enju_biblio 0.3.16 → 0.3.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/app/models2/agent.rb +331 -0
  3. data/app/models2/agent_import_file.rb +259 -0
  4. data/app/models2/agent_import_file_state_machine.rb +19 -0
  5. data/app/models2/agent_import_file_transition.rb +20 -0
  6. data/app/models2/agent_import_result.rb +20 -0
  7. data/app/models2/agent_merge.rb +17 -0
  8. data/app/models2/agent_merge_list.rb +27 -0
  9. data/app/models2/agent_relationship.rb +24 -0
  10. data/app/models2/agent_relationship_type.rb +20 -0
  11. data/app/models2/agent_type.rb +19 -0
  12. data/app/models2/carrier_type.rb +50 -0
  13. data/app/models2/content_type.rb +19 -0
  14. data/app/models2/country.rb +47 -0
  15. data/app/models2/create.rb +29 -0
  16. data/app/models2/create_type.rb +19 -0
  17. data/app/models2/doi_record.rb +36 -0
  18. data/app/models2/donate.rb +15 -0
  19. data/app/models2/form_of_work.rb +19 -0
  20. data/app/models2/frequency.rb +19 -0
  21. data/app/models2/identifier.rb +83 -0
  22. data/app/models2/identifier_type.rb +18 -0
  23. data/app/models2/import_request.rb +77 -0
  24. data/app/models2/import_request_state_machine.rb +9 -0
  25. data/app/models2/import_request_transition.rb +21 -0
  26. data/app/models2/isbn_record.rb +51 -0
  27. data/app/models2/isbn_record_and_manifestation.rb +18 -0
  28. data/app/models2/issn_record.rb +49 -0
  29. data/app/models2/issn_record_and_manifestation.rb +18 -0
  30. data/app/models2/item.rb +173 -0
  31. data/app/models2/item_custom_property.rb +18 -0
  32. data/app/models2/item_custom_value.rb +17 -0
  33. data/app/models2/language.rb +39 -0
  34. data/app/models2/license.rb +18 -0
  35. data/app/models2/manifestation.rb +764 -0
  36. data/app/models2/manifestation_custom_property.rb +18 -0
  37. data/app/models2/manifestation_custom_value.rb +17 -0
  38. data/app/models2/manifestation_relationship.rb +27 -0
  39. data/app/models2/manifestation_relationship_type.rb +20 -0
  40. data/app/models2/medium_of_performance.rb +19 -0
  41. data/app/models2/own.rb +29 -0
  42. data/app/models2/periodical.rb +33 -0
  43. data/app/models2/periodical_and_manifestation.rb +16 -0
  44. data/app/models2/picture_file.rb +60 -0
  45. data/app/models2/produce.rb +30 -0
  46. data/app/models2/produce_type.rb +19 -0
  47. data/app/models2/realize.rb +29 -0
  48. data/app/models2/realize_type.rb +19 -0
  49. data/app/models2/resource_export_file.rb +64 -0
  50. data/app/models2/resource_export_file_state_machine.rb +15 -0
  51. data/app/models2/resource_export_file_transition.rb +21 -0
  52. data/app/models2/resource_import_file.rb +909 -0
  53. data/app/models2/resource_import_file_state_machine.rb +19 -0
  54. data/app/models2/resource_import_file_transition.rb +21 -0
  55. data/app/models2/resource_import_result.rb +24 -0
  56. data/app/models2/series_statement.rb +72 -0
  57. data/app/models2/series_statement_merge.rb +17 -0
  58. data/app/models2/series_statement_merge_list.rb +17 -0
  59. data/app/views/manifestations/_book_jacket.html.erb +9 -5
  60. data/app/views/manifestations/_colorbox.html.erb +1 -1
  61. data/app/views/manifestations/_pickup.html.erb +1 -1
  62. data/lib/enju_biblio/version.rb +1 -1
  63. data/spec/dummy/yarn.lock +7560 -0
  64. metadata +61 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 14cf69a7672b13c89901dff244c8bdee230a07559357dff1f201e9c3a1357e65
4
- data.tar.gz: a65e04a7a889cc94e6c384443a56949494540c94efdcf79d1bb81a6fd212862e
3
+ metadata.gz: 3a07786c452c6bdb16fed38094a30d3f6c2b5c567973f55745e8ce50abb29670
4
+ data.tar.gz: b7b9f2fafcc17bdc5fc5aec3ea2ca55bc7548591a8997fd7251d0b2c56834549
5
5
  SHA512:
6
- metadata.gz: 4c7432c6014d1fe4943d137598c16c83048252d2d65eda23fe235d222bff97e00886f407ba1b6d003d287ef3e706df5b56f360d7e5f11560a134bafdb02b5828
7
- data.tar.gz: 0aa1a8a22c15e84486894ca72a97eef72eb5fea69572cc81cb825cf5e51e255c79515d89b4b952e56c65b20644c99eb2b970d0f95db8458c53a946a1b24c0fab
6
+ metadata.gz: b17efe33b6a93312ade16a8aa6e38c153a87b31fa6ab7506e04189cb47964bd4da70b430e64e1131748385c313201c76b0d1888f0a8dff9b12b1fe3eaeed4b75
7
+ data.tar.gz: 452be3f6d713a630534997eea4a2f73b3761177504011d7ac7674ce14bae7c83205f508a5d4c713b4bd7b40435902060154ae0be47486287468280ec1a105007
@@ -0,0 +1,331 @@
1
+ class Agent < ApplicationRecord
2
+ scope :readable_by, lambda{ |user|
3
+ if user
4
+ where('required_role_id <= ?', user.try(:user_has_role).try(:role_id))
5
+ else
6
+ where('required_role_id <= 1')
7
+ end
8
+ }
9
+ has_many :creates, dependent: :destroy, inverse_of: :agent
10
+ has_many :works, through: :creates
11
+ has_many :realizes, dependent: :destroy, inverse_of: :agent
12
+ has_many :expressions, through: :realizes
13
+ has_many :produces, dependent: :destroy, inverse_of: :agen, inverse_of: :agentt
14
+ has_many :manifestations, through: :produces
15
+ has_many :children, foreign_key: 'parent_id', class_name: 'AgentRelationship', dependent: :destroy
16
+ has_many :parents, foreign_key: 'child_id', class_name: 'AgentRelationship', dependent: :destroy
17
+ has_many :derived_agents, through: :children, source: :child
18
+ has_many :original_agents, through: :parents, source: :parent
19
+ has_many :picture_files, as: :picture_attachable, dependent: :destroy
20
+ has_many :donates, dependent: :destroy
21
+ has_many :donated_items, through: :donates, source: :item
22
+ has_many :owns, dependent: :destroy, inverse_of: :agent
23
+ has_many :items, through: :owns
24
+ has_many :agent_merges, dependent: :destroy
25
+ has_many :agent_merge_lists, through: :agent_merges
26
+ belongs_to :agent_type
27
+ belongs_to :required_role, class_name: 'Role', foreign_key: 'required_role_id'
28
+ belongs_to :language
29
+ belongs_to :country
30
+ has_one :agent_import_result
31
+ belongs_to :profile, optional: true
32
+
33
+ validates :full_name, presence: true, length: { maximum: 255 }
34
+ validates :birth_date, format: { with: /\A\d+(-\d{0,2}){0,2}\z/ }, allow_blank: true
35
+ validates :death_date, format: { with: /\A\d+(-\d{0,2}){0,2}\z/ }, allow_blank: true
36
+ validates :email, format: { with: /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i }, allow_blank: true
37
+ validate :check_birth_date
38
+ before_validation :set_role_and_name, on: :create
39
+ before_save :set_date_of_birth, :set_date_of_death
40
+ after_save do |agent|
41
+ agent.works.map{|work| work.touch; work.index}
42
+ agent.expressions.map{|expression| expression.touch; expression.index}
43
+ agent.manifestations.map{|manifestation| manifestation.touch; manifestation.index}
44
+ agent.items.map{|item| item.touch; item.index}
45
+ Sunspot.commit
46
+ end
47
+ after_destroy do |agent|
48
+ agent.works.map{|work| work.touch; work.index}
49
+ agent.expressions.map{|expression| expression.touch; expression.index}
50
+ agent.manifestations.map{|manifestation| manifestation.touch; manifestation.index}
51
+ agent.items.map{|item| item.touch; item.index}
52
+ Sunspot.commit
53
+ end
54
+
55
+ attr_accessor :agent_id
56
+
57
+ searchable do
58
+ text :name, :place, :address_1, :address_2, :other_designation, :note
59
+ string :zip_code_1
60
+ string :zip_code_2
61
+ time :created_at
62
+ time :updated_at
63
+ time :date_of_birth
64
+ time :date_of_death
65
+ integer :work_ids, multiple: true
66
+ integer :expression_ids, multiple: true
67
+ integer :manifestation_ids, multiple: true
68
+ integer :agent_merge_list_ids, multiple: true
69
+ integer :original_agent_ids, multiple: true
70
+ integer :required_role_id
71
+ integer :agent_type_id
72
+ end
73
+
74
+ paginates_per 10
75
+
76
+ def set_role_and_name
77
+ self.required_role = Role.find_by(name: 'Librarian') if required_role_id.nil?
78
+ set_full_name
79
+ end
80
+
81
+ def set_full_name
82
+ if full_name.blank?
83
+ if LibraryGroup.site_config.family_name_first
84
+ self.full_name = [last_name, middle_name, first_name].compact.join(" ").to_s.strip
85
+ else
86
+ self.full_name = [first_name, last_name, middle_name].compact.join(" ").to_s.strip
87
+ end
88
+ end
89
+ if full_name_transcription.blank?
90
+ self.full_name_transcription = [last_name_transcription, middle_name_transcription, first_name_transcription].join(" ").to_s.strip
91
+ end
92
+ [full_name, full_name_transcription]
93
+ end
94
+
95
+ def set_date_of_birth
96
+ return if birth_date.blank?
97
+ begin
98
+ date = Time.zone.parse(birth_date.to_s)
99
+ rescue ArgumentError
100
+ begin
101
+ date = Time.zone.parse("#{birth_date}-01")
102
+ rescue ArgumentError
103
+ begin
104
+ date = Time.zone.parse("#{birth_date}-01-01")
105
+ rescue
106
+ nil
107
+ end
108
+ end
109
+ end
110
+ self.date_of_birth = date
111
+ end
112
+
113
+ def set_date_of_death
114
+ return if death_date.blank?
115
+ begin
116
+ date = Time.zone.parse(death_date.to_s)
117
+ rescue ArgumentError
118
+ begin
119
+ date = Time.zone.parse("#{death_date}-01")
120
+ rescue ArgumentError
121
+ begin
122
+ date = Time.zone.parse("#{death_date}-01-01")
123
+ rescue
124
+ nil
125
+ end
126
+ end
127
+ end
128
+
129
+ self.date_of_death = date
130
+ end
131
+
132
+ def check_birth_date
133
+ if date_of_birth.present? && date_of_death.present?
134
+ if date_of_birth > date_of_death
135
+ errors.add(:birth_date)
136
+ errors.add(:death_date)
137
+ end
138
+ end
139
+ end
140
+
141
+ #def full_name_generate
142
+ # # TODO: 日本人以外は?
143
+ # name = []
144
+ # name << self.last_name.to_s.strip
145
+ # name << self.middle_name.to_s.strip unless self.middle_name.blank?
146
+ # name << self.first_name.to_s.strip
147
+ # name << self.corporate_name.to_s.strip
148
+ # name.join(" ").strip
149
+ #end
150
+
151
+ def full_name_without_space
152
+ full_name.gsub(/[\s,]/, "")
153
+ # # TODO: 日本人以外は?
154
+ # name = []
155
+ # name << self.last_name.to_s.strip
156
+ # name << self.middle_name.to_s.strip
157
+ # name << self.first_name.to_s.strip
158
+ # name << self.corporate_name.to_s.strip
159
+ # name.join("").strip
160
+ end
161
+
162
+ def full_name_transcription_without_space
163
+ full_name_transcription.to_s.gsub(/\s/, "")
164
+ end
165
+
166
+ def full_name_alternative_without_space
167
+ full_name_alternative.to_s.gsub(/\s/, "")
168
+ end
169
+
170
+ def name
171
+ name = []
172
+ name << full_name.to_s.strip
173
+ name << full_name_transcription.to_s.strip
174
+ name << full_name_alternative.to_s.strip
175
+ name << full_name_without_space
176
+ #name << full_name_transcription_without_space
177
+ #name << full_name_alternative_without_space
178
+ #name << full_name.wakati rescue nil
179
+ #name << full_name_transcription.wakati rescue nil
180
+ #name << full_name_alternative.wakati rescue nil
181
+ name
182
+ end
183
+
184
+ def date
185
+ if date_of_birth
186
+ if date_of_death
187
+ "#{date_of_birth} - #{date_of_death}"
188
+ else
189
+ "#{date_of_birth} -"
190
+ end
191
+ end
192
+ end
193
+
194
+ def creator?(resource)
195
+ resource.creators.include?(self)
196
+ end
197
+
198
+ def publisher?(resource)
199
+ resource.publishers.include?(self)
200
+ end
201
+
202
+ def created(work)
203
+ creates.find_by(work_id: work.id)
204
+ end
205
+
206
+ def realized(expression)
207
+ realizes.find_by(expression_id: expression.id)
208
+ end
209
+
210
+ def produced(manifestation)
211
+ produces.find_by(manifestation_id: manifestation.id)
212
+ end
213
+
214
+ def owned(item)
215
+ owns.where(item_id: item.id)
216
+ end
217
+
218
+ def self.import_agents(agent_lists)
219
+ agents = []
220
+ agent_lists.each do |agent_list|
221
+ name_and_role = agent_list[:full_name].split('||')
222
+ if agent_list[:agent_identifier].present?
223
+ agent = Agent.find_by(agent_identifier: agent_list[:agent_identifier])
224
+ else
225
+ agents_matched = Agent.where(full_name: name_and_role[0])
226
+ agents_matched = agents_matched.where(place: agent_list[:place]) if agent_list[:place]
227
+ agent = agents_matched.first
228
+ end
229
+ role_type = name_and_role[1].to_s.strip
230
+ unless agent
231
+ agent = Agent.new(
232
+ full_name: name_and_role[0],
233
+ full_name_transcription: agent_list[:full_name_transcription],
234
+ agent_identifier: agent_list[:agent_identifier],
235
+ place: agent_list[:place],
236
+ language_id: 1,
237
+ )
238
+ agent.required_role = Role.find_by(name: 'Guest')
239
+ agent.save
240
+ end
241
+ agents << agent
242
+ end
243
+ agents.uniq!
244
+ agents
245
+ end
246
+
247
+ def agents
248
+ self.original_agents + self.derived_agents
249
+ end
250
+
251
+ def self.new_agents(agents_params)
252
+ return [] unless agents_params
253
+ agents = []
254
+ Agent.transaction do
255
+ agents_params.each do |k, v|
256
+ next if v['_destroy'] == '1'
257
+
258
+ agent = nil
259
+
260
+ if v['agent_id'].present?
261
+ agent = Agent.find(v['agent_id'])
262
+ elsif v['id'].present?
263
+ agent = Agent.find(v['id'])
264
+ end
265
+
266
+ if !agent or agent.full_name != v['full_name']
267
+ v.delete('id')
268
+ v.delete('agent_id')
269
+ v.delete('_destroy')
270
+ agent = Agent.create(v)
271
+ end
272
+
273
+ agents << agent
274
+ end
275
+ end
276
+
277
+ agents
278
+ end
279
+ end
280
+
281
+ # == Schema Information
282
+ #
283
+ # Table name: agents
284
+ #
285
+ # id :integer not null, primary key
286
+ # last_name :string
287
+ # middle_name :string
288
+ # first_name :string
289
+ # last_name_transcription :string
290
+ # middle_name_transcription :string
291
+ # first_name_transcription :string
292
+ # corporate_name :string
293
+ # corporate_name_transcription :string
294
+ # full_name :string
295
+ # full_name_transcription :text
296
+ # full_name_alternative :text
297
+ # created_at :datetime
298
+ # updated_at :datetime
299
+ # zip_code_1 :string
300
+ # zip_code_2 :string
301
+ # address_1 :text
302
+ # address_2 :text
303
+ # address_1_note :text
304
+ # address_2_note :text
305
+ # telephone_number_1 :string
306
+ # telephone_number_2 :string
307
+ # fax_number_1 :string
308
+ # fax_number_2 :string
309
+ # other_designation :text
310
+ # place :text
311
+ # postal_code :string
312
+ # street :text
313
+ # locality :text
314
+ # region :text
315
+ # date_of_birth :datetime
316
+ # date_of_death :datetime
317
+ # language_id :integer default(1), not null
318
+ # country_id :integer default(1), not null
319
+ # agent_type_id :integer default(1), not null
320
+ # lock_version :integer default(0), not null
321
+ # note :text
322
+ # required_role_id :integer default(1), not null
323
+ # required_score :integer default(0), not null
324
+ # email :text
325
+ # url :text
326
+ # full_name_alternative_transcription :text
327
+ # birth_date :string
328
+ # death_date :string
329
+ # agent_identifier :string
330
+ # profile_id :integer
331
+ #
@@ -0,0 +1,259 @@
1
+ class AgentImportFile < ApplicationRecord
2
+ include Statesman::Adapters::ActiveRecordQueries[
3
+ transition_class: AgentImportFileTransition,
4
+ initial_state: :pending
5
+ ]
6
+ include ImportFile
7
+ default_scope { order('agent_import_files.id DESC') }
8
+ scope :not_imported, -> { in_state(:pending) }
9
+ scope :stucked, -> { in_state(:pending).where('agent_import_files.created_at < ?', 1.hour.ago) }
10
+
11
+ if ENV['ENJU_STORAGE'] == 's3'
12
+ has_attached_file :agent_import, storage: :s3,
13
+ s3_credentials: {
14
+ access_key: ENV['AWS_ACCESS_KEY_ID'],
15
+ secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
16
+ bucket: ENV['S3_BUCKET_NAME'],
17
+ s3_host_name: ENV['S3_HOST_NAME'],
18
+ s3_region: ENV['S3_REGION']
19
+ },
20
+ s3_permissions: :private
21
+ else
22
+ has_attached_file :agent_import,
23
+ path: ":rails_root/private/system/:class/:attachment/:id_partition/:style/:filename"
24
+ end
25
+ validates_attachment_content_type :agent_import, content_type: [
26
+ 'text/csv',
27
+ 'text/plain',
28
+ 'text/tab-separated-values',
29
+ 'application/octet-stream',
30
+ 'application/vnd.ms-excel'
31
+ ]
32
+ validates_attachment_presence :agent_import
33
+ belongs_to :user
34
+ has_many :agent_import_results, dependent: :destroy
35
+
36
+ has_many :agent_import_file_transitions, autosave: false, dependent: :destroy
37
+
38
+ attr_accessor :mode
39
+
40
+ def state_machine
41
+ AgentImportFileStateMachine.new(self, transition_class: AgentImportFileTransition)
42
+ end
43
+
44
+ delegate :can_transition_to?, :transition_to!, :transition_to, :current_state,
45
+ to: :state_machine
46
+
47
+ def import_start
48
+ case edit_mode
49
+ when 'create'
50
+ import
51
+ when 'update'
52
+ modify
53
+ when 'destroy'
54
+ remove
55
+ else
56
+ import
57
+ end
58
+ end
59
+
60
+ def import
61
+ transition_to!(:started)
62
+ num = { agent_imported: 0, user_imported: 0, failed: 0 }
63
+ rows = open_import_file
64
+ field = rows.first
65
+ row_num = 1
66
+ if [field['first_name'], field['last_name'], field['full_name']].reject{|field| field.to_s.strip == ""}.empty?
67
+ raise "You should specify first_name, last_name or full_name in the first line"
68
+ end
69
+ #rows.shift
70
+
71
+ AgentImportResult.create!(agent_import_file_id: id, body: rows.headers.join("\t"))
72
+ rows.each do |row|
73
+ row_num += 1
74
+ import_result = AgentImportResult.create!(agent_import_file_id: id, body: row.fields.join("\t"))
75
+ next if row['dummy'].to_s.strip.present?
76
+
77
+ agent = Agent.new
78
+ agent = set_agent_value(agent, row)
79
+
80
+ if agent.save!
81
+ import_result.agent = agent
82
+ num[:agent_imported] += 1
83
+ if row_num % 50 == 0
84
+ Sunspot.commit
85
+ GC.start
86
+ end
87
+ end
88
+
89
+ import_result.save!
90
+ end
91
+ Sunspot.commit
92
+ rows.close
93
+ transition_to!(:completed)
94
+ mailer = AgentImportMailer.completed(self)
95
+ send_message(mailer)
96
+ return num
97
+ rescue => e
98
+ self.error_message = "line #{row_num}: #{e.message}"
99
+ transition_to!(:failed)
100
+ mailer = AgentImportMailer.failed(self)
101
+ send_message(mailer)
102
+ raise e
103
+ end
104
+
105
+ def self.import
106
+ AgentImportFile.not_imported.each do |file|
107
+ file.import_start
108
+ end
109
+ rescue
110
+ Rails.logger.info "#{Time.zone.now} importing agents failed!"
111
+ end
112
+
113
+ def modify
114
+ transition_to!(:started)
115
+ rows = open_import_file
116
+ rows.shift
117
+ row_num = 1
118
+
119
+ rows.each do |row|
120
+ row_num += 1
121
+ next if row['dummy'].to_s.strip.present?
122
+ agent = Agent.find_by(id: row['id'])
123
+ if agent
124
+ agent.full_name = row['full_name'] if row['full_name'].to_s.strip.present?
125
+ agent.full_name_transcription = row['full_name_transcription'] if row['full_name_transcription'].to_s.strip.present?
126
+ agent.first_name = row['first_name'] if row['first_name'].to_s.strip.present?
127
+ agent.first_name_transcription = row['first_name_transcription'] if row['first_name_transcription'].to_s.strip.present?
128
+ agent.middle_name = row['middle_name'] if row['middle_name'].to_s.strip.present?
129
+ agent.middle_name_transcription = row['middle_name_transcription'] if row['middle_name_transcription'].to_s.strip.present?
130
+ agent.last_name = row['last_name'] if row['last_name'].to_s.strip.present?
131
+ agent.last_name_transcription = row['last_name_transcription'] if row['last_name_transcription'].to_s.strip.present?
132
+ agent.address_1 = row['address_1'] if row['address_1'].to_s.strip.present?
133
+ agent.address_2 = row['address_2'] if row['address_2'].to_s.strip.present?
134
+ agent.save!
135
+ end
136
+ end
137
+ transition_to!(:completed)
138
+ mailer = AgentImportMailer.completed(self)
139
+ send_message(mailer)
140
+ rescue => e
141
+ self.error_message = "line #{row_num}: #{e.message}"
142
+ transition_to!(:failed)
143
+ mailer = AgentImportMailer.failed(self)
144
+ send_message(mailer)
145
+ raise e
146
+ end
147
+
148
+ def remove
149
+ transition_to!(:started)
150
+ rows = open_import_file
151
+ rows.shift
152
+ row_num = 1
153
+
154
+ rows.each do |row|
155
+ row_num += 1
156
+ next if row['dummy'].to_s.strip.present?
157
+ agent = Agent.find_by(id: row['id'].to_s.strip)
158
+ if agent
159
+ agent.picture_files.destroy_all
160
+ agent.reload
161
+ agent.destroy
162
+ end
163
+ end
164
+ transition_to!(:completed)
165
+ mailer = AgentImportMailer.completed(self)
166
+ send_message(mailer)
167
+ rescue => e
168
+ self.error_message = "line #{row_num}: #{e.message}"
169
+ transition_to!(:failed)
170
+ mailer = AgentImportMailer.failed(self)
171
+ send_message(mailer)
172
+ raise e
173
+ end
174
+
175
+ private
176
+
177
+ def open_import_file
178
+ tempfile = Tempfile.new(self.class.name.underscore)
179
+ if ENV['ENJU_STORAGE'] == 's3'
180
+ uploaded_file_path = agent_import.expiring_url(10)
181
+ else
182
+ uploaded_file_path = agent_import.path
183
+ end
184
+ open(uploaded_file_path){|f|
185
+ f.each{|line|
186
+ tempfile.puts(convert_encoding(line))
187
+ }
188
+ }
189
+ tempfile.close
190
+
191
+ file = CSV.open(tempfile, col_sep: "\t")
192
+ header = file.first
193
+ rows = CSV.open(tempfile, headers: header, col_sep: "\t")
194
+ tempfile.close(true)
195
+ file.close
196
+ rows
197
+ end
198
+
199
+ def set_agent_value(agent, row)
200
+ agent.first_name = row['first_name'] if row['first_name']
201
+ agent.middle_name = row['middle_name'] if row['middle_name']
202
+ agent.last_name = row['last_name'] if row['last_name']
203
+ agent.first_name_transcription = row['first_name_transcription'] if row['first_name_transcription']
204
+ agent.middle_name_transcription = row['middle_name_transcription'] if row['middle_name_transcription']
205
+ agent.last_name_transcription = row['last_name_transcription'] if row['last_name_transcription']
206
+
207
+ agent.full_name = row['full_name'] if row['full_name']
208
+ agent.full_name_transcription = row['full_name_transcription'] if row['full_name_transcription']
209
+
210
+ agent.address_1 = row['address_1'] if row['address_1']
211
+ agent.address_2 = row['address_2'] if row['address_2']
212
+ agent.zip_code_1 = row['zip_code_1'] if row['zip_code_1']
213
+ agent.zip_code_2 = row['zip_code_2'] if row['zip_code_2']
214
+ agent.telephone_number_1 = row['telephone_number_1'] if row['telephone_number_1']
215
+ agent.telephone_number_2 = row['telephone_number_2'] if row['telephone_number_2']
216
+ agent.fax_number_1 = row['fax_number_1'] if row['fax_number_1']
217
+ agent.fax_number_2 = row['fax_number_2'] if row['fax_number_2']
218
+ agent.note = row['note'] if row['note']
219
+ agent.birth_date = row['birth_date'] if row['birth_date']
220
+ agent.death_date = row['death_date'] if row['death_date']
221
+
222
+ #if row['username'].to_s.strip.blank?
223
+ agent.email = row['email'].to_s.strip
224
+ agent.required_role = Role.find_by(name: row['required_role'].to_s.strip.camelize) || Role.find_by(name: 'Guest')
225
+ #else
226
+ # agent.required_role = Role.where(name: row['required_role'].to_s.strip.camelize).first || Role.where('Librarian').first
227
+ #end
228
+ language = Language.find_by(name: row['language'].to_s.strip.camelize)
229
+ language = Language.find_by(iso_639_2: row['language'].to_s.strip.downcase) unless language
230
+ language = Language.find_by(iso_639_1: row['language'].to_s.strip.downcase) unless language
231
+ agent.language = language if language
232
+ country = Country.find_by(name: row['country'].to_s.strip)
233
+ agent.country = country if country
234
+ agent
235
+ end
236
+ end
237
+
238
+ # == Schema Information
239
+ #
240
+ # Table name: agent_import_files
241
+ #
242
+ # id :integer not null, primary key
243
+ # parent_id :integer
244
+ # content_type :string
245
+ # size :integer
246
+ # user_id :integer
247
+ # note :text
248
+ # executed_at :datetime
249
+ # agent_import_file_name :string
250
+ # agent_import_content_type :string
251
+ # agent_import_file_size :integer
252
+ # agent_import_updated_at :datetime
253
+ # created_at :datetime
254
+ # updated_at :datetime
255
+ # agent_import_fingerprint :string
256
+ # error_message :text
257
+ # edit_mode :string
258
+ # user_encoding :string
259
+ #
@@ -0,0 +1,19 @@
1
+ class AgentImportFileStateMachine
2
+ include Statesman::Machine
3
+
4
+ state :pending, initial: true
5
+ state :started
6
+ state :completed
7
+ state :failed
8
+
9
+ transition from: :pending, to: [:started, :failed]
10
+ transition from: :started, to: [:completed, :failed]
11
+
12
+ after_transition(from: :pending, to: :started) do |agent_import_file|
13
+ agent_import_file.update_column(:executed_at, Time.zone.now)
14
+ end
15
+
16
+ before_transition(from: :started, to: :completed) do |agent_import_file|
17
+ agent_import_file.error_message = nil
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ class AgentImportFileTransition < ApplicationRecord
2
+ include Statesman::Adapters::ActiveRecordTransition
3
+
4
+
5
+ belongs_to :agent_import_file, inverse_of: :agent_import_file_transitions
6
+ end
7
+
8
+ # == Schema Information
9
+ #
10
+ # Table name: agent_import_file_transitions
11
+ #
12
+ # id :integer not null, primary key
13
+ # to_state :string
14
+ # metadata :text default({})
15
+ # sort_key :integer
16
+ # agent_import_file_id :integer
17
+ # created_at :datetime
18
+ # updated_at :datetime
19
+ # most_recent :boolean not null
20
+ #
@@ -0,0 +1,20 @@
1
+ class AgentImportResult < ApplicationRecord
2
+ default_scope { order('agent_import_results.id') }
3
+ scope :file_id, proc{|file_id| where(agent_import_file_id: file_id)}
4
+ scope :failed, -> { where(agent_id: nil) }
5
+
6
+ belongs_to :agent_import_file
7
+ belongs_to :agent, optional: true
8
+ end
9
+
10
+ # == Schema Information
11
+ #
12
+ # Table name: agent_import_results
13
+ #
14
+ # id :integer not null, primary key
15
+ # agent_import_file_id :integer
16
+ # agent_id :integer
17
+ # body :text
18
+ # created_at :datetime
19
+ # updated_at :datetime
20
+ #
@@ -0,0 +1,17 @@
1
+ class AgentMerge < ApplicationRecord
2
+ belongs_to :agent
3
+ belongs_to :agent_merge_list
4
+
5
+ paginates_per 10
6
+ end
7
+
8
+ # == Schema Information
9
+ #
10
+ # Table name: agent_merges
11
+ #
12
+ # id :integer not null, primary key
13
+ # agent_id :integer not null
14
+ # agent_merge_list_id :integer not null
15
+ # created_at :datetime
16
+ # updated_at :datetime
17
+ #