Class: ODDB::Doctors::DoctorPlugin

Inherits:
Plugin show all
Defined in:
src/plugin/doctors.rb,
test/test_plugin/zdoctor.rb

Constant Summary

RECIPIENTS =
[]
PARSER =
Mock.new('Parser')
MEDDATA_SERVER =
DRbObject.new(nil, MEDDATA_URI)

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

- (DoctorPlugin) initialize(*args)

A new instance of DoctorPlugin



17
18
19
20
21
22
# File 'src/plugin/doctors.rb', line 17

def initialize(*args)
  super
  @config = @app.config(:docparse, :ch)
  @doctors_created = 0
  @doctors_deleted = 0
end

Instance Attribute Details

- (Object) empty_id (readonly)

Returns the value of attribute empty_id



14
15
16
# File 'test/test_plugin/zdoctor.rb', line 14

def empty_id
  @empty_id
end

Instance Method Details

- (Object) delete_doctor(doc_id)



52
53
54
55
56
57
# File 'src/plugin/doctors.rb', line 52

def delete_doctor(doc_id)
  if(doc = @app.doctor_by_origin(:ch, doc_id))
    @app.delete(doc.pointer)
    @doctors_deleted += 1
  end
end

- (Object) fix_doctors(ambiguous, docs)



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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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
263
264
265
266
267
268
# File 'src/plugin/doctors.rb', line 164

def fix_doctors(ambiguous, docs)
  found = {}
  hypothesis = {}
  docs.each { |doc|
    criteria = {
      :name =>  doc.name,
    }
    check = [doc.name, doc.firstname]
    if((addr = doc.praxis_address) \
      || (addr = doc.addresses.first))
      criteria.store(:city, addr.city)
      check.push(addr.city)
    end
    eans = []
    MEDDATA_SERVER.session { |meddata|
      begin
        results = meddata.search(criteria).select { |res|
          check_with = res.values[0,check.size]
          check_with.at(1).gsub!(/\d/u, '')
          check_with.at(1).strip!
          check_with == check
        }
      rescue RuntimeError
        puts check.inspect, criteria.inspect
        hypothesis.store(doc, [ambiguous])
        next
      end
      eans = results.collect { |result|
        details = meddata.detail(result, {:ean13 => [1,0]})
        details[:ean13]
      }
    }
    if(eans.size == 1)
      found.store(doc, eans.first)
    else
      hypothesis.store(doc, eans)
    end
  }
  found.each_value { |ean|
    hypothesis.each { |doc, eans|
      eans.delete(ean)
    }
  }
  hypothesis.delete_if { |doc, eans| eans.empty? }
  sorted = hypothesis.sort_by { |doc, eans| eans.size }
  while(pair = sorted.shift)
    doc, eans = *pair
    if(eans.size > 1) ## ambiguous
      break
    elsif(eans.size == 0)
      next
    else
      if(sorted.any? { |pair| pair.at(1) == eans}) ## duplicate!
        sorted.delete_if { |pair| pair.at(1) == eans}
      else
        found.store(doc, eans.first)
      end
      sorted.each { |pair| pair.at(1).delete(eans.first) }
    end
    sorted = sorted.sort_by { |pair| pair.at(1).size }
  end
  if(found.values.size > found.values.uniq.size)
    ## ignore ambiguous values
    eans = found.values
    while(ean = eans.shift)
      if(eans.include?(ean))
        found.delete_if { |doc, ean13| ean == ean13 }
        eans.delete(ean)
      end
    end
  end
  docs.each { |doc|
    doc.ean13 = found[doc]
    doc.odba_store
  }
=begin
  result = MEDDATA_SERVER.search().first
  matchs = []
  docs.each { |doc|
    if([doc.name, doc.firstname] == result.values[0,2])
      matchs.push(doc)
    else
      #doc.ean13 = nil
      #doc.odba_store
    end
  }
  locations = []
  if(matchs.size > 1)
    matchs.each { |doc|
      if(doc.addresses.any? { |addr|result.values.at(2) })
        locations.push(doc)
      else
        #doc.ean13 = nil
        #doc.odba_store
      end
    }
  end
  if(locations.size > 1)
    puts "still #{locations.size} duplicate eans:"
    puts locations.collect { |doc| [doc.name, doc.firstname].join(' ') }
    raise "thats enuff"
  end
=end


end

- (Object) fix_duplicate_eans



268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'src/plugin/doctors.rb', line 268

def fix_duplicate_eans
  ean_table = {}
  @app.doctors.each { |id, doc|
    if(ean = doc.ean13)
      (ean_table[ean] ||= []).push(doc)
    end
  }
  ean_table.each { |ean, docs|
    if(docs.size > 1)
      fix_doctors(ean, docs)
    end
  }
end

- (Object) get_doctor_data(doc_id)



58
59
60
61
62
63
64
65
66
67
68
69
# File 'src/plugin/doctors.rb', line 58

def get_doctor_data(doc_id)
  retry_count = 3
  begin
    self::class::PARSER.doc_data_add_ean(doc_id)
  rescue Errno::EINTR, Errno::ECONNRESET => err
    puts "rescued #{err} -> #{retry_count} more tries"
    if(retry_count > 0)
      retry_count -= 1
      retry
    end
  end
end

- (Object) merge_address(target, source, sym)



93
94
95
96
97
# File 'src/plugin/doctors.rb', line 93

def merge_address(target, source, sym)
  target[sym] = [target[sym], source[sym]].flatten
  target[sym].delete('')
  target[sym].uniq!
end

- (Object) merge_addresses(addrs)



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'src/plugin/doctors.rb', line 77

def merge_addresses(addrs)
  merged = []
  addrs.each { |addr|
    if(equal = merged.select { |other|
      addr[:lines] == other[:lines]
    }.first)
      merge_address(equal, addr, :fon)
      merge_address(equal, addr, :fax)
    else
      merge_address(addr, addr, :fax)
      merge_address(addr, addr, :fon)
      merged.push(addr)
    end
  }
  merged
end

- (Object) prepare_addresses(hash)



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'src/plugin/doctors.rb', line 98

def prepare_addresses(hash)
  if(addrs = hash[:addresses])
    tmp_addrs = (addrs.is_a?(Array)) ? addrs : [addrs]
    merge_addresses(tmp_addrs).collect { |values|
      addr = Address.new
      values.each { |key, val| 
        meth = "#{key}="
        if(addr.respond_to?(meth))
          addr.send(meth, val)
        end
      }
      addr
    }
  else
    []
  end
end

- (Object) report



70
71
72
73
74
75
76
# File 'src/plugin/doctors.rb', line 70

def report
  report = "Doctors update \n\n"
  report << "Number of doctors: " << @app.doctors.size.to_s << "\n"
  report << "New doctors: " << @doctors_created.to_s << "\n"
  report << "Deleted doctors: " << @doctors_deleted.to_s << "\n"
  report
end

- (Object) restore(doc_id)



23
24
25
26
27
# File 'src/plugin/doctors.rb', line 23

def restore(doc_id)
  if(data = get_doctor_data(doc_id))
    store_doctor(doc_id, data)
  end
end

- (Object) store_doctor(doc_id, hash)



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

def store_doctor(doc_id, hash)
  pointer = nil
  if(doc = @app.doctor_by_origin(:ch, doc_id))
    pointer = doc.pointer
  else
    @doctors_created += 1
    ptr = Persistence::Pointer.new(:doctor)
    pointer = ptr.creator
  end
  extract = [
    :ean13,
    :exam,
    :email,
    :firstname,
    :language,
    :name,
    :praxis,
    :salutation,
    :specialities,
    :title,
  ]
  doc_hash = {}
  extract.each { |key|
    if(value = hash[key])
      case key
      when :praxis
        value = (value == 'Ja')
      when :specialities 
        if(value.is_a?(String))
          value = [value]
        end  
      end
      doc_hash.store(key, value)
    end
    
  }
  doc_hash.store(:origin_db, :ch)
  doc_hash.store(:origin_id, doc_id)
  doc_hash.store(:addresses, prepare_addresses(hash))
  @app.update(pointer, doc_hash)
end

- (Object) store_empty_ids(ids)



156
157
158
159
160
161
# File 'src/plugin/doctors.rb', line 156

def store_empty_ids(ids)
  values = {
    :empty_ids => ids,
  }
  @app.update(@config.pointer, values, :fmh)
end

- (Object) update



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'src/plugin/doctors.rb', line 28

def update
  #range = 5000..99999
  empty_ids = (@config.empty_ids || [])
  step = 250
  5000.step(100000, step) { |base|
    range = base...(base+step)
    puts "Next-Step: #{range.first} - #{range.last}"
    $stdout.flush
  top_doc_id = 0
  (range.to_a - empty_ids).each { |doc_id| 
    if(data = get_doctor_data(doc_id))
      store_doctor(doc_id, data)
      top_doc_id = doc_id
    else
      # 1. delete doctor if exists
      delete_doctor(doc_id)
      # 2. record id, muss das naechste mal nicht geprueft werden.
      empty_ids.push(doc_id)
    end
  }
  empty_ids.delete_if { |id| id > top_doc_id }
  store_empty_ids(empty_ids)
  }
end