Class: ODDB::MedwinPackagePlugin

Inherits:
MedwinPlugin show all
Defined in:
src/plugin/medwin.rb,
test/test_plugin/medwin.rb

Constant Summary

MEDDATA_SERVER =
DRbObject.new(nil, MEDDATA_URI)

Constants inherited from MedwinPlugin

HTTP_SERVER

Constants inherited from Plugin

ARCHIVE_PATH, RECIPIENTS

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Plugin

#l10n_sessions, #log_info, #recipients, #resolve_link, #update_rss_feeds

Methods included from HttpFile

#http_body, #http_file

Constructor Details

- (MedwinPackagePlugin) initialize(app)

A new instance of MedwinPackagePlugin



98
99
100
101
102
103
104
105
106
107
108
109
# File 'src/plugin/medwin.rb', line 98

def initialize(app)
  super
  @medwin_template = {
    :pharmacode =>  [3,2],
  }  
  @nonmatching_template = {
    :ean13      => [1,2],
    :pharmacode =>  [3,2],
  }
  @probable_errors_oddb = []
  @probable_errors_medwin = []
end

Instance Attribute Details

- (Object) update_package_called

Returns the value of attribute update_package_called



21
22
23
# File 'test/test_plugin/medwin.rb', line 21

def update_package_called
  @update_package_called
end

Instance Method Details

- (Object) report



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

def report
  lines = [
    @header,
    "Checked #{@checked} Packages",
    "Tried #{@found} Medwin Entries",
    "Updated  #{@updated.size} Packages",
    "Probable Errors in ODDB: #{@probable_errors_oddb.size}",
    "Probable Errors in Medwin: #{@probable_errors_medwin.size}",
    nil,
    "Probable Errors in ODDB: \#{@probable_errors_oddb.size}\nIn den folgenden F\303\244llen ist die Swissmedic-Packungsnummer von ODDB.org ziemlich\nsicher falsch, weil Sie tiefer ist als diejenige von Medwin.ch\n"
  ]
  @probable_errors_oddb.each { |pack|
    lines.push sprintf("%07i: http://ch.oddb.org/de/gcc/resolve/pointer/%s",
                       pack.pharmacode, pack.pointer)
  }
  lines.push nil
  lines.push "Probable Errors in Medwin: \#{@probable_errors_medwin.size}\nIn den folgenden F\303\244llen ist die Swissmedic-Packungsnummer von Medwin.ch\nziemlich sicher falsch, weil Sie tiefer ist als diejenige von ODDB.org.\n"
  @probable_errors_medwin.each { |pack|
    lines.push sprintf("%07i: http://ch.oddb.org/de/gcc/resolve/pointer/%s",
                       pack.pharmacode, pack.pointer)
  }
  lines.push nil
  lines.push("Errors:")
  @errors.each { |key, value|
    lines.push(key + " => " + value)
  }
  lines.join("\n")
end

- (Object) update



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'src/plugin/medwin.rb', line 148

def update
  @header = "Alle Packungen wurden \303\274berpr\303\274ft (checked).\nPackungen, welche im Handel sind, wurden bei MedWin abgefragt (tried).\nAls Update (updated) gekennzeichnet wurden diejenigen, bei denen der Pharmacode\nvon MedWin anders war als in der ODDB (inkl. diese, wo die ODDB noch keinen\nPharmacode hatte)\n\n"
  MEDDATA_SERVER.session(:product) { |meddata|
    @app.each_package { |pack|
      @checked += 1
      unless pack.out_of_trade
        @found += 1
        update_package(meddata, pack)
      end
    }
    nil # return nil across DRb
  }
end

- (Object) update_package(meddata, pack)



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

def update_package(meddata, pack)
  criteria = {
    :ean =>  pack.barcode.to_s,
  }
  template = @medwin_template
  results = meddata.search(criteria)
  if(results.empty? && pack.registration.package_count == 1)
    criteria = {
      :ean => pack.barcode.to_s[0,9]
    }
    template = @nonmatching_template
    results = meddata.search(criteria)
  end
  if(results.size == 1)
    result = results.first
    details = meddata.detail(result, template)
    if(ean13 = details.delete(:ean13))
       details.store(:medwin_ikscd, ean13[9,3])
       if(ean13 > pack.barcode.to_s)
         @probable_errors_oddb.push(pack)
       else
         @probable_errors_medwin.push(pack)
       end
    end
    unless pack.pharmacode == details[:pharmacode].to_i.to_s
      update_package_data(pack, details)
    end
  end
end

- (Object) update_package_data(pack, data)



246
247
248
249
250
251
# File 'src/plugin/medwin.rb', line 246

def update_package_data(pack, data)
  unless(data.empty?)
    @updated.push(pack.barcode)
    @app.update(pack.pointer, data, :refdata)
  end
end

- (Object) update_package_trade_status(refdata, pack)



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
238
239
240
241
242
243
244
245
# File 'src/plugin/medwin.rb', line 213

def update_package_trade_status(refdata, pack)
  criteria = {
    :ean =>  pack.barcode.to_s,
  }
  results = refdata.search(criteria)
  if results.empty? && (pcode = pack.pharmacode) && !pcode.strip.empty?
    begin
      ## there's no pharmacode in RefData, so we need to escape to
      #  MedData here
      MEDDATA_SERVER.session(:product) { |meddata|
        results = meddata.search(:pharmacode => pcode)
      }
    rescue MedData::OverflowError
      ## obviously something went wrong, and we have received too many
      #  results.. since we're only interested in results of size 1, we
      #  can safely ignore this.
    end
  end
  if(results.empty? && pack.registration.package_count == 1)
    criteria = {
      :ean => pack.barcode.to_s[0,9]
    }
    results = refdata.search(criteria)
  end
  if(results.size == 0 && !pack.out_of_trade)
    update_package_data(pack, {:out_of_trade => true})
  elsif(results.size == 1 && pack.out_of_trade)
    data = {:out_of_trade => false, :refdata_override => false}
    update_package_data(pack, data)
  else
    sleep(0.05)
  end
end

- (Object) update_trade_status



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'src/plugin/medwin.rb', line 169

def update_trade_status
  MEDDATA_SERVER.session(:refdata) { |meddata|
    @app.each_sequence { |seq|
      if(seq.active?)
        seq.each_package { |pack|
          @checked += 1
          update_package_trade_status(meddata, pack)
          sleep(0.1)
        }
      end
      nil # don't return the packages across DRb
    }
    nil # return nil across DRb
  }
end