Class: ODDB::BsvXmlPlugin::PreparationsListener

Inherits:
Listener show all
Defined in:
src/plugin/bsv_xml.rb,
test/test_plugin/bsv_xml.rb

Constant Summary

MEDDATA_SERVER =
DRbObject.new(nil, MEDDATA_URI)
GENERIC_TYPES =
{ 'O' => :original, 'G' => :generic }

Constants inherited from Listener

FORMATS

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Listener

#date, #text, #time, #update_chapter

Constructor Details

- (PreparationsListener) initialize(*args)

A new instance of PreparationsListener



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
# File 'src/plugin/bsv_xml.rb', line 149

def initialize *args
  super
  @known_packages = {}
  @app.each_package do |pac|
    if pac.public? && pac.sl_entry
      @known_packages.store pac.pointer, {
        :name_base           => pac.name_base,
        :atc_class           => (atc = pac.atc_class) && atc.code,
        :pharmacode_oddb     => pac.pharmacode,
        :swissmedic_no5_oddb => pac.iksnr,
        :swissmedic_no8_oddb => pac.ikskey,
      }
    end
  end
  @completed_registrations = {}
  @conflicted_packages = []
  @conflicted_packages_oot = []
  @conflicted_registrations = []
  @duplicate_iksnrs = []
  @missing_ikscodes = []
  @missing_ikscodes_oot = []
  @missing_pharmacodes = []
  @unknown_packages = []
  @unknown_packages_oot = []
  @unknown_registrations = []
  @created_sl_entries = 0
  @created_limitation_texts = 0
  @deleted_sl_entries = 0
  @deleted_limitation_texts = 0
  @updated_sl_entries = 0
  @updated_limitation_texts = 0
  @origin = @@today.strftime "#{ODDB.config.url_bag_sl_zip} (%d.%m.%Y)"
  @visited_iksnrs = {}
end

Instance Attribute Details

- (Object) change_flags (readonly)

Returns the value of attribute change_flags



140
141
142
# File 'src/plugin/bsv_xml.rb', line 140

def change_flags
  @change_flags
end

- (Object) conflicted_packages (readonly)

Returns the value of attribute conflicted_packages



140
141
142
# File 'src/plugin/bsv_xml.rb', line 140

def conflicted_packages
  @conflicted_packages
end

- (Object) conflicted_packages_oot (readonly)

Returns the value of attribute conflicted_packages_oot



140
141
142
# File 'src/plugin/bsv_xml.rb', line 140

def conflicted_packages_oot
  @conflicted_packages_oot
end

- (Object) conflicted_registrations (readonly)

Returns the value of attribute conflicted_registrations



140
141
142
# File 'src/plugin/bsv_xml.rb', line 140

def conflicted_registrations
  @conflicted_registrations
end

- (Object) created_limitation_texts (readonly)

Returns the value of attribute created_limitation_texts



140
141
142
# File 'src/plugin/bsv_xml.rb', line 140

def created_limitation_texts
  @created_limitation_texts
end

- (Object) created_sl_entries (readonly)

Returns the value of attribute created_sl_entries



140
141
142
# File 'src/plugin/bsv_xml.rb', line 140

def created_sl_entries
  @created_sl_entries
end

- (Object) deleted_limitation_texts (readonly)

Returns the value of attribute deleted_limitation_texts



140
141
142
# File 'src/plugin/bsv_xml.rb', line 140

def deleted_limitation_texts
  @deleted_limitation_texts
end

- (Object) deleted_sl_entries (readonly)

Returns the value of attribute deleted_sl_entries



140
141
142
# File 'src/plugin/bsv_xml.rb', line 140

def deleted_sl_entries
  @deleted_sl_entries
end

- (Object) duplicate_iksnrs (readonly)

Returns the value of attribute duplicate_iksnrs



140
141
142
# File 'src/plugin/bsv_xml.rb', line 140

def duplicate_iksnrs
  @duplicate_iksnrs
end

- (Object) missing_ikscodes (readonly)

Returns the value of attribute missing_ikscodes



140
141
142
# File 'src/plugin/bsv_xml.rb', line 140

def missing_ikscodes
  @missing_ikscodes
end

- (Object) missing_ikscodes_oot (readonly)

Returns the value of attribute missing_ikscodes_oot



140
141
142
# File 'src/plugin/bsv_xml.rb', line 140

def missing_ikscodes_oot
  @missing_ikscodes_oot
end

- (Object) missing_pharmacodes (readonly)

Returns the value of attribute missing_pharmacodes



140
141
142
# File 'src/plugin/bsv_xml.rb', line 140

def missing_pharmacodes
  @missing_pharmacodes
end

- (Object) unknown_packages (readonly)

Returns the value of attribute unknown_packages



140
141
142
# File 'src/plugin/bsv_xml.rb', line 140

def unknown_packages
  @unknown_packages
end

- (Object) unknown_packages_oot (readonly)

Returns the value of attribute unknown_packages_oot



140
141
142
# File 'src/plugin/bsv_xml.rb', line 140

def unknown_packages_oot
  @unknown_packages_oot
end

- (Object) unknown_registrations (readonly)

Returns the value of attribute unknown_registrations



140
141
142
# File 'src/plugin/bsv_xml.rb', line 140

def unknown_registrations
  @unknown_registrations
end

- (Object) updated_limitation_texts (readonly)

Returns the value of attribute updated_limitation_texts



140
141
142
# File 'src/plugin/bsv_xml.rb', line 140

def updated_limitation_texts
  @updated_limitation_texts
end

- (Object) updated_sl_entries (readonly)

Returns the value of attribute updated_sl_entries



140
141
142
# File 'src/plugin/bsv_xml.rb', line 140

def updated_sl_entries
  @updated_sl_entries
end

Instance Method Details

- (Object) completed_registrations



183
184
185
# File 'src/plugin/bsv_xml.rb', line 183

def completed_registrations
  @completed_registrations.values
end

- (Object) erroneous_packages



186
187
188
# File 'src/plugin/bsv_xml.rb', line 186

def erroneous_packages
  @known_packages.values.sort_by do |data| data[:name_base].to_s end
end

- (Object) find_typo_registration(iksnr, name)



192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'src/plugin/bsv_xml.rb', line 192

def find_typo_registration iksnr, name
  names = name.collect do |key, name| name.downcase end
  (iksnr.length - 1).times do |idx|
    typo = iksnr.dup
    typo[idx,2] = typo[idx,2].reverse
    if reg = @app.registration(typo)
      rnames = reg.sequences.collect do |seqnr, seq|
        seq.name_base.downcase end
      return reg unless (names & rnames).empty?
    end
  end
  nil
end

- (Object) flag_change(pointer, key)



189
190
191
# File 'src/plugin/bsv_xml.rb', line 189

def flag_change pointer, key
  (@change_flags[pointer] ||= []).push key
end

- (Object) identify_sequence(registration, name, substances)



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'src/plugin/bsv_xml.rb', line 205

def identify_sequence registration, name, substances
  subs = substances.collect do |data|
    [data[:lt], ODDB::Dose.new(data[:dose], data[:unit])]
  end
  seqs = registration.sequences.values
  sequence = seqs.find do |seq|
    agents = seq.active_agents
    subs.size == agents.size && subs.all? do |sub, dose|
      agents.any? do |act| act.same_as?(sub) && act.dose == dose end
    end
  end
  sequence ||= seqs.find do |seq| seq.active_agents.empty? end
  if sequence.nil?
    seqnr = (registration.sequences.keys.max || '00').next
    ptr = registration.pointer + [:sequence, seqnr]
    sequence = @app.update ptr.creator, :name_base => name[:de]
  end
  if sequence.active_agents.empty?
    cptr = sequence.pointer + :composition
    comp = @app.create cptr
    subs.each do |name, dose|
      substance = @app.substance name
      unless substance
        sptr = Persistence::Pointer.new :substance
        substance = @app.update sptr.creator, :lt => name
      end
      pointer = comp.pointer + [:active_agent, name]
      agent = @app.update pointer.creator, :dose => dose,
                                           :substance => substance.oid
    end
  end
  sequence
end

- (Object) load_ikskey(pcode)



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'src/plugin/bsv_xml.rb', line 238

def load_ikskey pcode
  return if pcode.to_s.empty?
  tries = 3
  ikskey = nil
  begin
    MEDDATA_SERVER.session(:product) { |meddata|
      results = meddata.search({:pharmacode => pcode})
      if(results.size == 1)
        data = meddata.detail(results.first, {:ean13 => [1,2]})
        if(ean13 = data[:ean13])
          ikskey = ean13[4,8]
        end
      end
    }
    ikskey
  rescue Errno::ECONNRESET
    if(tries > 0)
      tries -= 1
      sleep(3 - tries)
      retry
    else
      raise
    end
  end
end

- (Object) tag_end(name)



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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
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
# File 'src/plugin/bsv_xml.rb', line 331

def tag_end name
  case name
  when 'Pack'
    if @pack.nil? && @completed_registrations[@iksnr] && !@out_of_trade
      @deferred_packages.push({
        :ikscd    => @ikscd,
        :sequence => @seq_data,
        :package  => @data,
        :sl_entry => @sl_data,
        :lim_text => @lim_data,
        :size     => @size,
      })
    elsif @pack && !@conflict && !@duplicate_iksnr
      @report.store :pharmacode_oddb, @pack.pharmacode
      if seq = @pack.sequence
        @app.update seq.pointer, @seq_data, :bag
      end
      pold = @pack.price_public
      pnew = @data[:price_public]
      unless pold && pnew
        pold = @pack.price_exfactory
        pnew = @data[:price_exfactory]
      end
      if pold && pnew
        if pold > pnew
          flag_change @pack.pointer, :price_cut
        elsif pold < pnew
          flag_change @pack.pointer, :price_rise
        end
      end
      ## don't take the Swissmedic-Category unless it's missing in the DB
      @data.delete :ikscat if @pack.ikscat
      @app.update @pack.pointer, @data, :bag
      @sl_entries.store @pack.pointer, @sl_data
      @lim_texts.store @pack.pointer, @lim_data
    end
    @pcode, @pack, @sl_data, @lim_data, @out_of_trade, @ikscd, @data,
      @size = nil
  when 'Preparation'
    if !@deferred_packages.empty? \
      && seq = identify_sequence(@registration, @name, @substances)
      @deferred_packages.each do |info|
        ptr = seq.pointer + [:package, info[:ikscd]]
        @app.update seq.pointer, info[:sequence]
        @app.update ptr.creator, info[:package]
        pptr = ptr + [:part]
        size = info[:size].sub(/(^| )[^\d.,]+(?= )/, '')
        @app.update pptr.creator, :size => size,
                                  :composition => seq.compositions.first
        @sl_entries.store ptr, info[:sl_entry]
        @lim_texts.store ptr, info[:lim_text]
      end
    end
    @sl_entries.each do |pac_ptr, sl_data|
      pack = pac_ptr.resolve @app
      @known_packages.delete pac_ptr
      unless pack.nil?
        pointer = pac_ptr + :sl_entry
        if sl_data.empty?
          if pack.sl_entry
            @deleted_sl_entries += 1
            @app.delete pointer
          end
        else
          if pack.sl_entry
            @updated_sl_entries += 1
          else
            @created_sl_entries += 1
          end
          if (lim_data = @lim_texts[pac_ptr]) && !lim_data.empty?
            sl_data.store :limitation, true
          end
          @app.update pointer.creator, sl_data, :bag
        end
      end
    end
    @lim_texts.each do |pac_ptr, lim_data|
      if (pac = pac_ptr.resolve(@app)) && (sl_entry = pac.sl_entry)
        sl_ptr = sl_entry.pointer
        txt_ptr = sl_ptr + :limitation_text
        if lim_data.empty?
          if sl_entry.limitation_text
            @deleted_limitation_texts += 1
            @app.delete txt_ptr
          end
        else
          if sl_entry.limitation_text
            @updated_limitation_texts += 1
          else
            @created_limitation_texts += 1
          end
          # In order to refresh limitation text old objects in ODBA cache 
          # before updating. Otherwise, the link between sl_entry and 
          # limitation_text may not produced even if there are both objects 
          # in ODBA cache.
          if sl_entry.limitation_text || txt_ptr.resolve(@app)
            @app.delete txt_ptr
          end

          @app.update txt_ptr.creator, lim_data, :bag
        end
      end
    end
    if @registration
      @app.update @registration.pointer, @reg_data, :bag
    elsif @refdata_registration
      @unknown_registrations.push @report_data
    end
    @iksnr, @registration, @sl_entries, @lim_texts, @duplicate_iksnr,
      @atc_code, @deferred_packages, @substances = nil
  when 'AtcCode'
    @atc_code = @text
    @seq_data.store :atc_class, @text
  when 'SwissmedicNo5'
    @iksnr = "%05i" % @text.to_i
    @report_data.store :swissmedic_no5_bag, @iksnr
    atc, name = visited = @visited_iksnrs[@iksnr]
    if @iksnr == '00000'
      # ignore, is reported independently
    elsif visited.nil? || @atc_code == atc \
      || atc.to_s.empty? || @atc_code.to_s.empty? \
      || name == @name
      @registration = @app.registration(@iksnr)
      @visited_iksnrs.store @iksnr, [@atc_code, @name]
    elsif @registration = find_typo_registration(@iksnr, @name)
      @report_data.store :swissmedic_no5_bag,
        sprintf("%s (auto-corrected to %s)", @iksnr,
                @iksnr = @registration.iksnr)
      @duplicate_iksnrs.push @report_data
    else
      @duplicate_iksnr = true
      @report_data.store :swissmedic_no5_bag,
        sprintf("%s (belongs to %s)", @iksnr, name && name[:de])
      @duplicate_iksnrs.push @report_data
    end
    if @registration && @registration.packages.empty?
      @completed_registrations.store @iksnr, @report_data
    end
  when 'SwissmedicNo8'
    @report.store :swissmedic_no8_bag, @text
    if @text.strip.empty?
      if @out_of_trade
        @missing_ikscodes_oot.push @report
      else
        @missing_ikscodes.push @report
      end
    end
    unless @registration
      @registration = @app.registration("%05i" % @text[0..-4].to_i)
    end
    if @registration
      @ikscd = '%03i' % @text[-3,3].to_i
      @pack ||= @registration.package @ikscd
      if !@pcode.empty? && @pack && @pack.pharmacode \
        && @pack.pharmacode != @pcode && @pack.pharmacode.to_i != 0
        @report.store :pharmacode_oddb, @pack.pharmacode
        @conflict = true
      end
    end
  when 'SwissmedicCategory'
    @data.store :ikscat, @text
  when 'OrgGenCode'
    gtype = GENERIC_TYPES[@text]
    @reg_data.store :generic_type, (gtype || :unknown)
    @pac_data.store :sl_generic_type, gtype
  when 'FlagSB20'
    @pac_data.store :deductible, @text == 'Y' ? :deductible_o : :deductible_g
  when 'FlagNarcosis'
    @data.store :narcotic, @text == 'Y'
  when 'BagDossierNo'
    @sl_data.store :bsv_dossier, @text if @sl_data
  when /(.+)Price/u
    if @price > 0
      @data.store :"price_#{@price_type}", @price
    end
    @price, @price_type = nil
  when 'Price'
    @price.amount = @text.to_f if @price
  when 'ValidFromDate'
    if @price
      @price.valid_from = time(@text)
    elsif @sl_entries
      @sl_entries.each_value do |sl_data|
        sl_data.store :valid_from, date(@text)
      end
    end
  when 'ValidThruDate'
    if @sl_entries
      @sl_entries.each_value do |sl_data|
        sl_data.store :valid_until, date(@text)
      end
    end
  when 'StatusTypeCodeSl'
    if @sl_data
      @sl_data.store :status, @text
      active = false
      flag = nil
      case @text
      when '2', '6'
        if @pack && !@pack.sl_entry
          flag_change @pack.pointer, :sl_entry
        end
        active = true
      when '3', '7'
        if @pack && @pack.sl_entry
          flag_change @pack.pointer, :sl_entry_delete
        end
      when '9', '10'
        #  9: inactive
        # 10: pending
      else
        active = true
      end
      if active && !@out_of_trade
        if @registration && @pack.nil?
          @unknown_packages.push @report
        elsif @pack
          if @conflict
            @conflicted_packages.push @report
          end
        end
      elsif @registration && @pack.nil?
        @unknown_packages_oot.push @report
      elsif @conflict
        @conflicted_packages_oot.push @report
      end
    end
  when 'IntegrationDate'
    @sl_data.store :introduction_date, date(@text) if @sl_data
  when 'PriceChangeTypeCode'
    @price.mutation_code = @text
  when 'Limitation'
    @in_limitation = false
  when 'LimitationType'
    # ignore
  when /^Limitation([A-Z].+)$/u
    @sl_data.store $~[1].downcase.to_sym, @text if @sl_data
  when /^Name(..)$/u
    key = $~[1].downcase.to_sym
    @name[key] = @text
    @report_data.store(:name_base, @text) if key == :de
  when 'DescriptionLa'
    @substance.store :lt, @text
  when 'Quantity'
    @substance.store :dose, @text.to_f
  when 'QuantityUnit'
    @substance.store :unit, @text
  when 'Substance'
    @substances.push @substance
    @substance = nil
  when /^Description(..)$/u
    key = $~[1].downcase.to_sym
    if @in_limitation
      if @lim_data # we are within a Package
        chp = Text::Chapter.new
        update_chapter chp, @html
        @lim_data.store key, chp
      else
        @lim_texts.each_value do |text_data|
          chp = text_data[key] ||= Text::Chapter.new
          subheading = if @it_descriptions
                         [@itcode, @it_descriptions[key]].compact.join(': ')
                       else
                         @name[key]
                       end
          update_chapter chp, @html, subheading
        end
      end
    elsif @it_descriptions
      @it_descriptions[key] = @text
    elsif @data
      @size ||= @text
    else
      @descriptions[key] ||= @text
      @report_data[:name_descr] ||= @text if key == :de
    end
  when 'Points'
    if @sl_data
      value = @text.to_i
      @sl_data.store :limitation_points, value
      @sl_data.store :limitation, value > 0
    end
  when 'ItCode'
    @itcode = nil
    @it_descriptions = nil
  when 'Preparations'
    @known_packages.each do |pointer, data|
      @deleted_sl_entries += 1
      flag_change pointer, :sl_entry_delete
      sl_ptr = pointer + :sl_entry
      @app.delete sl_ptr
    end
  end
  @text, @html = nil
rescue StandardError => e
  e.message << "\n@report: " << @report.inspect
  raise
end

- (Object) tag_start(name, attrs)



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
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
# File 'src/plugin/bsv_xml.rb', line 263

def tag_start name, attrs
  case name
  when 'Pack'
    @ikscd = nil
    @pcode = attrs['Pharmacode'].to_s
    @data = @pac_data.dup
    @report = @report_data.dup.update(@data).update(@reg_data).
                               update(@seq_data)
    @report.delete(:sl_generic_type)
    @report.store :pharmacode_bag, @pcode
    @data.store :pharmacode, @pcode
    if @pcode.empty?
      @missing_pharmacodes.push @report
    end
    if !@pcode.empty? && @pack = Package.find_by_pharmacode(@pcode)
      @out_of_trade = @pack.out_of_trade
      @registration ||= @pack.registration
      if @registration
        @report.store :swissmedic_no5_oddb, @registration.iksnr
        unless ['00000', @registration.iksnr].include?(@iksnr)
          @conflicted_registrations.push @report
        end
      end
    elsif ikskey = load_ikskey(@pcode)
      @iksnr = ikskey[0,5] if @iksnr == '00000'
      @registration ||= @app.registration(ikskey[0,5])
      @pack = @registration.package ikskey[5,3] if @registration
      @refdata_registration = true
    else
      # package is out of trade
      @out_of_trade = true
    end
    @sl_data = { :limitation_points => nil, :limitation => nil }
    @lim_data = {}
    @conflict = false
  when 'Preparation'
    @descriptions = {}
    @reg_data = {}
    @seq_data = {}
    @pac_data = {}
    @sl_entries = {}
    @lim_texts = {}
    @name = {}
    @report_data = {}
    @deferred_packages = []
    @substances = []
    @refdata_registration = false
  when /(.+)Price/u
    @price_type = $~[1].downcase.to_sym
    @price = Util::Money.new(0, @price_type, 'CH')
    @price.origin = @origin
    @price.authority = :sl
  when 'Limitation'
    @in_limitation = true
  when 'ItCode'
    @itcode = attrs['Code']
    @reg_data.store :index_therapeuticus, @itcode
    @it_descriptions = {}
  when 'Substance'
    @substance = {}
  else
    @text = ''
    @html = ''
  end
rescue StandardError => e
  e.message << "\n@report: " << @report.inspect
  raise
end