Class: ResourceImportFile
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ResourceImportFile
- Includes:
- ImportFile, Statesman::Adapters::ActiveRecordQueries
- Defined in:
- app/models/resource_import_file.rb
Instance Attribute Summary collapse
-
#library_id ⇒ Object
Returns the value of attribute library_id.
-
#mode ⇒ Object
Returns the value of attribute mode.
Class Method Summary collapse
- .import ⇒ Object
- .import_expression(work, agents, options = {edit_mode: 'create'}) ⇒ Object
- .import_manifestation(expression, agents, options = {}, edit_options = {edit_mode: 'create'}) ⇒ Object
- .import_work(title, agents, options = {edit_mode: 'create'}) ⇒ Object
Instance Method Summary collapse
- #import ⇒ Object
- #import_marc(marc_type) ⇒ Object
- #import_start ⇒ Object
-
#modify ⇒ Object
def import_jpmarc marc = NKF::nkf(‘-wc’, self.db_file.data) marc.split(“rn”).each do |record| end end.
- #remove ⇒ Object
- #state_machine ⇒ Object
- #update_relationship ⇒ Object
Instance Attribute Details
#library_id ⇒ Object
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 |
#mode ⇒ Object
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
.import ⇒ Object
281 282 283 284 285 286 287 |
# File 'app/models/resource_import_file.rb', line 281 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
230 231 232 233 234 235 |
# File 'app/models/resource_import_file.rb', line 230 def self.import_expression(work, agents, = {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
237 238 239 240 241 242 243 244 245 |
# File 'app/models/resource_import_file.rb', line 237 def self.import_manifestation(expression, agents, = {}, = {edit_mode: 'create'}) manifestation = expression manifestation.during_import = true manifestation.reload manifestation.update_attributes!() manifestation.publishers = agents.uniq unless agents.empty? manifestation.reload manifestation end |
.import_work(title, agents, options = {edit_mode: 'create'}) ⇒ Object
223 224 225 226 227 228 |
# File 'app/models/resource_import_file.rb', line 223 def self.import_work(title, agents, = {edit_mode: 'create'}) work = Manifestation.new(title) work.save work.creators = agents.uniq unless agents.empty? work end |
Instance Method Details
#import ⇒ Object
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 |
# 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: 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")) if row['dummy'].to_s.strip.present? import_result. = "line #{row_num}: #{I18n.t('import.dummy')}" import_result.save! next end item_identifier = row['item_identifier'].to_s.strip item = Item.where(item_identifier: item_identifier).first if item import_result.item = item import_result.manifestation = item.manifestation import_result. = "line #{row_num}: #{I18n.t('import.item_found')}" import_result.save! num[:item_found] += 1 next end if row['manifestation_identifier'].present? manifestation = Manifestation.where(manifestation_identifier: row['manifestation_identifier'].to_s.strip).first end unless manifestation if row['manifestation_id'].present? manifestation = Manifestation.where(id: row['manifestation_id'].to_s.strip).first end end unless manifestation if row['doi'].present? doi = URI.parse(row['doi']).path.gsub(/^\//, "") identifier_type_doi = IdentifierType.where(name: 'doi').first identifier_type_doi = IdentifierType.where(name: 'doi').create! unless identifier_type_doi manifestation = Identifier.where(body: doi, identifier_type_id: identifier_type_doi.id).first.try(:manifestation) end end unless manifestation if row['jpno'].present? jpno = row['jpno'].to_s.strip identifier_type_jpno = IdentifierType.where(name: 'jpno').first identifier_type_jpno = IdentifierType.where(name: 'jpno').create! unless identifier_type_jpno manifestation = Identifier.where(body: jpno, identifier_type_id: identifier_type_jpno.id).first.try(:manifestation) end end unless manifestation if row['ncid'].present? ncid = row['ncid'].to_s.strip identifier_type_ncid = IdentifierType.where(name: 'ncid').first identifier_type_ncid = IdentifierType.where(name: 'ncid').create! unless identifier_type_ncid manifestation = Identifier.where(body: ncid, identifier_type_id: identifier_type_ncid.id).first.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.where(name: 'isbn').first identifier_type_isbn = IdentifierType.where(name: 'isbn').create! unless identifier_type_isbn m = Identifier.where(body: isbn, identifier_type_id: identifier_type_isbn.id).first.try(:manifestation) if m if m.series_statements.exists? manifestation = m end end else import_result. = "line #{row_num}: #{I18n.t('import.isbn_invalid')}" end end end if manifestation import_result. = "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. = "line #{row_num}: #{I18n.t('import.isbn_invalid')}" rescue EnjuNdl::RecordNotFound manifestation = nil import_result. = "line #{row_num}: #{I18n.t('import.isbn_record_not_found')}" end end end unless manifestation manifestation = fetch(row) num[:manifestation_imported] += 1 if manifestation end import_result.manifestation = manifestation if manifestation if item_identifier.present? or row['shelf'].present? or row['call_number'].present? import_result.item = create_item(row, manifestation) else if manifestation.fulltext_content? item = create_item(row, manifestation) item.circulation_status = CirculationStatus.where(name: 'Available On Shelf').first begin item.acquired_at = Time.zone.parse(row['acquired_at'].to_s.strip) rescue ArgumentError end end num[:failed] += 1 end else num[:failed] += 1 end import_result.save! num[:item_imported] +=1 if import_result.item if row_num % 50 == 0 Sunspot.commit GC.start end end Sunspot.commit rows.close transition_to!(:completed) Rails.cache.write("manifestation_search_total", Manifestation.search.total) num rescue => e self. = "line #{row_num}: #{e.}" save transition_to!(:failed) raise e end |
#import_marc(marc_type) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'app/models/resource_import_file.rb', line 247 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.where(full_name: record['700']['a']).first unless publisher publisher = Agent.new(full_name: full_name) publisher.save end manifestation.publishers << publisher end end |
#import_start ⇒ Object
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 |
#modify ⇒ Object
def import_jpmarc
marc = NKF::nkf('-wc', self.db_file.data)
marc.split("\r\n").each do |record|
end
end
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 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 295 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_identifier = row['item_identifier'].to_s.strip item = Item.where(item_identifier: item_identifier).first if item_identifier.present? if item if item.manifestation fetch(row, edit_mode: 'update') end shelf = Shelf.where(name: row['shelf'].to_s.strip).first circulation_status = CirculationStatus.where(name: row['circulation_status']).first checkout_type = CheckoutType.where(name: row['checkout_type']).first bookstore = Bookstore.where(name: row['bookstore']).first required_role = Role.where(name: row['required_role']).first use_restriction = UseRestriction.where(name: row['use_restriction'].to_s.strip).first item.shelf = shelf if shelf item.circulation_status = circulation_status if circulation_status item.checkout_type = checkout_type if checkout_type item.bookstore = bookstore if bookstore item.required_role = required_role if required_role item.use_restriction = use_restriction if use_restriction acquired_at = Time.zone.parse(row['acquired_at']) rescue nil binded_at = Time.zone.parse(row['binded_at']) rescue nil item.acquired_at = acquired_at if acquired_at item.binded_at = binded_at if binded_at item_columns = %w( call_number binding_item_identifier binding_call_number binded_at ) item_columns.each do |column| if row[column].present? item.assign_attributes(:"#{column}" => row[column]) end end item.price = row['item_price'] if row['item_price'].present? item.note = row['item_note'].try(:gsub, /\\n/, "\n") if row['item_note'].present? item.url = row['item_url'] if row['item_url'].present? if row['include_supplements'] if %w(t true).include?(row['include_supplements'].downcase.strip) item.include_supplements = true else item.include_supplements = false if item.include_supplements end end item.save! import_result.item = item else manifestation_identifier = row['manifestation_identifier'].to_s.strip manifestation = Manifestation.where(manifestation_identifier: manifestation_identifier).first if manifestation_identifier.present? unless manifestation manifestation = Manifestation.where(id: row['manifestation_id']).first end if manifestation fetch(row, edit_mode: 'update') import_result.manifestation = manifestation end end import_result.save! end transition_to!(:completed) rescue => e self. = "line #{row_num}: #{e.}" save transition_to!(:failed) raise e end |
#remove ⇒ Object
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# 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_identifier = row['item_identifier'].to_s.strip item = Item.where(item_identifier: item_identifier).first if item item.destroy if item.removable? end end transition_to!(:completed) rescue => e self. = "line #{row_num}: #{e.}" save transition_to!(:failed) raise e end |
#state_machine ⇒ Object
39 40 41 |
# File 'app/models/resource_import_file.rb', line 39 def state_machine ResourceImportFileStateMachine.new(self, transition_class: ResourceImportFileTransition) end |
#update_relationship ⇒ Object
396 397 398 399 400 401 402 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 |
# File 'app/models/resource_import_file.rb', line 396 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_identifier = row['item_identifier'].to_s.strip item = Item.where(item_identifier: item_identifier).first unless item item = Item.where(id: row['item_id'].to_s.strip).first end manifestation_identifier = row['manifestation_identifier'].to_s.strip manifestation = Manifestation.where(manifestation_identifier: manifestation_identifier).first unless manifestation manifestation = Manifestation.where(id: row['manifestation_id'].to_s.strip).first 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) end |